250+ TOP MCQs on POSIX IPCs – Message Queues, Shared Memory and Semaphores and Answers

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
  2.    #include
  3.    #include
  4.    #include
  5.    #include
  6.    #include
  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,"");
  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("%sn",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 “”
b) this program will give an error
c) this program will give segmentaion fault
d) none of the mentioned
Answer: b
Clarification: The fourth argument of the function msgrcv() is missing in this program.
Output:
[[email protected] ]# 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
[[email protected] ]#

2. What is the output of this program?

  1.    #include
  2.    #include
  3.    #include
  4.    #include
  5.    #include
  6.    #include
  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,"");
  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("%sn",data2.buff);
  28.        return 0;
  29.    }

a) this program will print the string “”
b) this program will print the garbage value
c) this program will give segmentation fault
d) none of the mentioned
Answer: b
Clarification: The message queue has been removed before recieving the message. Hence the program prints the grabage value of the buffer.
Output:
[[email protected] ]# ./san
msgrcv: Invalid argument
Ѕ�
[[email protected] ]#

3. What is the output of this program?

  1.    #include
  2.    #include
  3.    #include
  4.    #include
  5.    #include
  6.    #include
  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,"");
  21.        ret = msgsnd(m_id,&data1,11,0);
  22.        printf("%dn",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
Answer: a
Clarification: The function msgsnd() returns 0 when there is no error.
Ouptut:
[[email protected] ]# gcc -o san san.c
[[email protected] ]# ./san
0
[[email protected] ]#

4. What is the output of this program?

  1.    #include
  2.    #include
  3.    #include
  4.    #include
  5.    #include
  6.    #include
  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,"");
  21.        if(msgsnd(m_id,&data1,11,0) == -1)
  22.            perror("msgsnd");
  23.        ret = msgrcv(m_id,&data2,11,1,0);
  24.        printf("%dn",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
Answer: d
Clarification: The function msgrcv() returns the number of bytes actually copied into its second argument.
Output:
[[email protected] ]# gcc -o san san.c
[[email protected] ]# ./san
11
[[email protected] ]#

5. What is the output of this pogram?

  1.    #include
  2.    #include
  3.    #include
  4.    #include
  5.    #include
  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("n");
  40.        sem_v();
  41.        semctl(sem_id,0,IPC_RMID,myun);
  42.        return 0;
  43.    }

a) this program will print the string “”
b) this process will remain block
c) this program will print the string “” & process will remain block
d) none of the mentioned
Answer: a
Clarification: The function sem_p() will increment the value of semaphore but it will not block the process.
Output:
[[email protected] ]# gcc -o san san.c
[[email protected] ]# ./san

[[email protected] ]#

6. What is the output of this pogram?

  1.    #include
  2.    #include
  3.    #include
  4.    #include
  5.    #include
  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("n");
  40.        sem_v();
  41.        semctl(sem_id,0,IPC_RMID,myun);
  42.        return 0;
  43.    }

a) this program will print the string “”
b) this process will remain block
c) this program will print the string “” & process will remain block
d) none of the mentioned
Answer: b
Clarification: The initial value of semaphore in this program is 0. Hence the function sem_p() will block the process.
Output:
[[email protected] ]# gcc -o san san.c
[[email protected] ]# ./san
^Z
[24]+ Stopped ./san
[[email protected] ]#

7. What is the output of this program?

  1.    #include
  2.    #include
  3.    #include
  4.    #include
  5.    #include
  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("n");
  41.        sem_v();
  42.        return 0;
  43.    }

a) this process will remain block
b) this program will print the string “”
c) this program will print the string “” & process will remain block
d) none of the mentioned
Answer: b
Clarification: The semaphore has been removed before calling the function sem_p(). Hence the function sem() will not affect the process.
Output:
[[email protected] ]# gcc -o san san.c
[[email protected] ]# ./san

[[email protected] ]#

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
  3.    #include
  4.    #include
  5.    #include
  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,"");
  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
  32.    #include
  33.    #include
  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("%sn",addr);
  49.        if (shmdt(addr) != 0){
  50.            perror("shmdt");
  51.        }
  52.        return 0;
  53.     }

a) the program will print the string “”
b) the program will nothing
c) segmentaion fault
d) none of the mentioned
Answer: a
Clarification: The process of san1.c has written the string “” 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:
[[email protected] ]# gcc -o san1 san1.c
[[email protected] ]# ./san1

Output2:
[[email protected] ]# gcc -o san2 san2.c
[[email protected] ]# ./san2

[[email protected] ]#

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
  3.    #include
  4.    #include
  5.    #include
  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,"");
  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
  31.    #include
  32.    #include
  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("%sn",addr);
  48.        if (shmdt(addr) != 0){
  49.            perror("shmdt");
  50.        }
  51.        return 0;
  52.     }

a) the program will print the string “”
b) the program will nothing
c) segmentaion fault
d) none of the mentioned
Answer: b
Clarification: The process of san1.c has written the string “” in the shared memory and the process of san2.c could not access the string from the shared memory due to delay.
Output1:
[[email protected] ]# gcc -o san1 san1.c
[[email protected] ]# ./san1

Output2:
[[email protected] ]# gcc -o san2 san2.c
[[email protected] ]# ./san2

[[email protected] ]#

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
  3.     #include
  4.     #include
  5.     #include
  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,"");
  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
  32.     #include
  33.     #include
  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("%sn",addr);
  49.         if (shmdt(addr) != 0){
  50.             perror("shmdt");
  51.         }
  52.         return 0;
  53.      }

a) the program will print the string “”
b) the program will nothing
c) segmentaion fault
d) none of the mentioned
Answer: a
Clarification: The process of san1.c has written the string “” in the shared memory and the process of san2.c could not access that shared memory because the key is different.
Output1:
[[email protected] ]# gcc -o san1 san1.c
[[email protected] ]# ./san1

Output2:
[[email protected] ]# gcc -o san2 san2.c
[[email protected] ]# ./san2

[[email protected] ]#

Leave a Reply

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