1.
Which one of the following string will print first by this program?
#include<stdio.h>
#include<pthread.h>
void *fun_t(void *arg);
void *fun_t(void *arg)
{
printf("Sanfoundry\n");
pthread_exit("Bye");
}
int main()
{
pthread_t pt;
void *res_t;
if(pthread_create(&pt,NULL,fun_t,NULL) != 0)
perror("pthread_create");
printf("Linux\n");
if(pthread_join(pt,&res_t) != 0)
perror("pthread_join");
return 0;
}
Answer: Option 'B'
Sanfoundry
2.
What is the output of this program?
#include<stdio.h>
#include<pthread.h>
void *fun_t(void *arg);
void *fun_t(void *arg)
{
int ret;
ret = pthread_exit("Bye");
printf("%d\n",ret);
}
int main()
{
pthread_t pt;
void *res_t;
if(pthread_create(&pt,NULL,fun_t,NULL) != 0)
perror("pthread_create");
if(pthread_join(pt,&res_t) != 0)
perror("pthread_join");
return 0;
}
Answer: Option 'D'
none of these
3.
What is the output of this program?
#include<stdio.h>
#include<pthread.h>
void *fun_t(void *arg);
void *fun_t(void *arg)
{
printf("Sanfoundry\n");
pthread_exit("Bye");
}
int main()
{
pthread_t pt;
void *res_t;
if(pthread_create(&pt,NULL,fun_t,NULL) != 0)
perror("pthread_create");
return 0;
}
Answer: Option 'B'
this program will print nothing
4.
What is the output of this program?
#include<stdio.h>
#include<pthread.h>
void *fun_t(void *arg);
void *fun_t(void *arg)
{
printf("%d\n",a);
pthread_exit("Bye");
}
int main()
{
int a;
pthread_t pt;
void *res_t;
a = 10;
if(pthread_create(&pt,NULL,fun_t,NULL) != 0)
perror("pthread_create");
if(pthread_join(pt,&res_t) != 0)
perror("pthread_join");
return 0;
}
Answer: Option 'D'
none of these
5.
What is the output of this program?
#include<stdio.h>
#include<pthread.h>
int a;
void *fun_t(void *arg);
void *fun_t(void *arg)
{
printf("%d\n",a);
pthread_exit("Bye");
}
int main()
{
pthread_t pt;
void *res_t;
a = 10;
if(pthread_create(&pt,NULL,fun_t,NULL) != 0)
perror("pthread_create");
if(pthread_join(pt,&res_t) != 0)
perror("pthread_join");
return 0;
}
Answer: Option 'A'
10
6.
What is the output of this program?
#include<stdio.h>
#include<pthread.h>
int a;
void *fun_t(void *arg);
void *fun_t(void *arg)
{
a = 20;
pthread_exit("Bye");
}
int main()
{
pthread_t pt;
void *res_t;
a = 10;
if(pthread_create(&pt,NULL,fun_t,NULL) != 0)
perror("pthread_create");
if(pthread_join(pt,&res_t) != 0)
perror("pthread_join");
printf("%d\n",a);
return 0;
}
Answer: Option 'B'
[root@localhost sanfoundry]# gcc -o san san.c -lpthread
[root@localhost sanfoundry]# ./san
20
[root@localhost sanfoundry]#
7.
Which one of the following statement is not true about this program?
#include<stdio.h>
#include<pthread.h>
void *fun_t(void *arg);
void *fun_t(void *arg)
{
printf("%d\n",getpid());
pthread_exit("Bye");
}
int main()
{
pthread_t pt;
void *res_t;
if(pthread_create(&pt,NULL,fun_t,NULL) != 0)
perror("pthread_create");
if(pthread_join(pt,&res_t) != 0)
perror("pthread_join");
printf("%d\n",getpid());
return 0;
}
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]#
8.
What is the output of this program?
#include<stdio.h>
#include<pthread.h>
#include<fcntl.h>
int fd;
void *fun_t(void *arg);
void *fun_t(void *arg)
{
char buff[10];
int count;
count = read(fd,buff,10);
printf("%d\n",count);
pthread_exit("Bye");
}
int main()
{
pthread_t pt;
void *res_t;
fd = open("san.c",O_RDONLY);
if(pthread_create(&pt,NULL,fun_t,NULL) != 0)
perror("pthread_create");
if(pthread_join(pt,&res_t) != 0)
perror("pthread_join");
return 0;
}
Answer: Option 'A'
10
9.
What is the output of this program?
#include<stdio.h>
#include<pthread.h>
#include<fcntl.h>
void *fun_t(void *arg);
void *fun_t(void *arg)
{
pthread_exit("Bye");
printf("Sanfoundry\n");
}
int main()
{
pthread_t pt;
void *res_t;
if(pthread_create(&pt,NULL,fun_t,NULL) != 0)
perror("pthread_create");
if(pthread_join(pt,&res_t) != 0)
perror("pthread_join");
printf("%s\n",res_t);
return 0;
}
Answer: Option 'B'
Bye