2. cpu.c example

$ make
gcc -o cpu cpu.c -Wall
gcc -o mem mem.c -Wall
gcc -o threads threads.c -Wall -pthread
gcc -o io io.c -Wall
$ cat -n ostep/code/intro/cpu.c
     1	#include <stdio.h>
     2	#include <stdlib.h>
     3	#include "common.h"
     4
     5	int main(int argc, char *argv[])
     6	{
     7	    if (argc != 2) {
     8		fprintf(stderr, "usage: cpu <string>\n");
     9		exit(1);
    10	    }
    11	    char *str = argv[1];
    12
    13	    while (1) {
    14		printf("%s\n", str);
    15		Spin(1);
    16	    }
    17	    return 0;
    18	}
$ ./cpu abc
abc
abc
abc
^C
$
     
$ ./cpu A & ./cpu B & ./cpu C & ./cpu D &
[1] 3153
[2] 3154
[3] 3155
[4] 3156
$A
B
D
C
A
B
D
C
A
B
D
C
...
kill %1 %2 %3 %4
[1]   Terminated              ./cpu A
[2]   Terminated              ./cpu B
[3]-  Terminated              ./cpu C
[4]+  Terminated              ./cpu D
$
--> sleep() instead of Spin()