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;
}
Answer: Option 'B'
this process will block
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;
}
Answer: Option 'A'
0
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;
}
Answer: Option 'B'
this program will print the string “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;
}
Answer: Option 'C'
segmentation fault
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;
}
Answer: Option 'D'
none of these
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;
}
Answer: Option 'D'
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;
}
Answer: Option 'A'
mmap: Success
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;
}
Answer: Option 'A'
mmap: Success
mmap: Success
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;
}
Answer: Option 'A'
Permission granted : Success
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;
}
Answer: Option 'B'
this program will give an error