You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
838 B
42 lines
838 B
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <pthread.h>
|
|
#include <unistd.h>
|
|
#include <sys/types.h>
|
|
#include <semaphore.h>
|
|
sem_t a,b,c;
|
|
void* p1(void *arg){
|
|
sem_wait(&a);
|
|
for(int i=0;i<10;i++){
|
|
printf("A\n");
|
|
}
|
|
sem_post(&b);
|
|
}
|
|
void* p2(void *arg){
|
|
sem_wait(&b);
|
|
for(int i=0;i<10;i++){
|
|
printf("B\n");
|
|
}
|
|
sem_post(&c);
|
|
}
|
|
void* p3(void *arg){
|
|
sem_wait(&c);
|
|
for(int i=0;i<10;i++){
|
|
printf("C\n");
|
|
}
|
|
sem_post(&a);
|
|
}
|
|
int main()
|
|
{
|
|
|
|
sem_init(&a,0,1);
|
|
sem_init(&b,0,0);
|
|
sem_init(&c,0,0);
|
|
pthread_t tid[3];
|
|
pthread_create(&tid[0], NULL, p1, NULL);
|
|
pthread_create(&tid[1], NULL, p2, NULL);
|
|
pthread_create(&tid[2], NULL, p3, NULL);
|
|
for(int i = 0; i < 3; i++)
|
|
pthread_join(tid[i], NULL);
|
|
printf("main is over\n");
|
|
} |