Tricky and Buggy Questions & Answers on PThreads Handling

Basic Computer Knowledge Test Questions and Answers

1.

What is the output of this program

  1.    #include<stdio.h>
  2.    #include<pthread.h>
  3. 
     
  4.    void *fun_t(void *arg);
  5.    void *fun_t(void *arg)
  6.    {
  7.        pthread_exit("Bye");    
  8.    }
  9.    int main()
  10.    {
  11.        pthread_t pt;
  12.        void *res_t;
  13.        int ret;        
  14.        ret = pthread_join(pt,&res_t);
  15.        printf("%d\n",ret);
  16.        return 0;
  17.    }

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

Answer: Option 'D'

The function pthread_join() returns the error number on error.
Output:
[root@localhost sanfoundry]# gcc -o san san.c -lpthread
[root@localhost sanfoundry]# ./san
3
[root@localhost sanfoundry]#

Basic Computer Knowledge Test Questions and Answers

2.

What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<pthread.h>
  3. 
     
  4.    sem_t st;
  5.    void *fun_t(void *arg);
  6.    void *fun_t(void *arg)
  7.    {
  8.        pthread_exit("Bye");
  9.    }
  10.    int main()
  11.    {
  12.        pthread_t pt;
  13.        void *res_t;
  14.        if(pthread_create(&pt,NULL,fun_t,NULL) == -1)
  15.            perror("pthread_create");
  16.        if(sem_init(&st,1,2) != 0)
  17.            perror("sem_init");
  18.        if(pthread_join(pt,&res_t) == -1)
  19.            perror("pthread_join");
  20.        if(sem_destroy(&st) != 0)
  21.            perror("sem_destroy");
  22.        return 0;
  23.    }

   A.) this program will print nothing
   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

Basic Computer Knowledge Test Questions and Answers

3.

What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<pthread.h>
  3.    #include<semaphore.h>
  4. 
     
  5.    void *fun_t(void *arg);
  6.    void *fun_t(void *arg)
  7.    {
  8.        pthread_exit("Bye");
  9.    }
  10.    int main()
  11.    {
  12.        pthread_t pt;
  13.        sem_t st;
  14.        void *res_t;        
  15.        if(pthread_create(&pt,NULL,fun_t,NULL) == -1)
  16.            perror("pthread_create");
  17.        if(sem_init(&st,0,0) != 0)
  18.            perror("sem_init");
  19.        if(sem_wait(&st) != 0)
  20.            perror("sem_wait");
  21.        printf("Sanoundry\n");
  22.        if(pthread_join(pt,&res_t) == -1)
  23.            perror("pthread_join");
  24.        if(sem_destroy(&st) != 0)
  25.            perror("sem_destroy");
  26.        return 0;
  27.    }

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

Answer: Option 'C'

this process will remain block

Basic Computer Knowledge Test Questions and Answers

4.

What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<pthread.h>
  3.    #include<semaphore.h>
  4. 
     
  5.    void *fun_t(void *arg);
  6.    void *fun_t(void *arg)
  7.    {
  8.        sem_post(&st);
  9.        pthread_exit("Bye");
  10.    }
  11.    int main()
  12.    {
  13.        pthread_t pt;
  14.        sem_t st;
  15.        void *res_t;        
  16.        if(pthread_create(&pt,NULL,fun_t,NULL) == -1)
  17.            perror("pthread_create");
  18.        if(sem_init(&st,0,0) != 0)
  19.            perror("sem_init");
  20.        if(sem_wait(&st) != 0)
  21.            perror("sem_wait");
  22.        printf("Sanoundry\n");
  23.        if(pthread_join(pt,&res_t) == -1)
  24.            perror("pthread_join");
  25.        if(sem_destroy(&st) != 0)
  26.            perror("sem_destroy");
  27.        return 0;
  28.    }

   A.) this program will print the string “Sanfoundry”
   B.) this program will give an error
   C.) this program will print the string “Sanfoundry” & gives an error
   D.) none of these

Answer: Option 'B'

this program will give an error

Basic Computer Knowledge Test Questions and Answers

5.

Which one of the following string will print first by this program?

  1.    #include<stdio.h>
  2.    #include<pthread.h>
  3.    #include<semaphore.h>
  4. 
     
  5.    sem_t st;
  6.    void *fun_t(void *arg);
  7.    void *fun_t(void *arg)
  8.    {
  9.        printf("Linux\n");
  10.        sem_post(&st);
  11.        pthread_exit("Bye"); 
  12.    }
  13.    int main()
  14.    {
  15.        pthread_t pt;
  16.        void *res_t;
  17.        if(pthread_create(&pt,NULL,fun_t,NULL) == -1)
  18.            perror("pthread_create");
  19.        if(sem_init(&st,0,0) != 0)
  20.            perror("sem_init");
  21.        if(sem_wait(&st) != 0)
  22.            perror("sem_wait");
  23.        printf("Sanoundry\n");
  24.        if(pthread_join(pt,&res_t) == -1)
  25.            perror("pthread_join");
  26.        if(sem_destroy(&st) != 0)
  27.            perror("sem_destroy");
  28.        return 0;
  29.    }

   A.) Linux
   B.) Sanfoundry
   C.) Can not be predicted
   D.) None of these

Answer: Option 'A'

Linux

Basic Computer Knowledge Test Questions and Answers

6.

What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<pthread.h> 
  3.    #include<semaphore.h>
  4. 
     
  5.    sem_t st;
  6.    void *fun_t(void *arg);
  7.    void *fun_t(void *arg)
  8.    {
  9.        printf("Linux\n");
  10.        pthread_exit("Bye");
  11.        sem_post(&st);
  12.    }
  13.    int main()
  14.    {
  15.        pthread_t pt;
  16.        void *res_t;
  17.        if(pthread_create(&pt,NULL,fun_t,NULL) == -1)
  18.            perror("pthread_create");
  19.        if(sem_init(&st,0,0) != 0)
  20.            perror("sem_init");
  21.        if(sem_wait(&st) != 0)
  22.            perror("sem_wait");
  23.        printf("Sanoundry\n");
  24.        if(pthread_join(pt,&res_t) == -1)
  25.            perror("pthread_join");
  26.        if(sem_destroy(&st) != 0)
  27.            perror("sem_destroy");
  28.        return 0;
  29.    }

   A.) this program will print the only string “Linux”
   B.) this program will print the only string “Sanfoundry”
   C.) this program will print both the strings “Linux” and “Sanfoundry”
   D.) none of these

Answer: Option 'A'

this program will print the only string “Linux”
 

Basic Computer Knowledge Test Questions and Answers

7.

What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<pthread.h>
  3.    #include<semaphore.h>
  4. 
     
  5.    sem_t st;
  6.    void *fun_t(void *arg);
  7.    void *fun_t(void *arg)
  8.    {
  9.        printf("Linux\n");
  10.        pthread_exit("Bye");
  11.    }
  12.    int main()
  13.    {
  14.        pthread_t pt;
  15.        void *res_t;
  16.        if(pthread_create(&pt,NULL,fun_t,NULL) == -1)
  17.            perror("pthread_create");
  18.        if(sem_init(&st,0,2) != 0)
  19.            perror("sem_init");
  20.        if(sem_wait(&st) != 0)
  21.            perror("sem_wait");
  22.        printf("Sanoundry\n");
  23.        if(sem_wait(&st) != 0)
  24.            perror("sem_wait");
  25.        if(pthread_join(pt,&res_t) == -1)
  26.            perror("pthread_join");
  27.        if(sem_destroy(&st) != 0)
  28.            perror("sem_destroy");
  29.        return 0;
  30.    }

   A.) this program will print the only string “Linux”
   B.) this program will print the only string “Sanfoundry”
   C.) this program will print both the strings “Linux” and “Sanfoundry”
   D.) none of these

Answer: Option 'C'

this program will print both the strings “Linux” and “Sanfoundry”
 

Basic Computer Knowledge Test Questions and Answers

8.

In this program the semaphore

  1.    #include<stdio.h>
  2.    #include<pthread.h>
  3.    #include<semaphore.h>
  4. 
     
  5.    sem_t st;
  6.    void *fun_t(void *arg);
  7.    void *fun_t(void *arg)
  8.    {
  9.        pthread_exit("Bye");
  10.    }
  11.    int main()
  12.    {
  13.        pthread_t pt;
  14.        void *res_t;
  15.        if(pthread_create(&pt,NULL,fun_t,NULL) == -1)
  16.            perror("pthread_create");
  17.        if(sem_init(&st,1,2) != 0)
  18.            perror("sem_init");
  19.        if(pthread_join(pt,&res_t) == -1)
  20.            perror("pthread_join");
  21.        if(sem_destroy(&st) != 0)
  22.            perror("sem_destroy");
  23.        return 0;
  24.    }

   A.) can be used only for this process
   B.) can be used for any other process also
   C.) can be used
   D.) none of these

Answer: Option 'B'

can be used for any other process also

Basic Computer Knowledge Test Questions and Answers

9.

Which one of the following string will print by this program?

  1.     #include<stdio.h>
  2.     #include<pthread.h>
  3.     int main()
  4.     {
  5.         printf("Sanfoundry\n");
  6.         pthread_exit("Bye");
  7.         printf("Linux");
  8.         return 0;
  9.     }

   A.) Linux
   B.) Sanfoundry
   C.) Bye
   D.) None of these

Answer: Option 'B'

Sanfoundry

Tricky and Buggy Questions & Answers on PThreads Handling Download Pdf

Recent Posts