0% found this document useful (0 votes)
15 views

03 - Processes

The document discusses processes in operating systems. It defines what a process is and describes key attributes like program code, process ID, memory allocation, and states. It also explains process control blocks, context switching, process creation and termination, and interprocess communication.

Uploaded by

milton21207613
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

03 - Processes

The document discusses processes in operating systems. It defines what a process is and describes key attributes like program code, process ID, memory allocation, and states. It also explains process control blocks, context switching, process creation and termination, and interprocess communication.

Uploaded by

milton21207613
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

Processes

Lecture - 3

Soumyabrata DEV
https://soumyabrata.dev/
Processes

2
Process

• A process is a fundamental concept in operating systems. It


represents a program in execution, including its code, data, and the
state of its execution.

3
Process

The Key Attributes of a Process are as follows:

• Program Code: The instructions that the process is executing.


• Program Counter (PC): A register that keeps track of the address of
the next instruction to be executed.
• Stack Pointer (SP): Points to the top of the program's execution stack.
• Data: The process's variables and data.
• Registers: CPU registers storing intermediate values during execution.
• Process ID (PID): A unique identifier for each process.
• Execution State: Indicates whether the process is running, ready, or
waiting.
• Resources: Resources such as open files, memory space, and CPU
time allocated to the process.
4
Processes

• A process is a program in execution

• The operating system manages most things about processes

• it creates, deletes, suspends and resumes


processes
• it schedules & manages processes

5
Process or Program

• A program is a passive entity stored on a disk

• A program becomes a process when it is loaded into memory

• One program can be many processes

6
Multiple Processes

• Typical example: Microsoft Edge browser

• Each tab is a separate process

• They are all treated independently by the operating system

7
Process in Operating System

• Each process runs in its own address space (very important)

8
Process States

• As a process executes, it can be in a number of different states

• New: The process is being created

• 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

9
Process States

10
Process Control Block (PCB)

• The PCB is a data structure maintained by the


operating system for each process, containing
information about its state, registers, and resource
usage.

• It contains a wealth of information about the


process, which the operating system uses for
process scheduling, resource management, and
context switching.

11
Contents of a Process Control Block (PCB):
1. Process State:
• This field indicates the current state of the process, such as running,
ready, waiting, or terminated. It helps the operating system understand
what the process is currently doing and how it should be managed.
2. Process ID (PID):
• A unique identifier assigned to each process in the system. This allows
the operating system to distinguish between different processes.
3. Program Counter (PC):
• The address of the next instruction to be executed within the process's
program code. This is essential for resuming execution after a context
switch.

12
Contents of a Process Control Block (PCB):
4. Registers:
• PCB typically includes a set of CPU registers that store the process's context, including
general-purpose registers, program counter, stack pointer, and more. Saving and restoring
these registers is a crucial part of context switching.
5. Stack Pointer (SP):
• Points to the top of the process's execution stack, which is used for function calls and
storing local variables. The SP helps maintain the execution context.
6. Memory Management Information:
• Information about the memory allocated to the process, including the base and limit
registers. This helps the operating system protect processes from accessing each other's
memory areas.

13
Contents of a Process Control Block (PCB):
7. Open File Pointers:
• A list of files or I/O devices that the process has opened or is currently using. This
ensures proper resource management and access control
8. Priority and Scheduling Information:
• Information related to the process's priority in a scheduling algorithm, time
spent executing, and other scheduling-related data.
9. Accounting Information:
• Data on resource usage, such as CPU time consumed, file system usage, and
more. This information can be useful for performance analysis and billing.

14
Process Switching

• Using the information stored in the PCB the OS can


easily save and load the state of a process

• This allows processes to be easily switched; this is


called a context switch

15
Overhead in Context Switch

• While a context switch is happening, the system is


not doing any work

• The time taken is considered the overhead of the


operation

16
Overhead in Context Switch

• To minimise the amount of time wasted context


switches must be fast (hardware dependent)

• Also we don't want to switch too much (too much


overhead) or too little (not interactive enough)

17
Process Creation

•Processes are created by two main events:

• System boot

• Execution of process creation system call by another


process

18
Process Termination

•Processes are terminated in different


conditions:

• Voluntary: normal exit, error exit

• Involuntary: fatal error, killed by another process

19
Process Termination

• Voluntary termination can only happen from the


running state

• Involuntary termination can happen from any state

20
Child Processes

• A process can spawn (create) new processes

• The creator process is called the parent

• The created process is called the child

21
Process Tree

init
pid = 1

login kthreadd sshd


pid = 8415 pid = 2 pid = 3028

bash khelper pdflush sshd


pid = 8416 pid = 6 pid = 200 pid = 3610

emacs tcsch
ps
pid = 9204 pid = 4005
pid = 9298

22
Child Processes

• A hierarchical process structure is created in this way (any child


process only has one parent), called process tree
• In some OSs, stopping the parent stops all spawned children
• An orphan process is a computer process whose parent has stopped
but the process remains running.
• Normally these processes are adopted by another parent process
• If this does not happen , they become a zombie process
• They should then be removed or reaped from the system
• A failure to reap a zombie process is normally due to a bug in the
operating system.

23
Example (Unix)

• A child process is created using the fork() system call

• The child receives almost everything from its parent

• The parent address space & PCB are copied

• The child process must have a new PID, and will have
different pointers for its parent/child processes

• Because the PCB is copied the child process begins execution


after the fork() instruction
24
Example (Unix)

• The fork system call returns an integer value

• To the parent it will return the PID of the new child


process

• To the child it will return zero

25
Interprocess Communication (IPC)

• Processes within a system may be independent or


cooperating

• Cooperating process can affect or be affected by


other processes, including sharing data

26
Independent Processes

• Independent processes are those that can neither


affect nor be affected by the rest of the system

• Two independent processes cannot share system


state or data

•Example: processes running on different non-


networked computers

27
Properties of Independent Processes

• Deterministic behavior: only the input state


determines the results
• This means the results are reproducible

• Can be stopped and restarted without causing


problems

28
Cooperative Processes

• Cooperative processes are those that share


something (not necessarily for a purpose)

•Two processes are cooperative if the execution


of one of them may affect the execution of the
other

•Example: processes that share a single file


system
29
Properties of Cooperative Processes

• Nondeterministic behavior: many factors may


determine the result

•This means results may be difficult to


reproduce

•This makes testing and debugging very difficult

30
Properties of Cooperative Processes

• Cooperative processes are subject to race


conditions

• This means that the result of the process may


depend on the sequence or timing of events in
other processes

31
Why Allow Processes to Cooperate?

• Resources and Information sharing


• Multiple processes can share a single resource and
communicate

• Convenience
• We can do things like editing a file at the same time it is
being printed

32
Why Allow Processes to Cooperate?

• System speed-up, by introducing concurrency into


program execution

•We can overlap I/O with computations

33
Issues with Processes

• Processes are not very efficient:


• creation of a new process is costly
• all the process structures must be allocated upon creation

• Processes don’t (directly) share memory


• Each process runs in its own address space

• But parallel and concurrent processes often want to


manipulate the same data

• Most communications go through the OS: slow


34
Threads

Definition of Threads
• A thread is the smallest unit of execution within a process. Threads
share the same program code and data space but have their own
stack and registers.

Key Characteristics of Threads


• Lightweight: Threads are lighter in terms of memory and resource
overhead compared to processes because they share resources with
other threads in the same process.
• Concurrent Execution: Threads within a process can run concurrently,
allowing for parallelism and multitasking.

35
Motivation for Threads

• Consider a process that is running a file server

• Occasionally it will have to wait for the hard disk to


respond

• During this time the process will be blocked and


unable to respond to new requests

36
Motivation for Threads

• In order to speed up future operations, the process


will keep a cache of recent files in its memory

• A good idea would be to run a second concurrent


file server to work while the other waits

37
Motivation for Threads

• However it is not possible to efficiently achieve


concurrency by creating two independent processes

• They would have to run in the same address space


to efficiently share a common cache

38
Solution: Threads

• The idea is that there is more than one active entity


(thread of control) within a single process

39
File Server Example

• Considering our file server earlier


• The probability of a file being in cache is .6 and the
operation takes 15ms
• The probability of a file being on the disk is .4 and the
operation takes a further 75ms

• What is the maximum number of requests that can


be handled per second?

40
File Server Example

• Single Threaded: Average Time


• .6 x 15 + .4 x (15 + 75) = 45ms (=1000/45 = 22.22 requests/s)

• Multi-Threaded: additional threads can be started


when files are in disk without having to wait for the
I/O operation to complete
• The average time to handle a request is 15 ms (= 1000/15 =
66.67 requests/s)

41
Threads

•Modern OSs support both entities (process &


thread): multi-threaded OS

• Process: defines the address space and general


process attributes

• Thread: defines a single sequential execution


stream within a process

42
Concurrency in some existing OS

• MS-DOS: one address space, one thread

• Unix (originally): multiple address spaces, one thread per


address space

• OSX, Solaris, Windows 10: multiple address spaces, multiple


threads per address space (multi-threading)

43
Threads & Processes

• All threads belonging to a process share almost everything in the


process:
• Address space (code and data)
• Global variables
• Privileges
• Open files
• Timers
• Signals
• Etc.

44
Threads & Processes

• Threads however do not share:


• Register set, in particular:
• Program counter (PC)

• Stack pointer (SP)

• Interrupt vectors

• Stack

• State

• Child threads

45
Threads & Processes

• Threads do not exist on their own, they belong to processes


• There must always be at least one thread

• Threads are cheap to create (no need to allocate PCB, new address
space)

46
Thread Implementations

• Threads can be implemented in two basic ways


• Threads in user space (many-to-one model) .
• In the many to one model, many user-level threads are all mapped onto a single kernel thread.

• Threads in kernel space (kernel threads, one-to-one model) .


• The one-to-one model creates a separate kernel thread to handle each and every user thread.

• Additionally, it is possible to combine the two approaches in the Hybrid (many-to-


many) model .
• The many to many model multiplexes any number of user threads onto an equal or smaller
number of kernel threads, combining the best features of the one-to-one and many-to-one
models.

47
Threads in user space

• The kernel schedules processes (does not implement or know about


threads)

• per process thread table: process decides which of its threads to run
when it is running

48
Advantages of Threads in user space

• a single-threaded OS can emulate multi-threading


• thread scheduling controlled by run-time library, no system call
overheads
• portability: an OS independent user-space threads library is possible

49
Disadvantage of Threads in user space

• If a thread blocks, all other threads belonging to the same process


are blocked too

• This is because the OS only schedules processes

50
Threads in kernel space

• The kernel schedules threads

• In this case threads (and not processes) are the smallest units of
scheduling

• System-wide thread table (similar to a process table)

51
Advantages of Threads in kernel space

• Individual management of threads

• Better interactivity

52
Disadvantages of Threads in kernel space

• Scheduling and synchronisation operations always invoke the kernel,


which increases overheads

• Less portable

53
Threads in both levels

54
Thank you!

See you next class!


18 September, Monday, 9:55am to 11:30am, TB3-105

55

You might also like