![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| URGENT:::Can anybody help me in creating message queue appliction?? | arunchaudhary19 | High Level Programming | 9 | 11-20-2007 07:15 AM |
| creating 10 process | kpkant123 | Shell Programming and Scripting | 2 | 05-21-2007 05:28 PM |
| creating child process | Confuse | High Level Programming | 12 | 05-27-2005 11:48 AM |
| Redirection or piping error message | mariner | Shell Programming and Scripting | 2 | 05-10-2005 03:04 PM |
| How to know a new file is in process of creating? It has not been closed. | linkjack | High Level Programming | 2 | 02-11-2003 01:55 PM |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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 12:52 AM.. |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|