2.process and Threds
2.process and Threds
Email: azharmahmood8@hotmail.com
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
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
• 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
•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
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
• Resource Sharing
• Economy
• 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
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)
...
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