Professional Documents
Culture Documents
Counting Semaphores and Binary
Counting Semaphores and Binary
#include
#include
#include
#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);