1.
What is the output of this program?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
struct data_st{
long int id;
char buff[11];
};
int main()
{
int m_id;
struct data_st data1, data2;
m_id = msgget((key_t)181,0666|IPC_CREAT);
if(m_id == -1)
perror("msgget");
data1.id = 1;
strcpy(data1.buff,"Sanfoundry");
if(msgsnd(m_id,&data1,11,0) == -1)
perror("msgsnd");
if(msgctl(m_id,IPC_RMID,0) != 0)
perror("msgctl");
if(msgrcv(m_id,&data2,11,1,0) == -1)
perror("msgrcv");
printf("%s\n",data2.buff);
return 0;
}
2.
What is the output of this program?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
struct data_st{
long int id;
char buff[11];
};
int main()
{
int m_id,ret;
struct data_st data1, data2;
m_id = msgget((key_t)181,0666|IPC_CREAT);
if(m_id == -1)
perror("msgget");
data1.id = 1;
strcpy(data1.buff,"Sanfoundry");
ret = msgsnd(m_id,&data1,11,0);
printf("%d\n",ret);
if(msgrcv(m_id,&data2,11,1,0) == -1)
perror("msgrcv");
if(msgctl(m_id,IPC_RMID,0) != 0)
perror("msgctl");
return 0;
}
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; }
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. Output2:
Output1:
[root@localhost sanfoundry]# gcc -o san1 san1.c
[root@localhost sanfoundry]# ./san1
[root@localhost sanfoundry]# gcc -o san2 san2.c
[root@localhost sanfoundry]# ./san2
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; }