Chapter 2: Processes
â—ŹProcess Concept
â—Ź Process Scheduling
â—Ź Operations on Processes
â—Ź Interprocess Communication
â—Ź Examples of IPC Systems
â—Ź Communication in Client-Server Systems
4.
Process Concept
â—Ź Anoperating system executes a variety of programs:
● Batch system – jobs
● Time-shared systems – user programs or tasks
● Process – a program in execution; process execution must
progress in sequential fashion
â—Ź Multiple parts
â—Ź The program code, also called text section
â—Ź Current activity including program counter, processor
registers
â—Ź Stack containing temporary data
Function parameters, return addresses, local variables
â—Ź Data section containing global variables
â—Ź Heap containing memory dynamically allocated during run
time
5.
Process Concept (Cont.)
â—ŹProgram is passive entity stored on disk (executable file),
process is active
â—Ź Program becomes process when executable file loaded
into memory
â—Ź Execution of program started via GUI mouse clicks, command
line entry of its name, etc
â—Ź One program can be several processes
â—Ź Consider multiple users executing the same program
Process State
â—Ź Text:code segment contain executable
instructions.
â—Ź Data: contain global variable.
â—Ź Heap: segment of memory where dynamic
memory takes place.
â—Ź Stack: temporary data, local variables.
8.
Process State
â—Ź Asa process executes, it changes state
â—Ź new: The process is being created.
â—Ź ready: process has to be created and it is not
yet running.
â—Ź 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
Process Control Block(PCB)
Information associated with each process (also called task control
block)
● Process state – running, waiting, etc
• Process ID/Number: unique ID of that process
● Program counter – Address of next line of instruction to be
execute
● CPU registers – registers being used for particular process.ie
index register, stack pointers, general purpose register
â—Ź CPU scheduling information- priorities, scheduling queue
pointers
● Memory-management information – memory allocated to the
process
● Accounting information – resources used for particular process
[CPU ,time, Memory]
● I/O status information – I/O devices allocated to process, list of
open files
Context Switch
â—ŹWhen aninterrupt occurs, the system
needs to save the current context of
process currently running on the CPU
so that it can restore that context
when its processing is done,
suspending the process and then
resuming it.
â—ŹThe context is represented in the PCB
of the process.
14.
Context Switch
â—Ź WhenCPU switches to another
process, the system must save the
state of the old process and load the
saved state for the new process via a
context switch
â—Ź Context of a process represented in
the PCB
15.
Operations on Processes
â—ŹSystem must provide mechanisms for:
â—Ź process creation,
â—Ź process termination,
16.
process creation
• Aprocess is created when a **program is executed**.
The OS must set up the environment needed for the
process to run.
• Steps in Process Creation:
1. Assign a unique Process ID (PID)to the new process.
2. Allocate memory for process code, data, and stack.
3. Create process control block (PCB): includes PID, state,
CPU registers, memory limits, etc.
4. Set initial process state(usually “Ready”).
5. Add to scheduling queue to wait for CPU time.
17.
process creation
â—Ź Aprocess may create several new processes via create
system call during the course of execution.
â—Ź The creating process is called a parent process and new
processes are called the children of that process.
â—Ź Each of these new processes may in turn create other
processes forming a tree of processes.
â—Ź When a process creates a new process two possibilities
exits in terms of execution
• The parent continues to execute concurrently with its
children.
• The parents waits until some or all of its children have
terminated.
Process Creation
â—Ź Parentprocess create children processes, which,
in turn create other processes, forming a tree of
processes
â—Ź Generally, process identified and managed via a
process identifier (pid)
â—Ź Resource sharing options
â—Ź Parent and children share all resources
● Children share subset of parent’s resources
â—Ź Execution options
â—Ź Parent and children execute concurrently
â—Ź Parent waits until children terminate
20.
Process Creation (Cont.)
â—ŹAddress space of new process
â—Ź Child duplicate of parent
â—Ź Child has a program loaded into it
â—Ź UNIX examples
â—Ź fork() system call creates new process
â—Ź exec() system call used after a fork() to replace the
process’ memory space with a new program
#include <sys/types.h>
#include <stdio.h>
#include<unistd.h>
#include <sys/wait.h> // Required for wait()
int main() {
pid_t pid;
// fork a child process
pid = fork();
if (pid < 0) {
// error occurred
fprintf(stderr, "Fork Failed");
return 1;
}
else if (pid == 0) {
// child process
execlp("/bin/ls", "ls", NULL);
}
else {
// parent process
// wait for the child to complete
wait(NULL);
23.
Process Termination
• Aprocess is terminated when it completes execution
or is **forcefully stopped**.
• Reasons for Process Termination:
• Normal completion: Process finishes its task (e.g.,
program ends).
• Error termination: Due to divide-by-zero, invalid
memory access, etc.
• Killed by user or OS: Using commands like kill or
due to resource overuse.
• Parent termination: In some OS es, child processes
terminate if the parent ends.
24.
Process Termination
• Stepsin Process Termination:
1. Release resources (CPU, memory, I/O).
2. Remove process from all queues.
3. Delete its PCB.
4. Update process table to mark it as
terminated.
25.
Process Termination
â—Ź Aprocess when executing its final statement and
asks the operating system to delete it by using the
exit() system call.
â—Ź At that point the process may return a status value
to its parents process(via the wait ()system call)
â—Ź All the resources are deallocated by the operating
system.
26.
Process Termination
â—Ź Processexecutes last statement and then asks the operating
system to delete it using the exit() system call.
â—Ź Returns status data from child to parent (via wait())
● Process’ resources are deallocated by operating
system
â—Ź Parent may terminate the execution of children processes using
the abort() system call. Some reasons for doing so:
â—Ź Child has exceeded allocated resources
â—Ź Task assigned to child is no longer required
â—Ź The parent is exiting and the operating systems
does not allow a child to continue if its parent
terminates
27.
Process Termination
â—Ź Someoperating systems do not allow child to exists if its parent
has terminated. If a process terminates, then all its children
must also be terminated.
â—Ź cascading termination. All children, grandchildren, etc.
are terminated.
â—Ź The termination is initiated by the operating system.
â—Ź The parent process may wait for termination of a child process
by using the wait()system call. The call returns status
information and the pid of the terminated process
pid = wait(&status);
â—Ź If no parent waiting (did not invoke wait()) process is a
zombie
â—Ź If parent terminated without invoking wait , process is an
orphan