简体   繁体   中英

3 Processes Synchronization using 2 pipes

I am trying to create 2 child processes using fork() and synchronize them so they display a message one after another five times infinitely.

e.g.   Process 1 has process (pid)
       ...
       Process 2 has process (pid)
       ...
       Process 3 has process (pid)
       ...
       Process 1 has process (pid)
       ...
       ...

I know this could be done easier using 3 pipes but I was wondering if it would be possible to do it with just 2. For some reason the 1st process never gets the chance to run. Keep in mind that I can only use pipes to solve this problem.

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main()
{
    pid_t p, p1, mypid;
    int i;
    int a = 1;
    int b = 1;  
    int fd[2];      
    int fd1[2]; 
    pipe(fd);   
    p = fork();
    if (p < 0) {perror("Fork"); exit(1); }
    else if (p == 0)
    {
        mypid = getpid();        
        while(1)
        {           
            close(fd[1]);       
            if (read(fd[0], &a, sizeof(int)) == 0)                  
            {           
                for(i = 0; i < 5; i++) {printf("Process 2 has process %d\n", mypid); }      
                close(fd[0]);       
                a = 1;              
                write(fd[1], &a, sizeof(int)); } }
        }
    else
    {   
        p = fork();
        if (p < 0) {perror("Fork"); exit(1); }  
        pipe(fd1);          
        if (p > 0)
        {
            mypid = getpid();               
            while(1)
            {               
                close(fd1[1]);          
                if (read(fd1[0], &b, sizeof(int)) == 1)
                {
                    for(i = 0; i < 5; i++) {printf("Process 1 has process %d\n", mypid); }                  
                    close(fd[0]);
                    a = 0;                  
                    write(fd[1], &a, sizeof(int)); }                            
                close(fd[1]);               
                if ((read(fd1[0], &b, sizeof(int)) == 0) && (read(fd[0], &a, sizeof(int)) == 1))                
                {
                    close(fd1[0]);
                    b = 0;
                    write(fd1[1], &b, sizeof(int)); }
            } 
        }           
        else
        {               
            while(1)
            {                   
                close(fd1[1]);
                if (read(fd1[0], &b, sizeof(int)) == 0)
                {               
                    mypid = getpid();       
                    for(i = 0; i < 5; i++) {printf("Process 3 has process %d\n", mypid); }
                    close(fd1[0]);          
                    b = 1;                  
                    write(fd1[1], &b, sizeof(int)); }
            }           
        }
    }
    return(1); }

-It is a project for my unix class so I would really appreciate it if you just pointed out my mistakes instead of solving it for me. -I think I misunderstood the concept of the pipe/read. Please let me know if I missed something and perhaps link a source to get more specific details. -The program might contain some minor mistakes(Missing {} etc).

With a pipe, you get two file descriptors connected to one another by the pipe. When you spawn a child process, the file descriptors are copied to the child process. For the purpose of IPC, the two processes then need to agree on which process uses which end of the pipe. It doesn't matter if the parent gets end 0 and the child gets end 1, or vice-versa. The important thing is that they agree. Picture it like a physical pipe connecting two rooms. If you're in one room, you can put stuff in to the pipe or take stuff out, but you only do that on one end.

In your program, both parent and child are attempting to close the same end of the pipe, and then attempting to consume the same opposite end of the pipe. Your pipe is a bucket. :) To rectify this, in your children, close the end of the pipe that you read/write in the parent. In the parent process, close the end of the pipe that you read/write in the children. I suspect that you'll have greater success that way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM