systems programming
play

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


  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

  2. Threads Thread Ya3ni 5ai6

  3. A process , in the simplest terms, is an executing program. One or more threads run in the context of the process . A thread is the basic unit to which the operating system allocates processor time. A thread can execute any part of the process code, including parts currently being executed by another thread

  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

  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;

  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

  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.

  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);

  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)

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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend