summaryrefslogtreecommitdiff
path: root/user/progI.c
blob: a1988e3decf70aeefbde4025801f02f27fce4a72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <common.h>

#ifndef MAX_CHILDREN
#define MAX_CHILDREN 50
#endif

/**
** User function I:  exit, fork, exec, kill, sleep, waitpid, write
**
** Reports, then loops spawing userW, sleeps, kills two children, then
** loops checking the status of all its children
**
** Invoked as:  userI [ x [ n ] ]
**	 where x is the ID character (defaults to 'i')
**		   n is the number of children to spawn (defaults to 5)
*/

USERMAIN(main)
{
	int count = 5; // default child count
	char ch = 'i'; // default character to print
	int nap = 5; // nap time
	char buf[128];
	char ch2[] = "*?*";
	uint_t children[MAX_CHILDREN];
	int nkids = 0;

	// process the command-line arguments
	switch (argc) {
	case 3:
		count = str2int(argv[2], 10);
		// FALL THROUGH
	case 2:
		ch = argv[1][0];
		break;
	case 1: // just use the defaults
		break;
	default:
		sprint(buf, "userI: argc %d, args: ", argc);
		cwrites(buf);
		for (int i = 0; i <= argc; ++i) {
			sprint(buf, " %s", argv[argc] ? argv[argc] : "(null)");
			cwrites(buf);
		}
		cwrites("\n");
	}

	// secondary output (for indicating errors)
	ch2[1] = ch;

	// announce our presence
	write(CHAN_SIO, &ch, 1);

	// set up the argument vector
	// we run:	userW 10 5

	char *argsw[] = { "userW", "W", "10", "5", NULL };

	for (int i = 0; i < count; ++i) {
		int whom = spawn(ProgW, argsw);
		if (whom < 0) {
			swrites(ch2);
		} else {
			swritech(ch);
			children[nkids++] = whom;
		}
	}

	// let the children start
	sleep(SEC_TO_MS(nap));

	// kill two of them
	int32_t status = kill(children[1]);
	if (status) {
		sprint(buf, "!! %c: kill(%d) status %d\n", ch, children[1], status);
		cwrites(buf);
		children[1] = -42;
	}
	status = kill(children[3]);
	if (status) {
		sprint(buf, "!! %c: kill(%d) status %d\n", ch, children[3], status);
		cwrites(buf);
		children[3] = -42;
	}

	// collect child information
	while (1) {
		int n = waitpid(0, NULL);
		if (n == E_NO_CHILDREN) {
			// all done!
			break;
		}
		for (int i = 0; i < count; ++i) {
			if (children[i] == n) {
				sprint(buf, "== %c: child %d (%d)\n", ch, i, children[i]);
				cwrites(buf);
			}
		}
		sleep(SEC_TO_MS(nap));
	};

	// let init() clean up after us!

	exit(0);

	return (42); // shut the compiler up!
}