C c source le 350 c reference parameters 165 cache 9

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: function returns prematurely whenever the sleeping process receives a signal that is not ignored. But since the default action upon receipt of a SIGINT is to terminate the process (Figure 8.23), we must install a SIGINT handler to allow the sleep function to return. The handler simply catches the SIGNAL and returns control to the sleep function, which then returns immediately. code/ecf/snooze.c 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 #include "csapp.h" /* SIGINT handler */ void handler(int sig) { return; /* catch the signal and return */ } unsigned int snooze(unsigned int secs) { unsigned int rc = sleep(secs); printf("Slept for %u of %u secs.\n", secs - rc, secs); return rc; } int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "usage: %s <secs>\n", argv[0]); exit(0); } if (signal(SIGINT, handler) == SIG_ERR) /* install SIGINT handler */ unix_error("signal error\n"); (void)snooze(atoi(argv[1])); exit(0); } 728 APPENDIX B. SOLUTIONS TO PRACTICE PROBLEMS code/ecf/snooze.c B.9 Measuring Program Performance Problem 9.1 Solution: [Pg. 451] At first, it seems ridiculous to interrupt the CPU and execute 100,000 cycles just to deal with a single keystroke. When you work through the numbers, however, it becomes clear that the overall load on the CPU will be fairly small. 100 WPM corresponds to 10 keystrokes per second. The total number of cycles used per second by the 100 typists will be ½¼...
View Full Document

Ask a homework question - tutors are online