Linux Debugging Questions & Answers – System-V IPCs – Message Queues, Shared Memory and Semaphores

Basic Computer Knowledge Test Questions and Answers

1.

What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<fcntl.h>
  3.    #include<sys/stat.h>
  4.    #include<semaphore.h>
  5. 
     
  6.    int main()
  7.    {
  8.        sem_t* sem_id;
  9.        sem_id = sem_open("sem_value",O_CREAT,0666,0);
  10.        if(sem_id == SEM_FAILED)
  11.            perror("sem_open");
  12.        sem_wait(sem_id);
  13.        printf("Sanfoundry\n");
  14.        if(sem_close(sem_id) == -1)
  15.            perror("sem_close");    
  16.        return 0;
  17.    }

   A.) this program will print the string “Sanfoundry”
   B.) this process will block
   C.) segmentaion fault
   D.) none of these

Answer: Option 'B'

this process will block

Basic Computer Knowledge Test Questions and Answers

2.

What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<fcntl.h>
  3.    #include<sys/stat.h>
  4.    #include<semaphore.h>
  5. 
     
  6.    int main()
  7.    {
  8.        sem_t* sem_id;
  9.        int value;
  10.        sem_id = sem_open("sem_value",O_CREAT,0666,0);
  11.        if(sem_id == SEM_FAILED)
  12.            perror("sem_open");
  13.        if(sem_getvalue(sem_id,&value) == -1)
  14.            perror("sem_getvalue");
  15.        printf("%d\n",value);
  16.        sem_wait(sem_id);
  17.        printf("Sanfoundry\n");
  18.        if(sem_close(sem_id) == -1)
  19.            perror("sem_close");
  20.        return 0;
  21.    }

   A.) 0
   B.) Sanfoundry
   C.) Both 0 and Sanfoundry
   D.) None of these

Answer: Option 'A'

0

Basic Computer Knowledge Test Questions and Answers

3.

What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<fcntl.h>
  3.    #include<sys/stat.h>
  4.    #include<semaphore.h>
  5. 
     
  6.    int main()
  7.    {
  8.        sem_t* sem_id;
  9.        sem_id = sem_open("sem_value",O_CREAT,0666,0);
  10.        if(sem_id == SEM_FAILED)
  11.            perror("sem_open");
  12.        sem_post(sem_id);
  13.        printf("Sanfoundry\n");
  14.        if(sem_close(sem_id) == -1)
  15.            perror("sem_close");
  16.        return 0;
  17.    }

   A.) this process will block
   B.) this program will print the string “Sanfoundry”
   C.) segmentation fault
   D.) none of these

Answer: Option 'B'

this program will print the string “Sanfoundry”

Basic Computer Knowledge Test Questions and Answers

4.

What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<fcntl.h>
  3.    #include<sys/stat.h>
  4.    #include<semaphore.h>
  5. 
     
  6.    int main()
  7.    {
  8.        sem_t* sem_id;
  9.        sem_id = sem_open("sem_value",O_CREAT,0666,0);
  10.        if(sem_id == SEM_FAILED)
  11.            perror("sem_open");
  12.        if(sem_close(sem_id) == -1)
  13.            perror("sem_close");
  14.        sem_wait(sem_id);
  15.        printf("Sanfoundry\n");
  16.        return 0;
  17.    }

   A.) this process will block
   B.) this program will print the string “Sanfoundry”
   C.) segmentation fault
   D.) none of these

Answer: Option 'C'

segmentation fault

Basic Computer Knowledge Test Questions and Answers

5.

What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<fcntl.h>
  3.    #include<sys/stat.h>
  4.    #include<semaphore.h>
  5. 
     
  6.    int main()
  7.    {
  8.        sem_t* sem_id;
  9.        int value;
  10.        sem_id = sem_open("new_13",O_CREAT,0666,3);
  11.        if(sem_id == SEM_FAILED)
  12.            perror("sem_open");
  13.        sem_wait(sem_id);
  14.        sem_wait(sem_id);
  15.        sem_wait(sem_id);
  16.        sem_wait(sem_id);
  17.        sem_post(sem_id);
  18.        sem_post(sem_id);
  19.        sem_getvalue(sem_id,&value);
  20.        printf("%d\n",value);
  21.        if(sem_close(sem_id) == -1)
  22.            perror("sem_close");
  23.        return 0;
  24.    }

   A.) 2
   B.) 3
   C.) 0
   D.) none of these

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]#

Basic Computer Knowledge Test Questions and Answers

6.

What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<fcntl.h>
  3.    #include<sys/stat.h>
  4.    #include<sys/mman.h>
  5. 
     
  6.    int main()
  7.    {
  8.        int s_id;
  9.        s_id = shm_open("shared_mem",O_CREAT|O_RDWR,0666);
  10.        printf("%d\n",s_id);
  11.        if(shm_unlink("shared_mem") == -1)
  12.            perror("shm_unlink");
  13.        return 0;
  14.    }

   A.) -1
   B.) 1
   C.) 2
   D.) 3

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]

Basic Computer Knowledge Test Questions and Answers

7.

What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<fcntl.h>
  3.    #include<sys/stat.h>
  4.    #include<sys/mman.h>
  5. 
     
  6.    int main()
  7.    {
  8.        int s_id;
  9.        int *ptr;
  10.        s_id = shm_open("shared_mem",O_CREAT|O_RDWR,0666);
  11.        if(s_id == -1)
  12.            perror("shm_open");
  13.        ptr = mmap(NULL,100,PROT_WRITE,MAP_PRIVATE,s_id,0);
  14.        if(ptr == MAP_FAILED);
  15.            perror("mmap");
  16.        if(munmap(ptr,100) == -1)
  17.            perror("munmap");
  18.        if(shm_unlink("shared_mem") == -1)
  19.            perror("shm_unlink");
  20.        return 0;
  21.    }

   A.) mmap: Success
   B.) mmap: Failure
   C.) munmap: Failure
   D.) munmap: Success

Answer: Option 'A'

mmap: Success

Basic Computer Knowledge Test Questions and Answers

8.

What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<fcntl.h>
  3.    #include<sys/stat.h>
  4.    #include<sys/mman.h>
  5. 
     
  6.    int main()
  7.    {
  8.        int s_id;
  9.        int *ptr;
  10.        s_id = shm_open("shared_mem",O_CREAT|O_RDWR,0666);
  11.        if(s_id == -1)
  12.            perror("shm_open");
  13.        ptr = mmap(NULL,100,PROT_WRITE,MAP_PRIVATE,s_id,0);
  14.        if(ptr == MAP_FAILED);
  15.            perror("mmap");
  16.        ptr = mmap(ptr,100,PROT_WRITE,MAP_PRIVATE,s_id,0);
  17.        if(ptr == MAP_FAILED);
  18.            perror("mmap");
  19.        if(munmap(ptr,100) == -1)
  20.            perror("munmap");
  21.        if(shm_unlink("shared_mem") == -1)
  22.            perror("shm_unlink");
  23.        return 0;
  24.    }

   A.) mmap: Success
mmap: Success
   B.) mmap: Success
mmap: Failure
   C.) segmentation fault
   D.) none of these

Answer: Option 'A'

mmap: Success
mmap: Success

Basic Computer Knowledge Test Questions and Answers

9.

What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<fcntl.h>
  3.    #include<errno.h>
  4.    #include<sys/stat.h>
  5.    #include<sys/mman.h>
  6. 
     
  7.    int main()
  8.    {
  9.        int s_id;
  10.        s_id = shm_open("shared_mem",O_CREAT|O_RDWR,0666);
  11.        if(s_id != EACCES)
  12.            perror("Permission granted\n");
  13.        return 0;
  14.    }

   A.) Permission granted : Success
   B.) Permission granted
   C.) segmentation fault
   D.) none of these

Answer: Option 'A'

Permission granted : Success

Basic Computer Knowledge Test Questions and Answers

10.

What is the output of this program?

  1.     #include<stdio.h>
  2.     #include<fcntl.h>
  3.     #include<errno.h>
  4.     #include<sys/stat.h>
  5.     #include<sys/mman.h>
  6. 
     
  7.     int main()
  8.     {
  9.         int s_id;
  10.         s_id = shm_open("shared_memory",O_TRUNC,0666);
  11.         if(s_id == -1)
  12.             perror("shm_open\n");
  13.         return 0;
  14.     }

   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 these

Answer: Option 'B'

this program will give an error

Linux Debugging Questions & Answers – System-V IPCs – Message Queues, Shared Memory and Semaphores Download Pdf

Recent Posts