#include<pthread.h>
#include<semaphore.h>
#include<unistd.h>
#include<stdlib.h>
sem_t sem1;
int i=5,res=0;
void * fun1(void* p)
{
sem_wait(&sem1);
printf("thread 1 is running:\n");
i=0;
}
void * fun2(void* p)
{
printf("thread 2 is running:\n");
res=200/i;
sem_post(&sem1);
}
int main()
{
sem_init(&sem1,0,0);
int ret;
void *status;
pthread_t p_id,t_id;
pthread_create(&p_id,NULL,fun1,NULL);
pthread_create(&t_id,NULL,fun2,NULL);
//printf("%d",i);
pthread_join(p_id,&status);
// printf("thread1:%s\n",(char*)status);
pthread_join(t_id,&status);
// printf("thread1:%s",(char*)status);
return 0;
}
Comments