Expert C Programming

(Jeff_L) #1

/NOTREACHED/


}


main () {


signal(SIGINT, handler);


if (setjmp(buf)) {


printf("back in main\n");


return 0;


}else


printf("first time through\n");


loop:


/ spin here, waiting for ctrl-c /


goto loop;


}


Note: signal handlers are not supposed to call library functions (except under
restricted circumstances stated in the standard). If the signal is raised during the
"first time" printf, then the printf in the signal handler, coming in the middle of
all this, may get confused. We cheat here, because interactive I/O is the best way
to see what's going on. Don't cheat in real world code, OK?

Running this program results in this output:


% a.out


first time through


^C now got a SIGINT signal


back in main


This program uses setjmp/longjmp and signal handling, so that on receiving a control-C
(passed to a UNIX process as a SIGINT signal) the program restarts, rather than quits.


Chapter 8. Why Programmers Can't Tell Halloween from Christmas Day


Are you fed up with the slow speed of your workstation? Would you like to run your programs twice as
fast?


Here's how to do it in UNIX. Just follow these three easy steps:



  1. Design and code a high-performance UNIX vm kernel. Be careful! Your
    algorithm needs to run twice as fast as the present one to see the full 100%
    speed-up.

  2. Put your code in a file called /kernel/unix.c

  3. Issue the command


  4. cc -O4 -o /kernel/unix /kernel/unix.c



Free download pdf