You are on page 1of 43

Operating Systems 1

CS 241
Spring 2021

By
Marwa M. A. Elfattah

Main Reference
Operating System Concepts, Abraham Silbrschatz,
10th Edition
Processes
Process Concept
▪ Process is a program in execution.
Program is passive entity stored on disk (executable file);
process is active
Program becomes process when an executable file is
loaded into memory

Program Process
Process Concept
▪ One program can be
several processes
The same user may
invoke many copies of
an application. Each of
these is a separate
process.
Although the text
sections are equivalent,
the data, heap, and
stack sections vary.
▪ A process can itself
be an execution
environment for other
code, as Java virtual
machine
Process in Memory
▪ The executable code - text section
▪ Data section - global variables
Process in Memory
▪ Stack - temporary data
▪ Function parameters, return addresses, local
variables
Process in Memory
▪ Heap - memory dynamically allocated
during run time
Process in Memory
▪ Text and data sections have fixed size
Process in Memory
▪ The stack and heap sections can shrink
and grow dynamically during program
execution - never overlap
Process in Memory
▪ The stack and heap sections can shrink
and grow dynamically during program
execution - never overlap
Process Control Block (PCB)
Each process is represented in the operating
system by a data structure called PCB(also
called task control block)
▪ Process state – running, waiting, etc.
▪ Process number (PID)
▪ Program counter – location of instruction
to next execute
▪ CPU registers
 Alongwith the program counter, this state
information must be saved when an interrupt
occurs
▪ CPU scheduling information- priorities,
scheduling queue pointers
▪ ...
Start Running A New User Process
Kernel must:
▪ Allocate and initialize the process control block.
▪ Allocate memory for the process.
▪ Copy the program from disk into the newly allocated
memory.
▪ Allocate a user-level stack for user-level execution.
▪ Allocate a kernel-level stack for handling system calls,
interrupts and processor exceptions.
▪ Copy arguments into user memory.
• For example, when you click on a file icon, the kernel is asked to
start the application associated with the file, passing it the file name
to open.
▪ Transfer control to user mode.
Multiprogramming
▪ As a single process cannot, in general, keep either
the CPU or the I/O devices busy at all times,
An I/O-bound process: spends more of its time doing I/O
than doing computations.
A CPU-bound process: uses more of its time doing
computations.
▪ One of the most important aspects of an OS is the
ability to run multiple process - multiprogramming,
To increase CPU utilization
Also to keep users satisfied
Multiprogramming
▪ The Idea is:
The operating system keeps several processes in memory
simultaneously.
– The number of processes currently in memory is known
as the degree of multiprogramming.
The operating system picks and begins to execute one of
these processes.
Eventually, the process may have to wait for some task,
such as an I/O operation, to complete.
– In a non-multiprogrammed system, the CPU would sit
idle.
– In a multiprogrammed system, the operating system
simply switches to, and executes, another process.
Multitasking
▪ Multitasking is a logical extension of
multiprogramming.
▪ In multitasking systems, the CPU executes multiple
processes by switching among them, but the
switches occur frequently,
• providing the user with a fast response time.
Process State
▪ As a process executes, it changes state
• New: The process is being created
• Ready: The process is waiting to be assigned to
a processor
• Running: Instructions are being executed
Process State
▪ As a process executes, it changes state
• Waiting: The process is waiting for some event
to occur
• Terminated: The process has finished execution
Process State
▪ Only one process can be running on any processor
core at any instant.
▪ Many processes may be ready and waiting
Process Scheduling
▪ Process scheduler selects among available
processes for next execution on CPU core
▪ Goal:
• Maximize CPU use,
• Quickly switch processes onto CPU core
▪ It maintains scheduling queues of processes
• Ready queue – set of all processes residing in
main memory, ready and waiting to execute
• Wait queues – set of processes waiting for an
event (i.e., I/O)
Ready and Wait Queues
Process Scheduling
▪ A new process is initially put in the ready queue. It
• Waits until it is selected for execution, or
dispatched.
Process Scheduling
▪ Once the process is allocated a CPU core and is
executing, one of several events could causing
processes migration among the various queues
Process Scheduling
▪ A process continues this cycle until it terminates,
• It is removed from all queues
• Its PCB and resources are deallocated.
Context Switch
▪ Switching the CPU core to another process is
known context switch
▪ Context of a process represented in the PCB
▪ 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.

Kernel

User
Context Switch
Context Switch
▪ Context-switch time is pure overhead; the system
does no useful work while switching
• The more complex the OS and the PCB ➔ the longer the
context switch
• Also speed depends on memory speed, the number of
registers that must be copied,…
▪ Time dependent on hardware support
• Some hardware provides multiple sets of registers per
CPU ➔ multiple contexts loaded at once
• A context switch here simply requires changing the
pointer to the current register set.
Operations on Processes

▪ Process creation
▪ Process termination
Process Creation
▪ Why create a new process?
• May be ➔ program wants to run an additional instance of
itself.
E.g., web server receives request; creates additional
instance of itself to handle the request; original instance
continues listening for requests
• May be ➔ program wants to run a different program.
E.g., shell receives a command; creates an additional
instance of itself; additional instance overwrites itself
with requested program to handle command; original
instance continues listening for commands
Process Creation
▪ Generally, process identified and managed via a
process identifier (pid)
A unique identifier, which is typically an integer number.
Used as an index to access various attributes of a process
within the kernel.
▪ During the course of execution, a process may
create several new processes.
Parent process create children processes, which, in turn
create other processes, forming a tree of processes.
A parent process may have multiple child processes but a
child process only one parent process.
Parent and children execute concurrently, or parent waits
until children terminate.
EX: A Tree of Processes in Linux

▪ The systemd process (which always has a pid of 1) serves


as the root parent process for all user processes, and is the
first user process created when the system boots.
▪ Once the system has booted, the systemd process creates
processes which provide additional services
Process Creation
▪ The child process will need certain resources (CPU
time, memory, files, …) to accomplish its task.
• It may be able to obtain its resources directly from
the operating system,
• Or, it may be constrained to a subset of the
resources of the parent process.
The parent may have to partition its resources among its
children, or it may be able to share some resources (such
as memory or files) among several of its children.
• Restricting a child process to a subset of the parent’s
resources prevents any process from overloading the
system by creating too many child processes.
Process Termination
▪ Process executes last statement and then asks the
OS to delete it using the exit() system call.
• Returns status data from child to parent (via
wait())
Exit normally, upnormally, returned with value…
• Process’ resources are deallocated by operating
system
▪ Some operating 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).
Process Termination
▪ Parent may terminate the execution of its children.
• Some reasons for doing so:
Child has exceeded allocated resources
Task assigned to child is no longer required
• Such a system call can be invoked only by the
parent of the process that is to be terminated.
Otherwise, a user—or a misbehaving application—
could arbitrarily kill another user’s processes.
• Parent needs to know the identities of its children
if it is to terminate them.
When one process creates a new process, the identity
of the newly created process is passed to the parent.
Process Creation - Unix Examples
▪ fork() system call creates new process
pid_t fork(void);
▪ New (child) process is an exact duplicate of the
calling (parent) process
▪ Parent process and child process run concurrently
• If one CPU available ➔ OS provides the illusion
of parallel execution
▪ fork() is called once in parent process and returns
twice
• Once in parent process ➔ return the process id
of the child
• Once in child process ➔ In the child, return 0
Process Creation - Unix Examples
Simple fork Example
int main(void) {
printf("one\n");
fork();
printf("two\n");
return 0;
} What is the
Output?
Process Creation - Unix Examples
Simple fork Example
int main(void) {
printf("one\n");
Parent prints “one ”
fork(); Then
printf("two\n"); Parent forks a child
return 0; that duplicates the
parent
}
Parent and child
have identical but
distinct process
Process Creation - Unix Examples
Simple fork Example
int main(void) { int main(void) {
printf("one\n"); printf("one\n");
fork(); fork();
printf("two\n"); printf("two\n");
return 0; return 0; one
} } two
two
Parent and child processes execute concurrently
Each will print “two”
Process Creation - Unix Examples
Another fork Example
int main(void) {
pid_t pid; int x = 1;
pid = fork();
if (pid == 0) { /* child process */
x--; printf(”child: %d\n", x);}
else { /* parent process */
x++; printf(”parent: %d\n", x);}
return 0;
} Parent: 2 Child: 0
OR
Child: 0 Parent: 2
Process Creation - Unix Examples
▪ Parent process calls wait()waiting for the child to
terminate
int main(void) {
pid_t pid; int x = 1;
pid = fork();
if (pid == 0) { /* child process */ Child: 0
x--; printf(”child: %d\n", x);} Parent: 2

else { /* parent process */


wait (NULL);
x++; printf(”parent: %d\n", x);}
return 0;}
Process Creation - Unix Examples
▪ After a fork() system call, one of the two processes
typically can uses the exec() system call to replace
the process’s memory space with a new program.
int main(){
pid t pid;
pid = fork(); /* fork a child process */
if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
return 1;
}
...
Process Creation - Unix Examples
...
else if (pid == 0){ /* child process */
execlp("/bin/ls","ls",NULL);
}
else { /* parent process */
wait (NULL);
printf("Child Complete");
}
return 0;
}
Process Creation - Unix Examples
Thank You

You might also like