|
2 | 2 | * <n> processes share one semaphore initialized with <init>. Every moment,
|
3 | 3 | * <init> processes can talk at the same time. When talking, each process say
|
4 | 4 | * its number every second for a random number of seconds up to a certain
|
5 |
| - * maximum (say, 5). Each round, every process talks at least once. They keep |
6 |
| - * doing this until SIGINT is sent. Every round, the last number is in () so |
7 |
| - * that we know that process is gonna shut up. As a special case, with <init> |
8 |
| - * to be 1, only one process at a time can talk. |
| 5 | + * maximum (say, 5). When a process gets to talk, it talks at least once. They |
| 6 | + * keep doing this until SIGINT is sent. Every turn, the last number is in () |
| 7 | + * so that we know that process is gonna shut up. As a special case, with |
| 8 | + * <init> to be 1, only one process at a time can talk. |
9 | 9 | *
|
10 | 10 | * $ ./a.out 9 1
|
11 | 11 | * 000(0)11(1)2(2)(3)444(4)5(5)(6)...
|
|
18 | 18 | *
|
19 | 19 | * After sem_post(), you might need a short sleep to give other processes a
|
20 | 20 | * chance to wake up otherwise only a small group of processes could actually
|
21 |
| - * take turns -- ie. if after sem_post() the process could immediatelly do |
| 21 | + * take turns -- ie. after sem_post() the process could immediatelly do |
22 | 22 | * sem_wait() and be itself the one that actually decrements the semaphore
|
23 |
| - * again. |
| 23 | + * again, thus starving out other processes. |
24 | 24 | *
|
25 |
| - * Wait for all children before returning from the parent. |
| 25 | + * Wait for all children before exiting from the parent. |
26 | 26 | *
|
27 | 27 | * You must link with -pthread on Linux.
|
28 | 28 | *
|
|
0 commit comments