Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 49

Chapter- 2

Process Management
2.1 - Processes and Threads

By Kindu T.

Processes and Threads


Processes
• Process Concept
• Process Scheduling
• Operation on Processes
• Inter process Communication

Processes and Threads


Process Concept
• Process – a program in execution.
• process execution must progress in sequential fashion.
• To put it in simple terms, we write our computer programs in a text file and when we
execute this program, it becomes a process which performs all the tasks mentioned in the
program.
• When a program is loaded into the memory and it becomes a process, it can be divided
into four sections ─ stack, heap, text and data.
• The following image shows a simplified layout of a process inside main memory:

Processes and Threads


Process memory
Stack
• The process Stack contains the temporary data such
as method/function parameters, return address, and
local variables.
Heap
• Dynamically allocated memory for dynamic data structures
like arrays, linked lists, and objects.
Text(code):
• Contains the executable instructions of the program.
• Read-only segment where the actual program code resides.
• Typically shared among multiple instances of the same
program to save memory.
Data(global)
• This section contains the global and static variables.
Process State
• As a process executes, it changes state
onew: The process is being created.
orunning: Instructions are being executed.
owaiting: The process is waiting for some event to occur.
oready: The process is waiting to be assigned to a
process.
oterminated: The process has finished execution.

Processes and Threads


Diagram of Process State

Processes and Threads


Process Control Block (PCB)
 Each process is represents in the operating system using a process control block (PCB)
also called a task control block .
Information associated with each process.
o Process state :The current state of the process i.e., whether it is ready, running, waiting,
or whatever.
o Process ID :Unique identification for each of the process in the operating system.
o Program counter: is a pointer to the address of the next instruction to be executed for this
process.
o
o Pointer :A pointer
CPU registers: to parent
Various CPUprocess.
registers where process need to be stored for execution for
state
running
o CPU scheduling information :Process priority and other scheduling information which
is required to schedule the
o process.
Memory-management information: This includes the information of page table, memory
Segment table depending on memory used by the operating
limits,
o system.
Accounting information : This includes the amount of CPU used for process execution,
limits, execution ID etc.
time
o I/O status information: This includes a list of I/O devices allocated to the
process. Processes and Threads
Process Control Block (PCB)

The PCB is maintained for a process


throughout its lifetime, and is deleted once
the process terminates.

Processes and Threads


CPU Switch From Process to Process

Processes and Threads


Process Scheduling Queues
oMaximize CPU use, quickly switch processes onto CPU for time
sharing
oProcess scheduling queues select among available processes for next
execution on CPU.
oMaintain scheduling queues of processes.
o Job queue – set of all processes in the system.
o Ready queue – set of all processes residing in main memory,
ready and waiting to execute.
o Device queues – set of processes waiting for an I/O device. Like:
disks, printers…
o Process migration between the various queues.

Processes and Threads


Representation of Process
Scheduling

Processes and Threads


Schedulers
Scheduler is a component responsible for managing the allocation
of system resources to processes.
 Its main task is to determine which processes are allowed to run,
when, and for how long.
 The scheduler plays a crucial role in ensuring efficient and fair
utilization of CPU time among competing processes.
 Long-term scheduler (or job scheduler) – selects which
processes should be brought into the ready queue.
 Short-term scheduler (or CPU scheduler) – selects which
process should be executed next and allocates CPU.

Processes and Threads


Schedulers (Cont.)
oShort-term scheduler is invoked very frequently (milliseconds)
 (must be fast).
oLong-term scheduler is invoked very infrequently (seconds, minutes) 
(may be slow).
oThe long-term scheduler controls the degree of multiprogramming.
oProcesses can be described as either:
o I/O-bound process – spends more time doing I/O than computations, many short
CPU bursts.
o CPU-bound process – spends more time doing computations; few very long
CPU
bursts.
Processes and Threads
Operation on process
• There are different operations that can be performed on process such
as:-
 Process creation
 Process switching
 Process termination

Processes and Threads


Process Creation
• Parent process creates children processes, which, in turn create other
processes, forming a tree of processes.
• Resource sharing
• Parent and children share all resources.
• Children share subset of parent’s resources.
• Parent and child share no resources.
• Execution
• Parent and children execute concurrently.
• Parent waits until children terminate.

Processes and Threads


Context Switch
• 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.
• Context-switch time is overhead; the system does no useful work
while switching.
• Time dependent on hardware support.

Processes and Threads


Process Termination
• Process executes last statement and asks the operating system to
decide it (exit).
• Output data from child to parent (via wait).
• Process’ resources are DE allocated by operating system.
• Parent may terminate execution of children processes (abort).
• 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.

Processes and Threads


Process termination
When does a process terminate?
After a process has been created, it starts running and does
whatever its job is.
Sooner or later the new process will terminate, usually due to
one of the following conditions
1. Normal exit (voluntary)
2. Error exit (voluntary)
3. Fatal error (involuntary)
4. Killed by another process (involuntary)
Processes and Threads
Inter process Communication (IPC)
• IPC refers to the mechanisms and techniques used by processes
to communicate and exchange data with each other in an
operating system.
• Processes within a system may be independent or cooperating.
• Independent process cannot affect or be affected by the
execution of another process.
• Cooperating process can affect or be affected by other
processes.
• This cooperation can be performed by
Shared memory
Message passing
Processes and Threads
Inter process Communication(Cont..)
• Advantages for cooperating processes:
 Information sharing
 Computation speedup
 Modularity
 Convenience
Therefore, cooperating processes need inter process communication
(IPC)

Processes and Threads


Direct Communication
• Processes must name each other explicitly:
• send (P, message) – send a message to process P
• receive(Q, message) – receive a message from process Q
• Properties of communication link
• Links are established automatically.
• A link is associated with exactly
one pair of communicating
processes.
• Between each pair there exists
exactly one link.
• The link may be unidirectional, but
is usually bi-directional. Processes and Threads
Indirect Communication
• Messages are directed and received from mailboxes (also referred to as
ports).
• Each mailbox has a unique id.
• Processes can communicate only if they share a mailbox.
• Properties of communication link
• Link established only if processes share a common mailbox
• A link may be associated with many processes.
• Each pair of processes may share several communication links.
• Link may be unidirectional or bi-directional.
• Operations
• create a new mailbox
• send and receive messages through mailbox
• destroy a mailbox

Processes and Threads


Indirect Communication (Cont..)
• Mailbox sharing
• P1, P2, and P3 share mailbox A.
• P1, sends; P2 and P3 receive.

Processes and Threads


Thread overview
 A thread refers to the smallest unit of execution within a process.
 Threads within the same process can communicate with each other more
easily than processes running independently.
 In traditional operating systems, each process has an address space and a
single thread of control.
 A thread is also called a lightweight process.
• Multithreading: a single program made up of a number of different
concurrent activities.
• If a process has multiple threads of control, it can perform more than one
task at a time.
 A thread (or mini process) basic unit of CPU utilization; it consists of:
 program counter, register set and stack space
Processes and Threads
Thread overview(cont..)
• A thread shares the following with peer
threads:
Code section,
Data section and
 OS resources (open files, signals)
 No protection between threads
• Heavyweight process is a task with one thread.

Processes and Threads


Thread overview(cont..)
• There are four major categories of benefits to multi-threading:
1. Responsiveness - One thread may provide rapid response while other
threads are blocked or slowed down doing intensive calculations.
2. Resource sharing - By default threads share common code, data, and
other resources, which allows multiple tasks to be performed
simultaneously in a single address space.
3. Economy - Creating and managing threads ( and context switches
between them ) is much faster than performing the same tasks for
processes.
4. Scalability-Threads allow utilization of multiprocessor architectures to
a greater scale and efficiency.
Processes and Threads 26
Single and Multithreaded Processes

Processes and Threads


Multithreading Models
• There are two types of threads to be managed in a modern system:
User threads and kernel threads.
• User threads are supported above the kernel, without kernel
support. These are the threads that application programmers would
put into their programs.
• Kernel threads are supported within the kernel of the OS itself. All
modern OSes support kernel level threads, allowing the kernel to
perform multiple simultaneous tasks and/or to service multiple kernel
system calls simultaneously.

Processes and Threads


Multithreading Models(cont..)
• Some operating system provide a combined user level thread and
Kernel level thread facility.
• Solaris is a good example of this combined approach. In a combined
system, multiple threads within the same application can run in parallel
on multiple processors and a blocking system call need not block
the entire process. Multithreading models are three types:
• Many-to-One

• One-to-One

• Many-to-Many

Processes and Threads


Many-to-One
• In the many-to-one model, many user-level
threads are all mapped onto a single kernel thread.
• Thread management is handled by the thread
library in user space, which is very efficient.
• However, if a blocking system call is made, then
the entire process blocks, even if the other
user threads would otherwise be able to
continue.
• Because a single kernel thread can operate
only on
a single CPU, the many-to-one model does not
allow individual processes to be split
across multiple CPUs.
• Green threads for Solaris and GNU
Portable
Processes and Threads
Threads implement the many-to-one model in
One-to-One
• The one-to-one model creates a separate
kernel thread to handle each user
thread.
• One-to-one model overcomes the
problems
listed above involving blocking system
calls
and the splitting of processes across multiple
CPUs.
• However the overhead of managing the one-
to-one model is more significant, involving
more overhead and slowing down the
system.
• Most implementations of this model place a
limit on how many threads can be created.
• Linux and Windows from 95 to XP
implement the one-to-one model for
threads. Processes and Threads
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.
• Users have no restrictions on the number
of threads created. Blocking kernel system
calls do not block the entire process.
• Processes can be split across multiple
processors.
• Individual processes may be allocated
variable numbers of kernel threads,
depending on the number of CPUs present
and other factors.

Processes and Threads


Advantages of Thread over
process
Takes less time to create a new thread than a process
Less time to terminate a thread than a process
Switching between twoLessthreads
time to takes less time than switchingThreads enhance
terminate a efficiency in
between processes
Takes less
thread than a communication
process Switching between two between programs
Threads enhance efficiency in communication
time to between programs
threads takes less time
create a new than switching
thread than a between processes
process

Processes and Threads


Application Vs system programming
1. Application Programming:- Focuses on creating software
applications that fulfill specific user needs or serve particular
functions.
 The target audience for application programming is end-users who
interact directly with the software.
 Applications like web browsers, word processors, games, mobile
apps, and productivity software are all examples of applications
developed through application programming.
 Application programming often involves high-level languages such
as Python, JavaScript, Java, C#, Swift, etc., along with frameworks
and libraries specific to the platform or domain of the application.
2. System Programming:- It is concerned with creating software that
interacts directly with computer hardware or provides low-level
services to other software.
 System programmers typically work closer to the hardware and
focus on optimizing performance, managing resources, and
ensuring the correct functioning of the underlying system.
 Operating systems, device drivers, embedded systems, firmware,
and networking protocols are examples of software developed
through system programming.
 System programming often involves lower-level languages such as
C, C++, and assembly language. Tools like debuggers, profilers,
and system-level libraries are commonly used in system
programming.
C language and UNIX introduction
UNIX Operating System

 UNIX is a family of multitasking, multi-user operating systems


originally developed by Ken Thompson, Dennis Ritchie, and others
at Bell Labs in the late 1960s and early 1970s.
 It was designed to be a portable, flexible, and powerful operating
system for mainframe and later minicomputer systems.
Key features of UNIX
Multiuser and Multitasking: Allows multiple users to access the system
simultaneously and supports multitasking, enabling multiple processes to
run concurrently.
Hierarchical File System: Organizing files and directories in a tree-like
structure, making it easy to manage and navigate.
Shell and Command-Line Interface: Providing powerful text-based
tools for interacting with the system.
Networking Support: UNIX systems have built-in support for
networking, enabling communication between different systems over
networks.
Key features of UNIX…
Portability and Compatibility: UNIX was designed to be portable
across different hardware platforms, leading to the development of
various UNIX-like operating systems, including Linux and macOS.
C language introduction
 C is a powerful and widely used programming language that was
originally developed in the early 1970s by Dennis Ritchie at Bell
Labs.
 It is a general-purpose programming language. It can be used to
develop software like operating systems, databases, compilers, and
so on.
 It's known for its efficiency, flexibility, and close-to-the-hardware
nature, making it a popular choice for system programming,
embedded systems, and performance-critical applications.
 It is initially developed for UNIX operating system.
C language introduction
Example:
 #include <stdio.h>: This line includes the standard
input-output library, which provides functions like
printf() for printing to the console.
 void greet();: This is a function prototype or declaration.
It informs the compiler about the existence of a function
named greet that takes no arguments and returns void
(i.e., nothing).
 int main() { ... }: This is the main function of the
program. It's where the execution of the program
begins. In this example, it prints "Hello, world!" to the
console and then calls the greet() function.
 void greet() { ... }: This is the definition of the greet
function. It simply prints "Welcome to the world of C
programming!" to the console.
Class work
Q, Write a C program to add two numbers? (NB: accept numbers from user)
Solution:
Process control using C
1. Creating Processes: Example:

fork(): This system call is used to create a new


process by duplicating the existing process. After a
fork, the new process (child process) is an exact
copy of the calling process (parent process) except
for the return value of fork.
Process control using C…

2. Terminating Processes:
exit(): This function is used to terminate the calling process and
return an exit status to the operating system.

Example:
Process control using C…

3. Process Identification:
getpid(): This function returns the process ID (PID) of the calling process.
getppid(): This function returns the parent process ID (PPID) of the calling
process. Example:
Process control using C…
Example:

4. Waiting for Processes:


wait(): This system call is used by a parent
process to wait for the termination of its child
process.
Process control using C…

5. Executing Other Programs: Example:

exec() family of functions: These


functions are used to execute a new
program in the current process space,
replacing the current program.
File manipulation using C

1. Opening Files:
• Opens "source.txt" for reading and "destination.txt" for writing.

2. Copying Contents:
• Reads data from "source.txt" and writes it to "destination.txt" until the end of file is reached.

3. Closing Files:
• Closes both file descriptors.

4. Deleting Source File:


• Uses unlink() to delete the "source.txt" file after copying its contents.

Remember to handle errors properly, especially when dealing with file operations.
The perror() function is used to print descriptive error messages to stderr in case of
file manipulation errors.
File manipulation using C…
Example:
Thank
You!!!

You might also like