You are on page 1of 18

CSCI 332

Operating Systems

POSIX threads

Prof. Dimitrios Zorbas


Today’s class

Week 3: Threads
– Issues with threads
– Linux threads
– POSIX Threads
– Examples
Issues with threads

What will happen if a thread invokes fork?
– Copy the entire process?
– Copy only that thread?

How can we send signals to a specific thread?

How can we stop a thread while it is executing
something?
– Asynchronous and deferred cancellation
Threads on Linux

Does not distinguish between threads and
processes

The clone() system call is used to determine what
will be shared with the child (thread) task
– Flags are used to do so (e.g., CLONE_FS, CLONE_VM)
– man clone for more info
POSIX

Portable Operating System Interface

Standard to unify the programs and system calls
that many different OS provide

https://en.wikipedia.org/wiki/POSIX for more
information
Pthreads

Pthreads (POSIX threads) is a standard or API for
doing threading

Specification, not implementation

Can be implemented using User threading or
Kernel threading

Users can be oblivious of the underlying
implementation
Linux Thread Implementation

Linux Native POSIX Thread Library (LNPT)
– Since kernel v2.6

Library: Implemented in the form of a library (that you have to link to your
program)
– #include <pthread.h>

Rely on kernel to create / schedule threads
→ Comparison with Windows Thread API
– Not POSIX compliant but Pthreads ports exist
Pthread API

man pthreads

int pthread_create(pthread_t * thread,


const pthred_attr_t * attr,
void * (*start_routine)(void *),
void * arg);

void pthread_exit(void * value_ptr);


int pthread_join(pthread_t thread, void ** value_ptr);
int pthread_cancel(pthread_t thread);
int pthread_detach(void);
pthread_create()
Arguments:

thread - returns the thread id. (unsigned long int defined in bits/pthreadtypes.h)

attr - Set to NULL if default thread attributes are used. (else define members of the struct pthread_attr_t defined
in bits/pthreadtypes.h) Attributes include:
– detached state (joinable? Default: PTHREAD_CREATE_JOINABLE. Other option: PTHREAD_CREATE_DETACHED)
– scheduling policy (real-time? PTHREAD_INHERIT_SCHED,PTHREAD_EXPLICIT_SCHED,SCHED_OTHER) scheduling
parameter
– inheritsched attribute (Default: PTHREAD_EXPLICIT_SCHED Inherit from parent thread: PTHREAD_INHERIT_SCHED)
– scope (Kernel threads: PTHREAD_SCOPE_SYSTEM User threads: PTHREAD_SCOPE_PROCESS Pick one or the other not
both.)
– guard size
– stack address (See unistd.h and bits/posix_opt.h _POSIX_THREAD_ATTR_STACKADDR)
– stack size (default minimum PTHREAD_STACK_SIZE set in pthread.h),

void * (*start_routine) - pointer to the function to be threaded. Function has a single argument: pointer to void.

*arg - pointer to argument of function. To pass multiple arguments, send a pointer to a structure.
Example
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

void *print_message_function( void *ptr ){


char *message;
message = (char *) ptr;
printf("%s \n", message);
}

int main(){
pthread_t thread1, thread2;
char *message1 = "Hello from Thread 1";
char *message2 = "Hello from Thread 2";

int ret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);


int ret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);

return 0;
}
Questions

Are these two threads, orphan threads ?


Do these two threads invoke a system call ?
pthread_join()

The main process waits for a thread to terminate

int pthread_join(pthread_t thread, void **retval);


Change the previous code to use pthread_join()
pthread_self()

Returns the thread id (this is not the PID!)


Change the previous code to print out the thread
ids
pthread_yield()

Gives up using CPU resources so other threads can
make use of the CPU

Implemented using sched_yield()
– Requires #include <sched.h>
To be continued...
Any questions?
What to read

Dino book: 4.4.1, 4.6.1-3, 4.7.2

Tanenbaum book: 2.2.2–2.2.6, 10.3-10.3.3
Next class:

Week 3: Threads
– Commands
– OpenMP
– Pthreads examples

You might also like