aside : Teaching C | HackerNews discussion
Continuing to discuss chap 8 , 14-ecf-procs.pdf & 15-ecf-signals.pdf ...
practice 8.3 : fork , fflush , waitpid
int main(){
if (Fork() == 0) {
printf("a"); fflush(stdout);
} else {
printf("b"); fflush(stdout);
waitpid(-1, NULL, 0);
}
printf("c"); fflush(stdout);
exit(0);
}
/* from Bryant, Randal E., David O'Hallaron. Computer Systems, 3rd Edition */
practice 8.7 ... discuss after talking about signals.
Trick stuff ...
topics
practice 8.7 ...
/***********
* snooze2.c
*
* on shannon :
* $ gcc -O -pthread csapp.c snooze.c -o snooze
* $ ./snooze 10 # control-c
*
* Minor changes to snooze.c
* from Bryant, Randal E., David O'Hallaron. Computer Systems, 3rd Edition.
***********/
#include "csapp.h"
void sigint_handler(int sig){
return; /* Catch the signal and return */
}
void snooze(unsigned int secs) {
unsigned int slept = sleep(secs);
printf ("Slept for %d of %d secs.\n", secs - slept, secs);
}
int main(int argc, char **argv){
if (argc != 2) {
fprintf (stderr, "usage: %s <secs>\n", argv[0]);
exit(0);
}
if (signal(SIGINT, sigint_handler) == SIG_ERR){
unix_error("signal error\n");
}
snooze(atoi(argv[1]));
exit(0);
}
Discuss
To do for Thursday :
last modified | size | ||
snooze2.c | Tue Oct 15 2024 05:25 am | 855B |