SlideShare a Scribd company logo
PROCESSES & THREADS
PROCESSES AND THREADS
• Processes
• Threads
• Scheduling
• Interprocess communication
• Classical IPC problems
WHAT IS A PROCESS?
• Code, data, and stack
• Usually (but not always) has its own address space
• Program state
• CPU registers
• Program counter (current location in the code)
• Stack pointer
• Only one process can be running in the CPU at any
given time!
3
THE PROCESS MODEL
• Multiprogramming of four
programs
• Conceptual model
• 4 independent processes
• Processes run sequentially
• Only one program active at any
instant!
• That instant can be very short…
4
A
C
D
Single PC
(CPU’s point of view)
A
B
C D
Multiple PCs
(process point of view)
B
B
A
B
C
D
Time
WHEN IS A PROCESS CREATED?
• Processes can be created in two ways
• System initialization: one or more processes created
when the OS starts up
• Execution of a process creation system call: something
explicitly asks for a new process
• System calls can come from
• User request to create a new process (system call
executed from user shell)
• Already running processes
• User programs
• System daemons
5
PROCESS CREATION
• Parent process creates children processes, which, in turn
create other processes, forming a tree of processes.
• Generally, process identified and managed via a process
identifier (pid).
• Resource sharing options
• Parent and children share all resources
• Children share subset of parent’s resources
• Parent and child share no resources
• Execution options
• Parent and children execute concurrently
• Parent waits until children terminate
PROCESS CREATION (CONT.)
• Address space
• Child duplicate of parent
• Child has a program loaded into it
• UNIX examples
• fork() system call creates new process
• exec() system call used after a fork() to replace the
process’ memory space with a new program
WHEN DO PROCESSES END?
• Conditions that terminate processes can be
• Voluntary
• Involuntary
• Voluntary
• Normal exit
• Error exit
• Involuntary
• Fatal error (only sort of involuntary)
• Killed by another process
8
PROCESS TERMINATION
• Process executes last statement and then asks the
operating system to delete it using the exit() system call.
• Returns status data from child to parent (via wait())
• Process’ resources are deallocated by operating
system
• Parent may terminate the execution of children
processes using the abort() system call. Some reasons
for doing so:
• Child has exceeded allocated resources
• Task assigned to child is no longer required
• The parent is exiting and the operating systems does
not allow a child to continue if its parent terminates
PROCESS TERMINATION
• Some operating systems do not allow a child to exist if its parent
has terminated. If a process terminates, then all its children must
also be terminated.
• cascading termination. All children, grandchildren,
etc. are terminated.
• The termination is initiated by the operating system.
• The parent process may wait for termination of a child process by
using the wait()system call. The call returns status information and
the pid of the terminated process
pid = wait(&status);
• If no parent waiting (did not invoke wait()) process is a zombie
• If parent terminated without invoking wait , process is an orphan
PROCESS HIERARCHIES
• Parent creates a child process
• Child processes can create their own children
• Forms a hierarchy
• UNIX calls this a “process group”
• If a process exits, its children are “inherited” by the
exiting process’s parent
• Windows has no concept of process hierarchy
• All processes are created equal
11
PROCESS STATES
• Process in one of 5 states
• Created
• Ready
• Running
• Blocked
• Exit
• Transitions between states
1 - Process enters ready queue
2 - Scheduler picks this process
3 - Scheduler picks a different process
4 - Process waits for event (such as I/O)
5 - Event occurs
6 - Process exits
7 - Process ended by another process
12
Blocked
(waiting)
Created
Exit
Ready
Running
1
5
4
3
2
7
7
6
PROCESSES IN THE OS
• Two “layers” for processes
• Lowest layer of process-structured OS handles interrupts,
scheduling
• Above that layer are sequential processes
• Processes tracked in the process table
• Each process has a process table entry
13
Scheduler
0 1 N-2 N-1
…
Processes
WHAT’S IN A PROCESS TABLE
ENTRY?
14
File management
Root directory
Working (current) directory
File descriptors
User ID
Group ID
Memory management
Pointers to text, data, stack
or
Pointer to page table
Process management
Registers
Program counter
CPU status word
Stack pointer
Process state
Priority / scheduling parameters
Process ID
Parent process ID
Signals
Process start time
Total CPU usage
May be
stored
on stack
WHAT HAPPENS ON A
TRAP/INTERRUPT?
1. Hardware saves program counter (on stack or in a
special register)
2. Hardware loads new PC, identifies interrupt
3. Assembly language routine saves registers
4. Assembly language routine sets up stack
5. Assembly language calls C to run service routine
6. Service routine calls scheduler
7. Scheduler selects a process to run next (might be the
one interrupted…)
8. Assembly language routine loads PC & registers for
the selected process
15
THREADS: “PROCESSES” SHARING MEMORY
• Process == address space
• Thread == program counter / stream of instructions
• Two examples
• Three processes, each with one thread
• One process with three threads
16
Kernel Kernel
Threads
Threads
System
space
User
space
Process 1 Process 2 Process 3 Process 1
PROCESS & THREAD INFORMATION
17
Per process items
Address space
Open files
Child processes
Signals & handlers
Accounting info
Global variables
Per thread items
Program counter
Registers
Stack & stack pointer
State
Per thread items
Program counter
Registers
Stack & stack pointer
State
Per thread items
Program counter
Registers
Stack & stack pointer
State
THREADS & STACKS
18
Kernel
Process
Thread 1 Thread 2 Thread 3
Thread 1’s
stack
Thread 3’s
stack
Thread 2’s
stack
User space
=> Each thread has its own stack!
WHY USE THREADS?
• Allow a single application to
do many things at once
• Simpler programming model
• Less waiting
• Threads are faster to create
or destroy
• No separate address space
• Overlap computation and
I/O
• Could be done without
threads, but it’s harder
• Example: word processor
• Thread to read from keyboard
• Thread to format document 19
Kernel
MULTITHREADED WEB SERVER
20
Kernel
Network
connection
Dispatcher
thread
Worker
thread
Web page
cache
while(TRUE) {
getNextRequest(&buf);
handoffWork(&buf);
}
while(TRUE) {
waitForWork(&buf);
lookForPageInCache(&buf,&page);
if(pageNotInCache(&page)) {
readPageFromDisk(&buf,&page);
}
returnPage(&page);
}
THREE WAYS TO BUILD A SERVER
• Thread model
• Parallelism
• Blocking system calls
• Single-threaded process: slow, but easier to do
• No parallelism
• Blocking system calls
• Finite-state machine
• Each activity has its own state
• States change when system calls complete or interrupts occur
• Parallelism
• Nonblocking system calls
• Interrupts
21
IMPLEMENTING THREADS
22
Kernel
Run-time
system
Thread
table
Process
table
Kernel
Thread
Process
Thread
table
Process
table
User-level threads
+ No need for kernel support
- May be slower than kernel threads
- Harder to do non-blocking I/O
Kernel-level threads
+ More flexible scheduling
+ Non-blocking I/O
- Not portable
PRODUCER-CONSUMER PROBLEM
• Paradigm for cooperating processes, producer
process produces information that is consumed by a
consumer process
BOUNDED-BUFFER – SHARED-
MEMORY SOLUTION
• Shared data
• #define BUFFER_SIZE 10
• typedef struct {
• . . .
• } item;
• item buffer[BUFFER_SIZE];
• int in = 0;
• int out = 0;
• Solution is correct, but can only use BUFFER_SIZE-1
elements
BOUNDED-BUFFER – PRODUCER
item next_produced;
while (true) {
/* produce an item in next produced */
while (((in + 1) % BUFFER_SIZE) == out)
; /* do nothing */
buffer[in] = next_produced;
in = (in + 1) % BUFFER_SIZE;
}
BOUNDED BUFFER – CONSUMER
item next_consumed;
while (true) {
while (in == out)
; /* do nothing */
next_consumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
/* consume the item in next consumed */
}
26
SCHEDULING
• What is scheduling?
• Goals
• Mechanisms
• Scheduling on batch systems
• Scheduling on interactive systems
• Other kinds of scheduling
• Real-time scheduling
27
WHY SCHEDULE PROCESSES?
• Bursts of CPU usage alternate with periods of I/O wait
• Some processes are CPU-bound: they don’t make many I/O
requests
• Other processes are I/O-bound and make many kernel requests
28
CPU bound
I/O bound
CPU bursts I/O waits
Total CPU usage
Total CPU usage
Time
WHEN ARE PROCESSES
SCHEDULED?
• At the time they enter the system
• Common in batch systems
• Two types of batch scheduling
• Submission of a new job causes the scheduler to run
• Scheduling only done when a job voluntarily gives up
the CPU (i.e., while waiting for an I/O request)
• At relatively fixed intervals (clock interrupts)
• Necessary for interactive systems
• May also be used for batch systems
• Scheduling algorithms at each interrupt, and picks the next
process from the pool of “ready” processes
29
Scheduling goals
• All systems
• Fairness: give each process a fair share of the CPU
• Enforcement: ensure that the stated policy is carried out
• Balance: keep all parts of the system busy
• Batch systems
• Throughput: maximize jobs per unit time (hour)
• Turnaround time: minimize time users wait for jobs
• CPU utilization: keep the CPU as busy as possible
• Interactive systems
• Response time: respond quickly to users’ requests
• Proportionality: meet users’ expectations
• Real-time systems
• Meet deadlines: missing deadlines is a system failure!
• Predictability: same type of behavior for each time slice
30

More Related Content

Similar to Processes, Threads.pptx

Processes
ProcessesProcesses
Processes
RaviRaj339
 
Basic concept of process
Basic concept of processBasic concept of process
Basic concept of process
Nabin Dahal
 
Ch3 processes
Ch3   processesCh3   processes
Ch3 processes
Welly Dian Astika
 
Unit 2_OS process management
Unit 2_OS process management Unit 2_OS process management
Unit 2_OS process management
JayeshGadhave1
 
Os unit 3 , process management
Os unit 3 , process managementOs unit 3 , process management
Os unit 3 , process management
Arnav Chowdhury
 
Thread
ThreadThread
08 operating system support
08 operating system support08 operating system support
08 operating system support
Bitta_man
 
08 operating system support
08 operating system support08 operating system support
08 operating system support
dilip kumar
 
Operating system 18 process creation and termination
Operating system 18 process creation and terminationOperating system 18 process creation and termination
Operating system 18 process creation and termination
Vaibhav Khanna
 
08 operating system support
08 operating system support08 operating system support
08 operating system support
Sher Shah Merkhel
 
Processes and operating systems
Processes and operating systemsProcesses and operating systems
Processes and operating systems
RAMPRAKASHT1
 
opearating system notes mumbai university.pptx
opearating system notes mumbai university.pptxopearating system notes mumbai university.pptx
opearating system notes mumbai university.pptx
ssuser3dfcef
 
Computer organization and aChapter 2.pptx
Computer organization and aChapter 2.pptxComputer organization and aChapter 2.pptx
Computer organization and aChapter 2.pptx
gadisaAdamu
 
Systems Programming Assignment Help - Processes
Systems Programming Assignment Help - ProcessesSystems Programming Assignment Help - Processes
Systems Programming Assignment Help - Processes
HelpWithAssignment.com
 
UNIT I-Processes.pptx
UNIT I-Processes.pptxUNIT I-Processes.pptx
UNIT I-Processes.pptx
GaneshKumar537286
 
Process Management.pdf
Process Management.pdfProcess Management.pdf
Process Management.pdf
Yashjangid9
 
Threads in Operating System | Multithreading | Interprocess Communication
Threads in Operating System | Multithreading | Interprocess CommunicationThreads in Operating System | Multithreading | Interprocess Communication
Threads in Operating System | Multithreading | Interprocess Communication
Shivam Mitra
 
Operating Systems 1 (6/12) - Processes
Operating Systems 1 (6/12) - ProcessesOperating Systems 1 (6/12) - Processes
Operating Systems 1 (6/12) - Processes
Peter Tröger
 
How Operating system works.
How Operating system works. How Operating system works.
How Operating system works.
Fahad Farooq
 
MK Sistem Operasi.pdf
MK Sistem Operasi.pdfMK Sistem Operasi.pdf
MK Sistem Operasi.pdf
wisard1
 

Similar to Processes, Threads.pptx (20)

Processes
ProcessesProcesses
Processes
 
Basic concept of process
Basic concept of processBasic concept of process
Basic concept of process
 
Ch3 processes
Ch3   processesCh3   processes
Ch3 processes
 
Unit 2_OS process management
Unit 2_OS process management Unit 2_OS process management
Unit 2_OS process management
 
Os unit 3 , process management
Os unit 3 , process managementOs unit 3 , process management
Os unit 3 , process management
 
Thread
ThreadThread
Thread
 
08 operating system support
08 operating system support08 operating system support
08 operating system support
 
08 operating system support
08 operating system support08 operating system support
08 operating system support
 
Operating system 18 process creation and termination
Operating system 18 process creation and terminationOperating system 18 process creation and termination
Operating system 18 process creation and termination
 
08 operating system support
08 operating system support08 operating system support
08 operating system support
 
Processes and operating systems
Processes and operating systemsProcesses and operating systems
Processes and operating systems
 
opearating system notes mumbai university.pptx
opearating system notes mumbai university.pptxopearating system notes mumbai university.pptx
opearating system notes mumbai university.pptx
 
Computer organization and aChapter 2.pptx
Computer organization and aChapter 2.pptxComputer organization and aChapter 2.pptx
Computer organization and aChapter 2.pptx
 
Systems Programming Assignment Help - Processes
Systems Programming Assignment Help - ProcessesSystems Programming Assignment Help - Processes
Systems Programming Assignment Help - Processes
 
UNIT I-Processes.pptx
UNIT I-Processes.pptxUNIT I-Processes.pptx
UNIT I-Processes.pptx
 
Process Management.pdf
Process Management.pdfProcess Management.pdf
Process Management.pdf
 
Threads in Operating System | Multithreading | Interprocess Communication
Threads in Operating System | Multithreading | Interprocess CommunicationThreads in Operating System | Multithreading | Interprocess Communication
Threads in Operating System | Multithreading | Interprocess Communication
 
Operating Systems 1 (6/12) - Processes
Operating Systems 1 (6/12) - ProcessesOperating Systems 1 (6/12) - Processes
Operating Systems 1 (6/12) - Processes
 
How Operating system works.
How Operating system works. How Operating system works.
How Operating system works.
 
MK Sistem Operasi.pdf
MK Sistem Operasi.pdfMK Sistem Operasi.pdf
MK Sistem Operasi.pdf
 

More from SKUP1

serial_busses_i2c.pptx
serial_busses_i2c.pptxserial_busses_i2c.pptx
serial_busses_i2c.pptx
SKUP1
 
DESIGN PATTERN.pptx
DESIGN PATTERN.pptxDESIGN PATTERN.pptx
DESIGN PATTERN.pptx
SKUP1
 
INTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptxINTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptx
SKUP1
 
DATA STRUCTURES AND LINKED LISTS IN C.pptx
DATA STRUCTURES AND LINKED LISTS IN C.pptxDATA STRUCTURES AND LINKED LISTS IN C.pptx
DATA STRUCTURES AND LINKED LISTS IN C.pptx
SKUP1
 
C-Programming File-handling-C.pptx
C-Programming  File-handling-C.pptxC-Programming  File-handling-C.pptx
C-Programming File-handling-C.pptx
SKUP1
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptx
SKUP1
 
Finite State Machine.ppt.pptx
Finite State Machine.ppt.pptxFinite State Machine.ppt.pptx
Finite State Machine.ppt.pptx
SKUP1
 
FUNCTIONS IN C.pptx
FUNCTIONS IN C.pptxFUNCTIONS IN C.pptx
FUNCTIONS IN C.pptx
SKUP1
 
cprogramming strings.pptx
cprogramming strings.pptxcprogramming strings.pptx
cprogramming strings.pptx
SKUP1
 
UNIONS IN C.pptx
UNIONS IN C.pptxUNIONS IN C.pptx
UNIONS IN C.pptx
SKUP1
 
OPERATORS IN C.pptx
OPERATORS IN C.pptxOPERATORS IN C.pptx
OPERATORS IN C.pptx
SKUP1
 
cprogramming Structures.pptx
cprogramming Structures.pptxcprogramming Structures.pptx
cprogramming Structures.pptx
SKUP1
 
C-Programming Function pointers.pptx
C-Programming  Function pointers.pptxC-Programming  Function pointers.pptx
C-Programming Function pointers.pptx
SKUP1
 
POINTERS.pptx
POINTERS.pptxPOINTERS.pptx
POINTERS.pptx
SKUP1
 
STACKS AND QUEUES.pptx
STACKS AND QUEUES.pptxSTACKS AND QUEUES.pptx
STACKS AND QUEUES.pptx
SKUP1
 
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptxC-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
SKUP1
 
C MEMORY MODEL​.pptx
C MEMORY MODEL​.pptxC MEMORY MODEL​.pptx
C MEMORY MODEL​.pptx
SKUP1
 
DATATYPES,KEYWORDS,FORMATSPECS[1].pptx
DATATYPES,KEYWORDS,FORMATSPECS[1].pptxDATATYPES,KEYWORDS,FORMATSPECS[1].pptx
DATATYPES,KEYWORDS,FORMATSPECS[1].pptx
SKUP1
 
DYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptxDYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptx
SKUP1
 
COMPILATION PROCESS IN C.pptx
COMPILATION PROCESS IN C.pptxCOMPILATION PROCESS IN C.pptx
COMPILATION PROCESS IN C.pptx
SKUP1
 

More from SKUP1 (20)

serial_busses_i2c.pptx
serial_busses_i2c.pptxserial_busses_i2c.pptx
serial_busses_i2c.pptx
 
DESIGN PATTERN.pptx
DESIGN PATTERN.pptxDESIGN PATTERN.pptx
DESIGN PATTERN.pptx
 
INTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptxINTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptx
 
DATA STRUCTURES AND LINKED LISTS IN C.pptx
DATA STRUCTURES AND LINKED LISTS IN C.pptxDATA STRUCTURES AND LINKED LISTS IN C.pptx
DATA STRUCTURES AND LINKED LISTS IN C.pptx
 
C-Programming File-handling-C.pptx
C-Programming  File-handling-C.pptxC-Programming  File-handling-C.pptx
C-Programming File-handling-C.pptx
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptx
 
Finite State Machine.ppt.pptx
Finite State Machine.ppt.pptxFinite State Machine.ppt.pptx
Finite State Machine.ppt.pptx
 
FUNCTIONS IN C.pptx
FUNCTIONS IN C.pptxFUNCTIONS IN C.pptx
FUNCTIONS IN C.pptx
 
cprogramming strings.pptx
cprogramming strings.pptxcprogramming strings.pptx
cprogramming strings.pptx
 
UNIONS IN C.pptx
UNIONS IN C.pptxUNIONS IN C.pptx
UNIONS IN C.pptx
 
OPERATORS IN C.pptx
OPERATORS IN C.pptxOPERATORS IN C.pptx
OPERATORS IN C.pptx
 
cprogramming Structures.pptx
cprogramming Structures.pptxcprogramming Structures.pptx
cprogramming Structures.pptx
 
C-Programming Function pointers.pptx
C-Programming  Function pointers.pptxC-Programming  Function pointers.pptx
C-Programming Function pointers.pptx
 
POINTERS.pptx
POINTERS.pptxPOINTERS.pptx
POINTERS.pptx
 
STACKS AND QUEUES.pptx
STACKS AND QUEUES.pptxSTACKS AND QUEUES.pptx
STACKS AND QUEUES.pptx
 
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptxC-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
 
C MEMORY MODEL​.pptx
C MEMORY MODEL​.pptxC MEMORY MODEL​.pptx
C MEMORY MODEL​.pptx
 
DATATYPES,KEYWORDS,FORMATSPECS[1].pptx
DATATYPES,KEYWORDS,FORMATSPECS[1].pptxDATATYPES,KEYWORDS,FORMATSPECS[1].pptx
DATATYPES,KEYWORDS,FORMATSPECS[1].pptx
 
DYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptxDYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptx
 
COMPILATION PROCESS IN C.pptx
COMPILATION PROCESS IN C.pptxCOMPILATION PROCESS IN C.pptx
COMPILATION PROCESS IN C.pptx
 

Recently uploaded

Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
MDSABBIROJJAMANPAYEL
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
IJECEIAES
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
Anant Corporation
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
kandramariana6
 
Certificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi AhmedCertificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi Ahmed
Mahmoud Morsy
 
Seminar on Distillation study-mafia.pptx
Seminar on Distillation study-mafia.pptxSeminar on Distillation study-mafia.pptx
Seminar on Distillation study-mafia.pptx
Madan Karki
 
Applications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdfApplications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdf
Atif Razi
 
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
ecqow
 
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
shadow0702a
 
Curve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods RegressionCurve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods Regression
Nada Hikmah
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
VICTOR MAESTRE RAMIREZ
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
insn4465
 
Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...
bijceesjournal
 
BRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdfBRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdf
LAXMAREDDY22
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
IJECEIAES
 
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURSCompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
RamonNovais6
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
171ticu
 
integral complex analysis chapter 06 .pdf
integral complex analysis chapter 06 .pdfintegral complex analysis chapter 06 .pdf
integral complex analysis chapter 06 .pdf
gaafergoudaay7aga
 
CEC 352 - SATELLITE COMMUNICATION UNIT 1
CEC 352 - SATELLITE COMMUNICATION UNIT 1CEC 352 - SATELLITE COMMUNICATION UNIT 1
CEC 352 - SATELLITE COMMUNICATION UNIT 1
PKavitha10
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
KrishnaveniKrishnara1
 

Recently uploaded (20)

Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
 
Certificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi AhmedCertificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi Ahmed
 
Seminar on Distillation study-mafia.pptx
Seminar on Distillation study-mafia.pptxSeminar on Distillation study-mafia.pptx
Seminar on Distillation study-mafia.pptx
 
Applications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdfApplications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdf
 
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
 
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
 
Curve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods RegressionCurve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods Regression
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
 
Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...
 
BRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdfBRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdf
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
 
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURSCompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
 
integral complex analysis chapter 06 .pdf
integral complex analysis chapter 06 .pdfintegral complex analysis chapter 06 .pdf
integral complex analysis chapter 06 .pdf
 
CEC 352 - SATELLITE COMMUNICATION UNIT 1
CEC 352 - SATELLITE COMMUNICATION UNIT 1CEC 352 - SATELLITE COMMUNICATION UNIT 1
CEC 352 - SATELLITE COMMUNICATION UNIT 1
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
 

Processes, Threads.pptx

  • 2. PROCESSES AND THREADS • Processes • Threads • Scheduling • Interprocess communication • Classical IPC problems
  • 3. WHAT IS A PROCESS? • Code, data, and stack • Usually (but not always) has its own address space • Program state • CPU registers • Program counter (current location in the code) • Stack pointer • Only one process can be running in the CPU at any given time! 3
  • 4. THE PROCESS MODEL • Multiprogramming of four programs • Conceptual model • 4 independent processes • Processes run sequentially • Only one program active at any instant! • That instant can be very short… 4 A C D Single PC (CPU’s point of view) A B C D Multiple PCs (process point of view) B B A B C D Time
  • 5. WHEN IS A PROCESS CREATED? • Processes can be created in two ways • System initialization: one or more processes created when the OS starts up • Execution of a process creation system call: something explicitly asks for a new process • System calls can come from • User request to create a new process (system call executed from user shell) • Already running processes • User programs • System daemons 5
  • 6. PROCESS CREATION • Parent process creates children processes, which, in turn create other processes, forming a tree of processes. • Generally, process identified and managed via a process identifier (pid). • Resource sharing options • Parent and children share all resources • Children share subset of parent’s resources • Parent and child share no resources • Execution options • Parent and children execute concurrently • Parent waits until children terminate
  • 7. PROCESS CREATION (CONT.) • Address space • Child duplicate of parent • Child has a program loaded into it • UNIX examples • fork() system call creates new process • exec() system call used after a fork() to replace the process’ memory space with a new program
  • 8. WHEN DO PROCESSES END? • Conditions that terminate processes can be • Voluntary • Involuntary • Voluntary • Normal exit • Error exit • Involuntary • Fatal error (only sort of involuntary) • Killed by another process 8
  • 9. PROCESS TERMINATION • Process executes last statement and then asks the operating system to delete it using the exit() system call. • Returns status data from child to parent (via wait()) • Process’ resources are deallocated by operating system • Parent may terminate the execution of children processes using the abort() system call. Some reasons for doing so: • Child has exceeded allocated resources • Task assigned to child is no longer required • The parent is exiting and the operating systems does not allow a child to continue if its parent terminates
  • 10. PROCESS TERMINATION • Some operating systems do not allow a child to exist if its parent has terminated. If a process terminates, then all its children must also be terminated. • cascading termination. All children, grandchildren, etc. are terminated. • The termination is initiated by the operating system. • The parent process may wait for termination of a child process by using the wait()system call. The call returns status information and the pid of the terminated process pid = wait(&status); • If no parent waiting (did not invoke wait()) process is a zombie • If parent terminated without invoking wait , process is an orphan
  • 11. PROCESS HIERARCHIES • Parent creates a child process • Child processes can create their own children • Forms a hierarchy • UNIX calls this a “process group” • If a process exits, its children are “inherited” by the exiting process’s parent • Windows has no concept of process hierarchy • All processes are created equal 11
  • 12. PROCESS STATES • Process in one of 5 states • Created • Ready • Running • Blocked • Exit • Transitions between states 1 - Process enters ready queue 2 - Scheduler picks this process 3 - Scheduler picks a different process 4 - Process waits for event (such as I/O) 5 - Event occurs 6 - Process exits 7 - Process ended by another process 12 Blocked (waiting) Created Exit Ready Running 1 5 4 3 2 7 7 6
  • 13. PROCESSES IN THE OS • Two “layers” for processes • Lowest layer of process-structured OS handles interrupts, scheduling • Above that layer are sequential processes • Processes tracked in the process table • Each process has a process table entry 13 Scheduler 0 1 N-2 N-1 … Processes
  • 14. WHAT’S IN A PROCESS TABLE ENTRY? 14 File management Root directory Working (current) directory File descriptors User ID Group ID Memory management Pointers to text, data, stack or Pointer to page table Process management Registers Program counter CPU status word Stack pointer Process state Priority / scheduling parameters Process ID Parent process ID Signals Process start time Total CPU usage May be stored on stack
  • 15. WHAT HAPPENS ON A TRAP/INTERRUPT? 1. Hardware saves program counter (on stack or in a special register) 2. Hardware loads new PC, identifies interrupt 3. Assembly language routine saves registers 4. Assembly language routine sets up stack 5. Assembly language calls C to run service routine 6. Service routine calls scheduler 7. Scheduler selects a process to run next (might be the one interrupted…) 8. Assembly language routine loads PC & registers for the selected process 15
  • 16. THREADS: “PROCESSES” SHARING MEMORY • Process == address space • Thread == program counter / stream of instructions • Two examples • Three processes, each with one thread • One process with three threads 16 Kernel Kernel Threads Threads System space User space Process 1 Process 2 Process 3 Process 1
  • 17. PROCESS & THREAD INFORMATION 17 Per process items Address space Open files Child processes Signals & handlers Accounting info Global variables Per thread items Program counter Registers Stack & stack pointer State Per thread items Program counter Registers Stack & stack pointer State Per thread items Program counter Registers Stack & stack pointer State
  • 18. THREADS & STACKS 18 Kernel Process Thread 1 Thread 2 Thread 3 Thread 1’s stack Thread 3’s stack Thread 2’s stack User space => Each thread has its own stack!
  • 19. WHY USE THREADS? • Allow a single application to do many things at once • Simpler programming model • Less waiting • Threads are faster to create or destroy • No separate address space • Overlap computation and I/O • Could be done without threads, but it’s harder • Example: word processor • Thread to read from keyboard • Thread to format document 19 Kernel
  • 20. MULTITHREADED WEB SERVER 20 Kernel Network connection Dispatcher thread Worker thread Web page cache while(TRUE) { getNextRequest(&buf); handoffWork(&buf); } while(TRUE) { waitForWork(&buf); lookForPageInCache(&buf,&page); if(pageNotInCache(&page)) { readPageFromDisk(&buf,&page); } returnPage(&page); }
  • 21. THREE WAYS TO BUILD A SERVER • Thread model • Parallelism • Blocking system calls • Single-threaded process: slow, but easier to do • No parallelism • Blocking system calls • Finite-state machine • Each activity has its own state • States change when system calls complete or interrupts occur • Parallelism • Nonblocking system calls • Interrupts 21
  • 22. IMPLEMENTING THREADS 22 Kernel Run-time system Thread table Process table Kernel Thread Process Thread table Process table User-level threads + No need for kernel support - May be slower than kernel threads - Harder to do non-blocking I/O Kernel-level threads + More flexible scheduling + Non-blocking I/O - Not portable
  • 23. PRODUCER-CONSUMER PROBLEM • Paradigm for cooperating processes, producer process produces information that is consumed by a consumer process
  • 24. BOUNDED-BUFFER – SHARED- MEMORY SOLUTION • Shared data • #define BUFFER_SIZE 10 • typedef struct { • . . . • } item; • item buffer[BUFFER_SIZE]; • int in = 0; • int out = 0; • Solution is correct, but can only use BUFFER_SIZE-1 elements
  • 25. BOUNDED-BUFFER – PRODUCER item next_produced; while (true) { /* produce an item in next produced */ while (((in + 1) % BUFFER_SIZE) == out) ; /* do nothing */ buffer[in] = next_produced; in = (in + 1) % BUFFER_SIZE; }
  • 26. BOUNDED BUFFER – CONSUMER item next_consumed; while (true) { while (in == out) ; /* do nothing */ next_consumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; /* consume the item in next consumed */ } 26
  • 27. SCHEDULING • What is scheduling? • Goals • Mechanisms • Scheduling on batch systems • Scheduling on interactive systems • Other kinds of scheduling • Real-time scheduling 27
  • 28. WHY SCHEDULE PROCESSES? • Bursts of CPU usage alternate with periods of I/O wait • Some processes are CPU-bound: they don’t make many I/O requests • Other processes are I/O-bound and make many kernel requests 28 CPU bound I/O bound CPU bursts I/O waits Total CPU usage Total CPU usage Time
  • 29. WHEN ARE PROCESSES SCHEDULED? • At the time they enter the system • Common in batch systems • Two types of batch scheduling • Submission of a new job causes the scheduler to run • Scheduling only done when a job voluntarily gives up the CPU (i.e., while waiting for an I/O request) • At relatively fixed intervals (clock interrupts) • Necessary for interactive systems • May also be used for batch systems • Scheduling algorithms at each interrupt, and picks the next process from the pool of “ready” processes 29
  • 30. Scheduling goals • All systems • Fairness: give each process a fair share of the CPU • Enforcement: ensure that the stated policy is carried out • Balance: keep all parts of the system busy • Batch systems • Throughput: maximize jobs per unit time (hour) • Turnaround time: minimize time users wait for jobs • CPU utilization: keep the CPU as busy as possible • Interactive systems • Response time: respond quickly to users’ requests • Proportionality: meet users’ expectations • Real-time systems • Meet deadlines: missing deadlines is a system failure! • Predictability: same type of behavior for each time slice 30