1. What is the output of this program
#include<stdio.h>
#include<pthread.h>
void *fun_t(void *arg);
void *fun_t(void *arg)
{
pthread_exit("Bye");
}
int main()
{
pthread_t pt;
void *res_t;
int ret;
ret = pthread_join(pt,&res_t);
printf("%d\n",ret);
return 0;
}
a) 0
b) -1
c) 2
d) 3
View Answer
Explanation: 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]#
2. What is the output of this program?
#include<stdio.h>
#include<pthread.h>
sem_t st;
void *fun_t(void *arg);
void *fun_t(void *arg)
{
pthread_exit("Bye");
}
int main()
{
pthread_t pt;
void *res_t;
if(pthread_create(&pt,NULL,fun_t,NULL) == -1)
perror("pthread_create");
if(sem_init(&st,1,2) != 0)
perror("sem_init");
if(pthread_join(pt,&res_t) == -1)
perror("pthread_join");
if(sem_destroy(&st) != 0)
perror("sem_destroy");
return 0;
}
a) this program will print nothing
b) this program will give an error
c) this program will give segmentation fault
d) none of the mentioned
View Answer
Explanation: The header file semaphore.h is required for the function sem_init.
Output:
[root@localhost sanfoundry]# gcc -o san san.c -lpthread
san.c:4:4: error: unknown type name ‘sem_t’
[root@localhost sanfoundry]#
3. What is the output of this program?
#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
sem_t st;
void *fun_t(void *arg);
void *fun_t(void *arg)
{
pthread_exit("Bye");
}
int main()
{
pthread_t pt;
void *res_t;
if(pthread_create(&pt,NULL,fun_t,NULL) == -1)
perror("pthread_create");
if(sem_init(st,1,2) != 0)
perror("sem_init");
if(pthread_join(pt,&res_t) == -1)
perror("pthread_join");
if(sem_destroy(&st) != 0)
perror("sem_destroy");
return 0;
}
a) this program will print nothing
b) this program will give an error
c) this program will give segmentation fault
d) none of the mentioned
View Answer
Explanation: The first arguement of the sem_init() if of type sem_t*.
Output:
[root@localhost sanfoundry]# gcc -o san san.c -lpthread
2_thread.c: In function ‘main’:
2_thread.c:17:2: error: incompatible type for argument 1 of ‘sem_init’
/usr/include/semaphore.h:37:12: note: expected ‘union sem_t *’ but argument is of type ‘sem_t’
[root@localhost sanfoundry]#
4. What is the output of this program?
#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
void *fun_t(void *arg);
void *fun_t(void *arg)
{
pthread_exit("Bye");
}
int main()
{
pthread_t pt;
sem_t st;
void *res_t;
if(pthread_create(&pt,NULL,fun_t,NULL) == -1)
perror("pthread_create");
if(sem_init(&st,0,0) != 0)
perror("sem_init");
if(sem_wait(&st) != 0)
perror("sem_wait");
printf("Sanoundry\n");
if(pthread_join(pt,&res_t) == -1)
perror("pthread_join");
if(sem_destroy(&st) != 0)
perror("sem_destroy");
return 0;
}
a) this program will print the string “Sanfoundry”
b) this program will give segmentation fault
c) this process will remain block
d) none of the mentioned
View Answer
Explanation: In this program, initial value of semaphore is 0. The sem_wait() function call blocks the process until the value of semaphore becomes 1.
Output:
[root@localhost sanfoundry]# gcc -o san san.c -lpthread
[root@localhost sanfoundry]# ./san
^Z
[3]+ Stopped ./san
[root@localhost sanfoundry]#
5. What is the output of this program?
#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
void *fun_t(void *arg);
void *fun_t(void *arg)
{
sem_post(&st);
pthread_exit("Bye");
}
int main()
{
pthread_t pt;
sem_t st;
void *res_t;
if(pthread_create(&pt,NULL,fun_t,NULL) == -1)
perror("pthread_create");
if(sem_init(&st,0,0) != 0)
perror("sem_init");
if(sem_wait(&st) != 0)
perror("sem_wait");
printf("Sanoundry\n");
if(pthread_join(pt,&res_t) == -1)
perror("pthread_join");
if(sem_destroy(&st) != 0)
perror("sem_destroy");
return 0;
}
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 the mentioned
View Answer
Explanation: The semaphore object st is locally declared.
Output:
[root@localhost sanfoundry]# gcc -o san san.c -lpthread
san.c: In function ‘fun_t’:
san.c:8:12: error: ‘st’ undeclared (first use in this function)
san.c:8:12: note: each undeclared identifier is reported only once for each function it appears in
[root@localhost sanfoundry]#
6. Which one of the following string will print first by this program?
#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
sem_t st;
void *fun_t(void *arg);
void *fun_t(void *arg)
{
printf("Linux\n");
sem_post(&st);
pthread_exit("Bye");
}
int main()
{
pthread_t pt;
void *res_t;
if(pthread_create(&pt,NULL,fun_t,NULL) == -1)
perror("pthread_create");
if(sem_init(&st,0,0) != 0)
perror("sem_init");
if(sem_wait(&st) != 0)
perror("sem_wait");
printf("Sanoundry\n");
if(pthread_join(pt,&res_t) == -1)
perror("pthread_join");
if(sem_destroy(&st) != 0)
perror("sem_destroy");
return 0;
}
a) Linux
b) Sanfoundry
c) Can not be predicted
d) None of the mentioned
View Answer
Explanation: The string “Linux” will print first because of semaphores.
Ouptut:
[root@localhost sanfoundry]# gcc -o san san.c -lpthread
[root@localhost sanfoundry]# ./san
Linux
Sanoundry
[root@localhost sanfoundry]#
7. What is the output of this program?
#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
sem_t st;
void *fun_t(void *arg);
void *fun_t(void *arg)
{
printf("Linux\n");
pthread_exit("Bye");
sem_post(&st);
}
int main()
{
pthread_t pt;
void *res_t;
if(pthread_create(&pt,NULL,fun_t,NULL) == -1)
perror("pthread_create");
if(sem_init(&st,0,0) != 0)
perror("sem_init");
if(sem_wait(&st) != 0)
perror("sem_wait");
printf("Sanoundry\n");
if(pthread_join(pt,&res_t) == -1)
perror("pthread_join");
if(sem_destroy(&st) != 0)
perror("sem_destroy");
return 0;
}
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 the mentioned
View Answer
Explanation: The value of semaphore will never become 1 in this program.
Output:
[root@localhost sanfoundry]# gcc -o san san.c -lpthread
[root@localhost sanfoundry]# ./san
Linux
^Z
[4]+ Stopped ./san
[root@localhost sanfoundry]#
8. What is the output of this program?
#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
sem_t st;
void *fun_t(void *arg);
void *fun_t(void *arg)
{
printf("Linux\n");
pthread_exit("Bye");
}
int main()
{
pthread_t pt;
void *res_t;
if(pthread_create(&pt,NULL,fun_t,NULL) == -1)
perror("pthread_create");
if(sem_init(&st,0,2) != 0)
perror("sem_init");
if(sem_wait(&st) != 0)
perror("sem_wait");
printf("Sanoundry\n");
if(sem_wait(&st) != 0)
perror("sem_wait");
if(pthread_join(pt,&res_t) == -1)
perror("pthread_join");
if(sem_destroy(&st) != 0)
perror("sem_destroy");
return 0;
}
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 the mentioned
View Answer
Explanation: The initial value of semaphore is 2. Hence sem_wait() will only decrement the value of semaphore and the process will not block.
Ouptut:
[root@localhost sanfoundry]# gcc -o san san.c -lpthread
[root@localhost sanfoundry]# ./san
Linux
Sanoundry
[root@localhost sanfoundry]#
9. In this program the semaphore
#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
sem_t st;
void *fun_t(void *arg);
void *fun_t(void *arg)
{
pthread_exit("Bye");
}
int main()
{
pthread_t pt;
void *res_t;
if(pthread_create(&pt,NULL,fun_t,NULL) == -1)
perror("pthread_create");
if(sem_init(&st,1,2) != 0)
perror("sem_init");
if(pthread_join(pt,&res_t) == -1)
perror("pthread_join");
if(sem_destroy(&st) != 0)
perror("sem_destroy");
return 0;
}
a) can be used only for this process
b) can be used for any other process also
c) can be used
d) none of the mentioned
View Answer
Explanation: The value of second argument of sem_init() is 1.
Output:
[root@localhost sanfoundry]# gcc -o san san.c -lpthread
[root@localhost sanfoundry]# ./san
[root@localhost sanfoundry]#
10. Which one of the following string will print by this program?
#include<stdio.h>
#include<pthread.h>
int main()
{
printf("Sanfoundry\n");
pthread_exit("Bye");
printf("Linux");
return 0;
}
a) Linux
b) Sanfoundry
c) Bye
d) None of the mentioned
View Answer
Explanation: The main thread exits before printing the string “Linux”;
Output:
[root@localhost sanfoundry]# gcc -o san san.c -lpthread
[root@localhost sanfoundry]# ./san
Sanfoundry
[root@localhost sanfoundry]#
Sanfoundry Global Education & Learning Series – Linux Administration & Programming.
Here’s the list of Best Books in Linux Commands & Shell Programming.
Here’s the list of Best Books in Linux Kernel, Device-Drivers & System Programming.
To practice all questions on Linux Administration & Programming, here is complete set of 1000+ Multiple Choice Questions and Answers on Linux.
- Apply for Programming Internship
- Check Information Technology Books
- Check Linux Books
- Practice Programming MCQs