signals ii
play

Signals - II Tevfik Ko ar Louisiana State University October 9 th , - PDF document

CSC 4304 - Systems Programming Process Groups Fall 2008 Lecture - XI Signals - II Tevfik Ko ar Louisiana State University October 9 th , 2008 1 2 Sending Signals Signals from Keyboard 3 4 Signals from Command-Line Signals from a


  1. CSC 4304 - Systems Programming Process Groups Fall 2008 Lecture - XI Signals - II Tevfik Ko � ar Louisiana State University October 9 th , 2008 1 2 Sending Signals Signals from Keyboard 3 4 Signals from Command-Line Signals from a Process 5 6

  2. void fork12(int N) { pid_t pid[N]; Sending Signals (Example) int i, child_status; Catching the Signal for (i = 0; i < N; i++){ pid[i] = fork(); if (pid[i] == 0){ if (i==2) signal(SIGINT, SIG_IGN); while(1); /* Child infinite loop */ } else if (pid[i]>0) printf("Child process %d is created.\n", pid[i]); } /* Parent terminates the child processes */ for (i = 0; i < N; i++) { printf("Killing process %d..\n", pid[i]); kill(pid[i], SIGINT); } /* Parent reaps terminated children */ for (i = 0; i < N; i++) { pid_t wpid = wait(&child_status); if (WIFEXITED(child_status)) printf("Child %d terminated with exit status %d!\n", wpid, WEXITSTATUS(child_status)); else printf("Child %d terminated abnormally\n", wpid); } } 7 8 Actions on Signal Non-Catchable Signals 9 10 Default Actions Signal Semantics 11 12

  3. Implementation Receiving Signals 13 14 Example from Last Lecture Overlapping Signals main (int ac, char *av[]) { • SIGY interrupts SIGX void inthandler(int); – ex: phone then door void quithandler(int); char input[100]; – When you press CTRL-C then CTRL-\, the program first jumps to inthandler, then to quithandler, then back to inthandler, then back to main loop. signal( SIGINT, inthandler ); //set trap signal( SIGQUIT, quithandler ); //set trap • SIGX interrupts SIGX – ex: two people coming to your door do { – Three ways this can be handled: printf("\nType a message\n"); 1.Recursively call the same handler if ( gets(input) == NULL ) 2.Ignore the second signal, like a phone without call waiting perror("Saw EOF "); 3.Block the second signal until done handling the first else • Original systems used method 1, though method 3 is safest. printf("You typed: %s\n", input); • Interrupted System Calls } while( strcmp( input , "quit" ) != 0 ); • receiving a signal while waiting for input 15 } 16 Example from Last Lecture (cont.) Ignore other Interrupts inside Handler? void quithandler (int s) void inthandler (int s) { { printf(" Received signal %d .. waiting\n", s ); printf(" Received signal %d .. waiting\n", s ); sleep(3); sleep(2); printf(" Leaving quithandler \n"); printf(" Leaving inthandler \n"); } } void inthandler (int s) void quithandler (int s) { { signal(SIGQUIT, SIG_IGN); printf(" Received signal %d .. waiting\n", s ); printf(" Received signal %d .. waiting\n", s ); sleep(3); sleep(2); printf(" Leaving quithandler \n"); printf(" Leaving inthandler \n"); } signal( SIGQUIT, quithandler ); } 17 18

  4. sigaction() Function sigaction() Function (cont.) 19 20 sigaction Structure sa_flags SA_NOCLDSTOP: If signum is SIGCHLD, do not receive notification when child processes stop. SA_NOCLDWAIT: If signum is SIGCHLD, do not transform children into zombies when they terminate. SA_RESETHAND: Restore the signal action to the default state once the signal handler has been called. SA_ONSTACK: Call the signal handler on an alternate signal stack provided by sigaltstack(2). SA_RESTART: Provide behaviour compatible with BSD signal semantics. by making certain system calls restartable across signals. struct sigaction { SA_NODEFER: Do not prevent the signal from being received from within its void (*sa_handler)(int); own signal handler. void (*sa_sigaction)(int, siginfo_t *, void *); sigset_t sa_mask; SA_SIGINFO: The signal handler takes 3 arguments, not one. In this case, int sa_flags; sa_sigaction should be set instead of sa_handler. void (*sa_restorer)(void); } 21 22 main() sigaction() Example sigaction() (cont.) { struct sigaction newhandler; sigset_t blocked; void inthandler(); char x[INPUTLEN]; newhandler.sa_handler = inthandler; newhandler.sa_flags = SA_RESETHAND | SA_RESTART; sigemptyset(&blocked); sigaddset(&blocked, SIGQUIT); newhandler.sa_mask = blocked; if ( sigaction(SIGINT, &newhandler, NULL) == -1 ) perror("sigaction"); else while( 1 ){ fgets(x, INPUTLEN, stdin); printf("input: %s", x); } } 23 24

  5. Masking Signals - Avoid Race Conditions Masking Signals (cont.) 25 26 sigprocmask() Function Suspending Masked Signals 27 28 Manipulating Signal Sets Real-time Signals 29 30

  6. Summary Acknowledgments • Advanced Programming in the Unix Environment by R. • Signals Hmm. Stevens – Generating & Catching Signals . • The C Programming Language by B. Kernighan and D. – Overlapping Signals Ritchie – Preventing Race Conditions – Masking Signals • Understanding Unix/Linux Programming by B. Molay • Lecture notes from B. Molay (Harvard), T . Kuo (UT- Austin), G. Pierre (Vrije), M. Matthews (SC), B. Knicki HW 2 out today, due Oct 14th, Tuesday! (WPI), M. Shacklette (UChicago), and J.Kim (KAIST). 31 32

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend