Sei sulla pagina 1di 1

Page 1 of 1

#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>

sem_t s1, s2;

void *f1(void *arg)


{
printf("first\n");
sem_post(&s1);
}

void *f2(void *arg)


{
sem_wait(&s1);
printf("Second\n");
sem_post(&s2);
}

void *f3(void *arg)


{
sem_wait(&s2);
printf("Third\n");
}

int main()
{
pthread_t tid1, tid2, tid3;

if (sem_init(&s1, 0, 0) == -1) {
perror("Initializing semaphore 1");
exit(1);
}
if (sem_init(&s2, 0, 0) == -1) {
perror("Initializing semaphore 2");
exit(1);
}
if (pthread_create(&tid1, NULL, f2, NULL))
{
perror("Thread creation\n");
exit(2);
}
if (pthread_create(&tid2, NULL, f3, NULL))
{
perror("Thread creation\n");
exit(2);
}
if (pthread_create(&tid3, NULL, f1, NULL))
{
perror("Thread creation\n");
exit(2);
}

pthread_exit(NULL);
}

mhtml:file://C:\Documents and Settings\user\Desktop\Raed\CpE 472\Sample Codes\p1... 4/27/2008

Potrebbero piacerti anche