开发者

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

开发者 https://www.devze.com 2023-03-18 17:19 出处:网络
/*In alarm.c, the first function, ding, simulates an alarm clock.*/ #include <signal.h> #include <stdio.h>
/*  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?


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.


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.


No, variables are not shared across a fork(). In your code, the child process never touches alarm_fired. What the child does is send a signal to the parent. That signal fires a signal handler in the parent process' context, setting the variable.

0

精彩评论

暂无评论...
验证码 换一张
取 消