The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #1 (permalink)  
Old 06-16-2009
p00ndawg p00ndawg is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 8
Creating 3 process and piping a message

sorry im very new to this but i am supposed to create 3 processes A,B, and C and have a direct link from a to b, b to c, and c to a.

here is my code. It does work, however if you look at what I bolded as long as my final read is p[0] it seems to always work, regardless of the bolded section.

can anyone explain this?


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

#define MSGSIZE    14 
 
char *message = "hello, world!"; 
 
 
main() { 
char inbuf[MSGSIZE];
int p[3];

pid_t pid;
pid_t pid1;
 
if (pipe(p) == -1){ 
perror("pipe call"); 
exit(1); 
 
}


pid = fork();
pid1 = fork();


if(pid == -1){

perror("Fork failed");
exit(1);

}

if(pid | pid1 == 0)//process A
{
    close(p[0]); 
    write(p[1], message, MSGSIZE);
    read(p[1], message, MSGSIZE);
    write(p[2], message, MSGSIZE);
    
}

/*else if(pid1 == 0){

    close(p[1]);
    //read(p[1], message, MSGSIZE);
    write(p[2], message, MSGSIZE);
    
}*/


else{
    //parent process C
    close(p[2]); 
    read(p[0], inbuf, MSGSIZE);
    printf("Pipelined message return:%s\n", inbuf);
    wait(NULL); 
}

exit(0);
}


Last edited by vino; 06-17-2009 at 01:52 AM..