1. What is the output of this program?
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
-
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,"");
-
if(msgsnd(m_id,&data1,11,0) == -1)
-
perror("msgsnd");
-
if(msgrcv(m_id,&data2,11,0) == -1)
-
perror("msgrcv");
-
printf("%sn",data2.buff);
-
if(msgctl(m_id,IPC_RMID,0) != 0)
-
perror("msgctl");
-
return 0;
-
}
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?
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
-
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,"");
-
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("%sn",data2.buff);
-
return 0;
-
}
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] ]#