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(msgrcv(m_id,&data2,11,0) == -1)
perror("msgrcv");
printf("%s\n",data2.buff);
if(msgctl(m_id,IPC_RMID,0) != 0)
perror("msgctl");
return 0;
}
a) this program will print the string “sanfoundry”
b) this program will give an error
c) this program will give segmentaion fault
d) none of the mentioned
View Answer
Explanation: The fourth argument of the function msgrcv() is missing in this program.
Output:
[root@localhost sanfoundry]# 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
[root@localhost sanfoundry]#
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;
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;
}
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 the mentioned
View Answer
Explanation: 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]#
3. 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;
}
a) 0
b) 1
c) -1
d) none of the mentioned
View Answer
Explanation: 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]#
4. 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");
if(msgsnd(m_id,&data1,11,0) == -1)
perror("msgsnd");
ret = msgrcv(m_id,&data2,11,1,0);
printf("%d\n",ret);
if(msgctl(m_id,IPC_RMID,0) != 0)
perror("msgctl");
return 0;
}
a) 0
b) -1
c) 1
d) 11
View Answer
Explanation: The function msgrcv() returns the number of bytes actually copied into its second argument.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
11
[root@localhost sanfoundry]#
5. What is the output of this pogram?
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
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("Sanfoundry\n");
sem_v();
semctl(sem_id,0,IPC_RMID,myun);
return 0;
}
a) this program will print the string “Sanfoundry”
b) this process will remain block
c) this program will print the string “Sanfoundry” & process will remain block
d) none of the mentioned
View Answer
Explanation: The function sem_p() will increment the value of semaphore but it will not block the process.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
Sanfoundry
[root@localhost sanfoundry]#
6. What is the output of this pogram?
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
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("Sanfoundry\n");
sem_v();
semctl(sem_id,0,IPC_RMID,myun);
return 0;
}
a) this program will print the string “Sanfoundry”
b) this process will remain block
c) this program will print the string “Sanfoundry” & process will remain block
d) none of the mentioned
View Answer
Explanation: The initial value of semaphore in this program is 0. Hence the function sem_p() will block the process.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
^Z
[24]+ Stopped ./san
[root@localhost sanfoundry]#
7. What is the output of this program?
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
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,IPC_RMID,myun);
semctl(sem_id,0,SETVAL,myun);
sem_p();
printf("Sanfoundry\n");
sem_v();
return 0;
}
a) this process will remain block
b) this program will print the string “Sanfoundry”
c) this program will print the string “Sanfoundry” & process will remain block
d) none of the mentioned
View Answer
Explanation: The semaphore has been removed before calling the function sem_p(). Hence the function sem() will not affect the process.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
Sanfoundry
[root@localhost sanfoundry]#
8. 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<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,"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<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 “Sanfoundry”
b) the program will nothing
c) segmentaion fault
d) none of the mentioned
View Answer
Explanation: The process of san1.c has written the string “Sanfoundry” in the shared memory and the process of san2.c accessed the string from the shared memory. This is valid only for 10 seconds from the execution of first program san1.c.
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
Sanfoundry
[root@localhost sanfoundry]#
9. 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<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,"Sanfoundry");
if (shmdt(addr) != 0){
perror("shmdt");
}
if( shmctl(shm_id,IPC_RMID,0) == -1){
perror("shmctl");
}
return 0;
}
/*This is san2.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 “Sanfoundry”
b) the program will nothing
c) segmentaion fault
d) none of the mentioned
View Answer
Explanation: 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
10. 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<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,"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<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)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 the mentioned
View Answer
Explanation: The process of san1.c has written the string “Sanfoundry” in the shared memory and the process of san2.c could not access that shared memory because the key is different.
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
Sanfoundry Global Education & Learning Series – Linux Administration & Programming.
Here’s the list of Best Books in Linux Commands & Shell Programming.
Here’s the list of Best Books in Linux Kernel, Device-Drivers & System Programming.
To practice all questions on Linux Administration & Programming, here is complete set of 1000+ Multiple Choice Questions and Answers on Linux.
If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]
- Check Information Technology Books
- Check Linux Books
- Practice Programming MCQs
- Apply for Programming Internship