Task Control: Signals and Alarms Chapter 7 and 8Multi-taskingExamplesLinux ProcessesTermination of a processProcess TerminationSignalsPowerPoint PresentationRealtime signalsIntercept SignalsHandling SignalsSignals and AlarmsSample programsPingpongObserve in pingpong.cInput/output ResourcesVolatileSummaryB. RAMAMURTHYPage 1Task Control:Signals and AlarmsChapter 7 and 801/14/19Multi-taskingPage 2How 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 messagesHow to (software) interrupt a process? signals01/14/19ExamplesPage 3Consider g++ myProg.cYou want to kill this process after you started the compilation..hit cntrl-CConsider execution of a program called “badprog”>badprogIt core dumps .. What happened? The error in the program results in a signal to kernel to stop and dump the offending codeConsider “kill –p <pid>”Kill issues a termination signal to the process identified by the pid01/14/19Linux ProcessesPage 4Similar to XINU Procs.Lets understand how to create a linux process and control it.Chapter 7 and 8 of text book.Chapter 7 : multi-taskingChapter 8: Task communication and synchronization01/14/19Termination of a processPage 5•Normal completion, time limit exceeded, memory unavailable•Bounds violation, protection error, arithmetic error, invalid instruction•IO failure, Operator intervention, parent termination, parent request, killed by another process•A number of other conditions are possible. •Segmentation fault : usually happens when you try write/read into/from a non-existent array/structure/object component. Or access a pointer to a dynamic data before creating it. (new etc.)•Bus error: Related to function call and return. You have messed up the stack where the return address or parameters are stored.01/14/19Page 6Process TerminationProcess executes last statement and asks the operating system to delete it (exit)Output data from child to parent (via wait)Process’ resources are deallocated by operating systemParent may terminate execution of children processes (abort)Child has exceeded allocated resourcesTask assigned to child is no longer requiredIf parent is exitingSome operating system do not allow child to continue if its parent terminatesAll children terminated - cascading termination01/14/19SignalsPage 7Signals provide a simple method for transmitting software interrupts to UNIX processSignals cannot carry information directly, which limits their usefulness as an general inter-process communication mechanismHowever each type of signal is given a mnemonic name; Ex: SIGINTSee signal.h for othersSIGHUP, SIGINT, SIGILL, SIGTRAP, SIGFPE, SIGKILLSIGALRM (sent by kernel to a process after an alarm timer has expired)SIGTERM signal (signal id, function) simply arms the signal01/14/1901/14/19Page 8Signal Value Action Comment ------------------------------------------------------------------------- SIGHUP 1 Term Hangup detected on controlling terminal or death of controlling process SIGINT 2 Term Interrupt from keyboard SIGQUI 3 Core Quit from keyboard SIGILL 4 Core Illegal Instruction SIGABR 6 Core Abort signal from abort(3) SIGFP 8 Core Floating point exception SIGKILL 9 Term Kill signal SIGSEG 11 Core Invalid memory reference SIGPIPE 13 Term Broken pipe: write to pipe with no readers SIGALRM 14 Term Timer signal from alarm(2) SIGTERM 15 Term Termination signal SIGUSR1 30,10,16 Term User-defined signal 1 SIGUSR2 31,12,17 Term User-defined signal 2 SIGCHLD 20,17,18 Ign Child stopped or terminated SIGCONT 19,18,25 Cont Continue if stopped SIGSTOP 17,19,23 Stop Stop process SIGTSTP 18,20,24 Stop Stop typed at tty SIGTTIN 21,21,26 Stop tty input for background process SIGTTOU 22,22,27 Stop tty output for background process The signals SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.Realtime signalsLinux supports real-time signals as originally defined in the POSIX.1b real-time extensions (and now included in POSIX.1-2001). Linux supports 32 real-time signals, numbered from 32 (SIGRTMIN) to 63 (SIGRT- MAX)Main difference is that these are queued and not lost.Realtime signals are delivered in guaranteed order.01/14/19Page 9Intercept SignalsPage 10Task1Task2Two essential parameters are destination process identifierand the signal code number: kill (pid, signal)Signals are a useful way of handling intermittent data arrivals or rare errorconditions. 01/14/19Handling SignalsPage 11Look at the examples:Catching SIGALRM Ignoring SIGALRMsigtest.csigHandler.cpingpong.cSee /usr/include/sys/iso/signal_iso.h for signal numbers01/14/19Signals and AlarmsPage 12#include <signal.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 tctime(&t); will convert time to ascii formatAlarm has a sigaction function that is set for configuring the alarm handler etc.sigaction(SIGALRM, &act, &oldact) ; the third paramter is for old action configuration01/14/19Sample programsPage 13Starting new tasks in linux: page 165Programs in pages: 174-180 on signals and alarmsSee demos directory for the codeSee page 175 for the second programSee page 178 … for the third program01/14/19PingpongPage 14ParentChildPSIG 43CSIG 4201/14/19Observe in pingpong.cpause(): indefinitesleep(): sleep is random/finite timeWhile loopSignal handlersRe-arming of the signals01/14/19Page 15Input/output ResourcesWhat is standard IO?These are resources allocated to the process at the time of creation:From Wikipedia/Standard_streams01/14/19Page 16Volatile Page 17A 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
View Full Document