0% found this document useful (0 votes)
41 views48 pages

2.process and Threds

This document discusses processes, threads, and system calls. It defines a process as a program in execution, and threads as specific flows of control within a process. Processes have components like program code, memory, and data sections. A program becomes a process when it is loaded into memory and begins running. Processes can create child processes. Threads allow multiple executions within the same process and share resources like memory. System calls provide an interface for processes and threads to request services from the operating system.

Uploaded by

Qasim Abbas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views48 pages

2.process and Threds

This document discusses processes, threads, and system calls. It defines a process as a program in execution, and threads as specific flows of control within a process. Processes have components like program code, memory, and data sections. A program becomes a process when it is loaded into memory and begins running. Processes can create child processes. Threads allow multiple executions within the same process and share resources like memory. System calls provide an interface for processes and threads to request services from the operating system.

Uploaded by

Qasim Abbas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 48

Chapter 2

Processes and Threads

Dr. Azhar Mahmood

Email: [email protected]
Processes
• A program in execution.
• Process components: Every thing that
interacts with its current activity includes:
• Program code
• Program counter
• Registers
• Main memory
• Program stack( temp var, parameters, etc.)
• Data section ( global variables , etc….)
From Program to Process
• Program – A prepared sequence of instructions to accomplish a
defined task
– A text editor is used to enter the human readable version of a
program into a source code file
– A compiler is used to convert the source code into a computer
readable version that is stored in an object code file
– A linker is used to combine the functions stored in one or more
object code files or libraries into a single executable file
– A loader is used to copy the contents of an executable file into a
program image in main memory
• Process – An instance of a program that is running (or scheduled to
run) in a computer
• Thread – A specific flow of control in a process
– It represents a thread of execution within a process
– Each thread has its own
• execution stack
• program counter
• register set
• process state (if using kernel threads)
Processes
Difference between process and program

When two users run “ls” it is same program different processes.

process program

Furniture Following
Ins. Ins.
To To build
Make The
A chair chair
The Process Model
• An operating system executes a variety of programs:
• Batch system – jobs
• Time-shared systems – user programs or tasks
• Process execution must progress in sequential fashion

(a) Multiprogramming of four programs.


(b) Conceptual model of four independent, sequential processes.
(c) Only one program is active at once.
Process Creation

Events which cause process creation when…

• System initialization.
• Execution of a process creation system call by a running
process.
• A user request to create a new process.
• Initiation of a batch job.
Process Creation
• Daemons: processes that are running in the background
mode (e.g. checking email)
• In UNIX, the ps program can be used to list the running
processes. In Windows, the task manager can be used.
• fork system call creates new process in Unix
• In Unix after that new process has same memory
image, same open files and etc. as its (only one)
parent
• exec system call used after a fork to replace the
process’ memory space with a new program in
Unix
• In both UNIX and Windows, after a process is created, the
parent and child have their own distinct address spaces.
Process in Memory
• Each process has some set of address space running from 0 to
some maximum

Process Address space


Process Termination
Events which cause process termination:
• Normal exit (voluntary). Process executes last statement and
asks the operating system to delete it
• Error exit (voluntary). Given bad parameters e.g. trying to
access a file that do not exist
• Fatal error (involuntary). program bug e.g. :
• Referencing non existing memory
• Divide by zero
• Executing illegal instruction
• Killed by another process (involuntary). process executes a
system call to kill some other process.

In UNIX this call is kill. Win32 function is TerminateProcess.


Process Hierarchies
Parent process create children processes, which in turn
create other processes, forming a tree of processes called
process hierarchy.

• In Unix all the processes in the system belong to a


single tree, with init at the root

•Windows
• No concept of a process hierarchy.
• All processes are equal.
• Special token handle can use to control the
child.
Processes Tree on a UNIX System

… the family tree


Swapper is the scheduler
Process State
• In the ready state process is willing to run but there is
no CPU available
• In blocked state process unable to run until some
external events happens
Process Implementation
• To implement the process model, the operating system
maintains a table called the process table as knows as
(PCB).

• Process table is used to maintain process information


( one for each process )

• Process table is array of structure

• PCB is the key to multiprogramming

• PCB must be saved when a process is switched from


running to ready or blocked states
Sequential Processes

The lowest layer of a process-structured operating system handles


interrupts and scheduling. Above that layer are sequential
processes.
Process Control Block (PCB)
Information associated with each process – uniquely identifies
and characterizes a particular process. Some information in
the PCB:

1. Process state – running, waiting, ready, etc.


2. Program counter
3. CPU register values
4. CPU scheduling information - used by short term scheduler
5. Memory-management information – base, limit registers, page
table info, etc.
6. Accounting information
7. I/O status & file information – devices allocated to proc., open
files, etc.
Fields of PCB

Some of the fields of a typical process table entry.


CPU Switch From Process to Process
What lowest level of the OS does when an
interrupt occurs

Skeleton of what the lowest level of the operating system does when an
interrupt occurs.
Threads
• Each process has a single address space that
contains program text, data and stack.
• Multiple threads of control within a process
that share a same address space are called
threads.
• Threads are lightweight processes that have
their own program counter, registers and state.
• Threads allow multiple executions to take
place in the same process environment.
• Multiple threads running in parallel in one
process share program address, open files,
physical memory, disk printers and other
resources.
Single and Multithreaded Processes
Threads Benefits
• Simple programming model

• Light weight process easy to create and destroy

• Resource Sharing

• Economy

• Utilization of Multi-Processing Architectures


Thread Usage (1)

A word processor with three threads.


Thread Usage (2)
For example for making a web server that cache the popular pages in
the memory
Thread Usage (3)
• Dispatcher reads incoming requests for work from the network.
• After examining the request, it chooses an idle (i.e., blocked)
worker thread and hands it

• The request, possibly by writing a pointer to the message into a


special word associated with each thread.

• When the worker wakes up, it checks to see if the request can be
satisfied from the Web page cache

• If not, it starts a read operation to get the page from the disk and
blocks until the disk operation completes

• When the thread blocks on the disk operation, another thread is


chosen to run, possibly the dispatcher, in order to acquire more
work, or possibly another worker that is now ready to run.
Thread Usage (3)

(a) Dispatcher thread. (b) Worker thread.


Multithreaded Server Architecture
Thread Usage (4)

Three ways to construct a server.


The Classical Thread Model (1)

(a) Three processes each with one thread.


(b) One process with three threads.
The Classical Thread Model (2)

The first column lists some items shared by all threads in a process.
The second one lists some items private to each thread.
The Classical Thread Model (3)

Each thread has its own stack.


POSIX Threads (1)

Some of the Pthreads function calls.


POSIX Threads (2)

...

Figure 2-15. An example program using threads.


Implementing Threads in User Space

(a) A user-level threads package.


(b) A threads package managed by the kernel.
Hybrid Implementations

Multiplexing user-level threads onto kernel-level threads.


Pop-Up Threads

Creation of a new thread when a message arrives.


(a) Before the message arrives.
(b) After the message arrives.
Making Single-Threaded Code Multithreaded

Threads can have private global variables.


System Calls
• Programming interface to the services provided by the OS

• Typically written in a high-level language (C or C++)

• Mostly accessed by programs via a high-level Application


Programming Interface (API) rather than direct system
call use

• Three most common APIs are Win32 API for Windows,


POSIX API for POSIX-based systems (including virtually
all versions of UNIX, Linux, and Mac OS X), and Java API
for the Java virtual machine (JVM)

Note that the system-call names used


throughout this text are generic
Example of System Calls

• System call sequence to copy the contents of one file to


another file
Example of Standard API
System Call Implementation
• Typically, a number associated with each system call
– System-call interface maintains a table indexed according to
these numbers
• The system call interface invokes the intended system call in OS
kernel and returns status of the system call and any return values
• The caller need know nothing about how the system call is
implemented
– Just needs to obey API and understand what OS will do as a
result call
– Most details of OS interface hidden from programmer by API
• Managed by run-time support library (set of functions built into
libraries included with compiler)
API – System Call – OS
Relationship
System Call Parameter Passing
• Often, more information is required than simply identity of
desired system call
– Exact type and amount of information vary according to OS and call

• Three general methods used to pass parameters to the OS

1. Simplest: pass the parameters in registers


In some cases, may be more parameters than registers
2. Parameters stored in a block, or table, in memory, and address
of block passed as a parameter in a register
This approach taken by Linux and Solaris
3. Parameters placed, or pushed, onto the stack by the program
and popped off the stack by the operating system

Block and stack methods do not limit the number or length of parameters
being passed
Parameter Passing via Table
Types of System Calls
• Process control
– create process, terminate process
– end, abort
– load, execute
– get process attributes, set process attributes
– wait for time
– wait event, signal event
– allocate and free memory
– Dump memory if error
– Debugger for determining bugs, single step execution
– Locks for managing access to shared data between processes
Types of System Calls
• File management
– create file, delete file
– open, close file
– read, write, reposition
– get and set file attributes
• Device management
– request device, release device
– read, write, reposition
– get device attributes, set device attributes
– logically attach or detach devices
Types of System Calls (Cont.)
• Information maintenance
– get time or date, set time or date
– get system data, set system data
– get and set process, file, or device attributes
• Communications
– create, delete communication connection
– send, receive messages if message passing model to host
name or process name
• From client to server
– Shared-memory model create and gain access to memory
regions
– transfer status information
– attach and detach remote devices
Examples of Windows and Unix System Calls
Standard C Library Example
• C program invoking printf() library call, which calls
write() system call

You might also like