DOC PREVIEW
U of I CS 241 - Threads

This preview shows page 1-2-3-22-23-24-45-46-47 out of 47 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 47 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 47 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 47 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 47 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 47 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 47 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 47 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 47 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 47 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 47 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

ThreadsSlide 2Creating a ThreadNormal function callThreaded function callThreads vs. ProcessesThreads versus ProcessesPthread OperationsExample ProgramThread Usage: Word ProcessorThread Usage: Web ServerWeb ServerThings Suitable for threadingThread PackagesImplementing Threads in User Space (Old Linux)User-level ThreadsTrade-offs?Hybrid Implementations (Solaris)Creating a thread with pthreadExample 1: Thread CreationExample 2: Passing Parameters to a ThreadSlide 22Example 3: FilesExiting and CancellationSlide 25Thread ExitCancel that thread!Zombies, Thread Detach & JoinDetaching a ThreadHow to make a Thread DetachedHow a thread can detach itself“Waiting” on a Thread: pthread_join()Pthread_joinQuiz: (Example 4) on Fork()Example on Fork()Slide 36Slide 37Slide 38Slide 39Slide 40Slide 41Slide 42Slide 43Slide 44Slide 45Slide 46Slide 47Copyright ©: Nahrstedt, Angrave, Abdelzaher 1ThreadsCopyright ©: Nahrstedt, Angrave, Abdelzaher2Processes versus ThreadsCopyright ©: Nahrstedt, Angrave, Abdelzaher3Creating a ThreadWhen a new thread is created it runs concurrently with the creating process.When creating a thread you indicate which function the thread should execute.Copyright ©: Nahrstedt, Angrave, Abdelzaher4Normal function callCopyright ©: Nahrstedt, Angrave, Abdelzaher5Threaded function callCopyright ©: Nahrstedt, Angrave, Abdelzaher6Threads vs. ProcessesEach thread execute separatelyThreads in the same process share resourcesNo protection among threads!!Copyright ©: Nahrstedt, Angrave, Abdelzaher7PropertyProcesses created with forkThreads of a process Ordinary function callsvariablesget copies of all variablesshare global variables share global variablesIDs get new process IDsshare the same process ID but have unique thread IDshare the same process ID (and thread ID)CommunicationMust explicitly communicate, e.g.pipesor use small integer return valueMay communicate with return valueor shared variablesif done carefullyMay communicate with return valueor shared variables(don't have to be careful)Parallelism (one CPU)Concurrent Concurrent SequentialParallelism (multiple CPUs)May be executed simultaneouslyKernel threads may be executed simultaneouslySequentialThreads versus ProcessesCopyright ©: Nahrstedt, Angrave, Abdelzaher8Pthread OperationsPOSIX function descriptionpthread_createcreate a threadpthread_detachset thread to release resourcespthread_equaltest two thread IDs for equalitypthread_exitexit a thread without exiting processpthread_killsend a signal to a threadpthread_joinwait for a threadpthread_selffind out own thread IDCopyright ©: Nahrstedt, Angrave, Abdelzaher9Example Program#include <pthread.h>#include <stdio.h>void *threadex(void *); int main() { pthread_t tid; /* stores the new thread ID */ pthread_create(&tid, NULL, threadex, NULL); /*create a new thread*/ pthread_join(tid, NULL); /*main thread waits for new thread to terminate */ return 0; /* main thread exits */ } void *threadex(void *arg) /*thread routine*/ { int i; for (i=0; i<5; i++) fprintf(stderr, "Hello, world! \n "); return NULL; }Copyright ©: Nahrstedt, Angrave, Abdelzaher10Thread Usage: Word ProcessorWhat if it is single-threaded?Copyright ©: Nahrstedt, Angrave, Abdelzaher11Thread Usage: Web ServerCopyright ©: Nahrstedt, Angrave, Abdelzaher12Web ServerRough outline of code for previous slide(a) Dispatcher thread(b) Worker threadCopyright ©: Nahrstedt, Angrave, Abdelzaher13Things Suitable for threadingBlock for potentially long waitsUse many CPU cycles Must respond to asynchronous events Are of lesser or greater importance than other tasks Are able to be performed in parallel with other tasksCopyright ©: Nahrstedt, Angrave, Abdelzaher14Thread PackagesKernel thread packages Implemented and supported at kernel levelUser-level thread packagesImplemented at user levelCopyright ©: Nahrstedt, Angrave, Abdelzaher15Implementing Threads in User Space (Old Linux)A user-level threads packageCopyright ©: Nahrstedt, Angrave, Abdelzaher16User-level ThreadsCopyright ©: Nahrstedt, Angrave, Abdelzaher17Trade-offs?Kernel thread packages Each thread can make blocking I/O callsCan run concurrently on multiple processorsThreads in User-level Fast context switchCustomized schedulingCopyright ©: Nahrstedt, Angrave, Abdelzaher18Hybrid Implementations (Solaris)Multiplexing user-level threads onto kernel-level threadsCopyright ©: Nahrstedt, Angrave, Abdelzaher19Creating a thread with pthreadA thread is created withint pthread_create( pthread_t *restrict thread, const pthread_attr_t *restrict attr, void *(*start_routine)(void *), void *restrict arg); The creating process (or thread) must provide a location for storage of the thread id. The third parameter is just the name of the function for the thread to run. The last parameter is a pointer to the arguments.Copyright ©: Nahrstedt, Angrave, Abdelzaher20Example 1: Thread Creation#include <pthread.h> #include <stdio.h> #define NUM_THREADS 5 void *PrintHello(void *threadid) { int tid; tid = (int)threadid; printf("Hello World! It's me, thread #%d!\n", tid); pthread_exit(NULL); } int main (int argc, char *argv[]) { pthread_t threads[NUM_THREADS]; int rc, t; for(t=0; t<NUM_THREADS; t++){ printf("In main: creating thread %d\n", t); rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t); if (rc) { printf("ERROR code is %d\n", rc); exit(-1); } } pthread_exit(NULL); }Copyright ©: Nahrstedt, Angrave, Abdelzaher21Example 2: Passing Parameters to a Thread#include <pthread.h>#include <stdio.h>#include <stdlib.h>#define NUM_THREADS 5void *PrintHello(void *ptr){ char *filename; int j; filename = (char *) ptr; while (1) { printf("Hello World! It's me, thread %s!\n", filename); for (j=1; j++; j<500); } pthread_exit(NULL);}Copyright ©: Nahrstedt, Angrave, Abdelzaher22Example 2: Passing Parameters to a Threadint main (int argc, char *argv[]){ pthread_t thread[100]; int err_code, i=0; char *filename; printf ("Enter thread name at any time to create thread\n"); while (1) { filename = (char *) malloc (80*sizeof(char)); scanf ("%s", filename); printf("In main: creating thread %d\n", i); err_code = pthread_create(&thread[i], NULL, PrintHello, (void *)filename); if (err_code){ printf("ERROR code is %d\n", err_code); exit(-1); } else i++; } pthread_exit(NULL);}Copyright ©: Nahrstedt, Angrave,


View Full Document

U of I CS 241 - Threads

Documents in this Course
Process

Process

28 pages

Files

Files

37 pages

File I/O

File I/O

52 pages

C Basics

C Basics

69 pages

Memory

Memory

23 pages

Threads

Threads

14 pages

Lecture

Lecture

55 pages

C Basics

C Basics

24 pages

Signals

Signals

27 pages

Memory

Memory

45 pages

Threads

Threads

28 pages

LECTURE

LECTURE

45 pages

Threads

Threads

30 pages

Threads

Threads

55 pages

Files

Files

37 pages

SIGNALS

SIGNALS

22 pages

Files

Files

37 pages

Threads

Threads

14 pages

Threads

Threads

13 pages

Load more
Download Threads
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Threads and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Threads 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?