Process characteristic
• Identifier: A unique identifier associated with
this process, to distinguish it from all other processes.
• State: If the process is currently executing, it is in
the running state.
• Priority: Priority level relative to other
processes.
• Program counter: The address of the next instruction
in the program to be executed.
• Memory pointers: Includes pointers to the program
code and data associated with this process, plus any memory blocks
shared with other processes.
• Context data: These are data that are present in
registers in the processor while the process is executing.
• I/O status information: Includes outstanding I/O requests,
I/O devices (e.g., disk drives) assigned to this process, a list
of files in use by the process, and so on.
• Accounting information: May include the amount of processor
time and clock time used, time limits, account numbers, and so
on.
Process Creation
-
Assign
a unique process identifier
-
Allocate
space for the process
-
Initialize
process control block
-
Set
up appropriate linkages
Ex: add new process to linked list used for scheduling queue
-
Create
of expand other data structures
Ex: maintain an accounting file
Process termination
Ø
Normal
completion
Ø
Time
limit exceeded
Ø
Memory
unavailable
Ø
Bounds
violation
Ø
Protection
error
-example write to read-only file
Ø
Arithmetic
error
Ø
Time
overrun
-process waited longer than a specified maximum for an event
Process termination
• I/O failure
• Invalid instruction
– happens when try to execute data
• Privileged instruction
• Data misuse
• Operating system intervention
– such as when deadlock occurs
• Parent terminates so child processes
terminate
• Parent request
Process Table
• Where process is located
• Attributes necessary for its
management
– Process ID
– Process state
– Location in memory
Process Location
• Process includes set of programs to
be executed
– Data locations for local and global
variables
– Any defined constants
– Stack
• Process control block
– Collection of attributes
• Process image
– Collection of program, data, stack,
and attributes
Process Control Block
• Process identification
– Identifiers
• Numeric identifiers that may be
stored with the process control block include
• Identifier of this process
• Identifier of the process that
created this process (parent process)
• User identifier
• Processor State Information
– User-Visible Registers
• A user-visible register is one that
may be referenced by means of the machine language that the processor executes.
Typically, there are from 8 to 32 of these registers, although some RISC
implementations have over 100.
• Processor State Information
– Control and Status Registers
These are a variety of processor registers that are employed to control
the operation of the processor. These include
• •Program counter: Contains
the address of the next instruction to be fetched
• •Condition codes: Result of
the most recent arithmetic or logical operation (e.g., sign, zero, carry,
equal, overflow)
•Status information:
Includes interrupt enabled/disabled flags, execution mode
•
Processor
State Information
–
Stack
Pointers
•
Each
process has one or more last-in-first-out (LIFO) system stacks associated with
it. A stack is used to store parameters and calling addresses for procedure and
system calls. The stack pointer points to the top of the stack.
•
Process
Control Information
–
Scheduling
and State Information
This is information that is needed by the operating system to perform
its scheduling function. Typical items of information:
•Process state: defines the readiness of the process to be
scheduled for execution (e.g., running, ready, waiting, halted).
•
•Priority:
One or more fields may be used to describe the scheduling priority of the
process. In some systems, several values are required (e.g., default, current,
highest-allowable)
•
•Scheduling-related
information: This will depend on the scheduling algorithm used. Examples
are the amount of time that the process has been waiting and the amount of time
that the process executed the last time it was running.
•Event: Identity of event the process is
awaiting before it can be resumed
Reason for Process switch
• Clock interrupt
– process has executed for the maximum
allowable time slice
• I/O interrupt
• Memory fault
– memory address is in virtual memory
so it must be brought into main memory
• Trap
– error occurred
– may cause process to be moved to
Exit state
• Supervisor call
– such as file open
Suspended Processes
• Processor is faster than I/O so all
processes could be waiting for I/O
• Swap these processes to disk to free
up more memory
• Blocked state becomes suspend state
when swapped to disk
• Two new states
– Blocked, suspend
– Ready, suspend
Cause of Process Suspension
• User mode
– Less-privileged mode
– User programs typically execute in
this mode
• System mode, control mode, or kernel
mode
– More-privileged mode
– Kernel of the operating system
Process Management System Call – fork()
• Creates a new process (child)
• Parent and children execute
concurrently
• Each
process can fork another process thus creating a process hierarchy
• A process can choose to wait for its
child to terminate
fork()
• Returns -1 if unsuccessful
• Returns 0 in the child
• Returns a [positive number, the
child’s identifier (child PID) in the parent
• When fork() is executed, it makes
two identical copies of the address space
• Both processes start execution
afterwards
• Therefore parent and child runs
independently
• The orders of execution of the
parent and child may be different on various platforms. The parent and
child processes are scheduled independently.
• One run on Ubuntu Linux, 1-2-3-4
(child then back to parent)
• Another run on Solaris, 1-3-4-2
(parent then child)
• In General, we never know whether
the child starts executing before the parent or vice versa. It
depends on the scheduling algorithm used by the kernel.
• The fork() is called once but
returns twice !
• Return value in the child is 0
• Return value in the parent is the
child's PID
• Both the child and parent continue
executing with the instruction that follows the call to fork(),
independently
• Current implementations do not
perform a complete copy of the parent's data, stack and heap; a technique
called copy on- write (COW) is used to make a copy of the piece
of memory modified by either process and the region of memory
shared by the parent and child are protected by the kernel to read-only
• A fork() is often followed by an
exec()
system()
• Executes a command from within a
program
• Much as if the command had been
typed into a shell
• Creates a subprocess running the
standard Bourne shell
• (/bin/sh) and hands the
command to that shell for
• execution; subject to the features,
limitations and security of
• the shell; on most GNU/Linux
systems, pointing to bash
system()
– example
#include
<stdlib.h>
int main ( )
{
int return_value ;
return_value =
system ( "ls -l /" );
return return_value;
}
exec()
family system calls
• Calling one of the exec() family
will terminate the currently running program and starts executing a new one
which is specified in the parameters of exec in the context of the existing
process.
• The process id is not changed.
• int execl( const char *path,
const char *arg, ...);
• int execv( const char *path,
char *const argv[]);
• int execle( const char *path, const
char *arg , ..., char * const envp[]);
• int execlp( const char *file,
const char *arg, ...);
• int execvp( const char *file,
char *const argv[]);
• execvp and execlp (with p)
• accept a program name and search for
a program by that name in the current execution path; functions that
don't contain the p must be given the full path of the program to be
executed
• execv, execvp, and execve (with v)
• accept the argument list for the new
program as a NULL-terminated array of pointers to strings • execl,
execlp, and execle (with l)
• accept the argument list using the C
language's varargs mechanism
• execve and execle (with e)
• accept an additional argument, an
array of environment variables. The argument should be a
NULL-terminated array of pointers to character strings. Each character
string should be of the form "VARIABLE=value".
execv()
• Executes a file, transforming the
calling process into a new process
• After successful execution, there is
no return to the calling process
fork()
and exec()
Other
system calls
• exit()
• Terminates the process normally
• Unblocks waiting parents
• wait()
• Used by parent
• Waits for child to finish execution
• getpid()
• Returns the identifier of the
calling process
• getppid()
• Returns the identifier of the parent
No comments:
Post a Comment