behavior of interrupt signal after forking
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I used the following code while studying signals.
#include<stdio.h>
#include<sys/stat.h>
#include<sys/wait.h>
#include<unistd.h>
#include<stdlib.h>
#include<signal.h>
#include<sys/types.h>
int handler(int sig)
printf("interrupt has been invokedn");
int main()
pid_t pid;//pid_t is the datatype for process ids
int status;
signal(SIGINT,handler);
if((pid=fork())==0)
while(1)
sleep(1);
exit(0);
else
wait(NULL);
and the output received on using ctrl+c was this:
^Cinterrupt has been invoked
interrupt has been invoked
^Cinterrupt has been invoked
interrupt has been invoked
^Cinterrupt has been invoked
interrupt has been invoked
Can someone please explain why "interrupt has been invoked" is being printed twice every time ctrl+c is used?
c signals
add a comment |Â
up vote
1
down vote
favorite
I used the following code while studying signals.
#include<stdio.h>
#include<sys/stat.h>
#include<sys/wait.h>
#include<unistd.h>
#include<stdlib.h>
#include<signal.h>
#include<sys/types.h>
int handler(int sig)
printf("interrupt has been invokedn");
int main()
pid_t pid;//pid_t is the datatype for process ids
int status;
signal(SIGINT,handler);
if((pid=fork())==0)
while(1)
sleep(1);
exit(0);
else
wait(NULL);
and the output received on using ctrl+c was this:
^Cinterrupt has been invoked
interrupt has been invoked
^Cinterrupt has been invoked
interrupt has been invoked
^Cinterrupt has been invoked
interrupt has been invoked
Can someone please explain why "interrupt has been invoked" is being printed twice every time ctrl+c is used?
c signals
Only use async-signal-safe functions in the handlers for asynchronous signals.printf()
is not async-signal-safe. stackoverflow.com/questions/16891019
â JdeBP
Jun 14 at 13:00
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I used the following code while studying signals.
#include<stdio.h>
#include<sys/stat.h>
#include<sys/wait.h>
#include<unistd.h>
#include<stdlib.h>
#include<signal.h>
#include<sys/types.h>
int handler(int sig)
printf("interrupt has been invokedn");
int main()
pid_t pid;//pid_t is the datatype for process ids
int status;
signal(SIGINT,handler);
if((pid=fork())==0)
while(1)
sleep(1);
exit(0);
else
wait(NULL);
and the output received on using ctrl+c was this:
^Cinterrupt has been invoked
interrupt has been invoked
^Cinterrupt has been invoked
interrupt has been invoked
^Cinterrupt has been invoked
interrupt has been invoked
Can someone please explain why "interrupt has been invoked" is being printed twice every time ctrl+c is used?
c signals
I used the following code while studying signals.
#include<stdio.h>
#include<sys/stat.h>
#include<sys/wait.h>
#include<unistd.h>
#include<stdlib.h>
#include<signal.h>
#include<sys/types.h>
int handler(int sig)
printf("interrupt has been invokedn");
int main()
pid_t pid;//pid_t is the datatype for process ids
int status;
signal(SIGINT,handler);
if((pid=fork())==0)
while(1)
sleep(1);
exit(0);
else
wait(NULL);
and the output received on using ctrl+c was this:
^Cinterrupt has been invoked
interrupt has been invoked
^Cinterrupt has been invoked
interrupt has been invoked
^Cinterrupt has been invoked
interrupt has been invoked
Can someone please explain why "interrupt has been invoked" is being printed twice every time ctrl+c is used?
c signals
edited Jun 14 at 12:25
ilkkachu
47.5k668130
47.5k668130
asked Jun 14 at 12:15
dhruv gupta
132
132
Only use async-signal-safe functions in the handlers for asynchronous signals.printf()
is not async-signal-safe. stackoverflow.com/questions/16891019
â JdeBP
Jun 14 at 13:00
add a comment |Â
Only use async-signal-safe functions in the handlers for asynchronous signals.printf()
is not async-signal-safe. stackoverflow.com/questions/16891019
â JdeBP
Jun 14 at 13:00
Only use async-signal-safe functions in the handlers for asynchronous signals.
printf()
is not async-signal-safe. stackoverflow.com/questions/16891019â JdeBP
Jun 14 at 13:00
Only use async-signal-safe functions in the handlers for asynchronous signals.
printf()
is not async-signal-safe. stackoverflow.com/questions/16891019â JdeBP
Jun 14 at 13:00
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
This is because the signal handler is valid for both parent and child after the fork() call.
Since the forked child runs in the same process group as the parent, both processes receive the signal.
You may like to use this printf() command:
printf("interrupt has been invoked in pid %dn", (int)getpid());
The tty driver has a tty process group set up and if you type ^C and ^C is set up as the TTY INTR character, then the tty driver sends SIGINT to all processes that are in the same process group as the tty driver.
but when the process group leader terminates,it sends the SIGHUP signal and not the SIGINT signal right?
â dhruv gupta
Jun 14 at 12:43
he process group leader does not send signals unless it callskill()
or similar.SIGHUP
is send by the tty driver in some cases.
â schily
Jun 14 at 12:45
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
This is because the signal handler is valid for both parent and child after the fork() call.
Since the forked child runs in the same process group as the parent, both processes receive the signal.
You may like to use this printf() command:
printf("interrupt has been invoked in pid %dn", (int)getpid());
The tty driver has a tty process group set up and if you type ^C and ^C is set up as the TTY INTR character, then the tty driver sends SIGINT to all processes that are in the same process group as the tty driver.
but when the process group leader terminates,it sends the SIGHUP signal and not the SIGINT signal right?
â dhruv gupta
Jun 14 at 12:43
he process group leader does not send signals unless it callskill()
or similar.SIGHUP
is send by the tty driver in some cases.
â schily
Jun 14 at 12:45
add a comment |Â
up vote
1
down vote
accepted
This is because the signal handler is valid for both parent and child after the fork() call.
Since the forked child runs in the same process group as the parent, both processes receive the signal.
You may like to use this printf() command:
printf("interrupt has been invoked in pid %dn", (int)getpid());
The tty driver has a tty process group set up and if you type ^C and ^C is set up as the TTY INTR character, then the tty driver sends SIGINT to all processes that are in the same process group as the tty driver.
but when the process group leader terminates,it sends the SIGHUP signal and not the SIGINT signal right?
â dhruv gupta
Jun 14 at 12:43
he process group leader does not send signals unless it callskill()
or similar.SIGHUP
is send by the tty driver in some cases.
â schily
Jun 14 at 12:45
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
This is because the signal handler is valid for both parent and child after the fork() call.
Since the forked child runs in the same process group as the parent, both processes receive the signal.
You may like to use this printf() command:
printf("interrupt has been invoked in pid %dn", (int)getpid());
The tty driver has a tty process group set up and if you type ^C and ^C is set up as the TTY INTR character, then the tty driver sends SIGINT to all processes that are in the same process group as the tty driver.
This is because the signal handler is valid for both parent and child after the fork() call.
Since the forked child runs in the same process group as the parent, both processes receive the signal.
You may like to use this printf() command:
printf("interrupt has been invoked in pid %dn", (int)getpid());
The tty driver has a tty process group set up and if you type ^C and ^C is set up as the TTY INTR character, then the tty driver sends SIGINT to all processes that are in the same process group as the tty driver.
edited Jun 14 at 12:50
answered Jun 14 at 12:38
schily
8,57421435
8,57421435
but when the process group leader terminates,it sends the SIGHUP signal and not the SIGINT signal right?
â dhruv gupta
Jun 14 at 12:43
he process group leader does not send signals unless it callskill()
or similar.SIGHUP
is send by the tty driver in some cases.
â schily
Jun 14 at 12:45
add a comment |Â
but when the process group leader terminates,it sends the SIGHUP signal and not the SIGINT signal right?
â dhruv gupta
Jun 14 at 12:43
he process group leader does not send signals unless it callskill()
or similar.SIGHUP
is send by the tty driver in some cases.
â schily
Jun 14 at 12:45
but when the process group leader terminates,it sends the SIGHUP signal and not the SIGINT signal right?
â dhruv gupta
Jun 14 at 12:43
but when the process group leader terminates,it sends the SIGHUP signal and not the SIGINT signal right?
â dhruv gupta
Jun 14 at 12:43
he process group leader does not send signals unless it calls
kill()
or similar. SIGHUP
is send by the tty driver in some cases.â schily
Jun 14 at 12:45
he process group leader does not send signals unless it calls
kill()
or similar. SIGHUP
is send by the tty driver in some cases.â schily
Jun 14 at 12:45
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f449798%2fbehavior-of-interrupt-signal-after-forking%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Only use async-signal-safe functions in the handlers for asynchronous signals.
printf()
is not async-signal-safe. stackoverflow.com/questions/16891019â JdeBP
Jun 14 at 13:00