Chapter 4 - Processes Creating An Executable Program
Chapter 4 - Processes Creating An Executable Program
Process
Process a program in execution.
Related terms: Job, Step, Load Module, Task.
Process execution must progress in sequential
fashion.
A process is more than a program code - It includes
3 segments:
Program: code/text.
Data: program variables.
Stack: for procedure calls and parameter
passing.
4.1
Note:
A program is a passive entity whereas a
process is an active entity with a program
counter specifying what to do next and a set of
associated resources.
All multiprogramming OSs are build around the
concept of processes.
Process States
A process can be in one of many possible states:
new: The process is being created but has not
been admitted to the pool of executable
processes by the operating system.
running: Instructions are being executed.
waiting: The process is waiting for some event to
occur.
ready: The process is waiting to be assigned to a
processor.
terminated: The process has finished execution.
4.2
Process Transitions
As a process executes, it changes its state
4.3
o
o
o
o
o
4.4
TASK_RUNNING
TASK_INTERRUPTIBLE
TASK_UNINTERRUPTIBLE
TASK_ZOMBIE
TASK_STOPPED
TASK_EXCLUSIVE
4.5
4.6
4.7
Schedulers
A process may migrate between the various
queues.
The OS must select, for scheduling purposes,
processes from these queues.
Long-term scheduler (or job scheduler) selects
which processes should be brought into the ready
queue.
It is invoked very infrequently (seconds,
minutes) (may be slow).
It controls the degree of multiprogramming.
Short-term scheduler (or CPU scheduler) selects
which process should be executed next and
allocates CPU.
Short-term scheduler is invoked very frequently
(milliseconds) (must be fast).
Midterm scheduler selects which partially executed
job, which has been swapped out, should be
brought into ready queue.
4.8
Process Context
4.9
task switching
Refers to operating systems or operating environments that enable you to switch
from one program to another without losing your spot in the first program. Many
utilities are available that add task switching to DOS systems.
Note that task switching is not the same as multitasking. In multitasking, the CPU
switches back and forth quickly between programs, giving the appearance that all
programs are running simultaneously. In task switching, the CPU does not switch
back and forth, but executes only one program at a time. Task switching does allow
you to switch smoothly from one program to another.
Task switching is sometimes called context switching.
Process Switch
A process switch may occur whenever the OS has
gained control of CPU. i.e., when:
Supervisor Call
Explicit request by the program (ex: file
open). The process will probably be
blocked.
Trap
An error resulted from the last instruction. It
may cause the process to be moved to the
Exit state.
Interrupt
The cause is external to the execution of
the current instruction. Control is
transferred to Interrupt Handler.
4.10
4.11
Context Switching
When CPU switches to another process, the system
must save the state of the old process and load the
saved state for the new process - this is called
context switch.
The time it takes is dependent on hardware support.
Context-switch time is overhead; the system does
no useful work while switching.
4.12
Operations on Processes
OS should be able to create and delete processes
dynamically.
Process Creation
When the OS or a user process decides to create a
new process, it can proceed as follows:
Assign a new process identifier and add its
entry to the primary process table.
Allocate space for the process (program+data)
and user stack. The amount of space required
can set to default values depending on the
process type. If a user process spawns a new
process, the parent process can pass these
values to the OS.
Create process control block.
Prepared by Dr. Amjad Mahmood
4.13
4.14
Process Termination
A process terminates when it executes last
statement and asks the operating system to delete it
by using exit system call. At that time, the child
process
Output data from child to parent (via wait).
Process resources are deallocated by
operating system.
Parent may terminate execution of children
processes via appropriate system called (e.g.
abort). A parent may terminate the execution of one
of its children for the following reasons:
Child has exceeded allocated resources.
Task assigned to child is no longer required.
Parent is exiting.
Operating system does not allow child to continue if
its parent terminates.
Cascading termination.
4.15
A Linux Example
#include <stdio.h>
void ChildProcess();
void main()
{
int pid, cid, r;
pid = getpid();
r = fork(); //create a new process
if (r == 0) //r=0 -> its child
{
cid = getpid(); //get process ID
printf("I am the child with cid = %d of pid =
%d \n", cid, pid);
ChildProcess();
exit(0);
}
else
{
printf("Parent waiting for the child...\n");
wait(NULL);
printf("Child finished, parent quitting
too!");
}
}
void ChildProcess()
{
int i;
Cooperating Processes
Prepared by Dr. Amjad Mahmood
4.16
4.17
Shared data
#define BUFFER_SIZE 10
Typedef struct {
...
} item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
Producer Process
item nextProduced;
while (1) {
while (((in + 1) % BUFFER_SIZE) == out)
; /* do nothing */
buffer[in] = nextProduced;
in = (in + 1) % BUFFER_SIZE;
}
Consumer process
item nextConsumed;
while (1) {
while (in == out)
; /* do nothing */
nextConsumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
}
4.18
4.19
Threads
A thread, also called a lightweight process (LWP), is
the basic unit of CPU utilization.
It has its own program counter, a register set, and
stack space.
It shares with the pear threads its code section, data
section, and OS resources such as open files and
signals, collectively called a task.
4.20
4.21
4.22