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

DigitalOcean Referral Badge DigitalOcean Referral Badge

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(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 these

Answer: Option 'B'

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]#

DigitalOcean Referral Badge

2.

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 these

Answer: Option 'A'

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]#

DigitalOcean Referral Badge

3.

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

   /*This is san1.c*/
   #include
   #include
   #include
   #include
 
   int main()
   {
       int shm_id;
       char *addr;
       struct shmid_ds ds;
       shm_id = shmget((key_t)1234,10,0666|IPC_CREAT);
       if(shm_id == -1){
           perror("shmget");
       }
       addr = (char*)shmat(shm_id,NULL,SHM_RND);
       if(addr == (char *)-1){
           perror("shmat");
       }
       strcpy(addr,"Sanfoundry");
       if (shmdt(addr) != 0){
           perror("shmdt");
       }
       if( shmctl(shm_id,IPC_RMID,0) == -1){
           perror("shmctl");
       }
       return 0;
   }
   /*This is san2.c*/
   #include
   #include
   #include
 
   int main()
   {
       int shm_id;
       char *addr;
       struct shmid_ds ds;
       shm_id = shmget((key_t)1234,10,0666|IPC_CREAT);
       if(shm_id == -1){
           perror("shmget");
       }
       addr = (char*)shmat(shm_id,NULL,SHM_RND);
       if(addr == (char *)-1){
           perror("shmat");
       }
       printf("%s\n",addr);
       if (shmdt(addr) != 0){
           perror("shmdt");
       }
       return 0;
    }

   A.) the program will print the string “Sanfoundry”
   B.) the program will nothing
   C.) segmentaion fault
   D.) none of these

Answer: Option 'B'

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]#

DigitalOcean Referral Badge

4.

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

    /*This is san1.c*/
    #include
    #include
    #include
    #include
 
    int main()
    {
        int shm_id;
        char *addr;
        struct shmid_ds ds;
        shm_id = shmget((key_t)1234,10,0666|IPC_CREAT);
        if(shm_id == -1){
            perror("shmget");
        }
        addr = (char*)shmat(shm_id,NULL,SHM_RND);
        if(addr == (char *)-1){
            perror("shmat");
        }
        strcpy(addr,"Sanfoundry");
        if (shmdt(addr) != 0){
            perror("shmdt");
        }
        sleep(10);      
        if( shmctl(shm_id,IPC_RMID,0) == -1){
            perror("shmctl");
        }
        return 0;
    }
    /*This is san2.c*/
    #include
    #include
    #include
 
    int main()
    {
        int shm_id;
        char *addr;
        struct shmid_ds ds;
        shm_id = shmget((key_t)111,10,0666|IPC_CREAT);
        if(shm_id == -1){
            perror("shmget");
        } 
        addr = (char*)shmat(shm_id,NULL,SHM_RND);
        if(addr == (char *)-1){
            perror("shmat");
        } 
        printf("%s\n",addr);
        if (shmdt(addr) != 0){
            perror("shmdt");
        }
        return 0;
     }

   A.) the program will print the string “Sanfoundry”
   B.) the program will nothing
   C.) segmentaion fault
   D.) none of these

Answer: Option 'A'

the program will print the string “Sanfoundry”

DigitalOcean Referral Badge

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

Recent Posts