You are on page 1of 2

Counting

#include
#include
#include

semaphore & binary


<semaphore.h>
<pthread.h>
<stdio.h>

#define NLOOPS 5
sem_t s0, s1, s2, s3;
void *process_A (void *unused)
{
int n = NLOOPS;
while (n--)
{
sem_wait (&s0);
putchar ('A');
sem_post (&s1);
putchar ('B');
sem_post (&s3);
sem_post (&s2);
sem_wait (&s0);
putchar ('A');
sem_post (&s1);
sem_wait (&s3);
putchar ('B');
sem_post (&s2);
}
return 0;
}
void *
process_B (void *unused)
{
int n = NLOOPS;
while (n--)
{
sem_wait (&s1);
sem_wait (&s3);
putchar ('C');
sem_wait (&s2);
putchar ('D');
sem_post (&s0);
sem_wait (&s1);
putchar ('C');
sem_post (&s3);
sem_wait (&s2);
putchar ('D');
sem_post (&s0);
printf("\n");
}
return 0;
}

int
main ()
{
pthread_t a, b;
sem_init
sem_init
sem_init
sem_init

(&s0,
(&s1,
(&s2,
(&s3,

0,
0,
0,
0,

1);
0);
0);
0);

pthread_create (&a, 0, process_A, 0);


pthread_create (&b, 0, process_B, 0);
pthread_join (a, 0);
pthread_join (b, 0);
printf("\n");
return 0;
}

You might also like