UNIX System Programming Overview 1. Definition 2. Signal Types

UNIX System Programming Overview 1. Definition 2. Signal Types

UNIX System Programming Overview Signals 1. Definition 2. Signal Types 3. Generating a Signal 4. Responding to a Signal Objectives 5. Common Uses of Signals – Introduce signals 6. Implementing a read() – Concentrate on sigaction() function Timeout continued 1730 UNIX System Programming: Signals Maria Hybinette 1 1730 UNIX System Programming: Signals Maria Hybinette 2 1. Definition 7. POSIX Signal Functions A signal is an asynchronous event which is 8. Interrupted System Calls delivered to a process. 9. System Calls inside Handlers 10. More Information Asynchronous means that the event can occur at any time – may be unrelated to the execution of the process – e.g. user types ctrl-C, or the modem hangs 1730 UNIX System Programming: Signals Maria Hybinette 3 1730 UNIX System Programming: Signals Maria Hybinette 4 2. Signal Types (31 in POSIX) Signal Sources terminal memory Name Description Default Action driver management shell command SIGINT Interrupt character typed terminate process SIGQUIT Quit character typed (^\) create core image SIGINT SIGHUP SIGKILL kill -9 terminate process SIGQUIT SIGSEGV Invalid memory reference create core image SIGKILL kernel SIGPIPE SIGPIPE Write on pipe but no reader terminate process SIGALRM alarm() clock ‘rings’ terminate process SIGUSR1 user-defined signal type terminate process SIGWINCH SIGALRM SIGUSR2 user-defined signal type terminate process window : manager a process SIGUSR1 See man 7 signal other user processes 1730 UNIX System Programming: Signals Maria Hybinette 5 1730 UNIX System Programming: Signals Maria Hybinette 6 UNIX: Signals Maria Hybinette 1 3. Generating a Signal kill() Use the UNIX command: $ kill -KILL 4481 Send a signal to a process (or group of processes). – send a SIGKILL signal to pid 4481 – check #include <signal.h> ps –l int kill( pid_t pid, int signo ); – to make sure process died kill is not a good name; send_signal Return 0 if ok, -1 on error. might be better. 1730 UNIX System Programming: Signals Maria Hybinette 7 1730 UNIX System Programming: Signals Maria Hybinette 8 4. Responding to a Signal Some pid Values A process can: – ignore/discard the signal (not possible with pid Meaning SIGKILL or SIGSTOP) > 0 send signal to process pid – execute a signal handler function, and then possibly resume execution or terminate == 0 send signal to all processes whose process group ID – carry out the default action for that signal equals the sender’s pgid. e.g. parent kills all children The choice is called the process’ signal disposition 1730 UNIX System Programming: Signals Maria Hybinette 9 1730 UNIX System Programming: Signals Maria Hybinette 10 signal(): library call Actual Prototype The actual prototype, listed in the “man” page is a bit Specify a signal handler function to deal with a signal type. perplexing but is an expansion of the Sigfunc type: #include <signal.h> void (*signal(int signo, void(*handler)(int)))(int); typedef void Sigfunc(int); /* my defn */ In Linux: Sigfunc *signal( int signo, Sigfunc *handler ); typedef void (*sighandler_t)(int); – signal returns a pointer to a function that returns an int (i.e. it sig_handler_t signal(int signo, sighandler_t handler); returns a pointer to Sigfunc Signal returns a pointer to a function that returns an int Returns previous signal disposition if ok, SIG_ERR on error. 1730 UNIX System Programming: Signals Maria Hybinette 11 1730 UNIX System Programming: Signals Maria Hybinette 12 UNIX: Signals Maria Hybinette 2 The signal function itself returnsSignal a pointer toHandling a function. Example The return type is the same as the function that is passedThe signal in, toThe be handler function int main() { i.e., aUse function the signal that handling takes caughtlibrary: an or ignored signal.hReceives a single integer signal( SIGINT, foo ); int and returns a void is given as argumentArgument and returns void : Then can use the signal call: sig /* do usual things until SIGINT */ #include <signal.h> return 0; } void (*signal( int sig, void (*handler)(int))) (int) ; void foo( int signo ) signal returns a pointer to the PREVIOUS signal { handler : /* deal with SIGINT signal */ The function to be called Signal #includeis a function <signal.h> when the specified signal that takestypedef two void Sigfunc(int); is /* received my defnThe */is returnedgiven as functiona return; /* return to program */ Sigfunc *signal( int signo, Sigfunc *handler ); } arguments: pointer totakes the function a integer sig and handler handler parameter. 1730 UNIX System Programming: Signals Maria Hybinette 13 1730 UNIX System Programming: Signals Maria Hybinette 14 sig_examp.c #include <stdio.h> #include <unistd.h> : #include <signal.h> while(1) { void sig_usr( int signo ); /* handles two signals */ printf( “%2d\n“, I ); pause(); int main() { /* pause until signal handler int i = 0; * has processed signal */ if( signal( SIGUSR1,sig_usr ) == SIG_ERR ) i++; printf( “Cannot catch SIGUSR1\n” ); } if( signal( SIGUSR2,sig_usr ) == SIG_ERR ) return 0; printf(“Cannot catch SIGUSR2\n”); } : continued continued 1730 UNIX System Programming: Signals Maria Hybinette 15 1730 UNIX System Programming: Signals Maria Hybinette 16 Usage void sig_usr( int signo ) /* argument is signal number */ { $ sig_examp & if( signo == SIGUSR1 ) [1] 4720 printf(“Received SIGUSR1\n”); 0 else if( signo == SIGUSR2 ) $ kill -USR1 4720 printf(“Received SIGUSR2\n”); Received SIGUSR1 else 1 printf(“Error: received signal $ kill -USR2 4720 %d\n”, signo); Received SIGUSR2 2 return; $ kill 4720 /* send SIGTERM */ } [1] + Terminated sig_examp & $ 1730 UNIX System Programming: Signals Maria Hybinette 17 1730 UNIX System Programming: Signals Maria Hybinette 18 UNIX: Signals Maria Hybinette 3 Special Sigfunc * Values Multiple Signals If many signals of the same type are waiting Value Meaning to be handled (e.g. two SIGINTs), then most UNIXs will only deliver one of them. SIG_IGN Ignore / discard the signal. SIG_IGN – the others are thrown away SIG_DFL Use default action to handle signal. SIG_ERR Returned by signal() as an error. If many signals of different types are waiting to be handled (e.g. a SIGINT, SIGSEGV, SIGUSR1), they are not delivered in any fixed order. 1730 UNIX System Programming: Signals Maria Hybinette 19 1730 UNIX System Programming: Signals Maria Hybinette 20 pause() The Reset Problem Suspend the calling process until a signal is caught. In Linux (and many other UNIXs), the #include <unistd.h> signal disposition in a process is reset to its int pause(void); signal disposition in a process is reset to its default action immediately after the signal Returns -1 with errno assigned EINTR. has been delivered. (Linux assigns it ERESTARTNOHAND). pause() only returns after a signal handler has Must call signal() again to reinstall the returned. signal handler function. 1730 UNIX System Programming: Signals Maria Hybinette 21 1730 UNIX System Programming: Signals Maria Hybinette 22 To keep catching Reset Problem the signal with this Reset Problem Example function, must call : the signal system int main() void ouch( int sig ) call again. { { signal(SIGINT, foo); printf( "OUCH! - I got signal %d\n", sig ); : (void) signal(SIGINT, ouch); /* do usual things until SIGINT */ } } Problem: from the time int main() that the interrupt function { starts to just before the void foo(int signo) signal handler is re-established (void) signal( SIGINT, ouch ); { the signal will not be while(1) handled. signal(SIGINT, foo); /* reinstall */ { : printf("Hello World!\n"); return; sleep(1); If another SIGINT signal is } } received during this time, default behavior will be done, } 1730 UNIX System Programming: Signals Maria Hybinette 23 1730 UNIX System Programming: Signals Maria Hybinette i.e., program will terminate.24 UNIX: Signals Maria Hybinette 4 Re-installation may be too 5. Common Uses of slow! Signals There is a (very) small time period in foo() 5.1. Ignore a Signal when a new SIGINT signal will cause the default action to be carried out -- process 5.2. Clean up and Terminate termination. 5.3. Dynamic Reconfiguration 5.4. Report Status With signal() there is no answer to this problem. 5.5. Turn Debugging on/off – POSIX signal functions solve it (and some 5.6. Restore Previous Handler other later UNIXs) 1730 UNIX System Programming: Signals Maria Hybinette 25 1730 UNIX System Programming: Signals Maria Hybinette 26 5.2. Clean up and 5.1. Ignore a Signal Terminate : int main() : { /* global variables */ signal(SIGINT, SIG_IGN); signal(SIGINT, SIG_IGN); int my_children_pids; signal(SIGQUIT, SIG_IGN); : : /* do work without interruptions */ void clean_up(int signo); } int main() Cannot ignore/handle SIGKILL or SIGSTOP { Should check for SIG_ERR signal(SIGINT, clean_up); : } continued 1730 UNIX System Programming: Signals Maria Hybinette 27 1730 UNIX System} Programming: Signals Maria Hybinette 28 Problems void clean_up(int signo) { If a program is run in the background then unlink(“/tmp/work-file”); the interrupt and quit signals (SIGINT, kill(my_children_pids, SIGTERM); SIGQUIT) are automatically ignored. wait((int *)0); fprintf(stderr, “Program terminated\n”); Your code should not override these exit(1); changes: } – check if the signal dispositions are SIG_IGN 1730 UNIX System Programming: Signals Maria Hybinette 29 1730 UNIX System Programming: Signals Maria Hybinette 30 UNIX: Signals Maria Hybinette 5 Checking the Disposition 5.3. Dynamic new disposition old disposition Reconfiguration : : if( signal(SIGINT, SIG_IGN ) != SIG_IGN ) void read_config(int signo);

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    15 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us