DOC PREVIEW
U of I CS 241 - Lecture notes

This preview shows page 1-2-17-18-19-35-36 out of 36 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 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 36 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 36 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 36 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 36 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 36 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 36 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

PthreadsContentsOverview: threads vs. processes (created with fork)Thread componentsSlide 5Normal function callThreaded function callSlide 8Slide 9Slide 10Slide 11The Thread IDCreating a threadDetaching and joiningHow to make a thread detachedHow a thread can detach itselfSlide 17Slide 18Exiting and CancellationSlide 20Slide 21Passing parameters and returning valuesUser Threads and Kernel ThreadsKernel-level threads are part of the OS.Slide 25Slide 26Slide 27Thread AttributesSettable properties of thread attributesSlide 30Create a detached threadThe thread stackThread schedulingCreate a thread that contends with other processesPOSIX functions that are not required to be thread-safe.Summary01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved1PthreadsCS 241 Lecture 8(R: Chapter 12 pp 409-443)Andrew Bennett01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved2ContentsProcesses vs ThreadsHOWTO: pthreadspthread attributes01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved3Overview: threads vs. processes (created with fork) PropertyProcesses created with forkThreads of a process Ordinary function callsvariables get copies of all variables share 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 simultaneouslySequential01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved4Thread componentsaddress space: code and global variables open files semaphores signals timers process ID A thread has its own program counter and stack, but shares a number of resources with its process and other threads of the process:01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved5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.01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved6Normal function call01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved7Threaded function call01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved8A function that is used as a thread must have a special format.It takes a single parameter of type pointer to void and returns a pointer to void. The parameter type allows any pointer to be passed.This can point to a structure, so in effect, the function can use any number of parameters.We will discuss the meaning of the return value later.01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved9The processfd function used above might have prototype:void *processfd(void *arg);Instead of passing the file descriptor to be monitored directly, we pass a pointer to it.The function my access the parameter as follows:====== int fd;====== fd = *((int *)arg);01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved10POSIX function descriptionpthread_cancelterminate another threadpthread_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 ID01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved11Most POSIX functions return 0 on success and a nonzero error code on failure.They do not set errno but the value returned when an error occurs has the value that errno would have.None of the POSIX thread functions ever return the error EINTR.01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved12The Thread IDEach thread has an id of type pthread_t.On most systems this is just an integer (like a process ID) but it does not have to be. A thread can get its ID with pthread_self and IDs can be compared with pthread_equal pthread_t pthread_self(void); int pthread_equal(thread_t t1, pthread_t t2);01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved13Creating a threadA thread is created with pthread_create int 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. •We will discuss thread attributes later. For now, pass a null pointer to indicate the default attributes. •The third parameter is just the name of the program for the thread to run. •The last parameter is a pointer to the arguments.01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved14Detaching and joiningA thread exits when its function returns.There are other ways for a thread to exit. Just like with processes, some of the threads resources stay around until it has been waited for or its process terminates. You wait for a thread with pthread_join. You can make a thread release its resources when it terminates by making it a detached thread. A detached thread cannot be joined. One way to make a thread detached is with pthread_detach int pthread_detach(pthread_t thread);01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved15How to make a thread detachedvoid *processfd(void *arg); int error; int fd pthread_t tid; if (error = pthread_create(&tid, NULL, processfd, &fd)) fprintf(stderr, "Failed to create thread: %s\n", strerror(error)); else if (error = pthread_detach(tid)) fprintf(stderr, "Failed to detach thread: %s\n", strerror(error));01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved16How a thread can detach itselfvoid *detachfun(void *arg) { int i = *((int *)(arg)); if (!pthread_detach(pthread_self())) return NULL; fprintf(stderr, "My argument is %d\n", i); return NULL; }01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved17A thread can pass a value to another thread through its return value. This is something like the return value that a parent gets from its child through its exit status. You are not restricted to a small integer as with processes. The return value is a pointer to arbitrary data. Since the threads all share an address space, this is simple to do. However, you must take care. Examples later. The pthread_join function suspends the calling


View Full Document

U of I CS 241 - Lecture notes

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

47 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 Lecture notes
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 Lecture notes 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 Lecture notes 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?