Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
OPERATING SYSTEMS THREADS TANNENBAUM, SECTION 2-2 THE BASICS • Sometimes called a lightweight process • Process • • • • It’s own memory space Pgm ctr Registers Stack • Thread • Sibling threads share memory • Makes context switching easier • Each thread is separately scheduled IMPLICATION • A program can have 1 or more process • Each process can 1 or more threads • Grown important in windowing applications • One thread could be blocked for i/o • Another—in the same process—could be doing other work • Example: Word programs with three threads • Handle keyboard input • Format page • Save output to disk every n seconds WORD PROCESSOR WITH THREE THREADS Each could be working, while the others are waiting A WEB SERVER EXAMPLE • • • • Some pages are more commonly accessed than others More common pages are kept in memory Less common pages are kept on disk Requests for pages come in: 1. Dispatcher thread hands off request to worker thread (ready to running) 2. Worker thread looks in cache. If not available, worker thread issues read disk request and blocks 3. Dispatcher is scheduled. Go to step 1 THREAD USAGE (2) A multithreaded Web server. Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved. MORE PRECISELY //dispatcher while(TRUE) { get_next_req(&buf); handoff_work(&buf); } //worker while(TRUE) { wait_for_work(&buf); look_in_cache(&buf, &page); if (page_not_in_cache(&page)) read_page_from_disk(&buf, &page); return_page(&page); } PROCESSES VS. THREADS Per Process Items • Address space • Global variables • Open files • Child processes • Pending signals • Accounting information Per Thread Items (each thread shares all of the above) • Program counter • Registers • Stack • State (running, ready, etc.) PTHREADS IEEE standard for Unix threads Pthread_create : creates a new thread Pthread_exit: terminates the threaad Pthread_join: waits for a specific thread to exit Pthread_yield: Release CPU to let another thread run See thread, thread1, thread2 on the web site. • • • • •