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?
-
#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] ]#
3. 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,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,"");
-
ret = msgsnd(m_id,&data1,11,0);
-
printf("%dn",ret);
-
if(msgrcv(m_id,&data2,11,1,0) == -1)
-
perror("msgrcv");
-
if(msgctl(m_id,IPC_RMID,0) != 0)
-
perror("msgctl");
-
return 0;
-
}
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?
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
-
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,"");
-
if(msgsnd(m_id,&data1,11,0) == -1)
-
perror("msgsnd");
-
ret = msgrcv(m_id,&data2,11,1,0);
-
printf("%dn",ret);
-
if(msgctl(m_id,IPC_RMID,0) != 0)
-
perror("msgctl");
-
return 0;
-
}
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?
-
#include
-
#include
-
#include
-
#include
-
#include
-
-
static int sem_p(void);
-
static int sem_v(void);
-
union semun{
-
int val;
-
struct semid_ds *buf;
-
unsigned short array;
-
};
-
int sem_id;
-
struct semid_ds myds;
-
struct sembuf mybuf;
-
union semun myun;
-
static int sem_p(void)
-
{
-
mybuf.sem_num = 0;
-
mybuf.sem_op = -1;
-
mybuf.sem_flg = SEM_UNDO;
-
semop(sem_id,&mybuf,1);
-
}
-
static int sem_v(void)
-
{
-
mybuf.sem_num = 0;
-
mybuf.sem_op = 1;
-
mybuf.sem_flg = SEM_UNDO;
-
semop(sem_id,&mybuf,1);
-
}
-
int main()
-
{
-
int wfd, rfd;
-
sem_id = semget((key_t)911,1,0666 | IPC_CREAT);
-
myun.val = 1;
-
semctl(sem_id,0,SETVAL,myun);
-
sem_p();
-
printf("n");
-
sem_v();
-
semctl(sem_id,0,IPC_RMID,myun);
-
return 0;
-
}
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?
-
#include
-
#include
-
#include
-
#include
-
#include
-
-
static int sem_p(void);
-
static int sem_v(void);
-
union semun{
-
int val;
-
struct semid_ds *buf;
-
unsigned short array;
-
};
-
int sem_id;
-
struct semid_ds myds;
-
struct sembuf mybuf;
-
union semun myun;
-
static int sem_p(void)
-
{
-
mybuf.sem_num = 0;
-
mybuf.sem_op = -1;
-
mybuf.sem_flg = SEM_UNDO;
-
semop(sem_id,&mybuf,1);
-
}
-
static int sem_v(void)
-
{
-
mybuf.sem_num = 0;
-
mybuf.sem_op = 1;
-
mybuf.sem_flg = SEM_UNDO;
-
semop(sem_id,&mybuf,1);
-
}
-
int main()
-
{
-
int wfd, rfd;
-
sem_id = semget((key_t)911,1,0666 | IPC_CREAT);
-
myun.val = 0;
-
semctl(sem_id,0,SETVAL,myun);
-
sem_p();
-
printf("n");
-
sem_v();
-
semctl(sem_id,0,IPC_RMID,myun);
-
return 0;
-
}
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?