8_threads - Part 8: Thread and Thread Synchronization...

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

View Full Document Right Arrow Icon
Part 8: Thread and Thread Synchronization CS252: Systems Programming Ramana Kompella Computer Science Department Purdue University
Background image of page 1

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

View Full DocumentRight Arrow Icon
Introduction to Threads q A thread is a path execution q By default, a C/C++ program has one thread called "main thread" that starts the main() function. main() --- --- printf( "hello\n" ); --- }
Background image of page 2
Introduction to Threads q You can create multiple paths of execution using: Ø POSIX threads ( standard ) pthread_create( &thr_id, attr, func, arg ) Ø Solaris threads thr_create( stack, stack_size, func, arg, flags, &thr_id ) Ø Windows CreateThread(attr, stack_size, func, arg, flags, &thr_id)
Background image of page 3

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

View Full DocumentRight Arrow Icon
Introduction to Threads q Every thread will have its own Ø Stack Ø PC – Program counter Ø Set of registers Ø State q Each thread will have its own function calls, and local variables. q The process table entry will have a stack, set of registers, and PC for every thread in the process.
Background image of page 4
Multi-threaded Program Example #include <pthread.h> void prstr( char *s ){ while( 1 ){ printf( "%s",s); } } int main(){ // thread 2 pthread_create( NULL, NULL, prstr, "b\n" ); // thread 3 pthread_create(NULL, NULL, prstr, "c\n" ); // thread 1 prstr( "a\n" ); }
Background image of page 5

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

View Full DocumentRight Arrow Icon
Multi-threaded Program Example void prstr( char *s ){ while( 1 ){ printf( "%s",s); } } void prstr( char *s ){ while( 1 ){ printf( "%s",s); } } main(): } T1 T2 T3
Background image of page 6
Output: Multi-Threaded Program Example
Background image of page 7

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

View Full DocumentRight Arrow Icon
Applications of Threads q Concurrent Server applications Ø Assume a web server that receives two requests: Ø First, one request from a computer connected through a modem that will take 2 minutes. Ø Then another request from a computer connected to a fast network that will take .01 secs. Ø If the web server is single threaded, the second request will be processed only after 2 minutes. Ø In a multi-threaded server, two threads will be created to process both requests simultaneously. The second request will be processed as soon as it arrives.
Background image of page 8
Application of Threads q Taking Advantage of Multiple CPUs Ø A program with only one thread can use only one CPU. If the computer has multiple cores, only one of them will be used. Ø If a program divides the work among multiple threads, the OS will schedule a different thread in each CPU. Ø This will make the program run faster.
Background image of page 9

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

View Full DocumentRight Arrow Icon
Applications of Threads q Interactive Applications. Ø Threads simplify the implementation of interactive applications that require multiple simultaneous activities. Ø Assume an Internet telephone application with the following threads: v Player thread - receives packets from the internet and plays them. v Capture Thread – captures sound and sends the voice packets v Ringer Server – Receives incoming requests and tells other phones when the phone is busy. Ø Having a single thread doing all this makes the code cumbersome and difficult to read.
Background image of page 10
Threads vs. Processes q Advantages of Threads Ø Fast thread creation - creating a new path of execution is faster than creating a new process with a new virtual memory address space and open file table.
Background image of page 11

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

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

This note was uploaded on 03/06/2012 for the course CS 252 taught by Professor Gustavorodriguez during the Spring '11 term at Purdue University-West Lafayette.

Page1 / 124

8_threads - Part 8: Thread and Thread Synchronization...

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

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