behavior of interrupt signal after forking

The name of the pictureThe name of the pictureThe name of the pictureClash 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?







share|improve this question





















  • 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














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?







share|improve this question





















  • 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












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?







share|improve this question













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?









share|improve this question












share|improve this question




share|improve this question








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
















  • 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










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.






share|improve this answer























  • 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










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















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






























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.






share|improve this answer























  • 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














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.






share|improve this answer























  • 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












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.






share|improve this answer















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.







share|improve this answer















share|improve this answer



share|improve this answer








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 calls kill() 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










  • 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















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












 

draft saved


draft discarded


























 


draft saved


draft discarded














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













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay