Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: PONTIFICIA UNIVERSIDAD CATLICA DEL PER FACULTAD DE CIENCIAS E INGENIERA SISTEMAS OPERATIVOS (4ta hoja de trabajos prcticos) (1er perodo de 2006) Horarios 0881, 0883: Prof. V. Khlebnikov Duracin: 1 h. 50 min. The QNX Neutrino Microkernel: Barriers A barrier is a synchronization mechanism that lets you "corral" several cooperating threads (e.g. in a matrix computation), forcing them to wait at a specific point until all have finished before any one thread can continue. Unlike the pthread_join() function, where you'd wait for the threads to terminate, in the case of a barrier you're waiting for the threads to rendezvous at a certain point. When the specified number of threads arrive at the barrier, we unblock all of them so they can continue to run. You first create a barrier with pthread_barrier_init(): #include <pthread.h> int pthread_barrier_init (pthread_barrier_t *barrier, const pthread_barrierattr_t *attr, unsigned int count); This creates a barrier object at the passed address (a pointer to the barrier object is in barrier), with the attributes as specified by attr. The count member holds the number of threads that must call pthread_barrier_wait(). Once the barrier is created, each thread will call pthread_barrier_wait() to indicate that it has completed: #include <pthread.h> int pthread_barrier_wait (pthread_barrier_t *barrier); When a thread calls pthread_barrier_wait(), it blocks until the number of threads specified initially in the pthread_barrier_init() function have called pthread_barrier_wait() (and blocked also). When the correct number of threads have called pthread_barrier_wait(), all those threads will unblock at the same time. Here's an example: /* * */ barrier1.c <stdio.h> <time.h> <pthread.h> <sys/neutrino.h> barrier; // barrier synchronization object #include #include #include #include pthread_barrier_t main () // ignore arguments { time_t now; // create a barrier object with a count of 3 pthread_barrier_init (&barrier, NULL, 3); // start up two threads, thread1 and thread2 pthread_create (NULL, NULL, thread1, NULL); pthread_create (NULL, NULL, thread2, NULL); // at this point, thread1 and thread2 are running // now wait for completion time (&now); printf ("main() waiting for barrier at %s", ctime (&now)); pthread_barrier_wait (&barrier); // after this point, all three threads have completed. time (&now); printf ("barrier in main() done at %s", ctime (&now)); } INF231 - Sistemas Operativos Prctica 4 (2006-1) 1 void * thread1 (void *not_used) { time_t now; time (&now); printf ("thread1 starting at %s", ctime (&now)); // do the computation // let's just do a sleep here... sleep (20); pthread_barrier_wait (&barrier); // after this point, all three threads have completed. time (&now); printf ("barrier in thread1() done at %s", ctime (&now)); } void * thread2 (void *not_used) { time_t now; time (&now); printf ("thread2 starting at %s", ctime (&now)); // do the computation // let's just do a sleep here... sleep (40); pthread_barrier_wait (&barrier); // after this point, all three threads have completed. time (&now); printf ("barrier in thread2() done at %s", ctime (&now)); } The main thread created the barrier object and initialized it with a count of the total number of threads that must be synchronized to the barrier before the threads may carry on. In the example above, we used a count of 3: one for the main() thread, one for thread1(), and one for thread2(). Then we start thread1() and thread2(). To simplify this example, we have the threads sleep to cause a delay, as if computations were occurring. To synchronize, the main thread simply blocks itself on the barrier, knowing that the barrier will unblock only after the two worker threads have joined it as well. In this release, the following barrier functions are included: Function pthread_barrierattr_getpshared() pthread_barrierattr_destroy() pthread_barrierattr_init() pthread_barrierattr_setpshared() pthread_barrier_destroy() pthread_barrier_init() pthread_barrier_wait() Description Get the value of a barrier's process-shared attribute Destroy a barrier's attributes object Initialize a barrier's attributes object Set the value of a barrier's process-shared attribute Destroy a barrier Initialize a barrier Synchronize participating threads at the barrier Reescriba en C para QNX el siguiente programa: program twoslots; var twoslots : array[false..true, 1..3] of integer; slot : boolean; process writer; var I : integer; begin for I := 1 to 30 do begin twoslots[slot,1] := I; twoslots[slot,2] := I; twoslots[slot,3] := I; slot := not slot end end; INF231 - Sistemas Operativos Prctica 4 (2006-1) 2 process reader; var I : integer; begin for I := 1 to 40 do begin write(twoslots[not slot,1], ' '); write(twoslots[not slot,2], ' '); write(twoslots[not slot,3], ' '); writeln end end; begin slot := true; twoslots[false,1] := 0; twoslots[false,2] := 0; twoslots[false,3] := 0; cobegin writer; reader coend; end. { initial value } creando un hilo de ejecucin para el lector y tres hilos separados para el escritor donde cada hilo suyo se encargar en modificacin del elemento correspondiente del vector. Estos tres hilos del escritor ejecutarn la misma funcin pasndole como parmetro el nmero del elemento. Despus sincronice los hilos con la herramienta descrita anteriormente: la barrera. La creacin de hilos en QNX se hace con pthread_create(): pthread_create() Create a thread Synopsis: #include <pthread.h> int pthread_create( pthread_t* thread, const pthread_attr_t* attr, void* (*start_routine)(void* ), void* arg ); Arguments: thread attr NULL, or a pointer to a pthread_t object where the function can store the thread ID of the new thread. A pointer to a pthread_attr_t structure that specifies the attributes of the new thread. Instead of manipulating the members of this structure directly, use pthread_attr_init() and the pthread_attr_set_* functions. For the exceptions, see "QNX extensions," below. If attr is NULL, the default attributes are used (see pthread_attr_init()). start_routine The routine where the thread begins, with arg as its only argument. If start_routine() returns, there's an implicit call to pthread_exit(), using the return value of start_routine() as the exit status. The thread in which main() was invoked behaves differently. When it returns from main(), there's an implicit call to exit(), using the return value of main() as the exit status. arg The argument to pass to start_routine. La funcin ctime() convierte el tiempo del calendario en el argumento al tiempo local en el formato siguiente: Wed Jun 07 11:36:19 2006\n\0 ------- o ------La prctica ha sido preparada por VK. Pando, 7 de junio de 2006 3 INF231 - Sistemas Operativos Prctica 4 (2006-1) ...
View Full Document

This note was uploaded on 06/18/2008 for the course ENG SO taught by Professor Bello during the Spring '08 term at Pontifical Catholic University of Peru.

Ask a homework question - tutors are online