Linux Debugging Questions & Answers – POSIX IPCs – Message Queues, Shared Memory and Semaphores

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

1. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.    #include<string.h>
  4.    #include<sys/types.h>
  5.    #include<sys/ipc.h>
  6.    #include<sys/msg.h>
  7.  
  8.    struct data_st{
  9.        long int id;
  10.        char buff[11];
  11.    };
  12.    int main()
  13.    {
  14.        int m_id;
  15.        struct data_st data1, data2;
  16.        m_id = msgget((key_t)181,0666|IPC_CREAT);
  17.        if(m_id == -1)
  18.            perror("msgget");
  19.        data1.id = 1;
  20.        strcpy(data1.buff,"Sanfoundry");
  21.        if(msgsnd(m_id,&data1,11,0) == -1)
  22.            perror("msgsnd");
  23.        if(msgrcv(m_id,&data2,11,0) == -1)
  24.            perror("msgrcv");
  25.        printf("%s\n",data2.buff);
  26.        if(msgctl(m_id,IPC_RMID,0) != 0)
  27.            perror("msgctl");
  28.        return 0;
  29.    }

a) this program will print the string “sanfoundry”
b) this program will give an error
c) this program will give segmentaion fault
d) none of the mentioned
View Answer

Answer: b
Explanation: The fourth argument of the function msgrcv() is missing in this program.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
san.c: In function ‘main’:
san.c:24:2: error: too few arguments to function ‘msgrcv’
/usr/include/sys/msg.h:73:16: note: declared here
[root@localhost sanfoundry]#

2. What is the output of this program?

advertisement
advertisement
  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.    #include<string.h>
  4.    #include<sys/types.h>
  5.    #include<sys/ipc.h>
  6.    #include<sys/msg.h>
  7.  
  8.    struct data_st{
  9.        long int id;
  10.        char buff[11];
  11.    };
  12.    int main()
  13.    {
  14.        int m_id;
  15.        struct data_st data1, data2;
  16.        m_id = msgget((key_t)181,0666|IPC_CREAT);
  17.        if(m_id == -1)
  18.            perror("msgget");
  19.        data1.id = 1;
  20.        strcpy(data1.buff,"Sanfoundry");
  21.        if(msgsnd(m_id,&data1,11,0) == -1)
  22.            perror("msgsnd");
  23.        if(msgctl(m_id,IPC_RMID,0) != 0)
  24.            perror("msgctl");
  25.        if(msgrcv(m_id,&data2,11,1,0) == -1)
  26.            perror("msgrcv");
  27.        printf("%s\n",data2.buff);
  28.        return 0;
  29.    }

a) this program will print the string “Sanfoundry”
b) this program will print the garbage value
c) this program will give segmentation fault
d) none of the mentioned
View Answer

Answer: b
Explanation: The message queue has been removed before recieving the message. Hence the program prints the grabage value of the buffer.
Output:
[root@localhost sanfoundry]# ./san
msgrcv: Invalid argument
Ѕ�
[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<stdlib.h>
  3.    #include<string.h>
  4.    #include<sys/types.h>
  5.    #include<sys/ipc.h>
  6.    #include<sys/msg.h>
  7.  
  8.    struct data_st{
  9.        long int id;
  10.        char buff[11];
  11.    };
  12.    int main()
  13.    {
  14.        int m_id,ret;
  15.        struct data_st data1, data2;
  16.        m_id = msgget((key_t)181,0666|IPC_CREAT);
  17.        if(m_id == -1)
  18.            perror("msgget");
  19.        data1.id = 1;
  20.        strcpy(data1.buff,"Sanfoundry");
  21.        ret = msgsnd(m_id,&data1,11,0);
  22.        printf("%d\n",ret);
  23.        if(msgrcv(m_id,&data2,11,1,0) == -1)
  24.            perror("msgrcv");
  25.        if(msgctl(m_id,IPC_RMID,0) != 0)
  26.            perror("msgctl");
  27.        return 0;
  28.    }

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

Answer: a
Explanation: The function msgsnd() returns 0 when there is no error.
Ouptut:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
0
[root@localhost sanfoundry]#
advertisement

4. What is the output of this program?

advertisement
  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.    #include<string.h>
  4.    #include<sys/types.h>
  5.    #include<sys/ipc.h>
  6.    #include<sys/msg.h>
  7.  
  8.    struct data_st{
  9.        long int id;
  10.        char buff[11];
  11.    };
  12.    int main()
  13.    {
  14.        int m_id,ret;
  15.        struct data_st data1, data2;
  16.        m_id = msgget((key_t)181,0666|IPC_CREAT);
  17.        if(m_id == -1)
  18.            perror("msgget");
  19.        data1.id = 1;
  20.        strcpy(data1.buff,"Sanfoundry");
  21.        if(msgsnd(m_id,&data1,11,0) == -1)
  22.            perror("msgsnd");
  23.        ret = msgrcv(m_id,&data2,11,1,0);
  24.        printf("%d\n",ret);     
  25.        if(msgctl(m_id,IPC_RMID,0) != 0)
  26.            perror("msgctl");
  27.        return 0;
  28.    }

a) 0
b) -1
c) 1
d) 11
View Answer

Answer: d
Explanation: The function msgrcv() returns the number of bytes actually copied into its second argument.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
11
[root@localhost sanfoundry]#

5. What is the output of this pogram?

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.    #include<sys/types.h>
  4.    #include<sys/ipc.h>
  5.    #include<sys/sem.h>
  6.  
  7.    static int sem_p(void);
  8.    static int sem_v(void);
  9.    union semun{
  10.        int val;
  11.        struct semid_ds *buf;
  12.        unsigned short array;
  13.    };
  14.    int sem_id;
  15.    struct semid_ds myds;
  16.    struct sembuf mybuf;
  17.    union semun myun;
  18.    static int sem_p(void)
  19.    {
  20.        mybuf.sem_num = 0;
  21.        mybuf.sem_op = -1;
  22.        mybuf.sem_flg = SEM_UNDO;
  23.        semop(sem_id,&mybuf,1);
  24.    }
  25.    static int sem_v(void)
  26.    {
  27.        mybuf.sem_num = 0;
  28.        mybuf.sem_op = 1;
  29.        mybuf.sem_flg = SEM_UNDO;
  30.        semop(sem_id,&mybuf,1);
  31.    }
  32.    int main()
  33.    {
  34.        int wfd, rfd;
  35.        sem_id = semget((key_t)911,1,0666 | IPC_CREAT);
  36.        myun.val = 1;
  37.        semctl(sem_id,0,SETVAL,myun);
  38.        sem_p();
  39.        printf("Sanfoundry\n");
  40.        sem_v();
  41.        semctl(sem_id,0,IPC_RMID,myun);
  42.        return 0;
  43.    }

a) this program will print the string “Sanfoundry”
b) this process will remain block
c) this program will print the string “Sanfoundry” & process will remain block
d) none of the mentioned
View Answer

Answer: a
Explanation: The function sem_p() will increment the value of semaphore but it will not block the process.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
Sanfoundry
[root@localhost sanfoundry]#

6. What is the output of this pogram?

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.    #include<sys/types.h>
  4.    #include<sys/ipc.h>
  5.    #include<sys/sem.h>
  6.  
  7.    static int sem_p(void);
  8.    static int sem_v(void);
  9.    union semun{
  10.        int val;
  11.        struct semid_ds *buf;
  12.        unsigned short array;
  13.    };
  14.    int sem_id;
  15.    struct semid_ds myds;
  16.    struct sembuf mybuf;
  17.    union semun myun;
  18.    static int sem_p(void)
  19.    {
  20.        mybuf.sem_num = 0;
  21.        mybuf.sem_op = -1;
  22.        mybuf.sem_flg = SEM_UNDO;
  23.        semop(sem_id,&mybuf,1);
  24.    }
  25.    static int sem_v(void)
  26.    {
  27.        mybuf.sem_num = 0;
  28.        mybuf.sem_op = 1;
  29.        mybuf.sem_flg = SEM_UNDO;
  30.        semop(sem_id,&mybuf,1);
  31.    }
  32.    int main()
  33.    {
  34.        int wfd, rfd;
  35.        sem_id = semget((key_t)911,1,0666 | IPC_CREAT);
  36.        myun.val = 0;
  37.        semctl(sem_id,0,SETVAL,myun);
  38.        sem_p();
  39.        printf("Sanfoundry\n");
  40.        sem_v();
  41.        semctl(sem_id,0,IPC_RMID,myun);
  42.        return 0;
  43.    }

a) this program will print the string “Sanfoundry”
b) this process will remain block
c) this program will print the string “Sanfoundry” & process will remain block
d) none of the mentioned
View Answer

Answer: b
Explanation: The initial value of semaphore in this program is 0. Hence the function sem_p() will block the process.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
^Z
[24]+ Stopped ./san
[root@localhost sanfoundry]#

7. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.    #include<sys/types.h>
  4.    #include<sys/ipc.h>
  5.    #include<sys/sem.h>
  6.  
  7.    static int sem_p(void);
  8.    static int sem_v(void);
  9.    union semun{
  10.        int val;
  11.        struct semid_ds *buf;
  12.        unsigned short array;
  13.    };
  14.    int sem_id;
  15.    struct semid_ds myds;
  16.    struct sembuf mybuf;
  17.    union semun myun;
  18.    static int sem_p(void)
  19.    {
  20.        mybuf.sem_num = 0;
  21.        mybuf.sem_op = -1;
  22.        mybuf.sem_flg = SEM_UNDO;
  23.        semop(sem_id,&mybuf,1);
  24.    }
  25.    static int sem_v(void)
  26.    {
  27.        mybuf.sem_num = 0;
  28.        mybuf.sem_op = 1;
  29.        mybuf.sem_flg = SEM_UNDO;
  30.        semop(sem_id,&mybuf,1);
  31.    }
  32.    int main()
  33.    {
  34.        int wfd, rfd;
  35.        sem_id = semget((key_t)911,1,0666 | IPC_CREAT);
  36.        myun.val = 0;
  37.        semctl(sem_id,0,IPC_RMID,myun);
  38.        semctl(sem_id,0,SETVAL,myun);
  39.        sem_p();
  40.        printf("Sanfoundry\n");
  41.        sem_v();
  42.        return 0;
  43.    }

a) this process will remain block
b) this program will print the string “Sanfoundry”
c) this program will print the string “Sanfoundry” & process will remain block
d) none of the mentioned
View Answer

Answer: b
Explanation: The semaphore has been removed before calling the function sem_p(). Hence the function sem() will not affect the process.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
Sanfoundry
[root@localhost sanfoundry]#

8. What is the output of second program if we run the san1 first and after that we run san2 in the different terminal?

  1.    /*This is san1.c*/
  2.    #include<stdio.h>
  3.    #include<sys/ipc.h>
  4.    #include<sys/shm.h>
  5.    #include<string.h>
  6.  
  7.    int main()
  8.    {
  9.        int shm_id;
  10.        char *addr;
  11.        struct shmid_ds ds;
  12.        shm_id = shmget((key_t)1234,10,0666|IPC_CREAT);
  13.        if(shm_id == -1){
  14.            perror("shmget");
  15.        }
  16.        addr = (char*)shmat(shm_id,NULL,SHM_RND);
  17.        if(addr == (char *)-1){
  18.            perror("shmat");
  19.        }
  20.        strcpy(addr,"Sanfoundry");
  21.        if (shmdt(addr) != 0){
  22.            perror("shmdt");
  23.        }
  24.        sleep(10);      
  25.        if( shmctl(shm_id,IPC_RMID,0) == -1){
  26.            perror("shmctl");
  27.        }
  28.        return 0;
  29.    }
  30.    /*This is san2.c*/
  31.    #include<stdio.h>
  32.    #include<sys/ipc.h>
  33.    #include<sys/shm.h>
  34.  
  35.    int main()
  36.    {
  37.        int shm_id;
  38.        char *addr;
  39.        struct shmid_ds ds;
  40.        shm_id = shmget((key_t)1234,10,0666|IPC_CREAT);
  41.        if(shm_id == -1){
  42.            perror("shmget");
  43.        }
  44.        addr = (char*)shmat(shm_id,NULL,SHM_RND);
  45.        if(addr == (char *)-1){
  46.            perror("shmat");
  47.        }
  48.        printf("%s\n",addr);
  49.        if (shmdt(addr) != 0){
  50.            perror("shmdt");
  51.        }
  52.        return 0;
  53.     }

a) the program will print the string “Sanfoundry”
b) the program will nothing
c) segmentaion fault
d) none of the mentioned
View Answer

Answer: a
Explanation: The process of san1.c has written the string “Sanfoundry” in the shared memory and the process of san2.c accessed the string from the shared memory. This is valid only for 10 seconds from the execution of first program san1.c.
Output1:
[root@localhost sanfoundry]# gcc -o san1 san1.c
[root@localhost sanfoundry]# ./san1

Output2:
[root@localhost sanfoundry]# gcc -o san2 san2.c
[root@localhost sanfoundry]# ./san2
Sanfoundry
[root@localhost sanfoundry]#

9. What is the output of second program if we run the san1 first and after that we run san2 in the different terminal?

  1.    /*This is san1.c*/
  2.    #include<stdio.h>
  3.    #include<sys/ipc.h>
  4.    #include<sys/shm.h>
  5.    #include<string.h>
  6.  
  7.    int main()
  8.    {
  9.        int shm_id;
  10.        char *addr;
  11.        struct shmid_ds ds;
  12.        shm_id = shmget((key_t)1234,10,0666|IPC_CREAT);
  13.        if(shm_id == -1){
  14.            perror("shmget");
  15.        }
  16.        addr = (char*)shmat(shm_id,NULL,SHM_RND);
  17.        if(addr == (char *)-1){
  18.            perror("shmat");
  19.        }
  20.        strcpy(addr,"Sanfoundry");
  21.        if (shmdt(addr) != 0){
  22.            perror("shmdt");
  23.        }
  24.        if( shmctl(shm_id,IPC_RMID,0) == -1){
  25.            perror("shmctl");
  26.        }
  27.        return 0;
  28.    }
  29.    /*This is san2.c*/
  30.    #include<stdio.h>
  31.    #include<sys/ipc.h>
  32.    #include<sys/shm.h>
  33.  
  34.    int main()
  35.    {
  36.        int shm_id;
  37.        char *addr;
  38.        struct shmid_ds ds;
  39.        shm_id = shmget((key_t)1234,10,0666|IPC_CREAT);
  40.        if(shm_id == -1){
  41.            perror("shmget");
  42.        }
  43.        addr = (char*)shmat(shm_id,NULL,SHM_RND);
  44.        if(addr == (char *)-1){
  45.            perror("shmat");
  46.        }
  47.        printf("%s\n",addr);
  48.        if (shmdt(addr) != 0){
  49.            perror("shmdt");
  50.        }
  51.        return 0;
  52.     }

a) the program will print the string “Sanfoundry”
b) the program will nothing
c) segmentaion fault
d) none of the mentioned
View Answer

Answer: b
Explanation: The process of san1.c has written the string “Sanfoundry” in the shared memory and the process of san2.c could not access the string from the shared memory due to delay.
Output1:
[root@localhost sanfoundry]# gcc -o san1 san1.c
[root@localhost sanfoundry]# ./san1

Output2:
[root@localhost sanfoundry]# gcc -o san2 san2.c
[root@localhost sanfoundry]# ./san2

[root@localhost sanfoundry]#

10. What is the output of second program if we run the san1 first and after that we run san2 in the different terminal?

  1.     /*This is san1.c*/
  2.     #include<stdio.h>
  3.     #include<sys/ipc.h>
  4.     #include<sys/shm.h>
  5.     #include<string.h>
  6.  
  7.     int main()
  8.     {
  9.         int shm_id;
  10.         char *addr;
  11.         struct shmid_ds ds;
  12.         shm_id = shmget((key_t)1234,10,0666|IPC_CREAT);
  13.         if(shm_id == -1){
  14.             perror("shmget");
  15.         }
  16.         addr = (char*)shmat(shm_id,NULL,SHM_RND);
  17.         if(addr == (char *)-1){
  18.             perror("shmat");
  19.         }
  20.         strcpy(addr,"Sanfoundry");
  21.         if (shmdt(addr) != 0){
  22.             perror("shmdt");
  23.         }
  24.         sleep(10);      
  25.         if( shmctl(shm_id,IPC_RMID,0) == -1){
  26.             perror("shmctl");
  27.         }
  28.         return 0;
  29.     }
  30.     /*This is san2.c*/
  31.     #include<stdio.h>
  32.     #include<sys/ipc.h>
  33.     #include<sys/shm.h>
  34.  
  35.     int main()
  36.     {
  37.         int shm_id;
  38.         char *addr;
  39.         struct shmid_ds ds;
  40.         shm_id = shmget((key_t)111,10,0666|IPC_CREAT);
  41.         if(shm_id == -1){
  42.             perror("shmget");
  43.         } 
  44.         addr = (char*)shmat(shm_id,NULL,SHM_RND);
  45.         if(addr == (char *)-1){
  46.             perror("shmat");
  47.         } 
  48.         printf("%s\n",addr);
  49.         if (shmdt(addr) != 0){
  50.             perror("shmdt");
  51.         }
  52.         return 0;
  53.      }

a) the program will print the string “Sanfoundry”
b) the program will nothing
c) segmentaion fault
d) none of the mentioned
View Answer

Answer: a
Explanation: The process of san1.c has written the string “Sanfoundry” in the shared memory and the process of san2.c could not access that shared memory because the key is different.
Output1:
[root@localhost sanfoundry]# gcc -o san1 san1.c
[root@localhost sanfoundry]# ./san1

Output2:
[root@localhost sanfoundry]# gcc -o san2 san2.c
[root@localhost sanfoundry]# ./san2

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