Date post: | 02-Apr-2018 |
Category: |
Documents |
Upload: | jason-crawford |
View: | 216 times |
Download: | 0 times |
of 29
7/27/2019 Ch7 osd
1/29
Ch.7 Process Contro
S.N.P.I.T & R.C,UM
7/27/2019 Ch7 osd
2/29
PROCESS CONTROL Control of Process Context
fork: create a new process
exit : terminate process execution
wait : allow parent process to synchronize its execution witchild process
exec : invoke a new program
brk: allocate more memory dynamically
signal : inform asynchronous event
major loop of the shell andof init
fork
dupreg
attachregdetachregallocregattachreggrowregloadregmapreg
exec brk
growreg
exit
detachreg
wait signal kill se
System Calls Dealing
with Memory Management
System Calls Dealing
with Synchronization
7/27/2019 Ch7 osd
3/29
Process Creation
The only way for a user to create a new process in the unixsystem is to invoke the fork system call.
The process that invokes fork is called the parent process, newly created process is called child process.
The syntax for the fork system call is
Pid = fork();
On return from the fork system call, the two processes havecopies of their user-level context except for the return valu
In the parent process ,pid is the child process ID; in the chil,pid is 0.
The process 0,created internally by the kernel when the sysbooted, is the only process not created via fork.
7/27/2019 Ch7 osd
4/29
The kernel does the following sequence of operafork.
1. It allocates a slot in the process table for the new
2. It assigns a unique ID number to the child proces
3. It makes a logical copy of the context for the parprocess.
4. It increments file and inode table counters for file
associated with the process.5. It returns the ID number of the child to the parent
and a 0 value to the child process.
7/27/2019 Ch7 osd
5/29
Algorithm for forkinput : none
output : to parent process, child PID number
to child process, 0
{
check for available kernel resources;
get free process table slot, unique PID number;
check that user not running too many process;
mark child state "being created";
copy data from parent process table to new child slot;
increment counts on current directory inode and changed root(if applicable);
increment open file counts in file table;
make copy of parent context(u area, text, data, stack) in memory;
push dummy system level context layer onto child system level context;
dummy context contains data allowing child process
to recognize itself, and start from here
when scheduled;
if ( executing process is parent )
{
change child state to "ready to run";
return(child ID); /* from system to user */
}
else /* executing process is the child process */
{
initialize u area timing fields;
return(0); /* to user */
}
7/27/2019 Ch7 osd
6/29
Fork Creating a New Process Co
Parent
DataPer Process
Region Table Open FilesCurrent Directory
Changed Root.....Kernel Stack
U Area
Per Process
Region Table Open FilesCurrent Directory
Changed Root.....Kernel Stack
U Area
Parent Process
Child Process
Parent
User
Stack
Shared
Text
Child
Data
Child
User
Stack
7/27/2019 Ch7 osd
7/29
Example of Sharing File Ac
#include
int fdrd, fdwt;
char c;
main( argc,argv )
int argc;
char *argv[];
{
if ( argc != 3 )
exit(1);
if ((fdrd=open(argv[1],O_RDONLY))==-1)
exit(1);
if ((fdwt=creat(argv[2],0666))==-1)
exit(1);
fork();
/*both process execute same code*/
rdwt();
exit(0);
}
rdwt(){
for(;;){if (read(fdrd,&c,1)!=-1)
return;write(fdwt,&c,1);
}}
7/27/2019 Ch7 osd
8/29
SIGNALS Signals inform processes of the occurrence asynchrono
Processes may send each signals with the kill system cmay send signals internally.
1. Signal having to do with the termination of a process, sprocess exits or when a process invokes the signal systdeath of child parameter.
2. Signals having to do with process induced exception suprocess accesses an address outside its virtual addressattempts to write memory that is read-only ,or when it eprivileged instruction or for various hardware errors.
3. Signal having to do with the unrecoverable conditions call , such as running out of system resources during exoriginal address space has been released.
4. Signal caused by an unexpected error condition duringsuch as making a nonexistent system call writing pipe reader processes , or using an illegal reference value
system call.
7/27/2019 Ch7 osd
9/29
5. Signal originating from a process in user mode, such as process wishes to receive an alarm signal after a periodwhen process send arbitrary signals to each other with tcall.
6. Signals related to terminal interaction such as when a ua terminal ,or when a user process the break or deletterminal keyboard.
7. Signals for tracing execution of a process.
7/27/2019 Ch7 osd
10/29
7/27/2019 Ch7 osd
11/29
Handling signals There are three cases for handling signals:
1. The process exist on receipt of the signal.
2. It ignores the signals.
3. It executes a particular function on receipt of the signal.
The default action is to call exit in kernel mode but a process cspecial action to take on receipt of certain signals with the sig
The syntax for the signal system call is
Oldfunction = system (signum , function); where signum = signal number the process is specifying the ac
Function= address of the function the process wants to invokethe signal.
Oldfunction = value of function in the most recently specified signum.
7/27/2019 Ch7 osd
12/29
The process can pass the value 1 or 0 instead
address :
The process will ignore future occurrences of t
the parameter value is 1 and exit in the kerneof the signal if its value is 1 .
And exit in the kernel on receipt of the signal
0.
The u area contains an array of signal-handle
for each signal defined in the system .
The kernel stores the address of the user funct
field that corresponds to the signal number.
Specification to handle signals of one type ha
on handling signals of other types.
7/27/2019 Ch7 osd
13/29
7/27/2019 Ch7 osd
14/29
Process Groups Processes on a Unix system are identified by a unique ID nu
must sometimes identify processes by group.
For instance ,processes with a common ancestor process th
are generally related, and therefore all such processes recea user hits the delete or break key or when the terminal
The setpgrp system call initializes the process group numberand sets it equal to the value of its process ID.
The syntax for the system call is
grp = setgrp()
Where , grp = new process group number
A child retain the process group number of its parent during
Setpgrp also has important ramification for setting up the coa process.
7/27/2019 Ch7 osd
15/29
Sending signal from processes Processes use the kill system call to send signals..
The syntax for the system call is
kill (pid,signum)
Where pid = set of processes to receive the signal
Signum = signal number being sent.
The following list shows the correspondence between values oprocesses.
1. If pid is a positive integer ,the kernel sends the signal to the pr
Id pid.
2. If pid is 0, the kernel sends the signal to all processes in the segroup.
3. If pid is -1 the kernel sends the signal to all processes whose rthe effective user ID of the sender.
4. If pid is a negative integer but not -1 the kernel sends the signin the process group equal to the absolute value of pid.
7/27/2019 Ch7 osd
16/29
Process Termination Processes on a UNIX system terminate by executing
system call.
An exiting process enters the zombie state ,relinquisresources and dismantles its context except for its slprocess table.
The syntax for call is
Exit (status);
Where the value of status is returned to the parent foexamination .
a gor m ex
7/27/2019 Ch7 osd
17/29
a gor m exinput : return code for parent process
output : none
{
ignore all signals;
if ( process group leader with associated control termina
{
send hangup signal to all members of process group;
reset process group for all members to 0;
}
close all open files(internal version of algorithm close)
release current directory(algorithm iput);
release current(changed) root, if exists (algorithm iput);
free regions, memory associated with process(algorithmwrite accounting record;
make process state zombie;
assign parent process ID of all child processes to be init
if any children were zombie, send death of child signal to
send death of child signal to parent process;
context switch;
}
7/27/2019 Ch7 osd
18/29
Awaiting Process Termination
wait system call
synchronize its execution with the terminatio
process
pid = wait(stat_addr);
pid : process id of the zombie child process
stat_addr : address of an integer to contain the exi
child
7/27/2019 Ch7 osd
19/29
Algorithm for Awaiting Process Term
1. searches the zombie child process
2. If no children, return error
3. if finds zombie children, extracts PID number and ex
4. adds accumulated time the child process executeuser and kernel mode to the fields in u area
5. Release process table slot
Al ith f W it
7/27/2019 Ch7 osd
20/29
Algorithm for Waitalgorithm wait
input : address of variables to store status of exiting process
output : child ID, child exit code
{
if (waiting process has no child process)
return(error);
for(;;)
{
if (waiting process has zombie child)
{
pick arbitrary zombie child;
add child CPU usage to parent;
free child process table entry;
return(childID,child exit code);
}
if (process has no child process)
return(error);
sleep at interruptible priority(event child process exits);
}
7/27/2019 Ch7 osd
21/29
Invoking Other Programs exec system call
invokes another program, overlaying the memory spacprocess with a copy of an executable file
execve(filename, argv, envp)
filename : the name of executable file being invoked
argv : a pointer to an array of character pointers that arethe executable program
envp : a pointer array of character pointers that are envirexecuted program
several library functions that calls exec system call
execl, execv, execle...
Algorithm for Exec
7/27/2019 Ch7 osd
22/29
Algorithm for Execalgorithm exec
input : (1) file name
(2) parameter list
(3) environment variables list
output : none
{
get file inode(algorithm namei)
verify file executable, user has permission toexecute;
read file headers, check that it is a load module;
copy exec parameters from old address space tosystem space;
for(every region attached to process)
detach all old regions(algorithm detach);
for(every region specified in load module)
{
allocate new regions(algorithm allocreg);
attach the regions(algorithm attachreg);
load region into memory if appropriate(algorithmloadreg);
}
copy exec parameters into nspecial processing for setuiinitialize user register save arelease inode of file(iput);}
7/27/2019 Ch7 osd
23/29
7/27/2019 Ch7 osd
24/29
7/27/2019 Ch7 osd
25/29
THE USER ID OF A PROCESS
7/27/2019 Ch7 osd
26/29
THE USER ID OF A PROCESS
The kernel associates two user IDs with a process, independenID the real user ID and the effective user ID or setuid (set user I
The real user ID identifies the user who is responsible for the run
The effective user ID is used to assign ownership of newly creatcheck file access permissions, and to check permission to senprocesses via the kill system call.
The kernel allows a process to change its effective user ID whesetuid program or when it invokes the setuid system call explic
The syntax for the setuid system call is
Setuid (uid)
Where uid = new user ID
New user ID ,its result depends on the current value of the effec
If the effective user ID of the calling process is super user the kreal and effective user ID fields in the process table and u area
CHANGING THE SIZE OF A PROCESS
7/27/2019 Ch7 osd
27/29
CHANGING THE SIZE OF A PROCESS
A process may increase or decrease the size of its data region bsystem call.
The syntax for the brk system call is brk(ends);
where ends becomes the value of the highest virtual address of t
of the process (called its break value)
Alternatively a user can call
Oldendds = sbrk (increment);
Where increment changes the current break value by the specifie
bytes and oldendds is the break value before the call.
Sbrk is a C library routine that call brk.
7/27/2019 Ch7 osd
28/29
7/27/2019 Ch7 osd
29/29
THE SHELL