What is the output of this program? [code] #include<stdio.h> #include<pthread.h> #include<semaphore.h> sem_t st; void *fun_t(voi

Ngô Quốc Huy

New member
What is the output of this program?
Code:
   #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 “123hoidap”
C. this program will print both the strings “Linux” and “123hoidap”
D. none of the mentioned
 
Answer: a
Explanation: The value of semaphore will never become 1 in this program.
Output:
[root@localhost 123hoidap]# gcc -o san san.c -lpthread
[root@localhost 123hoidap]# ./san
Linux
^Z
[4]+ Stopped ./san
[root@localhost 123hoidap]#
 

Tìm kiếm

Back
Top