1. What is the output of this program?
#include<stdio.h>
int main()
{
fork();
printf("Sanfoundry\n");
return 0;
}
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
Explanation: The “fork” system call creates a new process by duplicating the calling process. Hence the next statement is executed by two processes.
Output:
[[email protected] sanfoundry]# gcc -o san san.c
[[email protected] sanfoundry]# ./san
Sanfoundry
[[email protected] sanfoundry]# Sanfoundry [[email protected] sanfoundry]#
2. What is the output of this program?
#include<stdio.h>
#include<unistd.h>
int main()
{
pid_t child;
child = fork();
printf("%d\n",child);
return 0;
}
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
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:
[[email protected] sanfoundry]# gcc -o san san.c
[[email protected] sanfoundry]# ./san
17654
[[email protected] sanfoundry]# 0 [[email protected] sanfoundry]#
3. What is the output of this program?
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
pid_t child;
int status;
child = fork();
switch(child){
case -1 ;
perror("fork");
exit(1);
case 0 :
printf("%d\n",getppid());
break;
default :
printf("%d\n",getpid());
wait(&status);
break;
}
return 0;
}
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
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:
[[email protected] sanfoundry]# gcc -o san san.c
[[email protected] sanfoundry]# ./san
17729
17729
[[email protected] sanfoundry]# ./san
17731
17731
[[email protected] sanfoundry]#
4. This program will print ____ as output.
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
pid_t child;
child = fork();
switch(child){
case -1 :
perror("fork");
exit(1);
case 0 :
sleep(10);
printf("%d\n",getppid());
break;
default :
break;
}
return 0;
}
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
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:
[[email protected] sanfoundry]# gcc -o san san.c
[[email protected] sanfoundry]# ./san
[[email protected] sanfoundry]# 1 [[email protected] sanfoundry]#
5. What is the output of this program?
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
pid_t child;
int a, status;
a = 10;
child = fork();
switch(child){
case -1 :
perror("fork");
exit(1);
case 0 :
printf("%d\n",a);
break;
default :
wait(&status);
break;
}
return 0;
}
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
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:
[[email protected] sanfoundry]# gcc -o san san.c
[[email protected] sanfoundry]# ./san
10
[[email protected] sanfoundry]#
6. What is the output of this program?
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
pid_t child;
int status;
child = fork();
switch(child){
case -1 :
perror("fork");
exit(1);
case 0 :
exit(2);
break;
default :
wait(&status);
printf("%d\n",WEXITSTATUS(status));
break;
}
return 0;
}
a) 0
b) 1
c) 2
d) none of the mentioned
View Answer
Explanation: The “WEXITSTATUS” returns the low-order 8 bits of the exit status value from the child process.
Output:
[[email protected] sanfoundry]# gcc -o san san.c
[[email protected] sanfoundry]# ./san
2
[[email protected] sanfoundry]#
7. What is the output of this progarm?
#include<stdio.h>
#include<unistd.h>
int main()
{
execl("/bin/ls","ls",NULL);
return 0;
}
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
Explanation: The “execl” system call replaces the current process image with a new process image according to the arguments.
Output:
[[email protected] sanfoundry]# gcc -o san san.c
[[email protected] sanfoundry]# ./san
san san.c
[[email protected] sanfoundry]#
8. How many time “Sanfoundry” will print in this program?
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
if( execl("/bin/ls","ls",NULL) == -1){
perror("execl");
exit(1);
}
printf("Sanfoundry\n");
return 0;
}
a) 0
b) 1
c) 2
d) none of the mentioned
View Answer
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:
[[email protected] sanfoundry]# gcc -o san san.c
[[email protected] sanfoundry]# ./san
san san.c
[[email protected] sanfoundry]#
9. This program will create ____ child processes?
#include<stdio.h>
#include<unistd.h>
int main()
{
fork();
fork();
fork();
printf("Sanfoundry\n");
return 0;
}
a) 3
b) 5
c) 7
d) 9
View Answer
Explanation: None.
Output:
[[email protected] sanfoundry]# gcc -o san san.c
[[email protected] sanfoundry]# ./san
Sanfoundry
[[email protected] sanfoundry]# Sanfoundry
Sanfoundry
Sanfoundry
Sanfoundry
Sanfoundry
Sanfoundry
Sanfoundry [[email protected] sanfoundry]#
10. What is the output of this progarm?
#include<stdio.h>
#include<unistd.h>
int main()
{
pid_t child;
int a, b;
a = 10;
b = 20;
child = fork();
a = a + b;
if(child > 0){
printf("%d\n",a);
}
return 0;
}
a) 10
b) 30
c) 50
d) none of the mentioned
View Answer
Explanation: None.
Output:
[[email protected] sanfoundry]# gcc -o san san.c
[[email protected] sanfoundry]# ./san
30
[[email protected] sanfoundry]#
Sanfoundry Global Education & Learning Series – Linux Administration & Programming.
Here’s the list of Best Reference Books in Linux Commands & Shell Programming.
Here’s the list of Best Reference 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.