Interprocess Communication: Signals and Pipes
1. Signals
1.1 Signal Environments
1.2 Experiments with signal()
Copyright By PowCoder代写 加微信 powcoder
1.3 Experiments with POSIX.1 sigaction()
2.1 Named Pipes (FIFOS)
3. Concurrent servers: A First Look 3.1 Example: POLL()
CMPUT 379 (E.S. Elmallah)
1. Signals
Signals are viewed as software interrupts : they provide a way of handling asynchronous events.
Every signal has a name, and a default action: T = terminate, TC = terminate and core dump, I = ignore, S = stop
o keyboard generated
• SIGSTP [ctl-z] keyboard stop signal S • SIGINT [ctl-c] interrupt T
• SIGQUIT [ctl-\ ] quit signal TC
CMPUT 379 (E.S. Elmallah)
o hardware exceptions
• SIGSEGV • SIGFPE
segmentation fault TC
floating point exception TC bus error TC
o software conditions
• SIGCHLD change in child status I • SIGALARM timer alarm T • SIGHUP hangup T
CMPUT 379 (E.S. Elmallah)
Terminology: a signal is called
o posted (generated, or sent ) if the event that causes
the signal has occurred,
o delivered (or caught ) if the associated action is taken,
o pending if it has been posted but not delivered, or blocked,
o blocked if it is pending because the target process does not want it delivered
Signal disposition:
o Let the default apply
o Ignore the signal (except SIGKILL and SIGSTOP ) o Catch it by a user defined function (signal handler).
CMPUT 379 (E.S. Elmallah)
1.1 Signal Environments
Currently, one can distinguish 4 signal environments o The Traditional System-V Signal Environment
o The BSD4.3 Signal Environment
o The System V.3 (SVR3) Environment
o The POSIX Signal Environment
CMPUT 379 (E.S. Elmallah)
The Traditional System-V Signal Environment
o dates back to V7 research UNIX, provides an easy
interface: signal(), pause(), SIG_DFL, SIG_IGN o has some limitations that causes unreliable
behaviour:
• signal handlers are reset to SIG_DFL after a signal is received
• ”slow” system calls are interrupted (causing error return) when a signal is delivered
• no support for blocking
CMPUT 379 (E.S. Elmallah)
The BSD4.3 Signal Environment
o provides a more reliable environment
o modifies the V7 signal handling by providing
• signal handling remains installed during, and after delivery
• signal masking, and blocking during the duration of signal handling
o Interface: sigvec() , sigstack() , siginterrupt() , sigsetmask() , sigpause()
CMPUT 379 (E.S. Elmallah)
The System V.3 (SVR3) Environment
o uses a similar approach to BSD4.3
o Interface: sigset() , sighold() , sigrelse(), sigignore() , sigpause()
The POSIX Signal Environment
o supports all features in BSD4.3 and SVR3
o does not standardize all options; the existing implementations are largely identical
o the interface provides: sigaction() , sigprocmask() , sigemptyset(), sigfillset() , sigaddset() , sigdelset(), sigismember() , sigpending()
CMPUT 379 (E.S. Elmallah)
1.2 Experiments with signal()
On SunOS 5.8: signal() is defined as:
#include
void (*signal (int sig, void (*disp)(int))) (int);
CMPUT 379 (E.S. Elmallah)
Example: signal()
#include
void quit(int code) {
fprintf(stderr, “\nInterrupt (code= %d, i= %d)\n”, code,i); }
int main (void) { signal(SIGQUIT,quit); for (i= 0; i < 9e7; i++)
if (i % 10000 == 0) putc('.', stderr); return(0);
CMPUT 379 (E.S. Elmallah)
o you can also use “kill -QUIT pid_number” o can you interrupt several times?
CMPUT 379 (E.S. Elmallah)
#include
void quit (int code) {
char answer;
fputs(“\n restart the loop?”, stdout); answer= fgetc(stdin); if (answer == ‘y’) longjmp(return_pt,1);
main (void) {
if (setjmp(return_pt) == 0) signal(SIGQUIT,quit); printf (“\nloop begins:\n”);
for (i= 0; i < 9e7; i++)
if (i % 10000 == 0) { putchar('.'); fflush(stdout); } puts( "\nFinished!");
CMPUT 379 (E.S. Elmallah)
1.3 Experiments with POSIX.1 sigaction()
Defined as:
int sigaction (int signo, struct sigaction *act, struct sigaction *oact);
signo specifies the signal to be caught
act points to a structure specifying the new action to be taken
oact points to a structure storing the previous handler
sigaction returns 0 if OK & -1 on error
actions remain in effect, unless explicitly changed
CMPUT 379 (E.S. Elmallah)
struct sigaction:
int sa_flags;
void (*sa_handler)(int);
sigset_t sa_mask;
void (*sa_sigaction) (int, siginfo_t *, void *); };
struct siginfo_t :
int int int pid_t uid_t
si_signo si_errno si_code
si_pid si_uid
// signal number // error number // signal code
// sending process ID // sending user ID
CMPUT 379 (E.S. Elmallah)
On entry to the handler,
o sa_mask is added to the to the set of signals already
being blocked when the signal is delivered.
o In addition, the signal that caused the handler to be executed will also be blocked.
If sa_flags[SA_SIGINFO] = 1 the sender’s si_pid is returned in a siginfo_t structure.
CMPUT 379 (E.S. Elmallah)
Example: sigaction()
#include
struct sigaction oldAct, newAct;
void quitHandler (int sigNo, siginfo_t *sigInfo) {
fprintf (stderr, “\n signo= %d, sender= %d, i= %d)\n”,
sigNo, sigInfo->si_pid, i);
CMPUT 379 (E.S. Elmallah)
int main (void) {
newAct.sa_handler = quitHandler; newAct.sa_flags |= SA_SIGINFO;
// omit error checking now
sigaction (SIGQUIT, &newAct, &oldAct); for (i= 0; i < 9e7; i++)
if (i % 10000 == 0) putc(’.’, stderr); sigaction(SIGQUIT,&oldAct,&newAct); return(0);
CMPUT 379 (E.S. Elmallah)
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com