CPS 346 & 444/544 Lecture notes: Processes
(identification, creation, & termination)
Coverage: [UPE] §7.4 (pp. 220-225), and [USP] Chapter
2 (pp. 21-26, 51-53) and §§3.1-3.3 (pp. 59-70)
Overview
- processes (state, creation, termination)
- (logical) layout of the program image
- memory allocation/deallocation
(malloc family of memory allocation functions)
Process
- a process is a program in execution
- executable module of program resides in a program image in main memory
- each process id (do a ps in UNIX) and a process state (ready,
running, blocked, and so on)
Logical layout of program image
(modified version of
[USP] Fig. 2.1, p. 24; image courtesy Robbins & Robbins'
[USP] Chapter
2
webpage)
Activation record
(modified version of [USP] Fig. 1.1, p. 16)
A stack of these activation records at run-time is called the
execution stack
Process control block
For the duration of its existence, each process is represented
by a process control block (PCB) which contains information such as:
- state of the process (ready, running, blocked)
- register values
- priorities and scheduling information
- memory management information
- accounting information such as open files
- allocated resources
Specifics of a PCB depend on the system and techniques used.
(ref. [OSIDP] Fig. 3.1 on p. 110; image courtesy [OSIDP] webpage)
Process life cycle
A process moves through various states during its life:
- new,
- ready,
- running,
- blocked, and
- done states, and
- transitions between them
(regenerated from [USP] Fig. 3.1, p. 62)
Many processes may be ready or waiting at the same time,
but only one can be
running at a time in a uniprocessor system.
Process and process lifecycle nomenclature
- batch process: a process which is not connected to a terminal, or
a non-interactive process
- resident monitor: first software support for an
operating system; transferred CPU control from
process to process; provided automatic process sequencing
- multiprogramming:
multiple processes are kept in main memory and contend
for the CPU with the goal of reducing the time the processor is idle
- timesharing (or preemptive multi-tasking):
an extension of multiprogramming
in which
the CPU is switched between processes very quickly; a small amount of CPU time
is given to each process (called a time slice or quantum);
gives rise to interactive systems and enables
communication between a user and a process
- non-preemptive multi-tasking = multiprogramming without timesharing
-
job scheduling:
policy by which processes are loaded into main memory
or, in other words, the ready queue
- ready queue: contains all processes in main memory
ready to execute; a process is
dispatched from the ready queue to the CPU (called
process scheduling); its processing may cause it to put
on a device queue
-
process scheduling (or CPU scheduling):
policy by which processes are granted access to the CPU
- context switch: each process change requires a context switch
which is
purely overhead (i.e., no useful work is being accomplished); switching
the CPU to another process requires i) saving the state of the old process
and ii) loading the state for the new process
- context switch time:
time required to perform a context switch
- system call: call to a core OS service, such as a read or write,
and typically triggers a context switch;
`a system call is a request to the operating system for a service' ([USP]
p. 119)
or
`a system call is a request to the operating system for service that
causes the normal CPU cycle to be interrupted and control to be given
to the operating system' ([USP] p. 7)
a system call, unlike a library call, often involves a context-switch; library
calls are usually jackets for system calls (see [USP] exercise 4.19, p. 119 for
more information).
- interrupt (hardware): a hardware notification of an event, such as
completion of I/O (e.g., read or write),
error condition (e.g., divide by zero),
request for an OS service (e.g., in response to a system call), typically
generated by hardware; once loaded, an OS waits for an event to occur;
events are signaled by interrupts; therefore, an OS is event-driven (or
interrupt-driven) system
- interrupt service routine:
when an interrupt is generated, control is
transferred to an interrupt service routine, which deals with the particular
situation; after the interrupt is processed, control is
returned to the interrupted process
- (asynchronous or synchronous) signal (software):
a software notification of an event
- asynchronous event: any event which can occur at any time;
interrupts and signals are asynchronous events in that
there is no way to predict when they will occur
- synchronous event
- device driver: a program which identifies with and deals
with a device
(usually sends a signal in response to an interrupt)
UNIX is a multiprogramming and timeshared OS.
System calls cause context switches
(ref. [OSIDP] Fig. 1.5 on p. 16; image courtesy [OSIDP] webpage)
Process scheduling
- allocating the CPU to a different process to reduce idle time
- each process change requires a context switch
- a context switch is pure overhead
(i.e., involves no useful work)
Process identification
- getuid, getgid, geteuid, getegid
- process id, parent process id
- getpid and getppid
- real vs. effective user and group ids
- cast results of these functions to long
- output of ps command provides many of these details,
including process state; experiment with
the -a, -A, -l, and -o options
- top
Process creation
- an initial OS process begins running at boot time, which spawns (creates)
other processes
- a process hierarchy (tree) evolves with parent-child relationships
fork
- system call used for process creation in UNIX
- caller becomes parent and newly created process is child
- processes are the same except for the process id
- child inherits many attributes of its parent
(e.g., environment, privileges, open files and devices)
- returns 0 to the child and the child pid to the parent
- execution proceeds after the call to fork
in each process image
- parent code and child code
- effect of concurrency
- sleep function, takes seconds to block as an int
(e.g., sleep(30))
Graphical depiction of fork
(ref. [ATT] 6-11)
Process termination
- returned to OS (in shell variable $? in UNIX system)
- why 0 for success?
- does not seem to follow C boolean convention
- many ways to fail;
see bottom of manpage for meanings
of particular exit status
- echo is the UNIX print command
- for instance, echo hello world, echo $?
- exit vs. return
- in main they are the same
- outside of main
- exit can be used to exit the program
(from any function)
- return will transfer control to the caller
- absence of return or exit in main
has the same effect as exit(0)
- to install an exit handler use atexit
- how can a process terminate abnormally? external event (e.g., a
<ctrl-c> (an asynchronous event))
or an internal error (e.g., illegal memory access (a synchronous event))
NULL pointer
- it is illegal to read or write to address 0
- a null pointer
- any pointer with value 0
- returned by pointer-returning functions to signify failure
- NULL is defined in stdio.h as
- #define NULL 0, or
- #define NULL (void*) 0 in the new ANSI standard
- not to be confused with the null character \0
Memory allocation/deallocation
malloc, calloc, realloc, and free
- a general-purpose memory allocation package
- prototyped in stdlib.h
- malloc family allocates storage from the heap
- malloc returns a pointer to a block of at least size bytes
suitably aligned for any use
- malloc returns a pointer of type void*;
cast to appropriate type
- to declare and array of 10 ints:
int* int_ptr = (int*) malloc (sizeof(int)*10);
- PCB* process_str_ptr = (PCB*) malloc (sizeof(PCB));
- the argument to free is a pointer to a block previously
allocated by malloc
- after free is executed, this space is made available for further
allocation by the application, though not returned to the system;
memory is returned to the system only upon termination
of the process (there is more to the story than this)
- when passed NULL, recalloc acts like malloc
- when passed a non-NULL pointer and a size of 0,
recalloc acts like free
References
| [C] |
C Language for Experienced Programmers, Version 2.0.0, AT&T, 1988. |
| [CPL] |
B.W. Kernighan and D.M. Ritchie. The C Programming Language.
Prentice Hall, Upper Saddle River, NJ, Second edition, 1988. |
| [OSIDP] |
W. Stallings.
Operating Systems: Internals and Design Principles.
Prentice Hall, Upper Saddle River, NJ, Sixth edition, 2009.
|
| [UPE] |
B.W. Kernighan and R. Pike. The UNIX Programming Environment.
Prentice Hall, Upper Saddle River, NJ, Second edition, 1984.
|
| [USP] |
K.A. Robbins and S. Robbins.
UNIX Systems Programming: Concurrency, Communication, and Threads.
Prentice Hall, Upper Saddle River, NJ, Second edition, 2003
|
|