EMARO+ / JEMARO /
ROBOTICS ENGINEERING
PROCESS MANAGEMENT:
DATA STRUCTURES
V2.1.
So far
Switcher
Resident
System calls Interrupt Handlers I/O drivers
Non resident
User
Daemon
Processes
Process 1 Process 2 Process n
Main Memory Peripherals
a process image
The memory of a process image is organized
into three regions:
Re-entrant: shared Stack
by several
processes
Data
Text
Each region has an independent address space
Generated by compiler
#include ...
NON
char buffer[1000];
INTIALIZED
int version=1;
main(int argc, char *argv[]) INITIALIZED
{ int i;
while ...
...
exit (0); STACK
}
int fun(int n)
{ int j;
if ... TEXT
...
}
Executable
An executable is constituted by a set of headers and a
set of sections, containing code or data:
Main header,with magic number
Header Section 1
Header Section 2
Section 1
Section 2
Processes hierarchies
So far
Switcher
Resident
System calls Interrupt Handlers I/O drivers
Non resident
Stack Stack Stack
Data Data Data
Text Text Text
Process 1 Process 2 Process n
Main Memory Peripherals
Data structures for process management
Process Table
• It is in the kernel, and is resident
• It contains an entry for each process, and is sized statically at the time
of system configuration
• for each process contains information that allow the scheduling, and
which must always be resident
U-Area (user area)
• It is in the kernel, but not necessarely resident in memory
• It contains the information necessary to the kernel for the management
of the process (open files, signal masks, computing times and similar),
but it is not necessarely always resident in memory
Ready Queue
• Lists of ready processes (one for each priority level)
Data structures for process management
0 • PID (process ID)
1
• PPID (Parent Process ID)
2
3 • owner’s UID
4
… • Process state
• Flag if resident
Process Table
Scheduling parameters
(priority, used CPU time,
waiting time)
• Process dimension
… • Signals masks
• Pointer to page table
Page Table
•….
U-Area
Data structures for process management
• Pointer to the Process 0
Table entry 1
2
• Registers & PC save area
3
Kernel Stack for the
process
4
…
Stack
• Info regarding the current Process Table
system call
• Open File Descriptors Data
I/O operations parameters
• Current Directory and Root Text
Directory
• Accounting info Process
• Pointers to Process
regions
U-Area
Signals masks
•….
Text Table
Data structures for process management
0
2 1
1
Priority
2
0
3
-1
-2
4
…
Stack
Process Table
Ready Q
Data
Text
List of 0- priority
proceees Process
U-Area
Text Table
So far
Ready queue
Switcher
Kernel
Process Table System calls Interrupt handlers
U-Area U-Area U-Area
Stack Stack Stack
Dati Dati Dati
Testo Testo Testo
Processo Processo Process n
MEMORY MANAGEMENT
Paging
"Ancient versions" of Unix use paging techniques
To run a process, in memory there are at least:
⚫ its u-area
⚫ its page table
The pages of text, data and stack are brought
dynamically in memory by the kernel, one at a time,
when needed (that is, as a result of a page fault)
If u-area and page table are not in memory, the
swapper process loads them
Pagedaemon
Is a daemon who runs periodically to check the
number of free pages in memory:
⚫ If it is too low, frees some pages
⚫ If it is ok, returns to sleep
Swapper
It is a system process that:
• If free pages are under a given minimum, it removes
one or more processes from memory (typically those
inactive for the longest time): swapout
• Periodically verifies the existence of ready processes
on disk and, if possible, loads them into memory
(only u-area and page table); in general, those
outside since long time, unless they are not “too
large” : swapin
PROCESS MANAGEMENT:
SYSTEM CALL
Unix processes
• In Unix, each process is created by the kernel at
request of another process (called the parent
process), by means of an appropriate system call
(fork)
• There is an initial pseudo-process (PID = 0) who has
no father, and that is created by the kernel at the time
of boot (system initialization). It starts the first true
process (INIT, PID = 1).
• Each process terminates through a special system
call (exit)
Process creation
• In Unix a new process is created exclusively for
duplication of an existing process, through the
system call:
pid_t fork (void);
which creates a "child process" identical to his father,
and returns:
o to his son: 0
o to his father: PID of the child
o if an error occurs: -1
The only exception is the process 0, which is created at
system initialization
Fork: example
p = fork ();
if p <0 {/ * the fork failed * /}
else if p = 0 {/ * the son operations * /} else {/ *
Father operations * /}
In Unix, there is no system call to "create a process that
runs the executable x"
We have to do it using more system calls ...
Process creation
Ready queue
Switcher
Kernel
Process Table System calls Interrupt handlers
U-Area U-Area
P= n P=0
Stack Stack
Dati Dati
P=fork() Testo
Father Son
Processes hierarchies
fork()
• Allocates an entry in the Process Table for a new
process
• Assigns a unique PID to the new process and
initializes the Process Table entry fields
• Creates a copy of the parent process image (the text
is not duplicated but increments a reference count)
• Increases appropriate open file counters
• Initializes the counters of accounting in the u-area of
the new process
• Places the new process in ready state
• Returns the PID of the child to the father, 0 to the
son, or -1 on error
clone()
• a Linux kernel system call that replicates the
process that invokes it, like fork(), but simpler. It is
usually used to perform concurrency at the thread
level, rather than at the process level, although for
such purposes higher-level alternatives are often
preferred, like POSIX Threads.
• Clone() is discouraged! It does not duplicate the
stack!
exec()
exec (pathname, argomenti)
• Replaces the image of the caller with the
executable file pathname, and makes it
running by passing arguments
p = fork();
if p < 0 { /* fork failed */ }
else if p=0 { /*son’s code */ } exec (…)
else { /* parent’s code*/ } …...
Process creation
Ready queue
Switcher
Kernel
Process Table System calls Interrupt handlers
U-Area U-Area
Stack Stack
Dati Dati
Testo
Father Son
Exec
Ready queue
Switcher
Kernel
Process Table System calls Interrupt handlers
U-Area U-Area
Stack Stack
Dati Dati
Testo Testo
Father Son
Process termination
In Unix process terminate through the system
call
void exit(int status);
which:
o terminates the calling process;
o makes available to the parent process the value of
status, putting it in its process table entry
o flushes and closes all open files
(The father can read status through the wait primitive)
Wait
pid_t wait (int *status)
• It suspends the calling process until one of its children
terminates
• returns the PID of the terminated child (in case they are more
than one) or a -1 if there are no children
• assigns status the exit status of the the child
• waitpid (…) does the same for a specific child
Exit: example
father son
… …
int st; 0 exit (0)
…. ….
wait ( &st )
…
0
son’s process table entry
Exit
Ready queue
Kernel
Switcher
Process Table System calls Interrupt handlers
The process table entry of
U-Area U-Area the son cannot be removed
until the value of status is
Stack Stack read by father process
Dati Dati
Testo Testo
Father Son
Zombies and orphans
• a terminated process goes into a zombie state, and it
is finally removed after the father received his state of
termination through a wait
• a zombie process occupies a minimum set
• of resources (process table entry)
• an orphaned process (that is, whose father
• terminated) is "adopted" by the init process,
• therefore every process always has a father
Additional states of a process
exit
zombie
running
idle
scheduling waiting for event
fork time quantum
expired
asleep
ready
event occurred
INITIALIZATION
OF THE SYSTEM
BOOTSTRAP
Turning on the machine, the hardware launches the
bootstrap program ...
... that loads in memory the boot block from disk, which
contains a program ...
.... that loads in memory the kernel ...
... and then transfers control to an entry point (start),
that creates the process 0
bootstrap
System initialization
• initializes the kernel’s data structures
Start • creates process 1 (init): fork+exec)
Processo
Ø
• creates a process for each active terminaland network lines and
all daemons: in endless loop, is the father of all processes
fork fork fork
exec exec exec exec
LOGIN exec
/etc/init /etc/getty /bin/login cmd
SHELL
1
1
swapper
fork
exec
/etc/getty
exec
/bin/login
exec
LOGIN command
SHELL
fork
login shell
exec exec exec
LOGIN
/etc/getty /bin/login
SHELL
……. autenticates
fork
exec
daemons initializes terminal
kernel mode user mode
SHELL STRUCTURE
Shell: user interface
$file args foreground
<output>
$file args & background
$
Shell general structure
$file args<>
write (1, PROMPT); <output>
while ((n=read(0,buffer,80)=!0) { $
recognizes file name and args;
If ( (pid=fork()) ==0) { /* I/O redirection */ CHILD
if (exec(file, args)==-1) exit (1) }
procid=wait(status); FATHER
If (status !=0) write (2, error);
write(1,PROMPT)
} If <file> is excuted in background, does not execute
wait
INTER PROCESS
COMMUNICATION
Interprocess communication in Unix
Different mechanisms:
• Signal: new concept
• Pipe: not a new concept, is a “special” I/O
• Socket: not a new concept, is a “special” I/O
• Shared Memory between poricesses:
addressed as “special” I/O
Signals
A signal is a notification to a process that a particular event occurred:
⚫ A floating point error
⚫ The death of a child
⚫ A termination request
⚫ An alert by another process
The signals can be thought of as "software interrupts"
The signals can be sent:
⚫ from one process to another process
⚫ by a process to itself
⚫ from the kernel to a process
Each signal is identified by an integer number associated with a symbolic
name
Posix signals
Signal Value Action Comment
──────────────────────────────────────────────────────────────────────
SIGHUP 1 Term Hangup detected on controlling terminal or death of controlling process
SIGINT 2 Term Interrupt from keyboard
SIGQUIT 3 Core Quit from keyboard
SIGILL 4 Core Illegal Instruction
SIGABRT 6 Core Abort signal from abort(3)
SIGFPE 8 Core Floating-point exception
SIGKILL 9 Term Kill signal
SIGSEGV 11 Core Invalid memory reference
SIGPIPE 13 Term Broken pipe: write to pipe with no readers; see pipe(7)
SIGALRM 14 Term Timer signal from alarm(2)
SIGTERM 15 Term Termination signal
SIGUSR1 30,10,16 Term User-defined signal 1
SIGUSR2 31,12,17 Term User-defined signal 2
SIGCHLD 20,17,18 Ign Child stopped or terminated
SIGCONT 19,18,25 Cont Continue if stopped
SIGSTOP 17,19,23 Stop Stop process
SIGTSTP 18,20,24 Stop Stop typed at terminal
SIGTTIN 21,21,26 Stop Terminal input for background process
SIGTTOU 22,22,27 Stop Terminal output for background process
The signals SIGKILL and SIGSTOP cannot be caught, blocked, or ignored (masked)
System calls for signal management
kill (PID, sig #)
• sends the specified signal to the specified process
or process group
sigaction (sig #, action, ..)
• connects a signal to a function for treatment
• (Previously: signal() - not POSIX)
Signal effects
When a process receives a signal, it can:
⚫ manage the signal it using a specified
function ("handler")
⚫ ignore the signal
⚫ let activate the default action associated
with the signal (ends or sstops the
process, sometimes nop)
⚫ Signals can be masked
Example
• To terminate or suspend a foreground process, the
user can press the CTRL-C or CTRL-Z (respectively)
• The character is acquired by the terminal driver,
which notifies the SIGTSTP or SIGINT signal to the
process (respectively)
• By default, SIGINT terminates the process and
SIGTSTP suspends it
Note: These signals are sent to the whole group of
processes, if any
Protection
For security reasons, at least one of the
following conditions must hold:
⚫ The process that receives and the process
sending the signal have the same owner
⚫ The owner of the process sending the
signal is the superuser
Data structures for signal management
0
1
2
0 1 1 0 0 0 0
3
1 2 3 4 ..… 4
…
one bit per each signal is set Stack
by the “kill” Process Table
(only the last signal is
Data
remembered for each type))
Text
1 2 3 4 ..… Process
Pointer array to the U-Area
handlers defined by the
user for each signal
(null=default) Text Table