In original program kill(pid, SIGUSR1); is called first and the pause(); in parent process and In child process pause(); is called first and then kill(getppid(), SIGUSR1); its output is given below
In changed program if I replace the kill(getppid(), SIGUSR1); with pause(); in child process output is totally different I have pasted the output below the code.
Can someone explain me the why the output is changed
**********************ORIGINAL PROGRAM***********************************
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
void action(int dummy){
sleep(1);
printf("Switching\n");
}
int main(int argc, char *argv[]){
pid_t pid;
if((pid=fork())>0){//parent
sleep(1);
while(1){
printf("Parent is running\n");
kill(pid, SIGUSR1);
signal(SIGUSR1, action);
pause();
}
}
else //child code
while(1){//child
signal(SIGUSR1, action);
pause();
printf("Child is running\n");
kill(getppid(), SIGUSR1);
}
}
//OUTPUT OF THIS PROGRAM
Parent is running
Switching
Child is running
Switching
Parent is running
Switching
Child is running
Switching
Parent is running
Switching
Child is running
Switching
Parent is running
Switching
Child is running
Switching
Parent is running
Switching
Child is running
*********************CHANGED PROGRAM************************
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
void action(int dummy){
sleep(1);
printf("Switching\n");
}
int main(int argc, char *argv[]){
pid_t pid;
if((pid=fork())>0){//parent
sleep(1);
while(1){
printf("Parent is running\n");
kill(pid, SIGUSR1);
signal(SIGUSR1, action);
pause();
}
}
else //child code
while(1){//child
signal(SIGUSR1, action);
kill(getppid(), SIGUSR1);
printf("Child is running\n");
pause();
}
}
//OUTPUT OF THIS PROGRAM
//Child is running
//User defined signal 1
If a process receives SIGUSR1 before it has set up a signal handler for it (and is not ignoring or holding it), the process will be terminated. (For details. see the signal man page).
Your code (both versions) has several race conditions.
In the first version:
if((pid=fork())>0){//parent
sleep(1);
while(1){
printf("Parent is running\n");
kill(pid, SIGUSR1);
signal(SIGUSR1, action);
pause();
}
}
else //child code
while(1){//child
signal(SIGUSR1, action);
pause();
printf("Child is running\n");
kill(getppid(), SIGUSR1);
}
The child will wait for SIGUSR1.
At about the same time, the parent will sleep, then send SIGUSR1 to the child.
The child, after the signal has been received, will do a couple printfs, then send SIGUSR1 to the parent.
At about the same time, the parent will set up a signal handler for SIGUSR1.
It's likely (but not necessarily always the case) that the child will set up the signal handler while the parent is doing the sleep(1); the signal sent from the parent to the child will be caught rather than causing the child to be terminated.
It's likely (but not necessarily always the case) that by the time the child has done its two printfs, the parent has set up the signal handler; the signal sent from the child to the parent will be caught rather than causing the parent to be terminated.
But, since there are race conditions, slight changes in timing can cause things to break.
In the second version:
if((pid=fork())>0){//parent
sleep(1);
while(1){
printf("Parent is running\n");
kill(pid, SIGUSR1);
signal(SIGUSR1, action);
pause();
}
}
else //child code
while(1){//child
signal(SIGUSR1, action);
kill(getppid(), SIGUSR1);
printf("Child is running\n");
pause();
}
The child sends SIGUSR1 to the parent just after it forks, and this will almost certainly happen while the parent is in the middle of the sleep(1). Since the parent hasn't yet set up a handler for SIGUSR1, the signal will terminate it. The shell then prints out User defined signal 1, which is the long name of the SIGUSR1 signal.
Things will work better if you set up a signal handler for SIGUSR1 before the fork. That way, both parent and child will be ready to handle the signal.
Related
I`m writing the program in which parent and child processes synchronize their actions with signals.
So, as result, I need a repeated cycle:
The parent process should send signal SIGUSR1 to a group of the child processes.
After getting the signal from the parent, the child processes should send signal SIGUSR2 to the parent.
After getting the signals from all child processes, the parent process should sleep for 1 s. But if the parent process gets signal SIGTERM from the child, the parent should send SIGTERM to all child processes and they should stop working.
I`ve already written how to create the group of child processes and send to them signal SIGUSR1. But I don't know how to send signal SIGUSR2 from child processes to the parent process and verify if all child processes sent a signal and if the sent signals contain SIGTERM signal.
Please, help me!
##include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
static volatile sig_atomic_t got_signal;
void sig_child(int signo){
printf("Signal caught.");
}
void handler(int sig)
{
printf("caught signal: %d\n", getpid());
got_signal = 1;
signal(SIGUSR2, sig_child);
printf("sent signal: %d\n", getpid());
kill(getppid(), SIGUSR2);
}
int main() {
pid_t child;
pid_t children[3];
int status;
int i = 0;
signal(SIGUSR1, handler);
for (; i < 3; i++) {
switch (child = fork()) {
case -1:
perror("could not create child: ");
break;
case 0:
printf("child: %d\n", getpid());
while (!got_signal);
_exit(0);
default:
children[i] = child;
/*put all children in process group of eldest child*/
setpgid(child, children[0]);
}
}
sleep(1);
/* send signal to all child by sending signal to process group of eldest child */
kill(-children[0], SIGUSR1);
int n = 0;
while (n < 3) {
waitpid(children[n], &status, 0);
n++;
}
exit(0);
}
I am trying to register a handler for the SIGTRAP caused by calling int3 in child, but it does not work. Changing SIGTRAP to SIGCHLD works.
#include <signal.h>
#include <wait.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
void child();
void parent(pid_t pid);
void sigtrap_handler(int sig);
int main(){
pid_t pid = fork();
if(pid == 0){
child();
} else {
parent(pid);
}
}
void child(){
sleep(1);
asm("int3");
exit(EXIT_SUCCESS);
}
void parent(pid_t pid){
signal(SIGTRAP, sigtrap_handler);
int status;
do{
waitpid(pid, &status, WUNTRACED | WCONTINUED);
} while(!WIFEXITED(status) && !WIFSIGNALED(status));
exit(EXIT_SUCCESS);
}
void sigtrap_handler(int sig){
printf("Process %d received sigtrap %d.\n", getpid(), sig);
}
Only the parent process has registered a signal handler for SIGTRAP but not the child process. So when you raise SIGTRAP in child process, the parent process process is not aware of it.
SIGCHLD works (i.e. received by parent) because when the child process terminates (or stops), the signal SIGCHLD is sent to the parent process. But in case of SIGTRAP (or any other signal), it's not sent to the parent process. SIGCHLD is special in the sense, it's sent by default to the parent process on termination whereas other signals are not.
If you add a handler in child process, you'll see the handler getting invoked:
void child(){
signal(SIGTRAP, sigtrap_handler);
sleep(1);
asm("int3");
exit(EXIT_SUCCESS);
}
By the way, printf() is not an async-signal-safe function, so you can't safely call it in a signal handler.
I am trying to use signals to pass between a parent and child process, but after the first 2 statements are printed
for example in mine it shows:
CHILD 4225: Running, parent is 4224
PARENT 4224: Telling the Child Process 4225 to start
it just gets stuck running forever! I'm not sure where I am going wrong on this...
#include<stdio.h>
#include<signal.h>
#include<unistd.h>
#include<stdlib.h>
#include<errno.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
void p_sig_usr(int signo){
if(signo == SIGUSR1){
printf("*** Parent SIGUSR1 handler - Received 'task started' signal from child ***\n");
}
if(signo == SIGUSR2){
printf("*** Parent SIGUSR2 handler - Received 'task completed' signal from child ***\n");
}
else
printf("unexpected signal received");
return;
}
void c_sig_usr(int signo){
if(signo == SIGUSR1){
printf("*** Child SIGUSR1 handler - Received 'task start' signal from parent ***\n");
}
if(signo == SIGUSR2){
printf("*** Child SIGUSR2 handler - Received 'task complete verification' signal from parent ***\n");
}
else
printf("unexpected signal received");
return;
}
int main(void)
{
pid_t child_pid, parent_pid;
parent_pid = getpid();
struct sigaction p_sig;
sigemptyset(&p_sig.sa_mask);
p_sig.sa_flags = 0;
p_sig.sa_handler = p_sig_usr;
child_pid = fork();
if ( child_pid == -1){
perror("failed to fork a new process");
return 1;
}
if (child_pid == 0){
struct sigaction c_sig;
sigset_t c_myset;
sigemptyset(&c_sig.sa_mask);
c_sig.sa_flags = 0;
c_sig.sa_handler = c_sig_usr;
child_pid = getpid();
printf("CHILD %d: Running, parent is %d\n",child_pid, parent_pid);
sigfillset(&c_myset);
sigdelset(&c_myset, SIGUSR1);
sigsuspend(&c_myset);//suspend until get SIGUSR1
printf("CHILD: Telling parent that I'm starting task.\n");
sleep(3);
kill(parent_pid, SIGUSR1);
printf("CHILD: Performing task\n");
sigfillset(&c_myset);
sigdelset(&c_myset, SIGUSR2);
sigsuspend(&c_myset);//suspend and wait for SIGUSR2
printf("CHILD: Telling parent that work is done.\n");
kill(parent_pid, SIGUSR2);
printf("CHILD %d: Finished\n", child_pid);
}
else{
struct sigaction p_sig;
sigset_t p_myset;
sigemptyset(&p_myset);
sleep(3);//parent now sleeping to let child set up handlers
printf("PARENT %d: Telling the Child Process %d to start\n", parent_pid, child_pid);
kill(child_pid, SIGUSR1);
sigfillset(&p_myset);
sigdelset(&p_myset, SIGUSR1);
sigsuspend(&p_myset);//suspend until get SIGUSR1
sleep(3);
kill(child_pid,SIGUSR2);
printf("PARENT: Told child to notify of task completion.\n");
sigfillset(&p_myset);
sigdelset(&p_myset, SIGUSR2);//suspend until get SIGUSR2
printf("PARENT %d: Finished.", parent_pid);
}
return 0;
}
Thank you in advance for the help!
I'm just referring to the documentation for these functions—I have no experience using them.
It appears what sigfillset() is going to do is load the process signal mask into your sigset_t. This means that your sigset_t is going to contain the set of signals that are currently blocked by your process. I assume the default is nothing is blocked, so the set would be empty.
You might want to test this by printing out the contents of the set, or just looking at it in a debugger.
Now from the docs I understand what sigdelset(&p_myset, SIGUSR1) will do is remove the signal SIGUSR1 from the set you just filled. This set is by assumption already empty so it's unlikely this call does anything. Again, verify by looking at it in a debugger.
So now what sigsuspend() is going to do is replace your process signal mask with your new mask, which by assumption isn't any different than the default mask (again, check this in a debugger). Then on the child side will wait until the process receives SIGUSR1 and processes it via a signal handler. So your child will process SIGUSR1 but only because that's the default behaviour.
Your example code doesn't seem to have installed any signal handlers. I think you would have to call the sigaction() function to do that. Therefore very likely the default signal handler will run to process SIGUSR1.
According to this page, the default signal handling for SIGUSR1 is
(i) ... Abnormal termination of the process. The process is terminated with all the consequences of _exit() except that the status made available to wait() and waitpid() indicates abnormal termination by the specified signal.
So I'm guessing the child dies when the parent does kill(child_pid, SIGUSR1). This would mean the child isn't around to signal the parent back.
This is mainly guesswork on my part. What I recommend for you is learning how to use gdb or some other debugger so you can set some breakpoints and step through and learn what the program is actually doing.
You forgot to call sigaction after defining the struct sigaction on both the parent and child. Also, beware that the struct sigaction p_sig is redefined in the parent process.
So, I guess if you change your program to something like listed below, it should work.
--- foo.c 2014-06-16 16:37:10.918932118 -0300
+++ bar.c 2014-06-16 16:37:48.710228467 -0300
## -36,10 +36,6 ##
{
pid_t child_pid, parent_pid;
parent_pid = getpid();
- struct sigaction p_sig;
- sigemptyset(&p_sig.sa_mask);
- p_sig.sa_flags = 0;
- p_sig.sa_handler = p_sig_usr;
child_pid = fork();
if ( child_pid == -1){
perror("failed to fork a new process");
## -51,6 +47,7 ##
sigemptyset(&c_sig.sa_mask);
c_sig.sa_flags = 0;
c_sig.sa_handler = c_sig_usr;
+ sigaction(SIGUSR1, &c_sig, NULL);
child_pid = getpid();
printf("CHILD %d: Running, parent is %d\n",child_pid, parent_pid);
sigfillset(&c_myset);
## -69,6 +66,10 ##
}
else{
struct sigaction p_sig;
+ sigemptyset(&p_sig.sa_mask);
+ p_sig.sa_flags = 0;
+ p_sig.sa_handler = p_sig_usr;
+ sigaction(SIGUSR1, &p_sig, NULL);
sigset_t p_myset;
sigemptyset(&p_myset);
sleep(3);//parent now sleeping to let child set up handlers
What I want to do is create a parent process that lasts for 5 seconds. I also want it to send a SIGUSR1 signal every second. On this signal I want the child to do something.
The code that I put together so far is:
void alarmHandler(int sig) {
printf("\nalarm called\n");
exit(0);
}
void childHandler(int sig) {
printf("child called");
signal(SIGUSR1, childHandler);
}
int main() {
pid_t val;
if((val = fork())) { //parinte
signal(SIGALRM, alarmHandler);
printf("parent");
alarm(5);
while(1) {
kill(val, SIGUSR1);
sleep(1);
}
}else {
signal(SIGUSR1, childHandler);
printf("child");
}
return 0;
}
What I get is:
child
parent
alarm called
What I want:
child
parent
child called
child called
child called
child called
child called
alarm called
Your parent has the while loop. The child does the following:
signal(SIGUSR1, childHandler);
printf("child");
And then exits.
If it does receive SIGUSR before the exit, this will also be executed
printf("child called");
signal(SIGUSR1, childHandler);
Therefore you have a race condition as the number of child called is printed.
Just put a while (1) {} after the printf("child");
Base on the original code, add two blocks:
flush the printf: setbuf(stdout, NULL);
keep the child running: while (1) pause();
The code list as follow:
#include "stdio.h"
#include "stdlib.h"
#include <signal.h>
/* For a real-world program, printing from a signal handler is not very safe.
* A signal handler should do as little as it can, preferably only setting a flag here or there.
* And the flag should be declared `volatile`.
* Real-world example:
* I once worked on a system that used an Access database as a back end,
* and under certain circumstances a printf() call in a signal handler would write to the .mdb file instead of stdout,
* hosing the database beyond repair.
*/
void alarmHandler(int sig)
{
printf("\nparent signal alarm handler: times up\n");
exit(0);
}
void childHandler(int sig)
{
printf("\nchild signal handler\n");
// The old style should install the handler again.
signal(SIGUSR1, childHandler);
}
int main()
{
pid_t val;
signal(SIGALRM, alarmHandler);
// If not set this, we cann't the child's output.
// The stdout stream is buffered, so will only display what's in the buffer after it reaches a newline (or when it's told to).
setbuf(stdout, NULL);
if ((val = fork())) { //parinte
printf("\nparent\n");
// #Note that there is only one alarm clock per process.
// the alarm() function will return a value if another alarm has been previously set.
//
// sets a timer that generates the SIGALARM signal when times up.
// If we ignore or don’t catch this signal, the process is terminated.
alarm(5);
while(1) {
sleep(1);
kill(val, SIGUSR1);
}
printf("\nparent exit\n");
} else {
signal(SIGUSR1, childHandler);
printf("\nchild\n");
while (1)
pause(); // The pause() function suspends the process until a signal is caught.
printf("\nchild exit\n");
}
return 0;
}
And the output:
parent
child
child signal handler
child signal handler
child signal handler
child signal handler
parent signal alarm handler: times up
I'm learning the signal of inter process communication, I made the very simple test code below:
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
void sighup();
void sigint();
void sigquit();
int main(int argc, const char *argv[])
{
int child_pid;
if((child_pid = fork()) < 0) exit (1);
if(child_pid == 0) {
sleep(2);
signal(SIGHUP, sighup);
signal(SIGINT, sigint);
signal(SIGQUIT, sigquit);
puts("this is the end of the child process");
} else {
printf("\n Parent: sending SIGHUP signal to child\n\n");
kill(child_pid, SIGHUP);
printf("\n Parent: sending SIGINT signal to child\n\n");
kill(child_pid, SIGINT);
printf("\n Parent: sending SIGQUIT signal to child\n\n");
kill(child_pid, SIGQUIT);
}
}
void sighup() {
signal(SIGHUP, sighup);
printf("CHILD: I have received a SIGHUP\n");
}
void sigint() {
signal(SIGINT, sigint);
printf("CHILD: I have received a SIGINT\n");
}
void sigquit() {
sleep(2);
printf("CHILD: My parent process has killed me!!");
printf("CHILD: cleaning up...\n");
exit(0);
}
It seems like the child process doesn't do anything, even doesn't print the end of the process string. any idea?
Your signal handlers are not being invoked in the child because of a race condition. The parent thread sends the child thread a signal before the child calls signal() that overrides the signal handling behavior.
In this case, the child receives a SIGINT and performs its default behavior, which is to terminate. Thus the child terminates before executing the statements after sleep(2).