Thread_Magic - How one process can do two things at once...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: How one process can do two things at once Thread of execution? Share process memory but each has its own call-stack Create, Wait, Destroy How to use the POSIX API 'PThreads' Threads and Processes When multi-threaded processes die Copyright : University of Il inois CS 241 Staff Thread Magic Threads vs. Processes Process fork is expensive (time & memory) Thread Lightweight process Shared data space Does not require lots of memory or startup time Copyright : University of Il inois CS 241 Staff Processes vs. Threads a) Three processes each with one thread b) One process with three threads Copyright : University of Il inois CS 241 Staff Environment (resource) execution Process and Threads Each process can include many threads All threads of a process share: Process ID Memory (program code and global data) Open file/socket descriptors Semaphores Signal handlers and signal dispositions Working environment (current directory, user ID, etc.) Copyright : University of Il inois CS 241 Staff Thread Usage: Word Processor Working file can only be accessed by one process at a time Copyright : University of Il inois CS 241 Staff What would happen when this is single- threaded? Thread Usage: Word Processor Working file can only be accessed by one process at a time Copyright : University of Il inois CS 241 Staff Thread Usage: Web Server Copyright : University of Il inois CS 241 Staff What would happen if this were single- threaded? Web Server Pseudo-code for previous slide Dispatcher thread Worker thread Alternative Dispatcher thread Worker thread Copyright : University of Il inois CS 241 Staff while (TRUE) { get_next_request(&buf); handoff_work(&buf); } while (TRUE) { wait_for_work(&buf); look_for_page_in_cache(&buf, &page); if (page_not_in_cache( &page)) read_page_from_disk(&buf, &page); return_page(&page); } while (TRUE) { get_next_request(&buf); handoff_work(&buf); } work (&buf) { look_for_page_in_cache(&buf, &page); if (page_not_in_cache( &page)) read_page_from_disk(&buf, &page); return_page(&page); } What is the difference? Thread of Execution Sequential set of instructions Function calls & automatic (local) variables Need Program Counter and Stack for each thread Copyright : University of Il inois CS 241 Staff Compare: Normal function call (1 thread) Copyright : University of Il inois CS 241 Staff processfd(); processfd() { } Calling program Called function Thread of execution Compare: Threaded function call Copyright : University of Il inois CS 241 Staff pthread_create(); processfd() { } Creatingprogram Created thread processfd(); processfd() { } Cal ing program Cal ed function Thread creation Thread of execution...
View Full Document

Page1 / 8

Thread_Magic - How one process can do two things at once...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online