7. What will happen as we press the "Ctrl+c" key after running this program?

#include<stdio.h>
#include<signal.h>

void response (int);
void response (int sig_no)
{
printf("Linuxn");
}
int main()
{
signal(SIGINT,response);
while(1){
printf("googlen");
sleep(1);
}
return 0;
}
a) the string "Linux" will print
b) the process will be terminated after printing the string "Linux"
c) the process will terminate
d) none of the mentioned

a) the string "Linux" will print
Explanation:
The signal handler function "response" executes after recieving the signal SIGINT.
Output:
[root@localhost google]# gcc -o san san.c
[root@localhost google]# ./san
google
google
google
^CLinux
google
google
^CLinux
google
google
^CLinux
google
^Z
[2]+ Stopped ./san
[root@localhost google]#

8. What will happen if we press "Ctrl+c" key two times after running this program?

#include<stdio.h>
#include<signal.h>

void response(int);
void response(int sig_no)
{
printf("Linuxn");
signal(SIGINT,SIG_DFL);
}
int main()
{
signal(SIGINT,response);
while(1){
printf("googlen");
sleep(1);
}
return 0;
}
a) process will terminate in the first time
b) process will terminate in the second time
c) process will never terminate
d) none of the mentioned

c) process will never terminate
Explanation:
According to the signal handler function of this program as the SIGINT signal arrives second time, the signal performs its default operation i.e. termination of the process.
Output:
[root@localhost google]# gcc -o san san.c
[root@localhost google]# ./san
google
google
^CLinux
google
^C
[root@localhost google]#

9. What happens as the SIGINT signal hits the running process of this program?

#include<stdio.h>
#include<signal.h>
#include<stdlib.h>

int main()
{
pid_t child;
signal(SIGINT,SIG_IGN);
child=fork();
switch(child){
case -1:
perror("fork");
exit(1);
case 0:
while(1){
printf("Child Processn");
sleep(1);
}
break;
default :
while(1){
printf("Parent Processn");
pause();
}
break;
}
return 0;
}
a) child process terminates
b) parent process terminates
c) both child and parent process ignores the signal
d) none of the mentioned

c) both child and parent process ignores the signal
Explanation:
If a process ignores a signal then by default its child also ignores that signal.
Output:
[root@localhost google]# gcc -o san san.c
[root@localhost google]# ./san
Parent Process
Child Process
Child Process
^CChild Process
^CChild Process
^CChild Process
^Z
[3]+ Stopped ./san
[root@localhost signal]#

10. What will print as the SIGINT signal hits the running process of this program?

#include<stdio.h>
#include<stdlib.h>
#include<signal.h>

void response (int);
void response (int sig_no)
{
printf("%s",sys_siglist[sig_no]);
}
int main()
{
signal(SIGINT,response);
while(1){
printf("googlen");
sleep(1);
}
return 0;
}
a) Interrupt
b) Stop
c) Terminate
d) none of the mentioned

a) Interrupt
Explanation:
The messages associated with signals can be access by the function sys_siglist().
Output:
[root@localhost google]# gcc -o san san.c
[root@localhost google]# ./san
google
google
google
^CInterruptgoogle
google
^CInterruptgoogle
google
^CInterruptgoogle
google
google
^Z
[4]+ Stopped ./san
[root@localhost google]#

Download Interview PDF