Linux Debugging Questions & Answers – 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 these

Answer: Option 'A'

the string “Linux” will print

2.

What will happen if we press “Ctrl+c” key two times 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.        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 these

Answer: Option 'B'

process will terminate in the second time

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 these

Answer: Option 'C'

both child and parent process ignores the signal

4.

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

  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 these

Answer: Option 'A'

Interrupt

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 these

Answer: Option 'B'

the parent process kills the child process

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 these

Answer: Option 'B'

this program will print “Child Exited”

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 these

Answer: Option 'D'

none of these

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 these

Answer: Option 'A'

the child process kills the parent process

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 these

Answer: Option 'B'

“Alarm clock”

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 these

Answer: Option 'B'

the string “Linux” prints


Linux Debugging Questions & Answers – Signal Handling System Calls Download Pdf

Recent Posts