【问题标题】:Reading values from pipe in Linux在 Linux 中从管道中读取值
【发布时间】:2014-03-22 23:51:52
【问题描述】:

我正在尝试编写一个程序来找出两个数字之间的差异,但我需要计算不同子进程中的数字。

    #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);
    }
}
}

我被困在这里了。我认为其中一个管道没有响应,但我无法弄清楚。

【问题讨论】:

  • 管道用完后关闭如何?
  • 我刚试了没用。
  • 你可以在Windows机器上使用void main()(很遗憾);你不能在 Unix 上使用 void main(),特别是在 Linux 上。这只是main()的错误写法;它总是在 Unix/Linux 上返回 int。如果 GCC 没有抱怨它,那么您没有使用足够的警告选项。至少,您应该使用-Wall;最好至少使用-Wall -Wextra -Werror;我使用比那些更挑剔的选项。

标签: c linux process multiprocessing pipe


【解决方案1】:

您的读取调用已损坏 — 编译器应该会抱怨。这可能不是因为您没有包含足够的标头或未启用足够的编译器警告选项。

当我编译你的代码时,我得到:

$ 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

“以前的原型”警告是有效的,但如果您的代码省略了它们,我不会抱怨。其他警告更严重,readwrite 缺少原型意味着你在阻碍自己。

通过各种不正当手段解决基本问题,包括&lt;unistd.h&gt;会给出不同的警告。

代码

#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

这几乎可以逐行告诉您存在问题的地方。去修吧!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-25
    • 1970-01-01
    • 2014-01-24
    • 1970-01-01
    • 1970-01-01
    • 2020-06-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多