Lec22-POSIX Threads

Lec22-POSIX Threads - National University of Computer &...

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

View Full Document Right Arrow Icon
Operating System Operating System Spring 09 Spring 09 National University National University of Computer & Emerging of Computer & Emerging Sciences Sciences Lec22-POSIX Threads Lec22-POSIX Threads Prepared by Prepared by Uzma Maroof Uzma Maroof uzma.maroof@nu.edu.pk uzma.maroof@nu.edu.pk
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Reference Reference Introduction to POSIX Threads Programming.htm (only the thread creation, termination and suspend part)
Background image of page 2
3 Thread state Thread state Each thread has its own stack and local variables Globals are shared. File descriptors are shared. If one thread closes a file, all other threads can’t use The file I/O operations block the calling thread. Some other functions act on the whole process. For example, the exit() function operates terminates the entire and all associated threads.
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 Thread Implementation Thread Implementation The most important of these APIs, in the Unix world, is the one developed by the group known as POSIX. POSIX is a standard API supported Portable across most UNIX platforms. PTHREAD library contains implementation of POSIX standard To link this library to your program use lpthread gcc MyThreads.c -o MyThreadExecutable - lpthread
Background image of page 4
5 Thread Creation Thread Creation pthread_create ( pthread_t * threadid ,const pthread_attr_t * attr , void *(* start_routine )(void *),void * arg ); This routine creates a new thread and makes it executable. Thread stack is allocated and thread control block is created Once created, a thread may create other threads. Note that an "initial thread" exists by default and is the thread which runs main. Returns zero, if ok Returns Non-zero if error threadid The routine returns the new thread ID via the threadid The caller can use this thread ID to perform various operations This ID should be checked to ensure that the thread was successfully created.
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 Thread Creation Thread Creation pthread_create ( pthread_t * threadid ,const pthread_attr_t * attr , void *(* start_routine )(void *),void * arg );
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 21

Lec22-POSIX Threads - National University of Computer &...

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

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