80 likes | 191 Views
CMSC212: Intro to Low-Level Programming Concepts. Section 0101: 9:00am – 9:50am Section 0102: 10:00am-10:50am Monday & Wednesdays Room 3118. Signals. Definition: A message that notifies the process of some event that occurred from the system.
E N D
CMSC212: Intro to Low-Level Programming Concepts Section 0101: 9:00am – 9:50am Section 0102: 10:00am-10:50am Monday & Wednesdays Room 3118
Signals • Definition: A message that notifies the process of some event that occurred from the system. • Signals have corresponding numbers attached to them • However, not all signals must be handled by processes sent by the kernel
Signals Types • Low-level ones: • SIGFPE (8): Divide by zero • SIGILL (4): Illegal instruction • SIGSEGV (11): Illegal memory reference (All familiar with this by now) • High-level ones (software events or in other processes): • SIGINT (2): Ctrl-c • SIGKILL(9): Forcibly terminate another process • SIGCHLD(17): When child process terminates/stops, kernel sends this to parent process
Processes • Each process belongs to exactly one process group (PGID) • #include <unistd.h> • pid_t getpgrp(void); //returns PGID of calling process • int setpgid(pid_t pid, pid_t pgid); //returns 0 on success, -1 on failure • By default, processes belong to same ones as parent • To list the processes from the console: use ps
Sending Signals • kill [-some signal #] [some process #] • kill -9 15213 (sends signal SIGKILL to process 15213) • kill -9 -15213 (sends signal SIGKILL to process group 15213) • Ctrl-c: Sends SIGINT to all processes of foreground process’s group • Ctrl-z: Sends SIGTSTP for stopping (suspending) • Demo:forkkill.c
Sending Signals with alarm and sleep • Process may send signal SIGALARM to self by calling • Unsigned int alarm(unsigned int secs); //Returns remaining secs of previous alarm, or 0 if no previous alarm • Demo: signal1.c • Demo: signal2.c
More on process control • wait(): will wait until all child processes have terminated • waitpid(pid): does the above except for a particular process id • Demo: childrenwait.c • int execvp(const char *filename, char *const argv[]);//Executes filename in current directory or in path as a new process image • Demo: execvtrivial.c