diff options
Diffstat (limited to 'user/progI.c')
-rw-r--r-- | user/progI.c | 91 |
1 files changed, 47 insertions, 44 deletions
diff --git a/user/progI.c b/user/progI.c index c37eddf..a1988e3 100644 --- a/user/progI.c +++ b/user/progI.c @@ -1,7 +1,7 @@ #include <common.h> #ifndef MAX_CHILDREN -#define MAX_CHILDREN 50 +#define MAX_CHILDREN 50 #endif /** @@ -15,90 +15,93 @@ ** 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 +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; + 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" ); + 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 ); + 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 ); + for (int i = 0; i < count; ++i) { + int whom = spawn(ProgW, argsw); + if (whom < 0) { + swrites(ch2); } else { - swritech( ch ); + swritech(ch); children[nkids++] = whom; } } // let the children start - sleep( SEC_TO_MS(nap) ); + 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 ); + 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 ); + 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 ) { + 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 ); + 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) ); + sleep(SEC_TO_MS(nap)); }; // let init() clean up after us! - exit( 0 ); + exit(0); - return( 42 ); // shut the compiler up! + return (42); // shut the compiler up! } |