250+ TOP MCQs on System-V IPCs – Message Queues, Shared Memory and Semaphores and Answers

Linux Debugging questions and answers focuses on the System-V IPCs i.e. Message Queues, Shared Memory and Semaphores.1. What is the output of this program?

  1.    #include
  2.    #include
  3.    #include<sys/stat.h>
  4.    #include
  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("n");
  14.        if(sem_close(sem_id) == -1)
  15.            perror("sem_close");
  16.        return 0;
  17.    }

a) this program will print the string “”
b) this process will block
c) segmentaion fault
d) none of the mentioned

Answer: b

2. What is the output of this program?

  1.    #include
  2.    #include
  3.    #include<sys/stat.h>
  4.    #include
  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("%dn",value);
  16.        sem_wait(sem_id);
  17.        printf("n");
  18.        if(sem_close(sem_id) == -1)
  19.            perror("sem_close");
  20.        return 0;
  21.    }

a) 0
b)
c) Both 0 and
d) None of the mentioned

Answer: a

3. What is the output of this program?

  1.    #include
  2.    #include
  3.    #include<sys/stat.h>
  4.    #include
  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("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 “”
c) segmentation fault
d) none of the mentioned

Answer: b

4. What is the output of this program?

  1.    #include
  2.    #include
  3.    #include<sys/stat.h>
  4.    #include
  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("n");
  16.        return 0;
  17.    }

a) this process will block
b) this program will print the string “”
c) segmentation fault
d) none of the mentioned

Answer: c

5. What is the output of this program?

  1.    #include
  2.    #include
  3.    #include<sys/stat.h>
  4.    #include
  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("%dn",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 the mentioned

Answer: d

6. What is the output of this program?

  1.    #include
  2.    #include
  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("%dn",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: d

7. What is the output of this program?

  1.    #include
  2.    #include
  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: Success
d) munmap: Failure

Answer: a

8. What is the output of this program?

  1.    #include
  2.    #include
  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 the mentioned

Answer: a

9. What is the output of this program?

  1.    #include
  2.    #include
  3.    #include
  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 grantedn");
  13.        return 0;
  14.    }

a) Permission granted
: Success
b) Permission granted
c) segmentation fault
d) none of the mentioned

Answer: a

10. What is the output of this program?

  1.     #include
  2.     #include
  3.     #include
  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_openn");
  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 the mentioned

Answer: b

Leave a Reply

Your email address will not be published. Required fields are marked *