Download presentation
Presentation is loading. Please wait.
1
Concurrency and Threading: Introduction
CSCE 410/611 : Operating Systems Concurrency and Threading: Introduction Concurrency: “Why?” Concurrency: ”How?” Serial, Collaborative, and Preemptive Task Management Concurrent Tasks and Shared Global State Preemptive Task Management: Threads Example: POSIX pthreads Concurrency and Threading: Introduction
2
CSCE 410/611 : Operating Systems
Concurrency: “Why?” 1. Latency Reduction: Apply parallel algorithm. Concurrency in trivially parallelizable problems. client server task task task Concurrency and Threading: Introduction
3
CSCE 410/611 : Operating Systems
Concurrency: “Why?” 1. Latency Reduction: Apply parallel algorithm. Concurrency in trivially parallelizable problems. 2. Latency Hiding: Use concurrency to perform useful work while another operation is pending. (multiprogramming) Latency of operation is not affected, but hidden. client client client server server server task task task Concurrency and Threading: Introduction
4
CSCE 410/611 : Operating Systems
Concurrency: “Why?” 1. Latency Reduction: Apply parallel algorithm. Concurrency in trivially parallelizable problems. 2. Latency Hiding: Use concurrency to perform useful work while another operation is pending. (multiprogramming) Latency of operation is not affected, but hidden. 3. Throughput Increase: Employ multiple concurrent executions of sequential threads to accommodate more simultaneous work. Concurrency is then handled by specialized subsystems (OS, database, etc.) client client server task task Concurrency and Threading: Introduction
5
CSCE 410/611 : Operating Systems
Concurrency: “How?” Definition [Task]: Control flow. client client Serial Task Management: Execute each task to completion before starting new task. server task Concurrency and Threading: Introduction
6
CSCE 410/611 : Operating Systems
Concurrency: “How?” Definition [Task]: Control flow. client client Serial Task Management: Execute each task to completion before starting new task. server task Concurrency and Threading: Introduction
7
CSCE 410/611 : Operating Systems
Concurrency: “How?” Definition [Task]: Control flow. client client Serial Task Management: Execute each task to completion before starting new task. Cooperative Task Management: (Voluntarily) yield CPU at well-defined points in execution. server task task task Concurrency and Threading: Introduction
8
CSCE 410/611 : Operating Systems
Concurrency: “How?” Definition [Task]: Control flow. client client Serial Task Management: Execute each task to completion before starting new task. Cooperative Task Management: (Voluntarily) yield CPU at well-defined points in execution. server task task task you go! Concurrency and Threading: Introduction
9
CSCE 410/611 : Operating Systems
Concurrency: “How?” Definition [Task]: Control flow. client client Serial Task Management: Execute each task to completion before starting new task. Cooperative Task Management: (Voluntarily) yield CPU at well-defined points in execution. server task task task thanks! Concurrency and Threading: Introduction
10
CSCE 410/611 : Operating Systems
Concurrency: “How?” Definition [Task]: Control flow. client client Serial Task Management: Execute each task to completion before starting new task. Cooperative Task Management: (Voluntarily) yield CPU at well-defined points in execution. Preemptive Task Management: Execution of tasks can interleave. server scheduler task task Concurrency and Threading: Introduction
11
CSCE 410/611 : Operating Systems
Concurrency: “How?” Definition [Task]: Control flow. Tasks have access to shared global state. client client Serial Task Management: Execute each task to completion before starting new task. Cooperative Task Management: (Voluntarily) yield CPU at well-defined points in execution. Preemptive Task Management: Execution of tasks can interleave. server scheduler task task task task shared global state Concurrency and Threading: Introduction
12
CSCE 410/611 : Operating Systems
Shared Global State Example – Invariant: credits - debits == balance credits = 120 debits = 120 balance = 0 credits debits balance invariant holds Definition [Invariant]: Predicate that “has to be true at all times”. Concurrency and Threading: Introduction
13
CSCE 410/611 : Operating Systems
Shared Global State Example – Invariant: credits - debits == balance credits = 120 debits = 80 balance = 0 credits debits balance credits = 120 debits = 120 balance = 0 invariant violated! Definition [Invariant]: Predicate that “has to be true at all times”. Concurrency and Threading: Introduction
14
CSCE 410/611 : Operating Systems
Concurrency: “How?” Definition [Task]: Control flow. Tasks have access to shared global state. client client Serial Task Management: Execute each task to completion before starting new task. Cooperative Task Management: (Voluntarily) yield CPU at well-defined points in execution. Preemptive Task Management: Execution of tasks can interleave. server task task shared global state Concurrency and Threading: Introduction
15
Serial Task Management
CSCE 410/611 : Operating Systems Serial Task Management Pros: Only one task is running at a given time. No potential for conflict in accessing shared state. We can define so-called “inter-task invariants”; while one task is running, no other task can violate these invariants. client client server Cons: Only one task is running at a given time! No multiprogramming. No multiprocessor parallelism. task task Concurrency and Threading: Introduction
16
Cooperative Task Management
CSCE 410/611 : Operating Systems Cooperative Task Management Pros: Allows for some controlled multiprogramming. client client server task task task task Concurrency and Threading: Introduction
17
Cooperative Task Management
CSCE 410/611 : Operating Systems Cooperative Task Management Pros: Allows for some controlled multiprogramming. client client server task task task task Concurrency and Threading: Introduction
18
Cooperative Task Management
CSCE 410/611 : Operating Systems Cooperative Task Management Pros: Allows for some controlled multiprogramming. Invariants must be ensured at yielding points only. client client server you go! task task task task Concurrency and Threading: Introduction
19
Cooperative Task Management
CSCE 410/611 : Operating Systems Cooperative Task Management Pros: Allows for some controlled multiprogramming. Invariants must be ensured at yielding points only. client client server thanks! Cons: Invariants are not automatically enforced. task task Concurrency and Threading: Introduction
20
Preemptive Task Management
CSCE 410/611 : Operating Systems Preemptive Task Management Pros: Allows for high level of multiprogramming. Parallelism client client scheduler server task task Concurrency and Threading: Introduction
21
Preemptive Task Management
CSCE 410/611 : Operating Systems Preemptive Task Management Pros: Allows for high level of multiprogramming. Parallelism client client scheduler server task task Concurrency and Threading: Introduction
22
Preemptive Task Management
CSCE 410/611 : Operating Systems Preemptive Task Management Pros: Allows for high level of multiprogramming. Parallelism client client scheduler server next! huh?! task task Concurrency and Threading: Introduction
23
Preemptive Task Management
CSCE 410/611 : Operating Systems Preemptive Task Management Pros: Allows for high level of multiprogramming. Parallelism client client scheduler server huh?! task task Concurrency and Threading: Introduction
24
Preemptive Task Management
CSCE 410/611 : Operating Systems Preemptive Task Management Pros: Allows for high level of multiprogramming. Parallelism client client scheduler server Cons: Invariants are must hold at all times (!?) Mechanism: synchronization task task ?! Concurrency and Threading: Introduction
25
Preemptive Task Management: “How?”
CSCE 410/611 : Operating Systems Preemptive Task Management: “How?” Scheduler manages threads Process executable data segment file descr. registers stack scheduler thread of control Concurrency and Threading: Introduction
26
Preemptive Task Management: “How?”
CSCE 410/611 : Operating Systems Preemptive Task Management: “How?” Scheduler manages threads Process executable data segment file descr. Thread 1 registers registers stack Thread 2 registers stack stack scheduler Concurrency and Threading: Introduction
27
Example: POSIX pthreads
CSCE 410/611 : Operating Systems Example: POSIX pthreads pthread_create (create a thread) pthread_cancel (terminate another thread) pthread_detach (have thread release res’s) pthread_equal (two thread id’s equal?) pthread_exit (exit a thread) pthread_kill (send a signal to a thread) pthread_join (wait for a thread) pthread_self (what is my id?) Concurrency and Threading: Introduction
28
Example: POSIX pthreads
CSCE 410/611 : Operating Systems Example: POSIX pthreads pthread_create (create a thread) pthread_cancel (terminate another thread) pthread_detach (have thread release res’s) pthread_equal (two thread id’s equal?) pthread_exit (exit a thread) pthread_kill (send a signal to a thread) pthread_join (wait for a thread) pthread_self (what is my id?) int pthread_create(pthread_t * thread, const pthread_attr_t * attr, void *(*start_routine)(void *), void * arg) fd = open(“my.dat”, O_RDONLY); if (error = pthread_create(&t_id, NULL, processfd, &fd)) fprintf(stderr, “Failed create thread: %s\n”, strerror(error)); Concurrency and Threading: Introduction
29
pthreads: Thread Attributes
CSCE 410/611 : Operating Systems pthreads: Thread Attributes attribute objects pthread_attr_destroy pthread_attr_init state pthread_attr_getdetachstate pthread_attr_setdetachstate stack pthread_attr_getguardsize pthread_attr_setguardsize pthread_attr_getstack pthread_attr_setstack scheduling pthread_attr_getinheritedsched pthread_attr_setinheritedsched pthread_attr_getschedparam pthread_attr_setschedparam pthread_attr_getschedpolicy pthread_attr_setschedpolicy pthread_attr_getscope pthread_attr_setscope Concurrency and Threading: Introduction
30
pthreads: Thread Attributes – State
CSCE 410/611 : Operating Systems pthreads: Thread Attributes – State attribute objects pthread_attr_destroy pthread_attr_init state pthread_attr_getdetachstate pthread_attr_setdetachstate stack pthread_attr_getguardsize pthread_attr_setguardsize pthread_attr_getstack pthread_attr_setstack scheduling pthread_attr_getinheritedsched pthread_attr_setinheritedsched pthread_attr_getschedparam pthread_attr_setschedparam pthread_attr_getschedpolicy pthread_attr_setschedpolicy pthread_attr_getscope pthread_attr_setscope Detached threads release resources when terminate. Attached states hold on to resources until parent thread calls pthread_join. Concurrency and Threading: Introduction
31
pthreads: Thread Attributes – Stack
CSCE 410/611 : Operating Systems pthreads: Thread Attributes – Stack attribute objects pthread_attr_destroy pthread_attr_init state pthread_attr_getdetachstate pthread_attr_setdetachstate stack pthread_attr_getguardsize pthread_attr_setguardsize pthread_attr_getstack pthread_attr_setstack scheduling pthread_attr_getinheritedsched pthread_attr_setinheritedsched pthread_attr_getschedparam pthread_attr_setschedparam pthread_attr_getschedpolicy pthread_attr_setschedpolicy pthread_attr_getscope pthread_attr_setscope setstack defines location and size of stack. setguardsize allocates additional memory. If the thread overflows into this extra memory, an error is generated. Concurrency and Threading: Introduction
32
pthreads: Thread Attributes – Scheduling
CSCE 410/611 : Operating Systems pthreads: Thread Attributes – Scheduling attribute objects pthread_attr_destroy pthread_attr_init state pthread_attr_getdetachstate pthread_attr_setdetachstate stack pthread_attr_getguardsize pthread_attr_setguardsize pthread_attr_getstack pthread_attr_setstack scheduling pthread_attr_getinheritedsched pthread_attr_setinheritedsched pthread_attr_getschedparam pthread_attr_setschedparam pthread_attr_getschedpolicy pthread_attr_setschedpolicy pthread_attr_getscope pthread_attr_setscope PTHREAD_INHERIT_SCHED defines that scheduling parameters are inherited from parent thread. (as opposed to PTHREAD_EXPLICIT_SCHED). Scheduling policies: SCHED_FIFO, SCHED_RR, SCHED_SPORADIC, SCHED_OTHER, … contention scope defines whether process competes at process level or at system level for resources. Concurrency and Threading: Introduction
33
Concurrency and Threading: Introduction
CSCE 410/611 : Operating Systems Concurrency and Threading: Introduction Concurrency: “Why?” Concurrency: ”How?” Serial Task Management Collaborative Task Management Preemptive Task Management Concurrent Tasks and Shared Global State Preemptive Task Management: Threads Example: POSIX pthreads Concurrency and Threading: Introduction
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.