Question on pipes in C


 
Thread Tools Search this Thread
Top Forums Programming Question on pipes in C
# 1  
Old 04-11-2011
Error Question on pipes in C

If a code forks 2 childs, what can the values be for the process id's of each of the child? I child pid is supposed to be 0, but what if you fork 2 of them?
# 2  
Old 04-11-2011
Each child is independent and doesn't care what children were created before or after it. The return value of fork() is 0 in each child.
# 3  
Old 04-11-2011
so if a server creates 2 childs, how does it tell, which child is which if both have a pid of 0?

---------- Post updated at 03:13 PM ---------- Previous update was at 03:12 PM ----------

in a basic pipe example, they use
if pid>0 (then its the parent)
else of pid=0 (then its a child)
else error

so like this how do you seperate the 2 children?
# 4  
Old 04-11-2011
Quote:
Originally Posted by omega666
so if a server creates 2 childs, how does it tell, which child is which if both have a pid of 0?
They do not have PIDs of zero. Nothing has a PID of zero.
Quote:
so like this how do you seperate the 2 children?
I think this is one of these things we went over many moons ago...

Remember, the child is a complete clone of the parent. It will have a copy of any variables the parent had at the instant it made the fork. Beyond that it becomes basic logic.

Code:
int main(void)
{
    int n;

    for(n=0; n<10; n++)
    {
        pid_t pid=fork();
        if(pid < 0)
        {
            perror("Couldn't fork");
        }
        else if(pid > 0) // parent code
        {
            fprintf(stderr, "child %d = pid %d\n", n, pid);
        }
        else if(pid == 0) // child code
        {
            // Child will print different numbers depending on what child it is
            fprintf(stderr, "I am child %d\n", n);

            switch(n)
            {
            case 0:
                    fprintf(stderr, "Stuff only child 0 does\n");
                    break;
            case 1:
                    fprintf(stderr, "stuff only child 1 does\n");
                    break;
            default:
                    fprintf(stderr, "stuff any other child does\n");
                    break;
            }
            exit(0);
        }
    }

    for(n=0; n<10; n++)
    {
       pid_t pid;
       int status;
       wait(&status);
       fprintf(stderr, "pid %d returned %d\n", WEXITSTATUS(status));
    }
}

# 5  
Old 04-11-2011
but in your code, your forking 10 times, but how do you refer to ones u made before,

if I do this:
Code:
pid1=fork(); //first child has pid=0
if pid1>0 {
    pid2=fork(); // second child has pid=0
}

if first child {
 print hi1
}
if second child {
  print hi2
}

how would this work?

Last edited by pludi; 04-11-2011 at 06:46 PM..
# 6  
Old 04-12-2011
Save a variable that tells them which one is which. They'll get a copy of the variable as of the instant they forked. This is basic logic.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

[C] execl and pipes?

Hi, I have two programs, one is named "Master" and the other one "slave". What I want to do is , when I execute Master, inside slave will be called by excecl, do some calculations, and send those to the master program... A little example of what I am failing to do: if ((PID1=fork())==0) { //... (6 Replies)
Discussion started by: lamachejo
6 Replies

2. Programming

Problem with pipes

problem solved. (1 Reply)
Discussion started by: superfons
1 Replies

3. Shell Programming and Scripting

How to combine these to pipes?

ls --color=always -laX | awk '{print $1, $3, $4, $2, $8}' |sort -k 1,1 -k 9,9r they work separately... but i don't know how to combine this to work. thx! (1 Reply)
Discussion started by: surreal7z
1 Replies

4. Programming

Pipes in C

Hello all, I am trying to learn more about programming Unix pipes in C. I have created a pipe that does od -bc < myfile | head Now, I am trying to create od -bc < myfile | head | wc Here is my code, and I know I might be off, thats why I am here so I can get some clarification. #include... (1 Reply)
Discussion started by: petrca
1 Replies

5. Shell Programming and Scripting

Pipes not working

Hi, thanks for b4. can anyone tell me why following not working: noUsers=$(who | cut -d" " -f1 | wc -l) What i'm trying to do is get a list of logged on users and pass it to 'wc -l' and store the output to a variable. Any ideas? (1 Reply)
Discussion started by: Furqan_79
1 Replies

6. UNIX for Advanced & Expert Users

Consolidating Pipes

This is something I've given a lot of thought to and come up with no answer. Say you have a data stream passing from a file, through process A, into process B. Process A only modifies a few bytes of the stream, then prints the rest of the stream unmodified. Is there any way to stream the file... (4 Replies)
Discussion started by: Corona688
4 Replies

7. UNIX for Advanced & Expert Users

Question on forks and pipes

I am trying to figure out why when i have the following code int main( { printf("0\n"); fork(); printf("1\n"); exit(0);} and type in the shell a.out | cat the output of this program is 0 1 0 1 instead of 0 1 1 does anyone know? (3 Replies)
Discussion started by: Phantom12345
3 Replies

8. Shell Programming and Scripting

cd using pipes

Hi, Can the cd command be invoked using pipes??? My actual question is slightly different. I am trying to run an executable from different folders and the path of these folders are obtained dynamically from the front end. Is there a way in which i can actually run the executable... (2 Replies)
Discussion started by: Sinbad
2 Replies

9. Filesystems, Disks and Memory

PIPEs and Named PIPEs (FIFO) Buffer size

Hello! How I can increase or decrease predefined pipe buffer size? System FreeBSD 4.9 and RedHat Linux 9.0 Thanks! (1 Reply)
Discussion started by: Jus
1 Replies

10. Solaris

Question about pipes in solaris (and others) and buffering....

This is weird, so I'm hoping someone here knows solaris and how it handles pipes... OK... here goes... Theres this log file, right? I want to tail -f it, grep that, gzip that, then pipe that into more commands. Well thats easy, right? tail -f file | grep pattern | gzip | otherstuff... ... (1 Reply)
Discussion started by: sannik
1 Replies
Login or Register to Ask a Question