The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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



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

 
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #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 12:52 AM..
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:18 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0