Systems programming Thread management Most of the slides in this - - PowerPoint PPT Presentation

systems programming
SMART_READER_LITE
LIVE PREVIEW

Systems programming Thread management Most of the slides in this - - PowerPoint PPT Presentation

Systems programming Thread management Most of the slides in this lecture are either from or adapted from the slides provided by Dr. Ahmad Barghash Threads Thread Ya3ni 5ai6 A process , in the simplest terms, is an executing program. One or


slide-1
SLIDE 1

Systems programming

Thread management

Most of the slides in this lecture are either from or adapted from the slides provided by Dr. Ahmad Barghash

slide-2
SLIDE 2

Threads

Thread Ya3ni 5ai6

slide-3
SLIDE 3

A process, in the simplest terms, is an executing program. One or more threads run in the context

  • f the process. A thread is the

basic unit to which the

  • perating system allocates

processor time. A thread can execute any part of the process code, including parts currently being executed by another thread

slide-4
SLIDE 4

Thread management in C

All C programs using pthreads need to include the pthread.h header file (i.e.: #include <pthread.h>). There are four steps for creating a basic threaded program:

  • 1. Define thread reference variables
  • 2. Create an entry point for the thread
  • 3. Create the thread
  • 4. Join everything back up
slide-5
SLIDE 5

Defi fine thread reference variables

  • The variable type pthread_t is a means of referencing threads.
  • A pthread_t variable must exist for every created thread.

Example: pthread_t thread0;

slide-6
SLIDE 6

Create an entry ry point for the thread

  • The thread must point to a function for it to start execution.
  • The function must return void * and take a single void *

argument. For example: void *my_entry_function(void *param);

A void pointer can hold address of any type and can be typcasted to any type

slide-7
SLIDE 7

Create a thread

  • Once the pthread_t variable has been defined and the entry point function

created, we can create the thread using pthread_create.

  • Four arguments are needed:
  • 1. a pointer to the pthread_t variable,
  • 2. extra attribute (don’t worry about this for now – just set it to NULL)
  • 3. a pointer to the function to call (ie: the name of the entry point)
  • 4. The pointer being passed as the argument to the function.

Example: pthread_create(&thread0, NULL, my_entry_function, &param); If successful, the pthread_create() function returns a zero value. Otherwise, an error number is returned to indicate the error.

slide-8
SLIDE 8

Jo Join every rything back up

  • When the newly-created thread has finished doing it’s bits, we need

to join everything back up.

  • This is done by the pthread_join function which takes two

parameters:

  • 1. The pthread_t variable used when pthread_create was called

(not a pointer this time)

  • 2. A pointer to the return value pointer (don’t worry about this for

now – just set it to NULL). Example: pthread_join(thread0, NULL);

slide-9
SLIDE 9

All together

  • #include <pthread.h>
  • Define a worker function
  • void *foo(void *args)
  • Initialize pthread attribute to NULL
  • Create a thread
  • pthread t thread;
  • pthread create(&thread, &attr, worker function, arg);
  • Join everything back up
  • Exit current thread
  • pthread exit(status)
slide-10
SLIDE 10

First example

#include <pthread.h> #include <stdio.h> /* this function is run by the second thread */ void *inc_x(void *x_void_ptr) { /* increment x to 100 */ int *x_ptr = (int *)x_void_ptr; while(++(*x_ptr) < 100); printf("x increment finished\n"); return NULL; } int main() { int x = 0, y = 0; printf("x: %d, y: %d\n", x, y); pthread_t inc_x_thread; if(pthread_create(&inc_x_thread, NULL, inc_x, &x)) { fprintf(stderr, "Error creating thread\n"); return 1; } while(++y < 100); printf("y increment finished\n"); if(pthread_join(inc_x_thread, NULL)) { fprintf(stderr, "Error joining thread\n"); return 2; } /* show the results - x is now 100 thanks to the second thread */ printf("x: %d, y: %d\n", x, y); return 0; } x: 0, y: 0 y increment finished x increment finished x: 100, y: 100