The values ​​of the variables in the Fork () file

I already asked one question about fork (), here's another. Given the following code:

#include <unistd.h>
#include <stdio.h>

int main()
{
    pid_t pid1, pid2;
    pid1 = fork();
    pid2 = fork();
    if (pid1 != 0 && pid2 != 0)
        printf("A\n");
    if (pid1 != 0 || pid2 != 0)
        printf("B\n");
    exit(0);
}

After the second fork(), what will the values ​​be pid1and pid2?
As far as I understand, the first fork installs pid1 > 0and will be identical in all child elements created later. However, what will happen to pid2?

Thank!

+3
source share
5 answers

I am going to check this out for you, but let me tell you what I expect.

                               / pid2 = [child3 pid] {pid1 = child1; pid2 = child3;}
        pid1 = [child1 pid] fork ()
       / \
      /                          pid2=0               { pid1 = child1;pid2 = 0;}
fork()
      \                          pid2=[child2 pid]    { pid1 = 0;     pid2 = child2;}
       \                       /
        pid1=0         - fork()
                               \ 
                                 pid2=0               { pid1 = 0;     pid2 = 0;}

EDIT .

#include <stdio.h>
#include <unistd.h>

int main()
{
    pid_t pid1, pid2;
    pid1 = fork();
    pid2 = fork();
    printf("PID %d: pid1=%d, pid2=%d\n",getpid() ,pid1, pid2);
    exit(0);
}

:

PID 30805: pid1 = 30806, pid2 = 30807
PID 30806: pid1 = 0, pid2 = 30808
PID 30807: pid1 = 30806, pid2 = 0
PID 30808: pid1 = 0, pid2 = 0

+2

, . fork PID . fork 0.

Parent process:
pid1 = PID of child 1
pid2 = PID of child 3

Child 1
pid1 = 0
pid2 = PID of child 2

Child 2
pid1 = 0
pid2 = 0

Child 3
pid1 = PID of child 1
pid2 = 0
+6

, , :

#include <unistd.h>
#include <stdio.h>
int main (void) {
    pid_t pid1 = -1, pid2 = -1;
    pid1 = fork();
    pid2 = fork();
    printf ("%5d/%5d: %5d %5d\n", getpid(), getppid(), pid1, pid2);
    sleep (5); // to prevent inheritance by init process on parent death
    return 0;
}

:

  PID/ PPID   pid1  pid2
 ----------   ----  ----
 2507/ 2386:  2508  2509      first process
 2508/ 2507:     0  2510      first fork from 'first process'
 2509/ 2507:  2508     0      second fork from 'first process'
 2510/ 2508:     0     0      second fork from "first fork from 'first process'"

:

  • 2507 pid1 pid2 .
  • 2508 pid1 0, fork, pid2 2510, .
  • 2509 pid1 ( ), . pid2 0, .
  • 2510 pid1 ( ), . pid2 0, .
+4

fork(), pid1 pid2?

, . ( ) :

A (original process)
` - B (created by first fork in original process)
|   ` - C (created by second fork in B)
` - D (created by second fork in original process)

, A, pid1 > 0 pid2 > 0, .

B, pid1 == 0 pid2 > 0, .

C, pid1 == 0 pid2 == 0, pid1 (B) fork.

D, pid1 > 0 pid2 == 0, pid1 (A) .

, , , . , , , , .

+2

: http://linux.die.net/man/2/fork

pid1 0 , , - . pid2 0 , . - .

                                            /-[parent]pid1=?, pid2=?
      /-[parent]pid1=?, pid2=uninit -> fork()
fork()                                      \-[child2 of parent]pid1=?, pid2=0
     \
      \                                               /-[child1 of parent]pid1=0,pid2=?
       \[child1 of parent] pid1=0, pid2=uninit -> fork()
                                                      \-[child of child] pid1=0, pid2=0
+1

Source: https://habr.com/ru/post/1780576/


All Articles