您的读取调用已损坏 — 编译器应该会抱怨。这可能不是因为您没有包含足够的标头或未启用足够的编译器警告选项。
当我编译你的代码时,我得到:
$ gcc -O3 -g -std=c11 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition -Werror -c borked.c
borked.c:8:9: error: no previous prototype for ‘squareArea’ [-Werror=missing-prototypes]
int squareArea(int x){return x*x;}
^
borked.c:9:9: error: no previous prototype for ‘rectangleArea’ [-Werror=missing-prototypes]
int rectangleArea(int x, int y){return x*y;}
^
borked.c:11:10: error: function declaration isn’t a prototype [-Werror=strict-prototypes]
void main(){
^
borked.c:11:10: error: return type of ‘main’ is not ‘int’ [-Werror=main]
borked.c: In function ‘main’:
borked.c:11:10: error: old-style function definition [-Werror=old-style-definition]
borked.c:14:8: error: implicit declaration of function ‘pipe’ [-Werror=implicit-function-declaration]
pipe(squarePipe);
^
borked.c:18:1: error: implicit declaration of function ‘fork’ [-Werror=implicit-function-declaration]
pId = fork();
^
borked.c:21:3: error: implicit declaration of function ‘read’ [-Werror=implicit-function-declaration]
read( squarePipe[0],sideOfSq,sizeof(int) );
^
borked.c:22:3: error: implicit declaration of function ‘write’ [-Werror=implicit-function-declaration]
write( squarePipe[1], squareArea(sideOfSq),sizeof(int) );
^
borked.c:13:12: error: unused variable ‘sentinel’ [-Werror=unused-variable]
int sentinel = 0;
^
cc1: all warnings being treated as errors
“以前的原型”警告是有效的,但如果您的代码省略了它们,我不会抱怨。其他警告更严重,read 和 write 缺少原型意味着你在阻碍自己。
通过各种不正当手段解决基本问题,包括<unistd.h>会给出不同的警告。
代码
#include <stdio.h>
#include <unistd.h>
int squarePipe[2];
int rectangleXPipe[2];
int rectangleYPipe[2];
static inline int squareArea(int x) { return x * x; }
static inline int rectangleArea(int x, int y) { return x * y; }
int main(void)
{
int pId, pId2, sideOfSq, xOfRec, yOfRec, areaOfSq, areaOfRec;
int sentinel = 0;
pipe(squarePipe);
pipe(rectangleXPipe);
pipe(rectangleYPipe);
pId = fork();
if (pId == 0)
{
read( squarePipe[0], sideOfSq, sizeof(int) );
write( squarePipe[1], squareArea(sideOfSq), sizeof(int) );
}
else
{
pId2 = fork();
if (pId2 == 0)
{
read( rectangleXPipe[0], xOfRec, sizeof(int) );
read( rectangleYPipe[0], yOfRec, sizeof(int) );
write( rectangleXPipe[1], rectangleArea(xOfRec, yOfRec), sizeof(int) );
}
else
{
printf("Enter the side of square\n");
scanf("%d", &sideOfSq);
write( squarePipe[1], sideOfSq, sizeof(int));
printf("Enter the x sides of rectangle\n");
scanf("%d", &xOfRec);
printf("Enter the y sides of rectangle\n");
scanf("%d", &yOfRec); // bilgilerin alınması
write( rectangleXPipe[1], xOfRec, sizeof(int));
write( rectangleYPipe[1], yOfRec, sizeof(int));
read( rectangleXPipe[0], areaOfRec, sizeof(int) );
read( squarePipe[0], areaOfSq, sizeof(int) );
printf("difference = %d", areaOfSq - areaOfRec);
}
}
return 0;
}
编译警告
和以前一样的命令行。
borked.c: In function ‘main’:
borked.c:24:9: error: passing argument 2 of ‘read’ makes pointer from integer without a cast [-Werror]
read( squarePipe[0], sideOfSq, sizeof(int) );
^
In file included from borked.c:2:0:
/usr/include/unistd.h:466:10: note: expected ‘void *’ but argument is of type ‘int’
ssize_t read(int, void *, size_t) __DARWIN_ALIAS_C(read);
^
borked.c:25:9: error: passing argument 2 of ‘write’ makes pointer from integer without a cast [-Werror]
write( squarePipe[1], squareArea(sideOfSq), sizeof(int) );
^
In file included from borked.c:2:0:
/usr/include/unistd.h:490:10: note: expected ‘const void *’ but argument is of type ‘int’
ssize_t write(int, const void *, size_t) __DARWIN_ALIAS_C(write);
^
borked.c:32:13: error: passing argument 2 of ‘read’ makes pointer from integer without a cast [-Werror]
read( rectangleXPipe[0], xOfRec, sizeof(int) );
^
In file included from borked.c:2:0:
/usr/include/unistd.h:466:10: note: expected ‘void *’ but argument is of type ‘int’
ssize_t read(int, void *, size_t) __DARWIN_ALIAS_C(read);
^
borked.c:33:13: error: passing argument 2 of ‘read’ makes pointer from integer without a cast [-Werror]
read( rectangleYPipe[0], yOfRec, sizeof(int) );
^
In file included from borked.c:2:0:
/usr/include/unistd.h:466:10: note: expected ‘void *’ but argument is of type ‘int’
ssize_t read(int, void *, size_t) __DARWIN_ALIAS_C(read);
^
borked.c:34:13: error: passing argument 2 of ‘write’ makes pointer from integer without a cast [-Werror]
write( rectangleXPipe[1], rectangleArea(xOfRec, yOfRec), sizeof(int) );
^
In file included from borked.c:2:0:
/usr/include/unistd.h:490:10: note: expected ‘const void *’ but argument is of type ‘int’
ssize_t write(int, const void *, size_t) __DARWIN_ALIAS_C(write);
^
borked.c:40:13: error: passing argument 2 of ‘write’ makes pointer from integer without a cast [-Werror]
write( squarePipe[1], sideOfSq, sizeof(int));
^
In file included from borked.c:2:0:
/usr/include/unistd.h:490:10: note: expected ‘const void *’ but argument is of type ‘int’
ssize_t write(int, const void *, size_t) __DARWIN_ALIAS_C(write);
^
borked.c:47:13: error: passing argument 2 of ‘write’ makes pointer from integer without a cast [-Werror]
write( rectangleXPipe[1], xOfRec, sizeof(int));
^
In file included from borked.c:2:0:
/usr/include/unistd.h:490:10: note: expected ‘const void *’ but argument is of type ‘int’
ssize_t write(int, const void *, size_t) __DARWIN_ALIAS_C(write);
^
borked.c:48:13: error: passing argument 2 of ‘write’ makes pointer from integer without a cast [-Werror]
write( rectangleYPipe[1], yOfRec, sizeof(int));
^
In file included from borked.c:2:0:
/usr/include/unistd.h:490:10: note: expected ‘const void *’ but argument is of type ‘int’
ssize_t write(int, const void *, size_t) __DARWIN_ALIAS_C(write);
^
borked.c:50:13: error: passing argument 2 of ‘read’ makes pointer from integer without a cast [-Werror]
read( rectangleXPipe[0], areaOfRec, sizeof(int) );
^
In file included from borked.c:2:0:
/usr/include/unistd.h:466:10: note: expected ‘void *’ but argument is of type ‘int’
ssize_t read(int, void *, size_t) __DARWIN_ALIAS_C(read);
^
borked.c:51:13: error: passing argument 2 of ‘read’ makes pointer from integer without a cast [-Werror]
read( squarePipe[0], areaOfSq, sizeof(int) );
^
In file included from borked.c:2:0:
/usr/include/unistd.h:466:10: note: expected ‘void *’ but argument is of type ‘int’
ssize_t read(int, void *, size_t) __DARWIN_ALIAS_C(read);
^
borked.c:15:9: error: unused variable ‘sentinel’ [-Werror=unused-variable]
int sentinel = 0;
^
cc1: all warnings being treated as errors
这几乎可以逐行告诉您存在问题的地方。去修吧!