I am trying to understand how signal handler work. I saw an example on geeksforgeek:
int val = 10;
void handler(int sig){
val += 5;
}
int main(){
pid_t pid;
signal(SIGCHLD, handler);
if((pid = fork()) == 0){
val -= 3;
exit(0);
}
waitpid(pid, NULL, 0);
printf("val = %d\n", val);
exit(0);
}
I am confused about why the output value is 15. Initially, I guessed it is because the child process has variable val in different address. However, when I tried to print out the address of val in both child process and its parent process, they both display the same memory address.