What is the output of second program if we run the demo1 first and after that we run demo2 in the different terminal?
/*This is demo1.c*/
#include<stdio.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<string.h>
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,"Example");
if (shmdt(addr) != 0){
perror("shmdt");
}
if( shmctl(shm_id,IPC_RMID,0) == -1){
perror("shmctl");
}
return 0;
}
/*This is demo2.c*/
#include<stdio.h>
#include<sys/ipc.h>
#include<sys/shm.h>
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;
}
/*This is demo1.c*/
#include<stdio.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<string.h>
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,"Example");
if (shmdt(addr) != 0){
perror("shmdt");
}
if( shmctl(shm_id,IPC_RMID,0) == -1){
perror("shmctl");
}
return 0;
}
/*This is demo2.c*/
#include<stdio.h>
#include<sys/ipc.h>
#include<sys/shm.h>
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 "Example"
B. the program will nothing
C. segmentaion fault
D. none of the mentioned
Answer: Option B
Related Questions on Linux
What command is used to count the total number of lines, words, and characters contained in a file?
A. countw
B. wcount
C. wc
D. count p
E. None of the above
What command is used with vi editor to delete a single character?
A. x
B. y
C. a
D. z
E. None of the above

Join The Discussion