Linux Debugging Questions & Answers – fork, exec and wait System Calls

This set of Linux Debugging questions and answers focuses on the fork, exec and wait System Calls.

1. What is the output of this program?

  1.    #include<stdio.h>
  2.  
  3.    int main()
  4.    {
  5.        fork();
  6.        printf("Sanfoundry\n");
  7.        return 0;
  8.    }

a) the string “Sanfoundry” will print 1 time
b) the string “Sanfoundry” will print 2 times
c) the string “Sanfoundry” will print 3 times
d) none of the mentioned
View Answer

Answer: b
Explanation: The “fork” system call creates a new process by duplicating the calling process. Hence the next statement is executed by two processes.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
Sanfoundry
[root@localhost sanfoundry]# Sanfoundry

[root@localhost sanfoundry]#

2. What is the output of this program?

advertisement
advertisement
  1.    #include<stdio.h>
  2.    #include<unistd.h>
  3.  
  4.    int main()
  5.    {
  6.        pid_t child;
  7.        child = fork();
  8.        printf("%d\n",child);
  9.        return 0;
  10.    }

a) it will print “0”
b) it will print the PID of the child process
c) it will print “0” & the PID of the child process
d) none of the mentioned
View Answer

Answer: c
Explanation: The “fork” system call returns the PID of the child process when it is executed by the parent process and returns 0 when it is executed by the child process.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
17654
[root@localhost sanfoundry]# 0

[root@localhost sanfoundry]#

3. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.    #include<unistd.h>
  4.  
  5.    int main()
  6.    {
  7.        pid_t child;
  8.        int status;
  9.        child = fork();
  10.        switch(child){
  11.            case -1 ;
  12.                perror("fork");
  13.                exit(1);
  14.            case 0 :
  15.                printf("%d\n",getppid());
  16.                break;
  17.            default :
  18.                printf("%d\n",getpid());
  19.                wait(&status);
  20.                break;
  21.         }
  22.         return 0;
  23.    }

a) this program will print two same integer values
b) this program will print two different integer values
c) segmentation fault
d) none of the mentioned
View Answer

Answer: a
Explanation: In this program the child process is printing its parent PID and the parent process is printing its own PID. Hence both the integer values are same.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
17729
17729
[root@localhost sanfoundry]# ./san
17731
17731
[root@localhost sanfoundry]#
advertisement

4. This program will print ____ as output.

advertisement
  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.    #include<unistd.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.                sleep(10);
  15.                printf("%d\n",getppid());
  16.                break;
  17.            default :
  18.                break;
  19.        }
  20.        return 0;
  21.    }

a) 0
b) 1
c) an integer value except 0 and 1 i.e. PID of a process
d) none of the mentioned
View Answer

Answer: b
Explanation: In this program the parent process terminates before the child process. Hence the child process prints 1 as its parent process ID. The output of this program will appear after 10 seconds.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
[root@localhost sanfoundry]# 1

[root@localhost sanfoundry]#

5. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.    #include<unistd.h>
  4.  
  5.    int main()
  6.    {
  7.        pid_t child;
  8.        int a, status;
  9.        a = 10;
  10.        child = fork();
  11.        switch(child){
  12.            case -1 :
  13.                perror("fork");
  14.                exit(1);
  15.            case 0 :
  16.                printf("%d\n",a);
  17.                break;
  18.            default :
  19.                wait(&status);
  20.                break;
  21.        }
  22.        return 0;
  23.    }

a) 10
b) garbage value
c) segmentation fault
d) program will give an error because variable “a” is not defined in child process
View Answer

Answer: a
Explanation: The child’s stack, data and heap segments are exact duplicates of its parent process when the process is created by “fork” system call.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
10
[root@localhost sanfoundry]#

6. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.    #include<unistd.h>
  4.  
  5.    int main()
  6.    {
  7.        pid_t child;
  8.        int status;
  9.        child = fork();        
  10.            switch(child){
  11.                case -1 :
  12.                    perror("fork");
  13.                    exit(1);
  14.                case 0 :
  15.                    exit(2);
  16.                    break;
  17.                default :       
  18.                    wait(&status);
  19.                    printf("%d\n",WEXITSTATUS(status));
  20.                    break;
  21.            }
  22.            return 0;
  23.    }

a) 0
b) 1
c) 2
d) none of the mentioned
View Answer

Answer: c
Explanation: The “WEXITSTATUS” returns the low-order 8 bits of the exit status value from the child process.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
2
[root@localhost sanfoundry]#

7. What is the output of this progarm?

  1.    #include<stdio.h>
  2.    #include<unistd.h>
  3.  
  4.    int main()
  5.    {
  6.        execl("/bin/ls","ls",NULL);
  7.        return 0;
  8.    }

a) the program will give an compilation error
b) the program will give segmentation fault
c) the program will execute just like “ls” command
d) none of the mentioned
View Answer

Answer: c
Explanation: The “execl” system call replaces the current process image with a new process image according to the arguments.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
san san.c
[root@localhost sanfoundry]#

8. How many time “Sanfoundry” will print in this program?

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.    #include<unistd.h>
  4.  
  5.    int main()
  6.    {       
  7.        if( execl("/bin/ls","ls",NULL) == -1){         
  8.            perror("execl");
  9.            exit(1);
  10.        }
  11.        printf("Sanfoundry\n");
  12.        return 0;
  13.    }

a) 0
b) 1
c) 2
d) none of the mentioned
View Answer

Answer: a
Explanation: In this program, the next statement of “execl” will never execute because the current process is replaced by the process created by the “execl” system call.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
san san.c
[root@localhost sanfoundry]#

9. This program will create ____ child processes?

  1.    #include<stdio.h>
  2.    #include<unistd.h>
  3.  
  4.    int main()
  5.    {
  6.        fork();
  7.        fork();
  8.        fork();
  9.        printf("Sanfoundry\n");
  10.        return 0;
  11.    }

a) 3
b) 5
c) 7
d) 9
View Answer

Answer: c
Explanation: None.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
Sanfoundry
[root@localhost sanfoundry]# Sanfoundry
Sanfoundry
Sanfoundry
Sanfoundry
Sanfoundry
Sanfoundry
Sanfoundry

[root@localhost sanfoundry]#

10. What is the output of this progarm?

  1.     #include<stdio.h>
  2.     #include<unistd.h>
  3.  
  4.     int main()
  5.     {
  6.         pid_t child;
  7.         int a, b;
  8.         a = 10;
  9.         b = 20;
  10.         child = fork();
  11.         a = a + b;
  12.         if(child > 0){
  13.             printf("%d\n",a);
  14.         } 
  15.         return 0;
  16.     }

a) 10
b) 30
c) 50
d) none of the mentioned
View Answer

Answer: b
Explanation: None.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
30
[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.