我正在尝试编写一个找到两个数字之间差异的程序,但我需要计算不同子进程中的数字。
#include <stdio.h>
int squarePipe[2];
int rectangleXPipe[2];
int rectangleYPipe[2];
int squareArea(int x){return x*x;}
int rectangleArea(int x, int y){return x*y;}
void main(){
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);
}
}
}
我被困在这里。我认为其中一个管道没有响应,但我无法弄明白。
答案 0 :(得分:0)
您的读取调用已被破坏 - 编译器应该抱怨。这可能不是因为您没有包含足够的头文件或启用了足够的编译器警告选项。
当我编译你的代码时,我得到:
$ 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
这会在有问题的地方逐行告诉你。去解决!