SlideShare a Scribd company logo
1 of 48
Operating Systems
WEEK-07
Department of Software
Engineering, FOIT, UCP,
Lahore.
References:
[1] Silberschatz, A., Galvin, P. B., & Gagne, G. (2018). Operating System
Concepts, 10e Abridged Print Companion. John Wiley & Sons. Chapter-01
[2] Stallings, W., & Paul, G. K. (2012). Operating systems: internals and design
principles (Vol. 9). New York: Pearson. Chapter-01
Agenda
Threads
 What is a Thread?
 Single-threaded processes vs. multithreaded
processes
 Parallelism (Data and Task)
 Multithreading Models
• Many-to-One Model
• One-to-One Model
• Many-to-Many Model
Agenda (Continue…)
Multi-Threading Programming
 Thread Libraries
 Thread creation
 Parameter passing to threads
 Returning value from threads
 Threading Issues
 Thread Cancellation
 Implicit Threading
 Thread Pools
Sequential Programming
4
4
Punjab University College Of Information And Technology (PUCIT)
Sequential Programming
1
3
2
Suppose we want to add eight numbers x1
, x2
, x3
, .... x8
There are seven addition operations and if each
operation take 1 CPU cycle, the entire operation will
take seven cycles
x1
+ x2
+ x3
+ x4
+ x5
+ x6
+ x7
+ x8
7
.
.
.
Concurrent / Parallel Programming
5
5
Punjab University College Of Information And Technology (PUCIT)
Concurrent/Parallel Programming
Instructor:Arif Butt
Suppose we have 4xCPUs or a 4xCore CPU, the seven addition
operations can now be completed in just three CPU cycles, by
dividing the task among different CPUs
CPU4
x1
+ x2
CPU 1
CPU1 CPU3
CPU2
CPU2
CPU1
1st
CPU cycle
2nd
CPU cycle
3rd
CPU cycle
x3
+ x4
x5
+ x6
x7
+ x8
R1
+ R2
R3
+ R4
R5
+ R6
Ways to Achieve Concurrency
6
Processes and Threads
7
8
main()
{
…
f1(…);
…
f2(…);
…
}
f1(…)
{ … }
f2(…)
{ … }
Thread
Process
Terminate
d
f2
f1
Single Threaded Process
Thread Concept
9
 Previous slide is an example of a process with a single
thread
 Suppose we want that f1 and f2 should be executed by
separate threads, while main function is executed
concurrently by another thread
 Multi threading refers to the ability of an OS to
support multiple threads of execution with in a single
process. A single program made up of a number of
different concurrent activities; sometimes called
multitasking, as in Ada
 Multithreading works similar to multiprogramming. The
CPU switches rapidly back and forth among threads
providing the illusion that threads are running in parallel
Multi Threaded Process
10
main()
{
…
thread(t1,f1);
…
thread(t2,f2);
…
}
f1(…)
{ … }
f2(…)
{ … }
mai
n
t
2
t
1
Process Address Space
PC
PC
PC
11
Single and Multithreaded Processes
12
(a) Three processes, each with one thread
(b) One process with three threads
execution
Environment (resource)
Processes are used to group resources together; Threads are the
entities scheduled for execution on the CPU.
Single and Multithreaded Processes
13
Multi-Threaded Process
14
Single and Multithreaded Processes
15
• Each thread stack contains one frame for each procedure that has been called but not yet
returned from
• This frame contains the procedure’s local variables and their return address to use when
the procedure call has finished
• For example, if procedure X calls procedure Y and this one calls procedure Z, while Z is
executing the frames for X, Y, Z will be on the stack
• Each thread will generally call different procedures and thus has a different execution
history
Each Thread has its own Stack
Threads vs Processes
16
Similarities
 A thread can also be in one of many states like new,
ready, running, blocked, terminated
 Like processes only one thread is in running state
(single CPU)
 Like processes a thread can create a child thread
Differences
 No “automatic” protection mechanism is in place for
threads—they are meant to help each other
 Every process has its own address space, while all
threads within a process operate within the same
address space
Benefits of Threads
17
 Responsiveness. Multi-threaded servers (e.g., browsers)
can allow interaction with user while a thread is formulating
response to a previous user query (e.g., rendering a web page)
 Resource sharing. Process resources (code, data, etc.)
are shared by all threads. OS resources (PCB, PPFDT, etc.) are
also shared by all threads
 Economy. Take less time to create, schedule, and
terminate a thread. Solaris 2: Thread Creation is thirty times
faster than Process Creation and Thread Switching is five
times faster than process switching
 Performance. In multi-processor and multi-threaded
architectures (e.g., Intel’s P-IV HT) each thread may run on a
different processor in parallel
Disadvantages of Threads
18
 Problems due to Shared memory
 Race Problem
 Mutual Exclusion needs to be implemented on shared
memory to allow multiple threads to access data for
write/update
 Many library functions are not Thread Safe
 For resource sharing, synchronization is needed
between threads
 Lack of Robustness
 A severe error caused by one thread (e.g. segmentation
fault) terminates the whole process
Thread Usage: Web Server
19
• Dispatcher thread reads incoming requests from the NW.
• After examining the request, it chooses an idle worker thread and hands it the
request. It also wakes up the worker from blocked state to ready state.
• Worker now checks to see if the request can be satisfied from the Web page
cache, to which all threads have access. 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.
Request for pages
comes in and the
requested page is
sent back to the
client.
User Threads
20
 Thread management is done by user-level threads
libraries (eg, POSIX Pthread, Win32 threads, Java
threads, Solaris2 Threads, Mach C Threads) and
Kernel is not aware of the existence of threads
 An application can be programmed to be multithreaded
by using user level thread library, which contains code
for:
 Thread creation and termination
 Thread scheduling
 Saving and restoring thread context
 Passing messages and data between threads
 By default an application begins with a single thread,
within a process managed by Kernel. Later the
application may spawn a new thread within the same
process by invoking spawn utility in the thread library
User Threads vs Kernel Threads
21
Implementing Threads in User Space
22
User-level Threads
• Thread library used.
• Thread table
maintained in user
space.
• All thread management
is done in user space
by library
• Kernel knows nothing
about threads.
E.g.: pthread library
Kernel Threads
23
 Thread management done by kernel and
Kernel is aware of threads
 Kernel level threads are supported in almost
all modern operating systems:
 Windows NT/XP/2000
 Linux
 Solaris
 Tru64 UNIX
 Mac OS X
 There must be a relationship between user threads
and kernel threads. There exist three common
models of this interaction (1:1, M:1 and M:N)
Implementing Threads in the Kernel
24
Kernel-level Threads
• OS knows about
individual threads
within each process.
• Thread table
maintained by
kernel.
• E.g.: Windows
2K/XP
25
Thread Implementation Models
Thread Implementation Model (M:1)
26
Thread Implementation Model (1:1)
27
Thread Implementation Model (M:N)
28
29
Thread Libraries
30
 Thread libraries provides programmers with API for
creating and managing threads
 Two primary ways of implementing
 Library entirely in user space
 Kernel-level library supported by OS
 Pthreads may be provided either as user-level or kernel-
level. Pthread is a POSIX standard (IEEE 1003.1c) API for
thread creation and synchronization
 Java Threads managed by the JVM. Typically
implemented using the threads model provided by
underlying OS. Java threads may be created by: Extending
Thread class, or by implementing the Runnable interface.
If underlying OS is Windows, then implemented using
Win32 API; if it is Linux, then Pthreads.
31
Call Description
pthread_create() Similar to fork()
pthead_join() Similar to waitpid()
pthread_exit(void *status) To terminate a thread
Thread Management
Important POSIX System Calls
• Thread operations include thread creation, termination,
synchronization (joins, blocking), scheduling, data
management and process interaction.
• A thread does not maintain a list of created threads, nor
does it know the thread that created it.
32
Thread Creation
33
Thread Termination
34
Joining a Thread
Example 0
35
void f1();
void f2();
int main(){
f1();
f2();
printf("nBye Bye from mainn");
return 0;
}
void f1(){
for(int i=0; i<5; i++){
printf("%s", "PUCIT");
sleep(1);
}
}
void f1(){
for(int i=0; i<5; i++){
printf("%s", ”ARIF");
sleep(1);
}
}
Example 1
36
void* f1(void*);
void* f2(void*);
int main(){
pthread_t tid1, tid2;
pthread_create(&tid1, NULL, f1, NULL);
pthread_create(&tid2, NULL, f2, NULL);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
printf("nBye Bye from main threadn");
return 0;
}
void * f1(void * arg){
for(int i=0; i<5; i++){
printf("%s", “ABC");
fflush(stdout);
sleep(1);
}
pthread_exit(NULL);
}
void * f2(void * arg){
for(int i=0; i<5; i++){
printf("%s", “XYZ");
fflush(stdout);
sleep(1);
}
return NULL;
}
37
Compiling a multi-threaded Program
 Multi-Threaded program need to be linked with the
thread library /usr/lib/libpthread.so
$ gcc -c t1.c -D_REENTRANT
$ gcc t1.o -o t1 –lpthread
$./t1
PUCITARIFARIFPUCITARIFPUCITPUCITARIFPUCITARIF
Bye Bye from main thread
Question: What are the advantages of compiling multi-
threaded programs using the –D_REENTRANT flag?
Example 2
38
int main(){
pthread_t tid1, tid2;
pthread_create(&tid1, NULL, f1, NULL);
pthread_create(&tid2, NULL, f2, NULL);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
printf("nBye Bye from main threadn");
return 0;
}
void * f1(void * arg){
for(int i=0; i<1000;i++)
fprintf(stderr, "%c",'X');
pthread_exit(NULL);
}
void * f2(void * arg){
for(int i=0; i<800;i++)
fprintf(stderr, "%c",’O');
pthread_exit(NULL);
}
Example 3
39
int main(int argc, char* argv[]){
int countofX = atoi(argv[1]);
int countofO = atoi(argv[2]);
pthread_t tid1, tid2;
pthread_create(&tid1, NULL, f1, (void*)&countofX);
pthread_create(&tid2, NULL, f2, (void*)&countofO);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
printf("nBye Bye from main threadn");
return 0;}
void * f1(void * arg){
int ctr = *((int*)arg);
for(int i=0; i<ctr; i++)
fprintf(stderr, "%c", 'X');
pthread_exit(NULL);
}
void * f2(void * arg){
int ctr = *((int*)arg);
for(int i=0; i<ctr; i++)
fprintf(stderr, "%c", ’O');
pthread_exit(NULL);}
Example 4
40
struct mystruct{
char character; int count;
};
void * f1(void *);
int main(){
pthread_t tid1, tid2;
struct mystruct t1_args, t2_args;
t1_args.character = 'X'; t1_args.count = 1000;
pthread_create(&tid1, NULL, f1, (void*)&t1_args);
t2_args.character = 'O'; t2_args.count = 800;
pthread_create(&tid2, NULL, f1, (void*)&t2_args);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
printf("nBye Bye from main thread.n");
return 0;}
void * f1(void * args){
struct mystruct p = *(struct mystruct*)args;
for (int i = 0; i < p.count; i++)
putc(p.character,stdout);
pthread_exit(NULL);
}
Example 5
41
int main(int argc, char* argv[]){
if(argc != 3){
printf("Must pass two file names.n");
exit(1);
}
pthread_t tid1, tid2;
pthread_create(&tid1, NULL, func, (void*)argv[1]);
pthread_create(&tid2, NULL, func, (void*)argv[2]);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
printf(”Bye Bye from main threadn");
return 0;
}
void* func(void* args){
char* filename = (char*)args;
int ctr = 0; char ch;
int fd = open(filename, O_RDONLY);
while((read(fd, &ch, 1)) != 0)
ctr++;
close(fd);
printf("Characters in %s: %dn", filename, ctr);
pthread_exit(NULL);}
42
Threading Issues
Points to Ponder
43
Points to Ponder
44
Points to Ponder
45
Points to Ponder
46
Implicit Threading
 Growing in popularity as numbers of threads increase,
program correctness more difficult with explicit threads
 Creation and management of threads done by compilers and
run-time libraries rather than programmers
 Three methods explored
• Thread Pools
• OpenMP
• Grand Central Dispatch
 Other methods include Microsoft Threading Building Blocks
(TBB), java.util.concurrent package
Scheduler Activations
 Both M:M and Two-level models require
communication to maintain the appropriate
number of kernel threads allocated to the
application
 Typically use an intermediate data structure
between user and kernel threads – lightweight
process (LWP)
• Appears to be a virtual processor on which
process can schedule user thread to run
• Each LWP attached to kernel thread
• How many LWPs to create?
 Scheduler activations provide upcalls - a
communication mechanism from the kernel to
the upcall handler in the thread library
 This communication allows an application to
maintain the correct number kernel threads

More Related Content

Similar to WEEK07operatingsystemdepartmentofsoftwareengineering.pptx

Similar to WEEK07operatingsystemdepartmentofsoftwareengineering.pptx (20)

Java multi thread programming on cmp system
Java multi thread programming on cmp systemJava multi thread programming on cmp system
Java multi thread programming on cmp system
 
Threading.pptx
Threading.pptxThreading.pptx
Threading.pptx
 
Topic 4- processes.pptx
Topic 4- processes.pptxTopic 4- processes.pptx
Topic 4- processes.pptx
 
Intro To .Net Threads
Intro To .Net ThreadsIntro To .Net Threads
Intro To .Net Threads
 
Operating System Chapter 4 Multithreaded programming
Operating System Chapter 4 Multithreaded programmingOperating System Chapter 4 Multithreaded programming
Operating System Chapter 4 Multithreaded programming
 
Operating Systems - "Chapter 4: Multithreaded Programming"
Operating Systems - "Chapter 4:  Multithreaded Programming"Operating Systems - "Chapter 4:  Multithreaded Programming"
Operating Systems - "Chapter 4: Multithreaded Programming"
 
posix.pdf
posix.pdfposix.pdf
posix.pdf
 
Concept of thread
Concept of threadConcept of thread
Concept of thread
 
multi-threading
multi-threadingmulti-threading
multi-threading
 
Sucet os module_2_notes
Sucet os module_2_notesSucet os module_2_notes
Sucet os module_2_notes
 
Real-Time Scheduling Algorithms
Real-Time Scheduling AlgorithmsReal-Time Scheduling Algorithms
Real-Time Scheduling Algorithms
 
Multi Threading
Multi ThreadingMulti Threading
Multi Threading
 
Chapter04 new
Chapter04 newChapter04 new
Chapter04 new
 
Posix threads(asha)
Posix threads(asha)Posix threads(asha)
Posix threads(asha)
 
process and thread.pptx
process and thread.pptxprocess and thread.pptx
process and thread.pptx
 
PThreads Vs Win32 Threads
PThreads  Vs  Win32 ThreadsPThreads  Vs  Win32 Threads
PThreads Vs Win32 Threads
 
Ch04 threads
Ch04 threadsCh04 threads
Ch04 threads
 
Threads and multi threading
Threads and multi threadingThreads and multi threading
Threads and multi threading
 
Threads
ThreadsThreads
Threads
 
Ch4 Threads
Ch4 ThreadsCh4 Threads
Ch4 Threads
 

Recently uploaded

Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacingjaychoudhary37
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 

Recently uploaded (20)

Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacing
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 

WEEK07operatingsystemdepartmentofsoftwareengineering.pptx

  • 1. Operating Systems WEEK-07 Department of Software Engineering, FOIT, UCP, Lahore. References: [1] Silberschatz, A., Galvin, P. B., & Gagne, G. (2018). Operating System Concepts, 10e Abridged Print Companion. John Wiley & Sons. Chapter-01 [2] Stallings, W., & Paul, G. K. (2012). Operating systems: internals and design principles (Vol. 9). New York: Pearson. Chapter-01
  • 2. Agenda Threads  What is a Thread?  Single-threaded processes vs. multithreaded processes  Parallelism (Data and Task)  Multithreading Models • Many-to-One Model • One-to-One Model • Many-to-Many Model
  • 3. Agenda (Continue…) Multi-Threading Programming  Thread Libraries  Thread creation  Parameter passing to threads  Returning value from threads  Threading Issues  Thread Cancellation  Implicit Threading  Thread Pools
  • 4. Sequential Programming 4 4 Punjab University College Of Information And Technology (PUCIT) Sequential Programming 1 3 2 Suppose we want to add eight numbers x1 , x2 , x3 , .... x8 There are seven addition operations and if each operation take 1 CPU cycle, the entire operation will take seven cycles x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 7 . . .
  • 5. Concurrent / Parallel Programming 5 5 Punjab University College Of Information And Technology (PUCIT) Concurrent/Parallel Programming Instructor:Arif Butt Suppose we have 4xCPUs or a 4xCore CPU, the seven addition operations can now be completed in just three CPU cycles, by dividing the task among different CPUs CPU4 x1 + x2 CPU 1 CPU1 CPU3 CPU2 CPU2 CPU1 1st CPU cycle 2nd CPU cycle 3rd CPU cycle x3 + x4 x5 + x6 x7 + x8 R1 + R2 R3 + R4 R5 + R6
  • 6. Ways to Achieve Concurrency 6
  • 8. 8 main() { … f1(…); … f2(…); … } f1(…) { … } f2(…) { … } Thread Process Terminate d f2 f1 Single Threaded Process
  • 9. Thread Concept 9  Previous slide is an example of a process with a single thread  Suppose we want that f1 and f2 should be executed by separate threads, while main function is executed concurrently by another thread  Multi threading refers to the ability of an OS to support multiple threads of execution with in a single process. A single program made up of a number of different concurrent activities; sometimes called multitasking, as in Ada  Multithreading works similar to multiprogramming. The CPU switches rapidly back and forth among threads providing the illusion that threads are running in parallel
  • 10. Multi Threaded Process 10 main() { … thread(t1,f1); … thread(t2,f2); … } f1(…) { … } f2(…) { … } mai n t 2 t 1 Process Address Space PC PC PC
  • 11. 11
  • 12. Single and Multithreaded Processes 12 (a) Three processes, each with one thread (b) One process with three threads execution Environment (resource) Processes are used to group resources together; Threads are the entities scheduled for execution on the CPU.
  • 13. Single and Multithreaded Processes 13
  • 15. Single and Multithreaded Processes 15 • Each thread stack contains one frame for each procedure that has been called but not yet returned from • This frame contains the procedure’s local variables and their return address to use when the procedure call has finished • For example, if procedure X calls procedure Y and this one calls procedure Z, while Z is executing the frames for X, Y, Z will be on the stack • Each thread will generally call different procedures and thus has a different execution history Each Thread has its own Stack
  • 16. Threads vs Processes 16 Similarities  A thread can also be in one of many states like new, ready, running, blocked, terminated  Like processes only one thread is in running state (single CPU)  Like processes a thread can create a child thread Differences  No “automatic” protection mechanism is in place for threads—they are meant to help each other  Every process has its own address space, while all threads within a process operate within the same address space
  • 17. Benefits of Threads 17  Responsiveness. Multi-threaded servers (e.g., browsers) can allow interaction with user while a thread is formulating response to a previous user query (e.g., rendering a web page)  Resource sharing. Process resources (code, data, etc.) are shared by all threads. OS resources (PCB, PPFDT, etc.) are also shared by all threads  Economy. Take less time to create, schedule, and terminate a thread. Solaris 2: Thread Creation is thirty times faster than Process Creation and Thread Switching is five times faster than process switching  Performance. In multi-processor and multi-threaded architectures (e.g., Intel’s P-IV HT) each thread may run on a different processor in parallel
  • 18. Disadvantages of Threads 18  Problems due to Shared memory  Race Problem  Mutual Exclusion needs to be implemented on shared memory to allow multiple threads to access data for write/update  Many library functions are not Thread Safe  For resource sharing, synchronization is needed between threads  Lack of Robustness  A severe error caused by one thread (e.g. segmentation fault) terminates the whole process
  • 19. Thread Usage: Web Server 19 • Dispatcher thread reads incoming requests from the NW. • After examining the request, it chooses an idle worker thread and hands it the request. It also wakes up the worker from blocked state to ready state. • Worker now checks to see if the request can be satisfied from the Web page cache, to which all threads have access. 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. Request for pages comes in and the requested page is sent back to the client.
  • 20. User Threads 20  Thread management is done by user-level threads libraries (eg, POSIX Pthread, Win32 threads, Java threads, Solaris2 Threads, Mach C Threads) and Kernel is not aware of the existence of threads  An application can be programmed to be multithreaded by using user level thread library, which contains code for:  Thread creation and termination  Thread scheduling  Saving and restoring thread context  Passing messages and data between threads  By default an application begins with a single thread, within a process managed by Kernel. Later the application may spawn a new thread within the same process by invoking spawn utility in the thread library
  • 21. User Threads vs Kernel Threads 21
  • 22. Implementing Threads in User Space 22 User-level Threads • Thread library used. • Thread table maintained in user space. • All thread management is done in user space by library • Kernel knows nothing about threads. E.g.: pthread library
  • 23. Kernel Threads 23  Thread management done by kernel and Kernel is aware of threads  Kernel level threads are supported in almost all modern operating systems:  Windows NT/XP/2000  Linux  Solaris  Tru64 UNIX  Mac OS X  There must be a relationship between user threads and kernel threads. There exist three common models of this interaction (1:1, M:1 and M:N)
  • 24. Implementing Threads in the Kernel 24 Kernel-level Threads • OS knows about individual threads within each process. • Thread table maintained by kernel. • E.g.: Windows 2K/XP
  • 29. 29
  • 30. Thread Libraries 30  Thread libraries provides programmers with API for creating and managing threads  Two primary ways of implementing  Library entirely in user space  Kernel-level library supported by OS  Pthreads may be provided either as user-level or kernel- level. Pthread is a POSIX standard (IEEE 1003.1c) API for thread creation and synchronization  Java Threads managed by the JVM. Typically implemented using the threads model provided by underlying OS. Java threads may be created by: Extending Thread class, or by implementing the Runnable interface. If underlying OS is Windows, then implemented using Win32 API; if it is Linux, then Pthreads.
  • 31. 31 Call Description pthread_create() Similar to fork() pthead_join() Similar to waitpid() pthread_exit(void *status) To terminate a thread Thread Management Important POSIX System Calls • Thread operations include thread creation, termination, synchronization (joins, blocking), scheduling, data management and process interaction. • A thread does not maintain a list of created threads, nor does it know the thread that created it.
  • 35. Example 0 35 void f1(); void f2(); int main(){ f1(); f2(); printf("nBye Bye from mainn"); return 0; } void f1(){ for(int i=0; i<5; i++){ printf("%s", "PUCIT"); sleep(1); } } void f1(){ for(int i=0; i<5; i++){ printf("%s", ”ARIF"); sleep(1); } }
  • 36. Example 1 36 void* f1(void*); void* f2(void*); int main(){ pthread_t tid1, tid2; pthread_create(&tid1, NULL, f1, NULL); pthread_create(&tid2, NULL, f2, NULL); pthread_join(tid1, NULL); pthread_join(tid2, NULL); printf("nBye Bye from main threadn"); return 0; } void * f1(void * arg){ for(int i=0; i<5; i++){ printf("%s", “ABC"); fflush(stdout); sleep(1); } pthread_exit(NULL); } void * f2(void * arg){ for(int i=0; i<5; i++){ printf("%s", “XYZ"); fflush(stdout); sleep(1); } return NULL; }
  • 37. 37 Compiling a multi-threaded Program  Multi-Threaded program need to be linked with the thread library /usr/lib/libpthread.so $ gcc -c t1.c -D_REENTRANT $ gcc t1.o -o t1 –lpthread $./t1 PUCITARIFARIFPUCITARIFPUCITPUCITARIFPUCITARIF Bye Bye from main thread Question: What are the advantages of compiling multi- threaded programs using the –D_REENTRANT flag?
  • 38. Example 2 38 int main(){ pthread_t tid1, tid2; pthread_create(&tid1, NULL, f1, NULL); pthread_create(&tid2, NULL, f2, NULL); pthread_join(tid1, NULL); pthread_join(tid2, NULL); printf("nBye Bye from main threadn"); return 0; } void * f1(void * arg){ for(int i=0; i<1000;i++) fprintf(stderr, "%c",'X'); pthread_exit(NULL); } void * f2(void * arg){ for(int i=0; i<800;i++) fprintf(stderr, "%c",’O'); pthread_exit(NULL); }
  • 39. Example 3 39 int main(int argc, char* argv[]){ int countofX = atoi(argv[1]); int countofO = atoi(argv[2]); pthread_t tid1, tid2; pthread_create(&tid1, NULL, f1, (void*)&countofX); pthread_create(&tid2, NULL, f2, (void*)&countofO); pthread_join(tid1, NULL); pthread_join(tid2, NULL); printf("nBye Bye from main threadn"); return 0;} void * f1(void * arg){ int ctr = *((int*)arg); for(int i=0; i<ctr; i++) fprintf(stderr, "%c", 'X'); pthread_exit(NULL); } void * f2(void * arg){ int ctr = *((int*)arg); for(int i=0; i<ctr; i++) fprintf(stderr, "%c", ’O'); pthread_exit(NULL);}
  • 40. Example 4 40 struct mystruct{ char character; int count; }; void * f1(void *); int main(){ pthread_t tid1, tid2; struct mystruct t1_args, t2_args; t1_args.character = 'X'; t1_args.count = 1000; pthread_create(&tid1, NULL, f1, (void*)&t1_args); t2_args.character = 'O'; t2_args.count = 800; pthread_create(&tid2, NULL, f1, (void*)&t2_args); pthread_join(tid1, NULL); pthread_join(tid2, NULL); printf("nBye Bye from main thread.n"); return 0;} void * f1(void * args){ struct mystruct p = *(struct mystruct*)args; for (int i = 0; i < p.count; i++) putc(p.character,stdout); pthread_exit(NULL); }
  • 41. Example 5 41 int main(int argc, char* argv[]){ if(argc != 3){ printf("Must pass two file names.n"); exit(1); } pthread_t tid1, tid2; pthread_create(&tid1, NULL, func, (void*)argv[1]); pthread_create(&tid2, NULL, func, (void*)argv[2]); pthread_join(tid1, NULL); pthread_join(tid2, NULL); printf(”Bye Bye from main threadn"); return 0; } void* func(void* args){ char* filename = (char*)args; int ctr = 0; char ch; int fd = open(filename, O_RDONLY); while((read(fd, &ch, 1)) != 0) ctr++; close(fd); printf("Characters in %s: %dn", filename, ctr); pthread_exit(NULL);}
  • 47. Implicit Threading  Growing in popularity as numbers of threads increase, program correctness more difficult with explicit threads  Creation and management of threads done by compilers and run-time libraries rather than programmers  Three methods explored • Thread Pools • OpenMP • Grand Central Dispatch  Other methods include Microsoft Threading Building Blocks (TBB), java.util.concurrent package
  • 48. Scheduler Activations  Both M:M and Two-level models require communication to maintain the appropriate number of kernel threads allocated to the application  Typically use an intermediate data structure between user and kernel threads – lightweight process (LWP) • Appears to be a virtual processor on which process can schedule user thread to run • Each LWP attached to kernel thread • How many LWPs to create?  Scheduler activations provide upcalls - a communication mechanism from the kernel to the upcall handler in the thread library  This communication allows an application to maintain the correct number kernel threads