Threads

OS Laboratory Team
Fall 2018
  • In shared memory multiprocessor architectures, such as SMPs, threads can be used to implement parallelism.
  • For UNIX systems, a standardized C language threads programming interface has been specified by the IEEE POSIX 1003.1c standard.
  • Implementations that adhere to this standard are referred to as POSIX threads, or Pthreads.
  • Implementations of the API are available on many Unix-like POSIX-conformant operating systems such as FreeBSD, NetBSD, OpenBSD, Linux, Mac OS X, Android, Solaris and AUTOSAR Adaptive, typically bundled as a library libpthread.
  • DR-DOS and Microsoft Windows implementations also exist: within the SFU/SUA subsystem which provides a native implementation of a number of POSIX APIs, and also within third-party packages such as pthreads-w32, which implements pthreads on top of existing Windows API.
  • LinuxThreads was a partial implementation of POSIX Threads introduced in 1996.
  • It has been superseded by the Native POSIX Thread Library (NPTL)
  • NPTL uses a similar approach to LinuxThreads, in that the primary abstraction known by the kernel is still a process, and new threads are created with the clone() system call (called from the NPTL library).

Let's code :)

Thread Creation


          #include <pthread.h>

          int pthread_create(pthread_t *thread, const pthread_att_t *attr, void *(*start_routine) (void *), void *arg)
          
  • pthread_create creates a new thread and makes it executable
  • This routine can be called any number of times from anywhere within your code

          gcc -pthread
          

Let's code :)

Thread Synchronization


          #include <pthread.h>

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

          #include <pthread.h>

          pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
          pthread_mutex_init( &mutex, NULL );
          

          #include <pthread.h>

          pthread_mutex_lock( &mutex );
          pthread_mutex_unlock( &mutex );
          

          #include <semaphore.h>

          sem_t sem_name;
          sem_init(&sem_name, 0, 10);
          sem_wait(&sem_name);
          sem_post(&sem_name);
          

          int sem_init(sem_t *sem, int pshared, unsigned int value);
          
  • sem points to a semaphore object to initialize
  • pshared is a flag indicating whether or not the semaphore should be shared with fork()ed processes. LinuxThreads does not currently support shared semaphores
  • value is an initial value to set the semaphore to
  • If the value of the semaphore is negative, the calling process blocks; one of the blocked processes wakes up when another process calls sem_post.

          int sem_wait(sem_t *sem);
          
  • It increments the value of the semaphore and wakes up a blocked process waiting on the semaphore, if any.

          int sem_post(sem_t *sem);
          
  • destroys the semaphore; no threads should be waiting on the semaphore if its destruction is to succeed.

          int sem_destroy(sem_t *sem);