Linux Debugging Questions & Answers – Posix Threads

DigitalOcean Referral Badge DigitalOcean Referral Badge

1.

Which one of the following string will print first by 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.        printf("Sanfoundry\n");
  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) != 0)
  15.            perror("pthread_create");
  16.        printf("Linux\n");
  17.        if(pthread_join(pt,&res_t) != 0)
  18.            perror("pthread_join");
  19.        return 0;
  20.    }

   A.) Linux
   B.) Sanfoundry
   C.) it can not be predicted
   D.) none of these

Answer: Option 'B'

Sanfoundry

DigitalOcean Referral Badge

2.

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.        int ret;
  8.        ret = pthread_exit("Bye");
  9.        printf("%d\n",ret);
  10.    }
  11.    int main()
  12.    {
  13.        pthread_t pt;
  14.        void *res_t;
  15.        if(pthread_create(&pt,NULL,fun_t,NULL) != 0)
  16.            perror("pthread_create");
  17.        if(pthread_join(pt,&res_t) != 0)
  18.            perror("pthread_join");
  19.        return 0;
  20.    }

   A.) 0
   B.) 1
   C.) -1
   D.) none of these

Answer: Option 'D'

none of these

DigitalOcean Referral Badge

3.

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.        printf("Sanfoundry\n");
  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) != 0)                
  15.            perror("pthread_create");
  16.        return 0;
  17.    }

   A.) this program will print the string “Sanfoundry”
   B.) this program will print nothing
   C.) segmentation fault
   D.) run time error

Answer: Option 'B'

this program will print nothing

DigitalOcean Referral Badge

4.

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.        printf("%d\n",a);
  8.        pthread_exit("Bye");
  9.    }
  10.    int main()
  11.    {
  12.        int a;  
  13.        pthread_t pt;
  14.        void *res_t;         
  15.        a = 10;
  16.        if(pthread_create(&pt,NULL,fun_t,NULL) != 0)
  17.            perror("pthread_create");
  18.        if(pthread_join(pt,&res_t) != 0)
  19.            perror("pthread_join");
  20.        return 0;
  21.    }

   A.) 10
   B.) 0
   C.) -1
   D.) none of these

Answer: Option 'D'

none of these

DigitalOcean Referral Badge
    DigitalOcean Referral Badge DigitalOcean Referral Badge

5.

What is the output of this program?

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

   A.) 10
   B.) 0
   C.) -1
   D.) none of these

Answer: Option 'A'

10

DigitalOcean Referral Badge

6.

What is the output of this program?

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

   A.) 10
   B.) 20
   C.) segmentation fault
   D.) none of these

Answer: Option 'B'

[root@localhost sanfoundry]# gcc -o san san.c -lpthread
[root@localhost sanfoundry]# ./san
20
[root@localhost sanfoundry]#

DigitalOcean Referral Badge

7.

Which one of the following statement is not true about 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.        printf("%d\n",getpid());
  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) != 0)
  15.            perror("pthread_create");
  16.        if(pthread_join(pt,&res_t) != 0)
  17.            perror("pthread_join");
  18.        printf("%d\n",getpid());
  19.        return 0;
  20.    }

   A.) both printf statements will print the same value
   B.) both printf statements will print the different values
   C.) this program will print nothing
   D.) none of these

Answer: Option 'A'

All the threads of the same process have same PID.
Output:
[root@localhost sanfoundry]# gcc -o san san.c -lpthread
[root@localhost sanfoundry]# ./san
12981
12981
[root@localhost sanfoundry]#

DigitalOcean Referral Badge

8.

What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<pthread.h>
  3.    #include<fcntl.h>
  4. 
     
  5.    int fd;
  6.    void *fun_t(void *arg);
  7.    void *fun_t(void *arg)
  8.    {       
  9.        char buff[10];
  10.        int count;
  11.        count = read(fd,buff,10);        
  12.        printf("%d\n",count);
  13.        pthread_exit("Bye");
  14.    }
  15.    int main()
  16.    {
  17.        pthread_t pt;
  18.        void *res_t;
  19.        fd = open("san.c",O_RDONLY);        
  20.        if(pthread_create(&pt,NULL,fun_t,NULL) != 0)
  21.            perror("pthread_create");
  22.        if(pthread_join(pt,&res_t) != 0)
  23.            perror("pthread_join");
  24.        return 0;
  25.    }

   A.) 10
   B.) 0
   C.) -1
   D.) segmentation fault

Answer: Option 'A'

10

DigitalOcean Referral Badge

9.

What is the output of this program?

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

   A.) Sanfoundry
   B.) Bye
   C.) segementation fault
   D.) run time error

Answer: Option 'B'

Bye

DigitalOcean Referral Badge

Linux Debugging Questions & Answers – Posix Threads Download Pdf

Recent Posts