Parent Process

In the below code, I ran and the output gave me

0 for Line A
26111 for Line B
26111 for Line C
26110 for Line D

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main()
{
pid_t pid, pid1;

//fork a child process
pid = fork();

if (pid< 0) //error occurred
{
fprintf(stderr, "Fork Failed");
return 1;
}
else if (pid == 0) //child process
{
pid1 = getpid();
printf("child: pid = %d", pid); //line A
printf("child: pid1 = %d", pid1); //line B
}
else //parent process
{
pid1 = getpid();
printf("parent: pid = %d", pid); //line C
printf("parent: pid1 = %d", pid1); //line D
wait(NULL);
}

return 0;
}


My question is what would the output be if the parent pid was 2600 and the child pid was 2603? Is there a way to use those variables in the program. JUst by looking, I would think it would produce 0, 2600, 2600, 2603. Is this right?
Topic archived. No new replies allowed.