Threads - Operating Systems - Lecture Slides | COMP 310, Study notes of Operating Systems

Material Type: Notes; Class: Operating Systems; Subject: Computer Science; University: University of San Diego; Term: Fall 2004;

Typology: Study notes

Pre 2010

Uploaded on 08/18/2009

koofers-user-d01
koofers-user-d01 🇺🇸

8 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
COMP 310:
Operating Systems
Lecture 2: Threads
September 10, 2004
Christine Alvarado
(Based on slides by Geoffrey Voelker)
Today’s Topics
!Threads:
!Sort of like processes
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Threads - Operating Systems - Lecture Slides | COMP 310 and more Study notes Operating Systems in PDF only on Docsity!

COMP 310:

Operating Systems

Lecture 2: Threads

September 10, 2004

Christine Alvarado

(Based on slides by Geoffrey Voelker)

Today’s Topics

! Threads:

! Sort of like processes…

Last Time

! Processes:

! What were the benefits of processes? ! Allow many programs to run at the same time ! Use system resources efficiently Last Time

! Processes:

! 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!

Process fork()

thread_fork()

Threads

! Separate concept of process from its

execution state

! Share address space, privileges, and

resources within a process

! Have their own execution state

! One process can have many threads

Code Data Files registers stack thread Single Threaded (^) Multithreaded Code Data Files registersregisters registers stack stack stack Processes

Who creates threads?

! The OS: Kernel-level threads

! Kernel handles and schedules both threads and processes

! The user: User-level threads

! 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()?

! When do you fork the threads?

thread_yield()

! What would this code print out?

while ( 1 ) { cout << “hello”; thread.yield(); } Hello thread while ( 1 ) { cout << “goodbye”; thread.yield(); } Goodbye thread More on thread_yield

! Thread chooses to give CPU to another

thread

! It invokes a voluntary context switch

! So what does it mean for thread_yield() to

return?

! What did our program print out?

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

! A number of issues (for user-level 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