How variables are distributed between two processes when a fork is involved

/* In alarm.c, the first function, ding, simulates an alarm clock. */ #include <signal.h> #include <stdio.h> #include <unistd.h> #include <stdlib.h> static int alarm_fired = 0; void ding(int sig) { alarm_fired = 1; } /* In main, we tell the child process to wait for five seconds before sending a SIGALRM signal to its parent. */ int main() { pid_t pid; printf("alarm application starting\n"); pid = fork(); switch(pid) { case -1: /* Failure */ perror("fork failed"); exit(1); case 0: /* child */ sleep(5); printf("getppid: %d\n", getppid()); kill(getppid(), SIGALRM); exit(0); } /* The parent process arranges to catch SIGALRM with a call to signal and then waits for the inevitable. */ printf("waiting for alarm to go off\n"); (void) signal(SIGALRM, ding); printf("pid: %d\n", getpid()); pause(); if (alarm_fired) printf("Ding!\n"); printf("done\n"); exit(0); } I have run the above code under Ubuntu 10.04 LTS > user@ubuntu :~/Documents/./alarm > alarm application starting > waiting for alarm to go off > pid: 3055 > getppid: 3055 > Ding! > done 

I read the following expression from a book.

It is important to know about the difference between a challenge plug system and creating new threads. When a process makes a fork call, a new copy of the process is created with its own variables and its own PIDs. This new process is planned independently, and (in general) runs almost independently of who created it.

Question: It seems to me that the variable alarm_fired is split between the original process and the newly created process.

Is it correct?

+6
source share
3 answers

No, variables are not shared between fork() . In your code, the child process never touches alarm_fired . What the child does sends a signal to the parents. This signal starts the signal handler in the context of the parent process, setting the variable.

+1
source

Not. Each process gets its own copy of the variable (and almost everything else). If you change a variable in one process, it changes only in this process, and not in both. Each process has its own address space.

Compare this with threads, where all threads share the same address space, so changing a variable in one thread will be visible in all other threads (inside this process).

On Linux fork(2) manpage:

fork () creates a child process that differs from the parent process only in its PID and PPID, and that the use of resources is set to 0. File locks and pending signals are not inherited.

+7
source

It is split in the sense that immediately after fork it has the same meaning in both processes. BUT, when it is ever written to it, this change does not apply to another process (which is different.

Also, see copy while recording for interesting things.

EDIT

It seems that the newly created process has changed the alarm_fired variable which is later considered by the old process

The child sends a signal to the parent element. Then the parent executes the handler and personally sets alarm_fired to one. The child himself never touches this variable.

+2
source

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


All Articles