1 /* step.c for step.exp */
9 void alarm_handler1 ();
10 void alarm_handler2 ();
21 pthread_attr_t attr1, attr2;
24 pthread_mutexattr_t mut_attr;
26 pthread_condattr_t cv_attr_a, cv_attr_b;
27 pthread_cond_t cv_a, cv_b;
43 if (pthread_mutexattr_create (&mut_attr) == -1)
45 perror ("mutexattr_create");
50 if (pthread_mutex_init (&mut, mut_attr) == -1)
52 perror ("mutex_init");
57 if (pthread_condattr_create (&cv_attr_a) == -1)
59 perror ("condattr_create(1)");
63 if (pthread_cond_init (&cv_a, cv_attr_a) == -1)
65 perror ("cond_init(1)");
69 if (pthread_condattr_create (&cv_attr_b) == -1)
71 perror ("condattr_create(2)");
75 if (pthread_cond_init (&cv_b, cv_attr_b) == -1)
77 perror ("cond_init(2)");
81 /* create 2 threads of execution */
82 if (pthread_attr_create (&attr1) == -1)
84 perror ("attr_create(1)");
88 if (pthread_create (&tid1, attr1, thread1, &count1) == -1)
90 perror ("pthread_create(1)");
94 if (pthread_attr_create (&attr2) == -1)
96 perror ("attr_create(2)");
100 if (pthread_create (&tid2, attr2, thread2, &count2) == -1)
102 perror ("pthread_create(2)");
106 /* set alarm to print out data and exit */
107 signal (SIGALRM, alarm_handler);
121 printf ("Thread1 tid 0x%x (%d) \n", tid, tid);
122 printf ("Thread1 @tid=0x%x \n", &tid);
123 signal (SIGALRM, alarm_handler1);
127 if (pthread_mutex_lock (&mut) == -1)
129 perror ("pthread_mutex_lock(1)");
130 pthread_exit ((void *) 0);
133 while (test_struct.a == 0)
135 if (pthread_cond_wait (&cv_a, &mut) == -1)
137 perror ("pthread_cond_wait(1)");
138 pthread_exit ((void *) -1);
143 printf ("*******thread1 count %d\n", *count);
148 pthread_cond_signal (&cv_b);
150 if (pthread_mutex_unlock (&mut) == -1)
152 perror ("pthread_mutex_unlock(1)");
153 pthread_exit ((void *) -1);
165 printf ("Thread2 tid 0x%x (%d) \n", tid, tid);
166 printf ("Thread1 @tid=0x%x \n", &tid);
167 signal (SIGALRM, alarm_handler2);
171 if (pthread_mutex_lock (&mut) == -1)
173 perror ("pthread_mutex_lock(2)");
174 pthread_exit ((void *) 0);
177 while (test_struct.b == 0)
179 if (pthread_cond_wait (&cv_b, &mut) == -1)
181 perror ("pthread_cond_wait(2)");
182 pthread_exit ((void *) -1);
187 printf ("*******thread2 count %d\n", *count);
192 pthread_cond_signal (&cv_a);
194 if (pthread_mutex_unlock (&mut) == -1)
196 perror ("pthread_mutex_unlock(2)");
197 pthread_exit ((void *) -1);
206 printf ("\tcount1 (%d) \n\tcount2 (%d)\n", count1, count2);
213 printf ("ALARM thread 1\n");
219 printf ("ALARM thread 2\n");
220 pthread_exit ((void *) 0);