Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How variables are shared between two process when the fork is involved

Tags:

c

linux

fork

/*  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 have read the following statement from a book.

It’s important to be clear about the difference between the fork system call and the creation of new threads. When a process executes a fork call, a new copy of the process is created with its own variables and its own PID. This new process is scheduled independently, and (in general) executes almost independently of the process that created it.

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

Is that correct?

like image 423
q0987 Avatar asked Jul 05 '11 17:07

q0987


2 Answers

No. Each process gets its own copy of the variable (and pretty much everything else). If you change the variable in one process, it is changed only in that process, not in both. Each process has its own address space.

Compare that with threads, where all threads share a single address space, so a change in a variable in one thread will be visible in all other threads (within that process).

From the Linux fork(2) manpage:

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

like image 157
Adam Batkin Avatar answered Sep 28 '22 19:09

Adam Batkin


It is shared in the sense that immediately after the fork it has the same value in both processes. BUT when either writes to it the change is not propagated to the other process (that what different .

Also, see copy on write for interesting stuff.

EDIT

It seems that the new created process modified the variable alarm_fired which is then later seen by the old process

The child is sending a signal to the parent. The parent then executes the handler and personally sets alarm_fired to one. The child itself never touches that variable.

like image 21
cnicutar Avatar answered Sep 28 '22 18:09

cnicutar