






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Class: Operating Systems; Subject: Computer Science; University: University of San Diego; Term: Fall 2004;
Typology: Study notes
1 / 12
This page cannot be seen from the preview
Don't miss anything!







(Based on slides by Geoffrey Voelker)
! Sort of like processes…
Last Time
! What were the benefits of processes? ! Allow many programs to run at the same time ! Use system resources efficiently Last Time
! What resources did processes need to keep track of? ! Address space (code and data) ! System resources (e.g. open files), accounting info ! Execution state " PC " SP " Regs
But what did it really need? ! Shared with Parent ! Address space ! Code ! Data ! System resources ! Open files ! Network connections ! Privileges ! Needs own copy ! Execution state ! PC ! Stack ! Registers Solution: Threads!
thread_fork()
Threads
Code Data Files registers stack thread Single Threaded (^) Multithreaded Code Data Files registersregisters registers stack stack stack Processes
Who creates threads?
! Kernel handles and schedules both threads and processes
! Kernel handles processes, but user is in charge of creating and destroying (and scheduling) threads ! Why is this still an OS concept? K/L threads vs. U/L threads ! Kernel-level ! Scheduled by OS: if thread blocks, OS can handle it ! Thread requires system level call: slow ! Threads must be general for all apps ! User-level ! No scheduling by OS—can block entire process (what are the implications) ! Thread requires procedure call: fast ! No need for generality because implemented by U/L library Why not use both?
Interaction between U/L and K/L threads ! Many-to One ! Efficient, simple ! But threads unable to run on multiple processor ! One thread can block process ! One-to-One ! More concurrency ! Simple ! More overhead—can create too many threads ! Many-to-many ! More complex ! But solves other shortcomings What about fork() and exec()?
thread_yield()
while ( 1 ) { cout << “hello”; thread.yield(); } Hello thread while ( 1 ) { cout << “goodbye”; thread.yield(); } Goodbye thread More on thread_yield
Implementing thread_yield thread_yield { thread_t old_thread = current_thread; current_thread = getNextThread(); append_to_queue( ready_queue, old_thread ); context_switch( old_thread, current_thread ); return; } Thread control switches in the middle!!! context_switch does the magic What is append_to_queue for? Implementing Threads
! Interface between threads (communication) ! Context switching ! How do they get scheduled ! How are they SYNCHRONIZED? (very important, topic of several upcoming lectures) ! You will implement this functionality in NACHOS