I am learning c program fork and pipe. I create a fork shown below.
parent=> child1=>child2
In parent we are creating a pipe.And the file descriptor are stored in parentpip.
In Child1 also i am creating a pipe. And the file descriptor are stored in child1pip.
Now when i check the process id fd in /proc//fd. Below things are getting displayed.
If i do ls -l /proc/15585/fd .. This is for parent
lrwx------ 1 vipin vipin 64 Jan 14 17:33 0 -> /dev/pts/1
lrwx------ 1 vipin vipin 64 Jan 14 17:33 1 -> /dev/pts/1
lrwx------ 1 vipin vipin 64 Jan 14 17:33 2 -> /dev/pts/1
lr-x------ 1 vipin vipin 64 Jan 14 17:33 3 -> 'pipe:[354240]'
l-wx------ 1 vipin vipin 64 Jan 14 17:33 4 -> 'pipe:[354240]'
lr-x------ 1 vipin vipin 64 Jan 14 17:33 5 -> 'pipe:[354241]'
l-wx------ 1 vipin vipin 64 Jan 14 17:33 6 -> 'pipe:[354241]'
If i do ls -l /proc/15586/fd .. This is for child1
lrwx------ 1 vipin vipin 64 Jan 14 17:33 0 -> /dev/pts/1
lrwx------ 1 vipin vipin 64 Jan 14 17:33 1 -> /dev/pts/1
lrwx------ 1 vipin vipin 64 Jan 14 17:33 2 -> /dev/pts/1
lr-x------ 1 vipin vipin 64 Jan 14 17:33 3 -> 'pipe:[354240]'
l-wx------ 1 vipin vipin 64 Jan 14 17:33 4 -> 'pipe:[354240]'
lr-x------ 1 vipin vipin 64 Jan 14 17:33 5 -> 'pipe:[357693]'
l-wx------ 1 vipin vipin 64 Jan 14 17:33 6 -> 'pipe:[357693]'
If i do ls -l /proc/15587/fd .. This is for child2
lrwx------ 1 vipin vipin 64 Jan 14 17:33 0 -> /dev/pts/1
lrwx------ 1 vipin vipin 64 Jan 14 17:33 1 -> /dev/pts/1
lrwx------ 1 vipin vipin 64 Jan 14 17:33 2 -> /dev/pts/1
lr-x------ 1 vipin vipin 64 Jan 14 17:33 3 -> 'pipe:[354240]'
l-wx------ 1 vipin vipin 64 Jan 14 17:33 4 -> 'pipe:[354240]'
lr-x------ 1 vipin vipin 64 Jan 14 17:33 5 -> 'pipe:[357693]'
l-wx------ 1 vipin vipin 64 Jan 14 17:33 6 -> 'pipe:[357693]'
Here filedescriptor 3 and 4 is belong to parentpip. So same pipe among all the process pipe:[354240] and pipe:[354240]
When we see filedescriptor 5 and 6 child2 and child1 hold the same child1pip. pipe:[357693] and pipe:[357693]
But i can see 5 and 6 in parent also Why also in parent. And the pip is pipe:[354241] and pipe:[354241]
#include <stdio.h>
#include <stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<string.h>
int main(int argc,char * argv[])
{
int parentpip[2];
pipe(parentpip);
int child1pid =fork();
int child1pip[2];
pipe(child1pip);
if(child1pid==0)
{
printf("This child1");
int child2pid2=fork();
if(child2pid2==0)
{
printf("This child2");
}
sleep(20*60);
}
else
{
printf("This parent");
sleep(20*60) ;
}
return 0;
}
You have these things happening.
Note in point 3, the parent also runs the line of code: pipe(child1pip);
creating a new pipe - and so does the child1 process - both these processes creates their own pipes since this happens after fork().
You probably only need child1 to run pipe(child1pip);
so change this code:
int child1pip[2];
pipe(child1pip);
if(child1pid==0)
{
To this code:
if(child1pid==0)
{
int child1pip[2];
pipe(child1pip
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.