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

Thanh Hoan

New member
What is the output of this program?
Code:
   #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 “123hoidap”
B. this program will give an error
C. this program will print the string “123hoidap” & gives an error
D. none of the mentioned
 
Answer: b
Explanation: The semaphore object st is locally declared.
Output:
[root@localhost 123hoidap]# 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 123hoidap]#
 

Tìm kiếm

Back
Top