Sponsored Content
Top Forums Programming Creating 3 process and piping a message Post 302330539 by p00ndawg on Wednesday 1st of July 2009 02:30:03 PM
Old 07-01-2009
Quote:
Originally Posted by tetsujin
Sure. The reason you wound up with a total of four processes is because each call to fork() returns twice: once in the original process and once in the new one. So after you make the first call to fork(), there are two processes - both of which go on to call the second fork().

To create a total of three processes, make sure you only fork twice. You can do this by checking the return value of fork() to make sure you're in the process which should fork.

Code:
pid_b = fork();
if (pid_b == 0)
{
    /* fork() returned 0, so this is the new process, which I call process B. */
    pid_c = fork();
    if (pid_c == 0)
    {
        /* This is process C... */
    }
}

Now, if your communication channels only need to be such that A can send messages to B, and B can send messages to C, you wind up with something like this:

Code:
pipe(channel_ab);
 pid_b = fork();
if (pid_b > 0)
{
    /* Process A...  Send messages to B using channel_ab[1] */
    close(channel_ab[0]);
    write(channel_ab[1], msg, size);
} else if (pid_b == 0) {
    /* Process B */
    close(channel_ab[1]);

    pipe(channel_bc);
    pid_c = fork();
    if (pid_c > 0)
    {
        /* Still process B.  Receive messages from A using channel_ab[0], send messages to C using channel_bc[1]. */
        close(channel_bc[0]);
        msg_size = read(channel_ab[0], buffer, size);
        write(channel_bc[1], msg, size);
    } else if (pid_c == 0) {
        /* Process C.  Receive messages from B using channel_bc[0]. */
        msg_size = read(channel_bc[0], buffer, size);
    }
}

'Course there's various error checking and stuff you'd want to do in there that's not shown above - but that's the basic idea.
oh ok thanks alot, I think i understand now . I have a project coming up where ill need to use some pipes and I this really fills in some of my blanks.

thanks again.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Redirection or piping error message

I have written a script that appears to work correctly in testing, but is coming up with a starnge error message, script_name: test: 0403-004 Specify a parameter with this command. Redirection or piping of stdin or stdout is allowed only with -b. (156). The script is run by different... (2 Replies)
Discussion started by: mariner
2 Replies

2. Programming

creating child process

i want to create 3 child processes from the same parent using folk. I know how to use folk but my child processes did not come from the same parent. Any suggestion what i did wrong ? (12 Replies)
Discussion started by: Confuse
12 Replies

3. Shell Programming and Scripting

creating 10 process

can anyone tell me how to Create ten processes and display useful information about them including PID, starting-time in unix using c or c++ language. i tried to create 10 different processes using fork and exec but i couldn't display those process info:PID,STIME. my sample code ......... (2 Replies)
Discussion started by: kpkant123
2 Replies

4. Programming

message queues and multi-process

Hi, Am supposed to use message queues to send and receive messages between the processes. when i was working on that i realised that the message qid and the message queue related data should be maintained in a shared memory so that it can be accessed by all the processes. Could anybody refer... (10 Replies)
Discussion started by: rvan
10 Replies

5. Programming

URGENT:::Can anybody help me in creating message queue appliction??

hello, I had to implement a message queue application....between 30 processes...... all 30 proceses are getting data from serial port.... And here is THE FLOW::::::::1 connector process...which is linked with message queue to all the 30 applications. Processes get the data from serial port and... (9 Replies)
Discussion started by: arunchaudhary19
9 Replies

6. Shell Programming and Scripting

How to get rid of message when script kills process it started?

When I run the following script I get the following error message whcih I would like to suppress when the kill is issued: ./kill.sh: line 13: 31854 Killed nc -l -p 12345 Script: #!/bin/bash echo running nc in the background nc -l -p 12345 & PID=$! echo nc pid: $PID ... (1 Reply)
Discussion started by: cmarkle
1 Replies

7. Programming

Permission denied when creating message queue

Hi guys. i have wrote a simple program to test message queue attributes. here it is: #include <stdio.h> #include <stdlib.h> #include <mqueue.h> #include <fcntl.h> #include <string.h> #include <errno.h> #include <sys/stat.h> int main() { struct mq_attr attr; mqd_t mqd; ... (2 Replies)
Discussion started by: majid.merkava
2 Replies

8. Programming

creating a message queue using mq_open

Hi all, First of all thanks in advance for reading my post and for your heart for helping me. I am trying to create a message queue using mq_open(name,oflags,mode_t,attr) method. But that function call is returning with an error code EFAULT. By googling it I found that it happens when there is... (10 Replies)
Discussion started by: parusasi
10 Replies

9. Shell Programming and Scripting

Getting warning message while creating a file thru script

I have a script, it will create 3 files --------- file1=dataout/file1.txt file2=dataout/file2.txt file3=dataout/file3.txt echo "0" > $file3 --------- For file1 and file2 we are not initializing any values. But for file3 we are initializing the value of 0. When we execute the... (3 Replies)
Discussion started by: kmanivan82
3 Replies

10. AIX

Process using/creating files in the filesystem

Hello Team, In a application filesystem, there is a process keep creating the log files. Due to that the filesystem keep getting full. Please let me know how to identify the process which is keep writing in the filesystem. fuser -u <FS> will show only the user who using the filesystem.... (3 Replies)
Discussion started by: gowthamakanthan
3 Replies
ADVISE(3PVM)							  PVM Version 3.4						      ADVISE(3PVM)

NAME
pvm_advise() - Controls use of direct task-to-task routing. [In Version 3.2: Replaced by pvm_setopt] SYNOPSIS
C int info = pvm_advise( int route ) Fortran call pvmfadvise( route, info ) PARAMETERS
route Integer advising PVM to set up direct task-to-task links. route options PvmDontRoute 1 Don't allow direct links to this task PvmAllowDirect 2 Allow but don't request direct links PvmRouteDirect 3 Request direct links info Integer returning error status. DESCRIPTION
The routine pvm_advise advises PVM on whether or not to set up direct task-to-task links (using TCP) for all subsequent communication. Once a link is established it remains until the application finishes. If a direct link can not be established because one of the two tasks has requested PvmDontRoute or because no resources are available, then the default route through the PVM daemons is used. pvm_advise can be called multiple times to selectively establish direct links, but is typically set only once near the beginning of each task. PvmAllowDi- rect is the default advise setting. This setting on task A allows other tasks to set up direct links to A. Once a direct link is estab- lished between tasks both tasks will use it for sending messages. pvm_advise returns the error status in info. The performance of direct task-to-task links can be up to a factor of two better than the default route. The draw back is a lack of scalability of the direct links. Some versions of UNIX limit the number of links to no more than 30. EXAMPLES
C: info = pvm_advise( PvmRouteDirect ); Fortran: CALL PVMFADVISE( PVMROUTEDIRECT, INFO ) ERRORS
This error condition can be returned by pvm_advise PvmBadParam giving an invalid route value. SEE ALSO
pvm_setopt(3PVM) 30 August, 1993 ADVISE(3PVM)
All times are GMT -4. The time now is 08:06 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy