Linux Debugging Questions & Answers – System-V IPCs – Message Queues, Shared Memory and Semaphores

This set of Linux Debugging questions and answers focuses on the System-V IPCs i.e. Message Queues, Shared Memory and Semaphores.

1. 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
View Answer

Answer: b
Explanation: The initial value of semaphore is 0. As we call the sem_wait() in this program, it blocks the process.
Output:
[root@localhost sanfoundry]# gcc -o san san.c -lpthread
[root@localhost sanfoundry]# ./san
^Z
[37]+ Stopped ./san
[root@localhost sanfoundry]#

2. What is the output of this program?

advertisement
advertisement
  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 0 and Sanfoundry
d) None of the mentioned
View Answer

Answer: a
Explanation: The sem_getvalue() is used to get the current value of the semaphore.
Output:
[root@localhost sanfoundry]# gcc -o san san.c -lpthread
[root@localhost sanfoundry]# ./san
0
^Z
[58]+ Stopped ./san
[root@localhost sanfoundry]#
Note: Join free Sanfoundry classes at Telegram or Youtube

3. 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
View Answer

Answer: b
Explanation: The initial value of semaphore is 0 but the sem_post() increments the value of semaphore by 1.
Output:
[root@localhost sanfoundry]# gcc -o san san.c -lpthread
[root@localhost sanfoundry]# ./san
Sanfoundry
[root@localhost sanfoundry]#
advertisement

4. What is the output of this program?

advertisement
  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
View Answer

Answer: c
Explanation: The sem_wait() is trying to use the semaphore when it is already closed.
Output:
[root@localhost sanfoundry]# gcc -o san san.c -lpthread
[root@localhost sanfoundry]# ./san
Segmentation fault (core dumped)
[root@localhost sanfoundry]#

5. 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
View Answer

Answer: d
Explanation: This process will block when the sem_wait() has been called last time in this program.
Output:
[root@localhost sanfoundry]# gcc -o san san.c -lpthread
[root@localhost sanfoundry]# ./san
^Z
[64]+ Stopped ./san
[root@localhost sanfoundry]#

6. 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
View Answer

Answer: d
Explanation: On success the shm_open() returns a nonnegative file descriptor.
Output:
[root@localhost sanfoundry]# gcc -o san san.c -lrt
[root@localhost sanfoundry]# ./san
3
[root@localhost sanfoundry]

7. 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
View Answer

Answer: a
Explanation: Memory of the 100 bytes is mapped successfully as shared memory.
Output:
[root@localhost sanfoundry]# gcc -o san san.c -lrt
[root@localhost sanfoundry]# ./san
mmap: Success
[root@localhost sanfoundry]#

8. 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
View Answer

Answer: a
Explanation: None.
Output:
[root@localhost sanfoundry]# gcc -o san san.c -lrt
[root@localhost sanfoundry]# ./san
mmap: Success
mmap: Success
[root@localhost sanfoundry]#

9. 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
View Answer

Answer: a
Explanation: The shm_open() returns the error EACCES, when the permission is denied.
Output:
[root@localhost sanfoundry]# gcc -o san san.c -lrt
[root@localhost sanfoundry]# ./san
Permission granted
: Success
[root@localhost sanfoundry]#

10. 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
View Answer

Answer: b
Explanation: There is no shared memory object “shared_memory” already present in the system.
Output:
[root@localhost sanfoundry]# gcc -o san san.c -lrt
[root@localhost sanfoundry]# ./san
shm_open
: No such file or directory
[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.