Linux Set 6 (30 mcqs)

a1. 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

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) both (a) and (b)
d) none of the mentioned
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

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 the mentioned

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

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

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

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

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

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

11. What is the output of this program?
1.       #include<stdio.h>
2.       #include<fcntl.h>
3.       #include<sys/stat.h>
4.       #include<semaphore.h>
5.
6.       int main()
7.       {
8.   sem_t* sem_id;
9.   sem_id = sem_open(“sem_value”,O_CREAT,0666,0);
10.   if(sem_id == SEM_FAILED)
11.       perror(“sem_open”);
12.   sem_wait(sem_id);
13.   printf(“Sanfoundry\n”);
14.   if(sem_close(sem_id) == -1)
15.       perror(“sem_close”);
16.   return 0;
17.       }
a) this program will print the string “Sanfoundry”
b) this process will block
c) segmentaion fault
d) none of the mentioned

12. What is the output of this program?
1.       #include<stdio.h>
2.       #include<fcntl.h>
3.       #include<sys/stat.h>
4.       #include<semaphore.h>
5.
6.       int main()
7.       {
8.   sem_t* sem_id;
9.   int value;
10.   sem_id = sem_open(“sem_value”,O_CREAT,0666,0);
11.   if(sem_id == SEM_FAILED)
12.       perror(“sem_open”);
13.   if(sem_getvalue(sem_id,&value) == -1)
14.       perror(“sem_getvalue”);
15.   printf(“%d\n”,value);
16.   sem_wait(sem_id);
17.   printf(“Sanfoundry\n”);
18.   if(sem_close(sem_id) == -1)
19.       perror(“sem_close”);
20.   return 0;
21.       }
a) 0
b) Sanfoundry
c) both (a) and (b)
d) none of the mentioned

13. What is the output of this program?
1.       #include<stdio.h>
2.       #include<fcntl.h>
3.       #include<sys/stat.h>
4.       #include<semaphore.h>
5.
6.       int main()
7.       {
8.   sem_t* sem_id;
9.   sem_id = sem_open(“sem_value”,O_CREAT,0666,0);
10.   if(sem_id == SEM_FAILED)
11.       perror(“sem_open”);
12.   sem_post(sem_id);
13.   printf(“Sanfoundry\n”);
14.   if(sem_close(sem_id) == -1)
15.       perror(“sem_close”);
16.   return 0;
17.       }
a) this process will block
b) this program will print the string “Sanfoundry”
c) segmentation fault
d) none of the mentioned

14. What is the output of this program?
1.       #include<stdio.h>
2.       #include<fcntl.h>
3.       #include<sys/stat.h>
4.       #include<semaphore.h>
5.
6.       int main()
7.       {
8.   sem_t* sem_id;
9.   sem_id = sem_open(“sem_value”,O_CREAT,0666,0);
10.   if(sem_id == SEM_FAILED)
11.       perror(“sem_open”);
12.   if(sem_close(sem_id) == -1)
13.       perror(“sem_close”);
14.   sem_wait(sem_id);
15.   printf(“Sanfoundry\n”);
16.   return 0;
17.       }
a) this process will block
b) this program will print the string “Sanfoundry”
c) segmentation fault
d) none of the mentioned

15. What is the output of this program?
1.       #include<stdio.h>
2.       #include<fcntl.h>
3.       #include<sys/stat.h>
4.       #include<semaphore.h>
5.
6.       int main()
7.       {
8.   sem_t* sem_id;
9.   int value;
10.   sem_id = sem_open(“new_13”,O_CREAT,0666,3);
11.   if(sem_id == SEM_FAILED)
12.       perror(“sem_open”);
13.   sem_wait(sem_id);
14.   sem_wait(sem_id);
15.   sem_wait(sem_id);
16.   sem_wait(sem_id);
17.   sem_post(sem_id);
18.   sem_post(sem_id);
19.   sem_getvalue(sem_id,&value);
20.   printf(“%d\n”,value);
21.   if(sem_close(sem_id) == -1)
22.       perror(“sem_close”);
23.   return 0;
24.       }
a) 2
b) 3
c) 0
d) none of the mentioned

16. What is the output of this program?
1.       #include<stdio.h>
2.       #include<fcntl.h>
3.       #include<sys/stat.h>
4.       #include<sys/mman.h>
5.
6.       int main()
7.       {
8.   int s_id;
9.   s_id = shm_open(“shared_mem”,O_CREAT|O_RDWR,0666);
10.   printf(“%d\n”,s_id);
11.   if(shm_unlink(“shared_mem”) == -1)
12.       perror(“shm_unlink”);
13.   return 0;
14.       }
a) -1
b) 1
c) 2
d) 3

17. What is the output of this program?
1.       #include<stdio.h>
2.       #include<fcntl.h>
3.       #include<sys/stat.h>
4.       #include<sys/mman.h>
5.
6.       int main()
7.       {
8.   int s_id;
9.   int *ptr;
10.   s_id = shm_open(“shared_mem”,O_CREAT|O_RDWR,0666);
11.   if(s_id == -1)
12.       perror(“shm_open”);
13.   ptr = mmap(NULL,100,PROT_WRITE,MAP_PRIVATE,s_id,0);
14.   if(ptr == MAP_FAILED);
15.       perror(“mmap”);
16.   if(munmap(ptr,100) == -1)
17.       perror(“munmap”);
18.   if(shm_unlink(“shared_mem”) == -1)
19.       perror(“shm_unlink”);
20.   return 0;
21.       }
a) mmap: Success
b) mmap: Failure
c) munmap: Success
d) munmap: Failure

18. What is the output of this program?
1.       #include<stdio.h>
2.       #include<fcntl.h>
3.       #include<sys/stat.h>
4.       #include<sys/mman.h>
5.
6.       int main()
7.       {
8.   int s_id;
9.   int *ptr;
10.   s_id = shm_open(“shared_mem”,O_CREAT|O_RDWR,0666);
11.   if(s_id == -1)
12.       perror(“shm_open”);
13.   ptr = mmap(NULL,100,PROT_WRITE,MAP_PRIVATE,s_id,0);
14.   if(ptr == MAP_FAILED);
15.       perror(“mmap”);
16.   ptr = mmap(ptr,100,PROT_WRITE,MAP_PRIVATE,s_id,0);
17.   if(ptr == MAP_FAILED);
18.       perror(“mmap”);
19.   if(munmap(ptr,100) == -1)
20.       perror(“munmap”);
21.   if(shm_unlink(“shared_mem”) == -1)
22.       perror(“shm_unlink”);
23.   return 0;
24.       }
a) mmap: Success
mmap: Success
b) mmap: Success
mmap: Failure
c) segmentation fault
d) none of the mentioned

19. What is the output of this program?
1.       #include<stdio.h>
2.       #include<fcntl.h>
3.       #include<errno.h>
4.       #include<sys/stat.h>
5.       #include<sys/mman.h>
6.
7.       int main()
8.       {
9.   int s_id;
10.   s_id = shm_open(“shared_mem”,O_CREAT|O_RDWR,0666);
11.   if(s_id != EACCES)
12.       perror(“Permission granted\n”);
13.   return 0;
14.       }
a) Permission granted
: Success
b) Permission granted
c) segmentation fault
d) none of the mentioned

20. What is the output of this program?
1.#include<stdio.h>
2.#include<fcntl.h>
3.#include<errno.h>
4.#include<sys/stat.h>
5.#include<sys/mman.h>
6.
7.int main()
8.{
9.    int s_id;
10.    s_id = shm_open(“shared_memory”,O_TRUNC,0666);
11.    if(s_id == -1)
12.        perror(“shm_open\n”);
13.    return 0;
14.}
a) this program will give an error because OTRUNC is not a valid flag
b) this program will give an error
c) this program will give segmentation fault
d) none of the mentioned

21. What is the output of this program
1.       #include<stdio.h>
2.       #include<pthread.h>
3.
4.       void *fun_t(void *arg);
5.       void *fun_t(void *arg)
6.       {
7.   pthread_exit(“Bye”);
8.       }
9.       int main()
10.       {
11.   pthread_t pt;
12.   void *res_t;
13.   int ret;
14.   ret = pthread_join(pt,&res_t);
15.   printf(“%d\n”,ret);
16.   return 0;
17.       }
a) 0
b) -1
c) 2
d) 3

22. What is the output of this program?
1.       #include<stdio.h>
2.       #include<pthread.h>
3.
4.       sem_t st;
5.       void *fun_t(void *arg);
6.       void *fun_t(void *arg)
7.       {
8.   pthread_exit(“Bye”);
9.       }
10.       int main()
11.       {
12.   pthread_t pt;
13.   void *res_t;
14.   if(pthread_create(&pt,NULL,fun_t,NULL) == -1)
15.       perror(“pthread_create”);
16.   if(sem_init(&st,1,2) != 0)
17.       perror(“sem_init”);
18.   if(pthread_join(pt,&res_t) == -1)
19.       perror(“pthread_join”);
20.   if(sem_destroy(&st) != 0)
21.       perror(“sem_destroy”);
22.   return 0;
23.       }
a) this program will print nothing
b) this program will give an error
c) this program will give segmentation fault
d) none of the mentioned

23. What is the output of this program?
1.       #include<stdio.h>
2.       #include<pthread.h>
3.       #include<semaphore.h>
4.
5.       sem_t st;
6.       void *fun_t(void *arg);
7.       void *fun_t(void *arg)
8.       {
9.   pthread_exit(“Bye”);
10.       }
11.       int main()
12.       {
13.   pthread_t pt;
14.   void *res_t;
15.   if(pthread_create(&pt,NULL,fun_t,NULL) == -1)
16.       perror(“pthread_create”);
17.   if(sem_init(st,1,2) != 0)
18.       perror(“sem_init”);
19.   if(pthread_join(pt,&res_t) == -1)
20.       perror(“pthread_join”);
21.   if(sem_destroy(&st) != 0)
22.       perror(“sem_destroy”);
23.   return 0;
24.       }
a) this program will print nothing
b) this program will give an error
c) this program will give segmentation fault
d) none of the mentioned

24. What is the output of this program?
1.       #include<stdio.h>
2.       #include<pthread.h>
3.       #include<semaphore.h>
4.
5.       void *fun_t(void *arg);
6.       void *fun_t(void *arg)
7.       {
8.   pthread_exit(“Bye”);
9.       }
10.       int main()
11.       {
12.   pthread_t pt;
13.   sem_t st;
14.   void *res_t;
15.   if(pthread_create(&pt,NULL,fun_t,NULL) == -1)
16.       perror(“pthread_create”);
17.   if(sem_init(&st,0,0) != 0)
18.       perror(“sem_init”);
19.   if(sem_wait(&st) != 0)
20.       perror(“sem_wait”);
21.   printf(“Sanoundry\n”);
22.   if(pthread_join(pt,&res_t) == -1)
23.       perror(“pthread_join”);
24.   if(sem_destroy(&st) != 0)
25.       perror(“sem_destroy”);
26.   return 0;
27.       }
a) this program will print the string “Sanfoundry”
b) this program will give segmentation fault
c) this process will remain block
d) none of the mentioned

25. What is the output of this program?
1.       #include<stdio.h>
2.       #include<pthread.h>
3.       #include<semaphore.h>
4.
5.       void *fun_t(void *arg);
6.       void *fun_t(void *arg)
7.       {
8.   sem_post(&st);
9.   pthread_exit(“Bye”);
10.       }
11.       int main()
12.       {
13.   pthread_t pt;
14.   sem_t st;
15.   void *res_t;
16.   if(pthread_create(&pt,NULL,fun_t,NULL) == -1)
17.       perror(“pthread_create”);
18.   if(sem_init(&st,0,0) != 0)
19.       perror(“sem_init”);
20.   if(sem_wait(&st) != 0)
21.       perror(“sem_wait”);
22.   printf(“Sanoundry\n”);
23.   if(pthread_join(pt,&res_t) == -1)
24.       perror(“pthread_join”);
25.   if(sem_destroy(&st) != 0)
26.       perror(“sem_destroy”);
27.   return 0;
28.       }
a) this program will print the string “Sanfoundry”
b) this program will give an error
c) both (a) and (b)
d) none of the mentioned

26. Which one of the following string will print first by this program?
1.       #include<stdio.h>
2.       #include<pthread.h>
3.       #include<semaphore.h>
4.
5.       sem_t st;
6.       void *fun_t(void *arg);
7.       void *fun_t(void *arg)
8.       {
9.   printf(“Linux\n”);
10.   sem_post(&st);
11.   pthread_exit(“Bye”);
12.       }
13.       int main()
14.       {
15.   pthread_t pt;
16.   void *res_t;
17.   if(pthread_create(&pt,NULL,fun_t,NULL) == -1)
18.       perror(“pthread_create”);
19.   if(sem_init(&st,0,0) != 0)
20.       perror(“sem_init”);
21.   if(sem_wait(&st) != 0)
22.       perror(“sem_wait”);
23.   printf(“Sanoundry\n”);
24.   if(pthread_join(pt,&res_t) == -1)
25.       perror(“pthread_join”);
26.   if(sem_destroy(&st) != 0)
27.       perror(“sem_destroy”);
28.   return 0;
29.       }
a) Linux
b) Sanfoundry
c) can not be predicted
d) none of the mentioned

27. What is the output of this program?
1.       #include<stdio.h>
2.       #include<pthread.h>
3.       #include<semaphore.h>
4.
5.       sem_t st;
6.       void *fun_t(void *arg);
7.       void *fun_t(void *arg)
8.       {
9.   printf(“Linux\n”);
10.   pthread_exit(“Bye”);
11.   sem_post(&st);
12.       }
13.       int main()
14.       {
15.   pthread_t pt;
16.   void *res_t;
17.   if(pthread_create(&pt,NULL,fun_t,NULL) == -1)
18.       perror(“pthread_create”);
19.   if(sem_init(&st,0,0) != 0)
20.       perror(“sem_init”);
21.   if(sem_wait(&st) != 0)
22.       perror(“sem_wait”);
23.   printf(“Sanoundry\n”);
24.   if(pthread_join(pt,&res_t) == -1)
25.       perror(“pthread_join”);
26.   if(sem_destroy(&st) != 0)
27.       perror(“sem_destroy”);
28.   return 0;
29.       }
a) this program will print the only string “Linux”
b) this program will print the only string “Sanfoundry”
c) this program will print both the strings “Linux” and “Sanfoundry”
d) none of the mentioned

28. What is the output of this program?
1.       #include<stdio.h>
2.       #include<pthread.h>
3.       #include<semaphore.h>
4.
5.       sem_t st;
6.       void *fun_t(void *arg);
7.       void *fun_t(void *arg)
8.       {
9.   printf(“Linux\n”);
10.   pthread_exit(“Bye”);
11.       }
12.       int main()
13.       {
14.   pthread_t pt;
15.   void *res_t;
16.   if(pthread_create(&pt,NULL,fun_t,NULL) == -1)
17.       perror(“pthread_create”);
18.   if(sem_init(&st,0,2) != 0)
19.       perror(“sem_init”);
20.   if(sem_wait(&st) != 0)
21.       perror(“sem_wait”);
22.   printf(“Sanoundry\n”);
23.   if(sem_wait(&st) != 0)
24.       perror(“sem_wait”);
25.   if(pthread_join(pt,&res_t) == -1)
26.       perror(“pthread_join”);
27.   if(sem_destroy(&st) != 0)
28.       perror(“sem_destroy”);
29.   return 0;
30.       }
a) this program will print the only string “Linux”
b) this program will print the only string “Sanfoundry”
c) this program will print both the strings “Linux” and “Sanfoundry”
d) none of the mentioned

29. In this program the semaphore
1.       #include<stdio.h>
2.       #include<pthread.h>
3.       #include<semaphore.h>
4.
5.       sem_t st;
6.       void *fun_t(void *arg);
7.       void *fun_t(void *arg)
8.       {
9.   pthread_exit(“Bye”);
10.       }
11.       int main()
12.       {
13.   pthread_t pt;
14.   void *res_t;
15.   if(pthread_create(&pt,NULL,fun_t,NULL) == -1)
16.       perror(“pthread_create”);
17.   if(sem_init(&st,1,2) != 0)
18.       perror(“sem_init”);
19.   if(pthread_join(pt,&res_t) == -1)
20.       perror(“pthread_join”);
21.   if(sem_destroy(&st) != 0)
22.       perror(“sem_destroy”);
23.   return 0;
24.       }
a) can be used only for this process
b) can be used for any other process also
c) can be used
d) none of the mentioned

30. Which one of the following string will print by this program?
1.#include<stdio.h>
2.#include<pthread.h>
3.int main()
4.{
5.    printf(“Sanfoundry\n”);
6.    pthread_exit(“Bye”);
7.    printf(“Linux”);
8.    return 0;
9.}
a) Linux
b) Sanfoundry
c) Bye
d) none of the mentioned

Answers

1-b 2-c 3-a 4-b 5-
6-c 7-c 8-a 9-c 10-b
11-b 12-a 13-b 14-c 15-d
16-d 17-a 18-a 19-a 20-b
21-d 22-b 23-b 24-c 25-b
26-a 27-a 28-c 29-b 30-b
Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *