#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?
q0987 source share