1. What is the output of this program?
#include<stdio.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<semaphore.h>
int main()
{
sem_t* sem_id;
sem_id = sem_open("sem_value",O_CREAT,0666,0);
if(sem_id == SEM_FAILED)
perror("sem_open");
sem_wait(sem_id);
printf("Sanfoundry\n");
if(sem_close(sem_id) == -1)
perror("sem_close");
return 0;
}
a) this program will print the string “Sanfoundry”
b) this process will block
c) segmentaion fault
d) none of the mentioned
View Answer
Explanation: The initial value of semaphore is 0. As we call the sem_wait() in this program, it blocks the process.
Output:
[root@localhost sanfoundry]# gcc -o san san.c -lpthread
[root@localhost sanfoundry]# ./san
^Z
[37]+ Stopped ./san
[root@localhost sanfoundry]#
2. What is the output of this program?
#include<stdio.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<semaphore.h>
int main()
{
sem_t* sem_id;
int value;
sem_id = sem_open("sem_value",O_CREAT,0666,0);
if(sem_id == SEM_FAILED)
perror("sem_open");
if(sem_getvalue(sem_id,&value) == -1)
perror("sem_getvalue");
printf("%d\n",value);
sem_wait(sem_id);
printf("Sanfoundry\n");
if(sem_close(sem_id) == -1)
perror("sem_close");
return 0;
}
a) 0
b) Sanfoundry
c) Both 0 and Sanfoundry
d) None of the mentioned
View Answer
Explanation: The sem_getvalue() is used to get the current value of the semaphore.
Output:
[root@localhost sanfoundry]# gcc -o san san.c -lpthread
[root@localhost sanfoundry]# ./san
0
^Z
[58]+ Stopped ./san
[root@localhost sanfoundry]#
3. What is the output of this program?
#include<stdio.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<semaphore.h>
int main()
{
sem_t* sem_id;
sem_id = sem_open("sem_value",O_CREAT,0666,0);
if(sem_id == SEM_FAILED)
perror("sem_open");
sem_post(sem_id);
printf("Sanfoundry\n");
if(sem_close(sem_id) == -1)
perror("sem_close");
return 0;
}
a) this process will block
b) this program will print the string “Sanfoundry”
c) segmentation fault
d) none of the mentioned
View Answer
Explanation: The initial value of semaphore is 0 but the sem_post() increments the value of semaphore by 1.
Output:
[root@localhost sanfoundry]# gcc -o san san.c -lpthread
[root@localhost sanfoundry]# ./san
Sanfoundry
[root@localhost sanfoundry]#
4. What is the output of this program?
#include<stdio.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<semaphore.h>
int main()
{
sem_t* sem_id;
sem_id = sem_open("sem_value",O_CREAT,0666,0);
if(sem_id == SEM_FAILED)
perror("sem_open");
if(sem_close(sem_id) == -1)
perror("sem_close");
sem_wait(sem_id);
printf("Sanfoundry\n");
return 0;
}
a) this process will block
b) this program will print the string “Sanfoundry”
c) segmentation fault
d) none of the mentioned
View Answer
Explanation: The sem_wait() is trying to use the semaphore when it is already closed.
Output:
[root@localhost sanfoundry]# gcc -o san san.c -lpthread
[root@localhost sanfoundry]# ./san
Segmentation fault (core dumped)
[root@localhost sanfoundry]#
5. What is the output of this program?
#include<stdio.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<semaphore.h>
int main()
{
sem_t* sem_id;
int value;
sem_id = sem_open("new_13",O_CREAT,0666,3);
if(sem_id == SEM_FAILED)
perror("sem_open");
sem_wait(sem_id);
sem_wait(sem_id);
sem_wait(sem_id);
sem_wait(sem_id);
sem_post(sem_id);
sem_post(sem_id);
sem_getvalue(sem_id,&value);
printf("%d\n",value);
if(sem_close(sem_id) == -1)
perror("sem_close");
return 0;
}
a) 2
b) 3
c) 0
d) none of the mentioned
View Answer
Explanation: This process will block when the sem_wait() has been called last time in this program.
Output:
[root@localhost sanfoundry]# gcc -o san san.c -lpthread
[root@localhost sanfoundry]# ./san
^Z
[64]+ Stopped ./san
[root@localhost sanfoundry]#
6. What is the output of this program?
#include<stdio.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/mman.h>
int main()
{
int s_id;
s_id = shm_open("shared_mem",O_CREAT|O_RDWR,0666);
printf("%d\n",s_id);
if(shm_unlink("shared_mem") == -1)
perror("shm_unlink");
return 0;
}
a) -1
b) 1
c) 2
d) 3
View Answer
Explanation: On success the shm_open() returns a nonnegative file descriptor.
Output:
[root@localhost sanfoundry]# gcc -o san san.c -lrt
[root@localhost sanfoundry]# ./san
3
[root@localhost sanfoundry]
7. What is the output of this program?
#include<stdio.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/mman.h>
int main()
{
int s_id;
int *ptr;
s_id = shm_open("shared_mem",O_CREAT|O_RDWR,0666);
if(s_id == -1)
perror("shm_open");
ptr = mmap(NULL,100,PROT_WRITE,MAP_PRIVATE,s_id,0);
if(ptr == MAP_FAILED);
perror("mmap");
if(munmap(ptr,100) == -1)
perror("munmap");
if(shm_unlink("shared_mem") == -1)
perror("shm_unlink");
return 0;
}
a) mmap: Success
b) mmap: Failure
c) munmap: Success
d) munmap: Failure
View Answer
Explanation: Memory of the 100 bytes is mapped successfully as shared memory.
Output:
[root@localhost sanfoundry]# gcc -o san san.c -lrt
[root@localhost sanfoundry]# ./san
mmap: Success
[root@localhost sanfoundry]#
8. What is the output of this program?
#include<stdio.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/mman.h>
int main()
{
int s_id;
int *ptr;
s_id = shm_open("shared_mem",O_CREAT|O_RDWR,0666);
if(s_id == -1)
perror("shm_open");
ptr = mmap(NULL,100,PROT_WRITE,MAP_PRIVATE,s_id,0);
if(ptr == MAP_FAILED);
perror("mmap");
ptr = mmap(ptr,100,PROT_WRITE,MAP_PRIVATE,s_id,0);
if(ptr == MAP_FAILED);
perror("mmap");
if(munmap(ptr,100) == -1)
perror("munmap");
if(shm_unlink("shared_mem") == -1)
perror("shm_unlink");
return 0;
}
a) mmap: Success
mmap: Success
b) mmap: Success
mmap: Failure
c) segmentation fault
d) none of the mentioned
View Answer
Explanation: None.
Output:
[root@localhost sanfoundry]# gcc -o san san.c -lrt
[root@localhost sanfoundry]# ./san
mmap: Success
mmap: Success
[root@localhost sanfoundry]#
9. What is the output of this program?
#include<stdio.h>
#include<fcntl.h>
#include<errno.h>
#include<sys/stat.h>
#include<sys/mman.h>
int main()
{
int s_id;
s_id = shm_open("shared_mem",O_CREAT|O_RDWR,0666);
if(s_id != EACCES)
perror("Permission granted\n");
return 0;
}
a) Permission granted
: Success
b) Permission granted
c) segmentation fault
d) none of the mentioned
View Answer
Explanation: The shm_open() returns the error EACCES, when the permission is denied.
Output:
[root@localhost sanfoundry]# gcc -o san san.c -lrt
[root@localhost sanfoundry]# ./san
Permission granted
: Success
[root@localhost sanfoundry]#
10. What is the output of this program?
#include<stdio.h>
#include<fcntl.h>
#include<errno.h>
#include<sys/stat.h>
#include<sys/mman.h>
int main()
{
int s_id;
s_id = shm_open("shared_memory",O_TRUNC,0666);
if(s_id == -1)
perror("shm_open\n");
return 0;
}
a) this program will give an error because OTRUNC is not a valid flag
b) this program will give an error
c) this program will give segmentation fault
d) none of the mentioned
View Answer
Explanation: There is no shared memory object “shared_memory” already present in the system.
Output:
[root@localhost sanfoundry]# gcc -o san san.c -lrt
[root@localhost sanfoundry]# ./san
shm_open
: No such file or directory
[root@localhost sanfoundry]#
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.
- Check Information Technology Books
- Check Linux Books
- Practice Programming MCQs
- Apply for Programming Internship