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

Answer: Option 'B'

the string “Sanfoundry” will print 2 times

2.

What is the output of this program?

  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 these

Answer: Option 'C'

it will print “0” & the PID of the child process

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 these

Answer: Option 'A'

this program will print two same integer values

4.

This program will print ____ as output.

  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 these

Answer: Option 'B'

1

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

Answer: Option 'A'

10

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 these

Answer: Option 'C'

2

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 these

Answer: Option 'C'

the program will execute just like “ls” command

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 these

Answer: Option 'A'

0

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

Answer: Option 'C'

7

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 these

Answer: Option 'B'

30


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

Recent Posts