Linux Debugging Questions & Answers – Signal Handling System Calls

This set of Linux Debugging questions and answers focuses on signal handling System Calls.

1. What will happen as we press the “Ctrl+c” key after running this program?

  1.    #include<stdio.h>
  2.    #include<signal.h>
  3.  
  4.    void response (int);
  5.    void response (int sig_no)
  6.    {
  7.        printf("Linux\n");
  8.    }
  9.    int main()
  10.    {
  11.        signal(SIGINT,response);
  12.        while(1){          
  13.            printf("Sanfoundry\n");
  14.            sleep(1);
  15.        }
  16.        return 0;
  17.    }

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
View Answer

Answer: a
Explanation: The signal handler function “response” executes after recieving the signal SIGINT.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
Sanfoundry
Sanfoundry
Sanfoundry
^CLinux
Sanfoundry
Sanfoundry
^CLinux
Sanfoundry
Sanfoundry
^CLinux
Sanfoundry
^Z
[2]+ Stopped ./san
[root@localhost sanfoundry]#

2. What will happen if we press “Ctrl+c” key two times after running this program?

advertisement
advertisement
  1.    #include<stdio.h>
  2.    #include<signal.h>
  3.  
  4.    void response(int);
  5.    void response(int sig_no)
  6.    {
  7.        printf("Linux\n");
  8.        signal(SIGINT,SIG_DFL);
  9.    }
  10.    int main()
  11.    {
  12.        signal(SIGINT,response);
  13.        while(1){
  14.            printf("Sanfoundry\n");
  15.            sleep(1);
  16.        }
  17.        return 0;
  18.    }

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
View Answer

Answer: b
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 sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
Sanfoundry
Sanfoundry
^CLinux
Sanfoundry
^C
[root@localhost sanfoundry]#
Note: Join free Sanfoundry classes at Telegram or Youtube

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

  1.    #include<stdio.h>
  2.    #include<signal.h>
  3.    #include<stdlib.h>
  4.  
  5.    int main()
  6.    {
  7.        pid_t child;
  8.        signal(SIGINT,SIG_IGN);
  9.        child=fork();        
  10.        switch(child){
  11.            case -1:             
  12.                perror("fork");
  13.                exit(1);
  14.            case 0:
  15.                while(1){
  16.                    printf("Child Process\n");
  17.                    sleep(1);
  18.                }
  19.                break;
  20.            default :
  21.                while(1){
  22.                    printf("Parent Process\n");
  23.                    pause();
  24.                }
  25.                break;
  26.        }
  27.        return 0;
  28.    }

a) child process terminates
b) parent process terminates
c) both child and parent process ignores the signal
d) none of the mentioned
View Answer

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

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

advertisement
  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.    #include<signal.h>
  4.  
  5.    void response (int);
  6.    void response (int sig_no)
  7.    {
  8.        printf("%s",sys_siglist[sig_no]);
  9.    }
  10.    int main()
  11.    {
  12.        signal(SIGINT,response);
  13.        while(1){
  14.            printf("Sanfoundry\n");
  15.            sleep(1);
  16.        }
  17.        return 0;
  18.    }

a) Interrupt
b) Stop
c) Terminate
d) none of the mentioned
View Answer

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

5. In this program

  1.    #include<stdio.h>
  2.    #include<signal.h>
  3.    #include<stdlib.h>
  4.  
  5.    int main()
  6.    {
  7.        pid_t child;
  8.        child=fork();
  9.        switch(child){
  10.            case -1 :
  11.                perror("fork");
  12.                exit(1);
  13.            case 0 :
  14.                while(1){
  15.                    printf("Child Process\n");
  16.                    sleep(1);
  17.                }
  18.                break;              
  19.            default :
  20.                sleep(5);
  21.                kill(child,SIGINT);
  22.                printf("The child process has been killed by the parent process\n");
  23.                break;
  24.        }
  25.        return 0;
  26.    }

a) the child process kills the parent process
b) the parent process kills the child process
c) both the processes are killed by each other
d) none of the mentioned
View Answer

Answer: b
Explanation: The parnet process kills the child by sending a signal.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
Child Process
Child Process
Child Process
Child Process
Child Process
The child process has been killed by the parent process
[root@localhost sanfoundry]#

6. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<signal.h>
  3.  
  4.    void response (int);
  5.    void response (int sig_no)
  6.    {
  7.        printf("%s\n",sys_siglist[sig_no]);
  8.    }
  9.    int main()
  10.    {
  11.        pid_t child;
  12.        int status;
  13.        child = fork();        
  14.        switch(child){
  15.            case -1:
  16.                perror("fork");
  17.            case 0:
  18.                break;               
  19.            default :
  20.                signal(SIGCHLD,response);
  21.                wait(&status);
  22.                break;
  23.        }
  24.    }

a) this program will print nothing
b) this program will print “Child Exited”
c) segmentation fault
d) none of the mentioned
View Answer

Answer: b
Explanation: The child process sends SIGCHILD signal to its parent as it terminates.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
Child exited
[root@localhost sanfoundry]#

7. Which one of the following is not true about this program?

  1.    #include<stdio.h>
  2.    #include<signal.h>
  3.  
  4.    void response (int);
  5.    void response (int signo)
  6.    {
  7.        printf("%s\n",sys_siglist[signo]);
  8.        signal(SIGSEGV,SIG_IGN);
  9.    }
  10.    int main()
  11.    {
  12.        signal (SIGSEGV,response);
  13.        char *str;
  14.        *str = 10;        
  15.        return 0;
  16.    }

a) kernel sends SIGSEGV signal to a process as segmentation fault occurs
b) in this process signal handler will execute only one time of recieving the signal SIGSEGV
c) all of the mentioned
d) none of the mentioned
View Answer

Answer: d
Explanation: In this process the segmentation fault occurs because the memory is not allocated to the pointer *str.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
Segmentation fault
Segmentation fault (core dumped)
[root@localhost sanfoundry]#

8. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<signal.h>
  3.    #include<stdlib.h>
  4.  
  5.    void response (int);
  6.    void response (int sig_no)
  7.    {
  8.        printf("%s\n",sys_siglist[sig_no]);
  9.        printf("This is singal handler\n");
  10.    }
  11.    int main()
  12.    {
  13.        pid_t child;
  14.        int status;
  15.        child = fork();
  16.        switch (child){
  17.            case -1 :
  18.                perror("fork");
  19.                exit (1);
  20.            case 0 :
  21.                kill(getppid(),SIGKILL);
  22.                printf("I am an orphan process because my parent has been killed by me\n");
  23.                printf("Handler failed\n");
  24.                break;
  25.            default :
  26.                signal(SIGKILL,response);
  27.                wait(&status);
  28.                printf("The parent process is still alive\n");
  29.                break;
  30.        }
  31.        return 0;
  32.    }

a) the child process kills the parent process
b) the parent process kills the child process
c) handler function executes as the signal arrives to the parent process
d) none of the mentioned
View Answer

Answer: a
Explanation: The SIGKILL signal can not be handled by singal handler function.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
Killed
[root@localhost sanfoundry]# I am an orphan process because my parent has been killed by me
Handler failed

[root@localhost sanfoundry]#

9. This program will print

  1.    #include<stdio.h>
  2.    #include<signal.h>
  3.    #include<unistd.h>
  4.  
  5.    void response (int);
  6.    void response (int sig_no)
  7.    {
  8.        printf("%s is working\n",sys_siglist[sig_no]);
  9.    }
  10.    int main()
  11.    {
  12.        alarm(5);
  13.        sleep(50);
  14.        printf("Sanfoundry\n");        
  15.        signal(SIGALRM,response);
  16.        return 0;
  17.    }

a) “Sanfoundry”
b) “Alarm clock”
c) nothing
d) none of the mentioned
View Answer

Answer: b
Explanation: After 5 seconds of the execution of this program, the signal SIGALRM hits the process and handler executes.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
Alarm clock
[root@localhost sanfoundry]#

10. What happnes as the signal SIGINT hits the current process in the program?

  1.     #include<stdio.h>
  2.     #include<signal.h>
  3.  
  4.     void response (int);
  5.     void response (int sig_no)
  6.     {
  7.         printf("Linux\n");
  8.     }
  9.     int main()
  10.     {
  11.         struct sigaction act;
  12.         act.sa_handler = response;
  13.         act.sa_flags = 0;
  14.         sigemptyset(&act.sa_mask);
  15.         sigaction(SIGINT,&act,0);
  16.         while(1){
  17.             printf("Sanfoundry\n");
  18.             sleep(1);
  19.         }
  20.         return 0;
  21.     }

a) the process terminates
b) the string “Linux” prints
c) the string “Linux” prints and then process terminates
d) none of the mentioned
View Answer

Answer: b
Explanation: None.
Output:
[root@localhost sigaction]# gcc -o san san.c
[root@localhost sigaction]# ./san
Sanfoundry
Sanfoundry
Sanfoundry
^CLinux
Sanfoundry
Sanfoundry
^CLinux
Sanfoundry
^Z
[7]+ Stopped ./san
[root@localhost sanfoundry]#

Sanfoundry Global Education & Learning Series – Linux Administration & Programming.
Here’s the list of Best Books in Linux Commands & Shell Programming.
Here’s the list of Best Books in Linux Kernel, Device-Drivers & System Programming.

To practice all questions on Linux Administration & Programming, here is complete set of 1000+ Multiple Choice Questions and Answers on Linux.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.