80 likes | 200 Views
Task Control: Signals and Alarms Havilland and Salama’s Unix Systems Programming. B. Ramamurthy. Multi-tasking. How to create multiple tasks? Ex: Xinu create() How to control them? ready() resched() How to synchronize them? How to communicate among them?
E N D
Task Control:Signals and AlarmsHavilland and Salama’s Unix Systems Programming B. Ramamurthy
Multi-tasking • How to create multiple tasks? Ex: Xinu create() • How to control them? • ready() • resched() • How to synchronize them? How to communicate among them? • XINU: semaphores, send and receive messages • How to (software) interrupt a process? signals
Examples • Consider g++ myProg.c • You want to kill this process after you started the compilation..hit cntrl-C • Consider execution of a program called “badprog” >badprog It core dumps .. What happened? The error in the program results in a signal to kernel to stop and dump the offending code • Consider “kill –p <pid>” • Kill issues a termination signal to the process identified by the pid
Signals • Signals provide a simple method for transmitting software interrupts to UNIX process • Signals cannot carry information directly, which limits their usefulness as an general inter-process communication mechanism • However each type of signal is given a mnemonic name; Ex: SIGINT • See signal.h for others • SIGHUP, SIGINT, SIGILL, SIGTRAP, SIGFPE, SIGKILL, SIGALRM (sent by kernel to a process after an alarm timer has expired) • SIGTERM
Handling Signals • Look at the examples: • Catching SIGALRM • Ignoring SIGALRM • sigtest.c
Signals and Alarms #include <unistd.h> unsigned int alarm( unsigned int seconds ); alarm(a); will start a timer for a secsonds and will interrupt the calling process after a secs. time(&t); will get you current time in the variable t declared as time_t t ctime(&t); will convert time to ascii format
Sample program • Lets look at a sample program
Volatile • A variable should be declared volatile whenever its value could change unexpectedly. In practice, only three types of variables could change: • Memory-mapped peripheral registers • Global variables modified by an interrupt service routine • Global variables within a multi-threaded application