Docsity

Understanding Threads and Processes: A Comparison and Posix Threads Implementation, Slides of Computer Networks

Partial preview of the text

Download Understanding Threads and Processes: A Comparison and Posix Threads Implementation and more Slides Computer Networks in PDF only on Docsity!

Lecture 18

Threaded Programming

Threads vs. Processes

  • Creation of a new process using fork is expensive - Time - Memory
  • A thread does not require lots of memory or startup time - called a lightweight process

pthread_create()

Process A Thread 1 Global Variables

Code

Stack

Process A Thread 2

Stack

pthread_create()

Multiple Threads

Each process can include many threads.

All threads of a process share:

  • memory
    • program code and global data
  • open file/socket descriptors
  • signal handlers and signal dispositions
  • working environment
    • current directory, user ID, etc.

Posix Threads

  • We will focus on Posix Threads
    • most widely supported threads programming API.
  • Solaris
    • you need to link with โ€œ- lpthread โ€
  • On many systems, this also forces the compiler to link in re-entrant libraries - instead of plain C libraries

Thread Creation

pthread_create(

**pthread_t tid, const pthread_attr_t attr, void (func)( **void *), void *arg );**

func is the function to be called.

When func() returns the thread is terminated

pthread_t *tid

  • The book says you can specify NULL for

tid (thread ID),

  • this doesn't always work!
  • Thread attributes can be set using attr ,
  • including detached state and scheduling policy.
  • You can specify NULL and get the system defaults.

Thread IDs

  • Each thread has a unique ID, a thread can find out it's ID by calling pthread_self().
  • Thread IDs are of type pthread_t which is

usually an unsigned int. When debugging, it's often useful to do something like this:

  • printf("Thread %u:\n",pthread_self());

Thread args example

struct { int x,y } 2ints;

**void *blah( void *arg) { struct 2ints foo = (struct 2ints ) arg; printf("%u sum of %d and %d is %d\n", pthread_self(), foo->x, foo->y, foo->x+foo->y); return(NULL); }

Thread Lifespan

  • Once a thread is created,
    • it starts executing function func() specified in the call to pthread_create()
  • If func() returns, thread is terminated
  • A thread can also be terminated by calling pthread_exit()
  • If main() returns or any thread calls exit() Docsity.com

Joinable Thread

  • Joinable: on thread termination the thread ID and exit status are saved by the OS.
  • One thread can "join" another by calling pthread_join - which waits (blocks) until a specified thread exits

int pthread_join( pthread_t Docsity.com

Shared Global Variables

**int counter=0; void pancake(void arg) { counter++; printf("Thread %u is number %d\n", pthread_self(), counter); } main() { int i; pthread_t tid; for (i=0;i<10;i++) pthread_create(&tid,NULL,pancake,NULL); }

Avoiding Problems

  • pthreads includes support for Mutual Exclusion primitives that can be used to protect against this problem.
  • The general idea is to lock something before accessing global variables and to unlock as soon as you are done.
  • Shared socket descriptors should be treated as global variables!!! Docsity.com

pthread_mutex

  • A global variable of type pthread_mutex_t is required:

pthread_mutex_t counter_mtx= PTHREAD_MUTEX_INITIALIZER;

  • Initialization to PTHREAD_MUTEX_INITIALIZER is required for a static variable!