Presentation is loading. Please wait.

Presentation is loading. Please wait.

EECE.4810/EECE.5730 Operating Systems

Similar presentations


Presentation on theme: "EECE.4810/EECE.5730 Operating Systems"— Presentation transcript:

1 EECE.4810/EECE.5730 Operating Systems
Instructor: Dr. Michael Geiger Spring 2019 Lecture 9: Threads

2 Operating Systems: Lecture 8
Lecture outline Announcements/reminders Program 1 due today Write one program that does everything Objective list is an outline that could be used to guide development, but feel free to skip steps If wait() returns -1, called from process without child! Please respond to the exam scheduling poll Today’s lecture Review: multithreading basics Continue threading discussion Pthread example programs 8/26/2019 Operating Systems: Lecture 8

3 Operating Systems: Lecture 6
Review: Threads Thread: active sequence of instructions Basic unit of CPU utilization Thread creation is lightweight Multiple threads in same process can share address space Each thread needs own PC, register copies, stack + SP Threads provide concurrency within application HW support necessary for parallelism Major issue: non-deterministic ordering Solutions require atomic operations Avoid race condition: solution depends on timing/ordering of earlier events 8/26/2019 Operating Systems: Lecture 6

4 Operating Systems: Lecture 8
Example Thread A Thread B i = 0 i = 0 while (i < 10) while (i > -10) i++ i-- print “A done” print “B done” Which thread finishes first? Is winner guaranteed to print first? Is it guaranteed that one thread will win? What’s required to guarantee one thread will win? 8/26/2019 Operating Systems: Lecture 8

5 Multithreaded debugging
Non-deterministic ordering makes debugging difficult “Heisenbug”: bug that occurs non-deterministically All possible interleavings must be correct Race condition: output/result dependent on timing or ordering of earlier events Becomes bug when unanticipated ordering occurs Potentially disastrous consequences Over-radiation in Therac-25 2 modes of operation: direct low-power radiation, high-powered radiation + safeguards Race condition activated high-powered beam w/o safeguards Northeast blackout of 2003 Race condition in control software 8/26/2019 Operating Systems: Lecture 8

6 Operating Systems: Lecture 8
Synchronization Constrain interleavings between threads Goal: force all possible interleavings to produce correct result Correct concurrent program should work regardless of processor speed Try to constrain as little as possible Some events are independent—order irrelevant Order only matters in dependent events Synchronization: Controlling execution and order of threads 8/26/2019 Operating Systems: Lecture 8

7 Operating Systems: Lecture 6
Pthreads POSIX thread API (Pthreads) supported on most systems Necessary functions included in <pthread.h> Files may need to be compiled with –pthread or –lpthread option Library contains functions for: Thread creation and termination Thread joining (forced waiting  simple synch) Synchronization (locks, condition variables, barriers) Other thread management 8/26/2019 Operating Systems: Lecture 6

8 Operating Systems: Lecture 6
Pthread examples Three example programs Functions covered pthread_create(thread, attr, start_routine, arg) pthread_exit(status) pthread_join(thread, status) 8/26/2019 Operating Systems: Lecture 6

9 Operating Systems: Lecture 8
Final notes Next time Detailed synchronization discussion Reminders: Program 1 due today Write one program that does everything Objective list is an outline that could be used to guide development, but feel free to skip steps If wait() returns -1, called from process without child! 8/26/2019 Operating Systems: Lecture 8

10 Operating Systems: Lecture 8
Acknowledgements These slides are adapted from the following sources: Silberschatz, Galvin, & Gagne, Operating Systems Concepts, 9th edition Chen & Madhyastha, EECS 482 lecture notes, University of Michigan, Fall 2016 8/26/2019 Operating Systems: Lecture 8


Download ppt "EECE.4810/EECE.5730 Operating Systems"

Similar presentations


Ads by Google