Question

I'm trying to write a program that finds a difference between two number but i need to calculate the numbers in different child process.

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

I'm stuck here. I think one of those pipes does not respond but I could not figure it out.

Was it helpful?

Solution

Your read calls are broken — and the compiler should be complaining. It isn't presumably because you don't have enough headers included or enough compiler warning options enabled.

When I compile your code, I get:

$ 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

The 'previous prototype' warnings are valid but I'd not complain if your code omitted them. The other warnings are more serious, and the lack of prototype for read and write means you are hindering yourself.

Fixing up the basic problems by various devious means and including <unistd.h> gives different warnings.

Code

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

Compilation warnings

Same command line as before.

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

This tells you pretty much line by line where there are problems. Go fix!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top