Documente Academic
Documente Profesional
Documente Cultură
Signals
Gaurav Verma
Assistant Professor
Department of Electronics and Communication Engineering
Jaypee Institute of Information and Technology
Sector-62, Noida, Uttar Pradesh, India
Process Management
Processes
What is a Process?
Ans: A Program in execution is called a Process.
Start
execution
Ready
Running
Sleeping
Finish
execution
Ready state
Running state
Sleeping state
Processes will alternatively get into the CPU one after the
other (called the round robin scheme)
A process will be in a CPU for a very short time (quantum)
For Linux, each quantum is about 100msec
At the time that a process is selected to be in the CPU
It goes from ready state to running state
After that, it will be swapped out
It goes from running state back to ready state
Or it may due to the waiting of an I/O device, e.g. mouse
It goes from running state to sleeping state
When obtaining the required resource
It goes from sleeping state to ready state
d -> 0
User -> e
f -> d+e
d -> f
User -> e
f -> d+e
d -> f
User -> e
f -> d+e
d -> f
User -> e
f -> d+e
d -> f
User -> e
f -> d+e
d -> f
Print out d
Quantum
end
Waiting for
user input
Quantum
end
Waiting for
10
user input
b -> 0
c -> a+b
b -> c
c -> a+b
d -> 0
c -> a+b
Program
A
finishes
b -> c
Print out b
f -> d+e
d -> f
User -> e
User -> e
b -> c
c -> a+b
f -> d+e
d -> f
User -> e
b -> c
c -> a+b
b -> c
f -> d+e
f -> d+e
d -> f
User -> e
f -> d+e
d -> f
User -> e
Program
B
finishes
d -> f
Print out d
processes
Suspending and resuming processes
Providing mechanisms for process synchronization
Providing mechanisms for process communication
Providing mechanism for deadlock handling
11
write to an address?
Perhaps Nothing
Perhaps acts like regular memory
Perhaps ignores writes
Perhaps causes I/O operation
(Memory-mapped I/O)
Perhaps causes exception (fault)
12
Process stack:
Prog 1
Virtual
Address
Space 1
Prog 2
Virtual
Address
Space 2
Data 1
Heap 2
Code 2
OS code
Translation Map 1
OS data
Translation Map 2
OS heap &
Stacks
14
15
The state may be new, ready, running, waiting, halted, and so on.
Program Counter:
Process Creation
Parent process create children processes, which, in turn create
(pid)
Resource sharing
Parent and children share all resources
Children share subset of parents resources
Execution
Parent and children execute concurrently
Parent waits until children terminate
17
processes that are currently loaded with, for example, their PID,
status, and command string, the sort of information output by ps.
The operating system manages processes using their PIDs, and they
are used as an index into the process table. The table is of limited
size, so the number of processes a system will support is limited.
Early UNIX systems were limited to 256 processes.
More modern implementations have relaxed this restriction
considerably and may be limited only by the memory available to
construct a process table entry.
18
Viewing Processes
Terminal pts/0 has the editor vi
running
Terminal pts/1 is
executing ps to see
the processes of both
terminals
ps
19
Each process is allocated a unique number, called a process identifier or PID. This is usually
a positive integer between 2 and 32,768. When a process is started, the next unused
number in sequence is chosen and the numbers restart at 2 so that they wrap around.
The number 1 is typically reserved for the special init process, which manages other
processes.
PID
14748
TTY
pts/1
1
pts/0
14974 pts/0
14876 pts/1
Process ID
20
Terminal
name
STAT
S
S
S
R
TIME COMMAND
0:00 bash
0:00 bash
0:00 vi test1.txt
0:00 ps
How much time the
process is continuously
executing
State:
S Sleeping
(waiting for input)
R Running
21
22
Init process
In general, each process is started by another process known as
23
24
#include <stdlib.h>
#include <stdio.h>
int main()
{
printf(Running ps with system\n);
system(ps -ax);
printf(Done.\n);
exit(0);
}
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
pid_t pid;
char *message;
int n;
printf(fork program starting\n);
pid = fork();
switch(pid){
case -1:
printf(fork failed);
exit(1);
case 0:
message = This is the child;
n = 5;
break;
default:
message = This is the parent;
n = 3;
break;
}
for(; n > 0; n--) {
puts(message);
sleep(1);}
26
exit(0);}
by calling wait.
# include <sys/types.h>
# include <sys/wait.h>
pid_t wait(int *stat_loc);
The wait system call causes a parent process to pause until one of
Exit status
We can interpret the status information using macros defined in sys/wait.h. These include
Macro Definition
WIFEXITED( stat_val) ;
WEXITSTATUS( stat_val) ;
28
WTERMSIG( stat_val) ;
WIFSTOPPED( stat_val) ;
WSTOPSIG( stat_val);
Zombie Processes
Using fork to create processes can be very useful, but you must keep track of child processes.
When a child process terminates, an association with its parent survives until the parent in
turn either terminates normally or calls wait. The child process entry in the process table is
therefore not freed up immediately. Although no longer active, the child process is still in the
system because its exit code needs to be stored in case the parent subsequently calls wait. It
becomes what is known as defunct, or a zombie process.
We can see a zombie process being created if we change the number of messages in the fork example
program. If the child prints fewer messages than the parent, it will finish first and will exist as a
switch(pid)
zombie until the parent has finished.
{
If we run the program & and then call the ps program after the child has case -1:
finished but before the parent has finished, well see a line such as this. (Some perror(fork failed);
exit(1);
systems may say <zombie>rather than <defunct>.)
case 0:
$ ps al
message = This is the child;
n = 3;
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
break;
000 S 500 1603 1262 0 75 0 - 313 schedu pts/1 00:00:00 fork2
default:
003 Z 500 1604 1603 0 75 0 - 0 do_exi pts/1 00:00:00 fork2 <defunct> message = This is the
parent;
000 R 500 1605 1262 0 81 0 - 781 - pts/1 00:00:00 ps
n = 5;
break;}
30
Signal cont
Each signal may have a signal handler, which is a function that
gets called when the process receives that signal. The function is
called in "asynchronous mode", meaning that no where in your
program you have code that calls this function directly.
Instead, when the signal is sent to the process, the operating
system stops the execution of the process, and "forces" it to call
the signal handler function. When that signal handler function
returns, the process continues execution from wherever it
happened to be before the signal was received, as if this
interruption never occurred.
31
32
33
few signals that the process cannot catch, and cause the
process to terminate.
SIGKILL & SIGSTOP
35
36
#include <unistd.h>
#include <sys/ types.h>
#include <signal.h>
void catch_int( int signum)
{
signal(SIGINT, catch_int);
printf("Don't do that");
fflush( stdout);
}
main()
{
(void)signal(SIGINT, catch_int);
while(1);
37
signal(SIGINT, SIG_IGN);
SIG_DFL:
Causes the system to set the default signal handler for the given signal (i.e. the
same handler the system would have assigned for the signal when the process
started running):
signal(SIGTSTP, SIG_DFL);
38