Thursday, October 21 CS 375 UNIX System Programming - Lecture 16 1
Lecture 16
Log into Linux. Copy files from
/home/hwang/cs375/lecture16/*.*
Questions?
Lecture 16 Log into Linux. Copy files from - - PowerPoint PPT Presentation
Lecture 16 Log into Linux. Copy files from /home/hwang/cs375/lecture16/*.* Questions? Thursday, October 21 CS 375 UNIX System Programming - Lecture 16 1 Outline Zombie processes Sending signals to processes Thursday, October
Thursday, October 21 CS 375 UNIX System Programming - Lecture 16 1
Log into Linux. Copy files from
Questions?
Thursday, October 21 CS 375 UNIX System Programming - Lecture 16 2
Zombie processes Sending signals to processes
Thursday, October 21 CS 375 UNIX System Programming - Lecture 16 3
Recall that a parent process creates a child
In order for the child to terminate completely, its
Thursday, October 21 CS 375 UNIX System Programming - Lecture 16 4
See zmb_xmpl1.cpp and zmb_xmpl2.cpp It is not good for a system to accumulate
This is particularly bad if the parent process is a
Thursday, October 21 CS 375 UNIX System Programming - Lecture 16 5
If we want to "disown" a child process we can
if ( (pid = fork()) == 0) { // first child if ( (pid = fork()) == 0) { // second child // parent becomes init when first child exits execvp(program, args); } // first child, so exit // second child is adopted by init exit(0); } waitpid(pid, NULL, 0); // wait for first child // we're the parent – go on and do our own thing // we don't have to worry about the second child
Thursday, October 21 CS 375 UNIX System Programming - Lecture 16 6
Signals are software interrupts. Signals are
Signals can be raised by the kernel in response
The terminal driver sends signals in response
They can be used as a primitive form of IPC.
Thursday, October 21 CS 375 UNIX System Programming - Lecture 16 7
The kill( ) routine is used to send a signal:
There also is a kill utility that can be used to
raise(int sig) is kill(getpid( ), sig) and can be
Thursday, October 21 CS 375 UNIX System Programming - Lecture 16 8
Each signal has a unique name (and number,
Thursday, October 21 CS 375 UNIX System Programming - Lecture 16 9
When a process receives a signal it can:
ignore the signal (except for SIGKILL and
catch (or handle) the signal take the default action (for most the default is to
To catch the signal we must install a signal (or
Thursday, October 21 CS 375 UNIX System Programming - Lecture 16 10
For portability, sigaction( ) should be used
Thursday, October 21 CS 375 UNIX System Programming - Lecture 16 11
signum specifies the signal and can be any
If act is non-null, the new action is installed
The sigaction structure looks like:
Thursday, October 21 CS 375 UNIX System Programming - Lecture 16 12
sa_handler is a pointer to the desired signal
sa_mask is a signal set (see slide 15) that
Thursday, October 21 CS 375 UNIX System Programming - Lecture 16 13
sa_flags modifies the behavior of the signal
SA_NOCLDSTOP: Block child stop notification. SA_NOCLDWAIT: When SIGCHLD is received to
SA_RESETHAND: Restore the default handler after
SA_NODEFER: Do not block a signal in its own
Thursday, October 21 CS 375 UNIX System Programming - Lecture 16 14
You can use the pause( ) routine to sleep until
See sig_xmpl.cpp for example code that
Thursday, October 21 CS 375 UNIX System Programming - Lecture 16 15
The sigsetops routines are used to create and
Thursday, October 21 CS 375 UNIX System Programming - Lecture 16 16
alarm(sec) arranges for the current process to
SIGALRM will terminate a process by default. A
See alm_xmpl.cpp
Thursday, October 21 CS 375 UNIX System Programming - Lecture 16 17
alarm( ) can be used to timeout an operation
See the sleep( ), usleep( ), nanosleep( ) and
Thursday, October 21 CS 375 UNIX System Programming - Lecture 16 18
In addition to all of the standard signals
Write a program that creates a child process
See exercise_template.cpp to get started