10 pthread_t thread1, thread2;
12 static void * thread1_doit(void *forks_arg) {
15 if(DEBUG) printf("Thread 1: Posted to sem1\n");
20 static void * thread2_doit(void *forks_arg) {
21 for( int i = 0; i < START_NUM+1; i++) {
22 if(DEBUG) printf("Thread 2: Waiting for sem1\n");
25 if(DEBUG) printf("Thread 2: Posted to sem2\n");
30 int main(int argc, char* argv[]) {
32 printf("Usage: %s START_NUM DEBUG_FLAG\n", argv[0]);
36 START_NUM = atoi(argv[1]);
37 DEBUG = atoi(argv[2]);
39 sem_init(&sem1, 0, START_NUM);
40 sem_init(&sem2, 0, 1);
42 pthread_create(&thread1, NULL, &thread1_doit, NULL);
43 pthread_create(&thread2, NULL, &thread2_doit, NULL);
45 pthread_join(thread1, NULL);
46 pthread_join(thread2, NULL);