I don't understand what is happening here, I have a parent process which handles the SIGINT signal and then makes a child. What I expect when I press Ctrl+C is that both processes will print "SIGINT received" and then continue but it turns out that the parent process dies after receiving SIGINT but the child is still there. I can't understand that.
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <sys/signal.h>
#include <string.h>
void handler (int sig) {
printf("SIGINT received\n");
}
void child() {
while (1) {
printf("I'm the child\n");
sleep(1);
}
exit(0);
}
int main(int argc, char *argv[]) {
struct sigaction act;
memset(&act, 0, sizeof(act));
act.sa_handler = &handler;
// Link SIGINT with the handler
sigaction(SIGINT, &act, NULL);
// Create child
if (fork() == 0) child();
wait(NULL);
return 0;
}
An example of execution:
$ ./test_signals
I'm the child
^CSIGINT received
I'm the child
SIGINT received
$ I'm the child
I'm the child
So both processes handle SIGINT but the parent dies while the child continues...
The parent process is blocked in the main function and upon receiving the signal, handles it and returns from the call to wait with an error.
The child is just looping in the while handling SIGINT. When handled code returns where it was (probably blocked in sleep) and it continues to loop.
That code may illustrates what happens:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <sys/signal.h>
#include <string.h>
#include <sys/errno.h>
void handler (int sig) {
printf("SIGINT received %d\n",getpid());
}
void child() {
while (1) {
printf("I'm the child\n");
sleep(1);
}
exit(0);
}
int main(int argc, char *argv[]) {
struct sigaction act;
memset(&act, 0, sizeof(act));
act.sa_handler = &handler;
// Link SIGINT with the handler
sigaction(SIGINT, &act, NULL);
// Create child
if (fork() == 0) child();
int r = wait(NULL);
if (r==-1 && errno==EINTR) printf("signal probably received in parent\n");
return 0;
}
Be aware that calling printf in a signal handler is forbidden.
Related
Hello I'm having issues with sending signals from the father process to the child process. The Child doesn't respond to the signed sent by the father process:
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
pid_t pid;
pid = fork();
if(pid > 0) {
printf("Hello, my son!\n");
sleep(5); /* Sleep for 5 seconds... */
kill(pid, SIGUSR1);
printf("Goodbye, son!\n");
}
else {
printf("Son is running\n");
pause(); /* Wait for some signal... */
printf("I received the signal!");
}
}
The son doesn't received the signal it only says it's running here is the output:
output
The default behavior when SIGUSR1 is received is termination, so the child never prints the message. To handle the signal, you can use sigaction:
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
void handle(int sig, siginfo_t *i, void *v) { return; }
int
main(void)
{
pid_t pid = fork();
if( pid > 0 ){
printf("Hello, my child!\n");
sleep(1); /* Sleep for 1 second. */
kill(pid, SIGUSR1);
printf("Goodbye, child!\n");
} else {
struct sigaction act;
memset(&act, 0, sizeof act);
act.sa_sigaction = handle;
if( sigaction( SIGUSR1, &act, NULL ) ){
perror("sigaction");
exit(1);
}
printf("Child is running\n");
pause(); /* Wait for some signal... */
printf("I received the signal!\n");
}
}
the job is to continue executing a child process I stopped when I receive a SIGALRM signal.
so far I did the following, which doesn't seems to work:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
void handler(int sig)
{
printf("hello from the handler\n");
kill(getpid(),SIGCONT);
printf("child is continuing executing");
}
int main()
{
int pid1=fork();
signal(SIGALRM,handler);
if (pid1==0) {
kill(getpid(),SIGTSTP);
printf(" I am in the child\n");
} else {
printf("i am in the parent \n");
kill(pid1,SIGALRM);
}
}
I've tried many variations of the code, but printf("I am in the child"); is never executed.
Re: the question asked in a comment is ("how can I make the kill(pid1,SIGALRM) send the signal to the child?"). The call kill(pid1,SIGALRM) does send the signal to the child, but the child does not respond to it because it is stopped. The question asked is somewhat ambiguous, as it is not clear who "I" refers to in the phrase "when I receive a SIGALRM signal". If you want to have the child continue when the child receives a SIGALRM, you can't. You must send the child a SIGCONT before it will do anything. If you want the child to continue when the parent receives the SIGALRM, you could so something like:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
pid_t pid1;
void handler(int sig)
{
(void)sig;
if( pid1 ) {
kill(pid1, SIGCONT);
}
}
int main(void)
{
signal(SIGALRM, handler);
pid1 = fork();
if( pid1 == 0 ) {
kill(getpid(), SIGTSTP);
printf("Child continued\n");
} else {
alarm(1);
pause();
}
}
I create two children from the parent in the main program. The first and second child executes a program (signalSender) after their creation(alongside with the pid of the other child as an argument). signalSender has signal handler and is used for sending signal between process. The pid of the second child is given as zero as argument when first child executes signalShooter. The pid of the first child is given as argument when the second child executes sigShooter.
1) I want to find the pid of the first child via signal handler after second child sends the signal to first child. I tried to save it to global variable pid_t pid2 but it does not work.
2)I also have to send signal between these two children 100 times but i dont know where to use 'for loop' and ‘wait’ signal.
The main program:
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
int main()
{
pid_t pid1,pid2,wid;
char *my_args[5];
int aInt = 368
char str[15];
pid1 = fork();
if (pid1 < 0)
{
fprintf(stderr, ": fork failed: %s\n", strerror(errno));
exit(1);
}
if(pid1 == 0)
{
my_args[0] = "sigperf1";
my_args[1] = "0";
my_args[2] = NULL;
execv("signalSender",my_args);
fprintf(stderr,"signalSender cannot be executed...");
exit(-1);
}
pid2 = fork();
if(pid2 < 0)
{
fprintf(stderr, ": fork failed: %s\n", strerror(errno));
exit(1);
}
if(pid2 == 0)
{
sprintf(str, "%d", pid1);
my_args[0] = "sigperf1";
my_args[1] = str;
my_args[2] = NULL;
// printf("this is converted = %s\n",my_args[1]);
execv(“signalSender",my_args);
fprintf(stderr,"signalSender cannot be executed...");
exit(-1);
}
wid = wait(NULL);
}
The signalSender:
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <ctype.h>
pid_t pid2;
struct sigaction act;
void sighandler(int signum, siginfo_t *info, void *ptr)
{
printf("Received signal %d\n", signum);
printf("Signal originates from process %lu\n",
(unsigned long)info->si_pid);
pid2 = info->si_pid;
}
int main(int argc,char **argv)
{
memset(&act, 0, sizeof(act));
act.sa_sigaction = sighandler;
act.sa_flags = SA_SIGINFO;
sigaction(SIGUSR1, &act, NULL);
pid_t current, pidOther;
current = getpid();
pidOther = atol(argv[1]);
if(pidOther != 0) // we are in the second child
{
kill(pidOther,SIGUSR1); //sending signal from second child to first
}
if(pidOther == 0) // we are in the first child
{
kill(pid2,SIGUSR1);
}
return 0;
}
You have a synchronization issue here.
Both child processes start at roughly the same time. So you can't predict which one will kill the other first. If the first child runs kill first, it will pass 0 as the pid which will kill every process in the process group. Also, each child process quits immediately after calling kill, so one may exit before the other has a chance to send it a signal.
You need to introduce some type of synchronization method. A simple way to do this is to have the second process sleep briefly before calling kill to give the first process a chance to start up. Similarly, the first process should call pause, which will tell it to wait until it receives a signal.
Once you do that, then each process can call pause and kill in a loop to go back and forth.
if(pidOther != 0) // we are in the second child
{
sleep(1); // wait for first child to start
kill(pidOther,SIGUSR1); //sending signal from second child to first
for (i=0;i<5;i++) {
pause();
kill(pidOther,SIGUSR1);
}
}
if(pidOther == 0) // we are in the first child
{
pause(); // wait until we get a signal from the second child
kill(pid2,SIGUSR1);
for (i=0;i<5;i++) {
pause();
kill(pid2,SIGUSR1);
}
}
I am trying to use signal to sync N processes then print out something.
Each child process register a handler which print "yo" and "hihi" when catching SIGUSR1.
I use kill(0, SIGUSR1) to trigger every process. Since the default action for catching SIGUSR1 is being killed, I set a do-nothing handler for the main process so that it will wait all child died.
The fork and send signal program will repeat for k times, I expect it will show out N*k times "yo" and "hihi". However, it doesn't show enough "yo" and "hihi" as I expect. The number of "yo" is different every execution.
Here is my code, and thanks for your help!
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <time.h>
#include <signal.h>
#include <sys/time.h>
#include <string.h>
#define N 3
int pid_id[N];
void handler2 (int signum)
{
printf("hihi\n");
}
void handler (int signum)
{
signal(SIGUSR2, handler2);
printf("yo\n");
raise(SIGUSR2);
}
void handler_do_nothing (int signum)
{
;
}
void child(int process_index)
{
struct sigaction sa;
/* Register */
memset(&sa, 0, sizeof(sa));
sa.sa_handler = handler;
sigaction(SIGUSR1, &sa, NULL);
printf("I am %d.\n", getpid());
pid_id[process_index] = getpid();
sleep(1);
exit(0);
}
int main()
{
int i, k, status;
pid_t pid[N];
pid_t pid_wait;
struct sigaction sa_main;
/* Register */ /* Main process will terminate if catch SIGUSR1 by default setting*/
memset(&sa_main, 0, sizeof(sa_main));
sa_main.sa_handler = handler_do_nothing;
sigaction(SIGUSR1, &sa_main, NULL);
/* Race k times */
for (k=0;k<3;k++)
{
for (i=0;i<N;i++)
{
pid[i] = fork();
if (pid[i]==0)
{
child(i);
}
}
// sleep();
kill(0, SIGUSR1);
for (i=0;i<N;i++)
{
do
{
pid_wait = waitpid(pid[i], &status, WNOHANG);
printf("I am waiting..\n");
sleep(1);
}while(pid_wait != pid[i]);
}
}
printf("all done\n");
return 0;
}
Your child processes are being signalled before they have had time (i.e. execution resource scheduled) to install the new signal handler.
This means that when the main program sends SIGUSR1, some subset of the child processes will still have handler_do_nothing installed.
If you want to wait until the child processes have all finished setting up, you will need to add some interprocess communication mechanism - e.g. the children could signal the parent process when they are ready.
I want to know, when my child process is exiting. But I don't want to block my application, so I use WNOHANG.
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void handler(int nsig) {
int status;
waitpid(-1, &status, WNOHANG);
printf("nsig=%i; status=%i\n", nsig, status);
if (WIFEXITED(status)) {
printf("Exited\n");
}
}
int main() {
struct sigaction act;
act.sa_handler = handler;
if (sigaction(SIGCHLD, &act, 0) < 0) {
fprintf(stderr, "sigaction failed\n");
exit(-1);
}
pid_t fpid;
fpid = fork();
if (fpid == 0) {
execlp("okular", "okular", 0);
}
while(1);
return 0;
}
It works fine if I close "okular" as usual.
$ ./test
nsig=17; status=0
Exited
But if I do something like
kill -STOP OKULAR_PID
I have the same output and that's wrong for me, because okular didn't exit in fact.
I think it is right, because SIGCHLD is defined as Child terminated or stopped as can be seen in this man page for signal. SIGCLD is a synonym for SIGCHLD.