PTHREADS AND SEMAPHORES

Slides:



Advertisements
Similar presentations
CS Lecture 4 Programming with Posix Threads and Java Threads George Mason University Fall 2009.
Advertisements

Pthreads & Concurrency. Acknowledgements  The material in this tutorial is based in part on: POSIX Threads Programming, by Blaise Barney.
PTHREADS These notes are from LLNL Pthreads Tutorial
Computer Architecture II 1 Computer architecture II Programming: POSIX Threads OpenMP.
Lecture 18 Threaded Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
B.Ramamurthy1 POSIX Thread Programming 2/14/97 B.Ramamurthy.
Jonathan Walpole Computer Science Portland State University
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
The University of Adelaide, School of Computer Science
Thread Synchronization with Semaphores
10/16/ Realizing Concurrency using the thread model B. Ramamurthy.
Multi-threaded Programming with POSIX Threads CSE331 Operating Systems Design.
Threads and Thread Control Thread Concepts Pthread Creation and Termination Pthread synchronization Threads and Signals.
CS345 Operating Systems Threads Assignment 3. Process vs. Thread process: an address space with 1 or more threads executing within that address space,
CS333 Intro to Operating Systems Jonathan Walpole.
Professor: Shu-Ching Chen TA: Samira Pouyanfar.  An independent stream of instructions that can be scheduled to run  A path of execution int a, b; int.
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
POSIX Synchronization Introduction to Operating Systems: Discussion Module 5.
Lecture 7: POSIX Threads - Pthreads. Parallel Programming Models Parallel Programming Models: Data parallelism / Task parallelism Explicit parallelism.
Pthreads.
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Threads A thread is an alternative model of program execution
PThread Synchronization. Thread Mechanisms Birrell identifies four mechanisms commonly used in threading systems –Thread creation –Mutual exclusion (mutex)
NCHU System & Network Lab Lab #6 Thread Management Operating System Lab.
Thread Basic Thread operations include thread creation, termination, synchronization, data management Threads in the same process share:  Process address.
Thread Programming 김 도 형김 도 형. 2 Table of Contents  What is a Threads?  Designing Threaded Programs  Synchronizing Threads  Managing Threads.
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
Case Study: Pthread Synchronization Dr. Yingwu Zhu.
Tutorial 4. In this tutorial session we’ll see Threads.
A thread is a basic unit of CPU utilization within a process Each thread has its own – thread ID – program counter – register set – stack It shares the.
Realizing Concurrency using the thread model
CS 537 – Introduction to Operating Systems
Realizing Concurrency using the thread model
Today’s topics Project 2 - Work on CEREAL - Due: Oct 10, :00 pm
PThreads.
Principles of Operating Systems Lecture 11
Threads in C Caryl Rahn.
Shared-Memory Programming with Threads
Threads Threads.
Netprog: Threads Programming
Boost String API & Threads
CS399 New Beginnings Jonathan Walpole.
PTHREADS These notes are from LLNL Pthreads Tutorial
Multithreading Tutorial
Principles of Operating Systems Lecture 8
Realizing Concurrency using Posix Threads (pthreads)
Operating Systems Lecture 13.
Lecture 14: Pthreads Mutex and Condition Variables
Jonathan Walpole Computer Science Portland State University
Realizing Concurrency using the thread model
Multithreading Tutorial
Realizing Concurrency using the thread model
CS510 Operating System Foundations
Jonathan Walpole Computer Science Portland State University
Pthread Prof. Ikjun Yeom TA – Mugyo
Operating System Concepts
Chien-Chung Shen CIS/UD
Multithreading Tutorial
Jonathan Walpole Computer Science Portland State University
Multithreading Tutorial
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
Realizing Concurrency using the thread model
Lecture 14: Pthreads Mutex and Condition Variables
Realizing Concurrency using Posix Threads (pthreads)
Tutorial 4.
Programming with Shared Memory - 2 Issues with sharing data
Shared Memory Programming with Pthreads
POSIX Threads(pthreads)
Presentation transcript:

PTHREADS AND SEMAPHORES CSE 5331 / SP -05 Dr. Sharma Chakravarthy 12/5/2018 CSE5331- instructor:Dr.Sharma

CSE5331- instructor:Dr.Sharma Overview Introduction to pthreads Thread Management Synchronization Semaphore P and V operation in semaphore. 12/5/2018 CSE5331- instructor:Dr.Sharma

CSE5331- instructor:Dr.Sharma PTHREADS 12/5/2018 CSE5331- instructor:Dr.Sharma

CSE5331- instructor:Dr.Sharma Pthreads To full advantage of the capabilities provided by threads, a standardized programming interface was required. For UNIX systems, this interface has been specified by the IEEE POSIX 1003.1c standard (1995). Implementations which adhere to this standard are referred to as POSIX threads, or Pthreads. 12/5/2018 CSE5331- instructor:Dr.Sharma

CSE5331- instructor:Dr.Sharma Basics Multiple threads can be created with a process. Threads use and exist within process resources Scheduled by the operating system Run as independent entities within a process. If the main program blocks, all the threads will block. 12/5/2018 CSE5331- instructor:Dr.Sharma

CSE5331- instructor:Dr.Sharma Thread management Creating and deleting a thread. Pthread_create(thread,attr,start_routine,arg) where thread argument returns the new thread id. attr parameter for setting thread attributes. NULL for the default values. start_routine is the C routine that the thread will execute once it is created A single argument may be passed to start_routine via arg. It must be passed by reference as a pointer cast of type void. 12/5/2018 CSE5331- instructor:Dr.Sharma

Other thread functions pthread_self() Attribute Set pthread_attr_init(&attr) pthread_attr_setschedpolicy(&attr, SCHED_FIFO) Exiting pthread_exit(status); This routine terminates the calling thread and makes a status value available to any thread that calls pthread_join and specifies the terminating thread. 12/5/2018 CSE5331- instructor:Dr.Sharma

CSE5331- instructor:Dr.Sharma Example #include <pthread.h> #include <stdio.h> void *PrintHello(void *threadid) { printf("\n%d: Hello World!\n", threadid); pthread_exit(NULL); } int main(){ pthread_t thread; int rc, t =1; printf("Creating thread %d\n", t); rc = pthread_create(&thread, NULL, PrintHello, (void *)t); if (rc) { printf("ERROR; return code from pthread_create() is %d\n", rc); exit(-1); } } 12/5/2018 CSE5331- instructor:Dr.Sharma

CSE5331- instructor:Dr.Sharma Synchronization Mutexes: Deals with synchronization, which is an abbreviation for "mutual exclusion". Condition variables:Condition variables provide yet another way for threads to synchronize. While mutexes implement synchronization by controlling thread access to data, condition variables allow threads to synchronize based upon the actual value of data/condition. 12/5/2018 CSE5331- instructor:Dr.Sharma

CSE5331- instructor:Dr.Sharma Condition Variable To synchronize thread A and B Declare and initialize global data/variables for synchronization. e.g:condset[tid] =0 Declare and initialize a condition variable object. pthread_cond_init (condition,attr) Create and initialize associated mutex. pthread_mutex_init (mutex,attr) Create threads A and B to do work. 12/5/2018 CSE5331- instructor:Dr.Sharma

CSE5331- instructor:Dr.Sharma Thread A Lock associated mutex Change the value of the variable (If condset[tid] =0, set it to –1) ……… operations ………….. Set the global variable condset[tid]= 0, for thread B to continue Do , pthread_cond_signal(condition) or pthread_cond_broadcast(condition) Unlock mutex Continue Thread B Lock associated mutex and check value of a variable(condset[tid]=0) Call pthread_cond_wait to perform a blocking wait if condset[tid]!= 0. Note that a call to pthread_cond_wait automatically and atomically unlocks the associated mutex variable so that it can be used. When signalled, wake up. Mutex is automatically and atomically locked. Explicitly unlock mutex after completion of operation. Continue 12/5/2018 CSE5331- instructor:Dr.Sharma

CSE5331- instructor:Dr.Sharma Compilation Details Pthreads are defined as a set of C language programming types and procedure calls, implemented with a pthread.h a thread library ‘pthread’ has to be linked. ie. -lpthread 12/5/2018 CSE5331- instructor:Dr.Sharma

CSE5331- instructor:Dr.Sharma Semaphore 12/5/2018 CSE5331- instructor:Dr.Sharma

CSE5331- instructor:Dr.Sharma Semaphore Semaphores are a programming construct designed by E. W. Dijkstra in the late 1960s. Used to monitor and control the availability of system resources such as shared memory segments. Semaphores can be operated on as individual units or as elements in a set. 12/5/2018 CSE5331- instructor:Dr.Sharma

Example of semaphore usage Consider a stretch of railroad in which there is a single track over which only one train at a time is allowed. Guarding this track is a semaphore. A train must wait before entering the single track until the semaphore is in a state that permits travel. When the train enters the track, the semaphore changes state to prevent other trains from entering the track. A train that is leaving this section of track must again change the state of the semaphore to allow another train to enter. In the computer version, a semaphore appears to be a simple integer. A process (or a thread) waits for permission to proceed by waiting for the integer to become 0. The signal if it proceeds signals that this by performing incrementing the integer by 1. When it is finished, the process changes the semaphore's value by subtracting one from it. 12/5/2018 CSE5331- instructor:Dr.Sharma

CSE5331- instructor:Dr.Sharma Project semaphores Array of semaphores are generated by semid = semget(key, nsems, semflg) where nsems = 0 to no_of transactions. Semaphore 0(SHARED_MEM_AVAIL) is for locking the transaction manager. Semaphores: 1to no_of_transactions are used for threads to wait when objects are locked by other transactions. 12/5/2018 CSE5331- instructor:Dr.Sharma

CSE5331- instructor:Dr.Sharma Project semaphore semaphore creator can change its ownership or permissions using semctl(); and semaphore operations are performed via the semop() function Semaphore 0 is initialized to 1 i.e holds one resource(transaction manager).Do ‘p’(zgt_p) operation to obtain the resource and ‘v’(zgt_v) to release the resource. Rest of semaphores are initialized to 0 i.e hold no resources.Hence on the first p operation, the thread/process will wait till a v operation is done on the semaphore. 12/5/2018 CSE5331- instructor:Dr.Sharma

CSE5331- instructor:Dr.Sharma P and V operations int semop(int semid, struct sembuf *sops, size_t nsops); struct sembuf has: semaphore number semaphore operation operation flags Operations: A positive integer increments the semaphore value by that amount. A negative integer decrements the semaphore value by that amount. A value of zero means to wait for the semaphore value to reach zero. 12/5/2018 CSE5331- instructor:Dr.Sharma

CSE5331- instructor:Dr.Sharma Compilations #include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h> 12/5/2018 CSE5331- instructor:Dr.Sharma

CSE5331- instructor:Dr.Sharma Thank You 12/5/2018 CSE5331- instructor:Dr.Sharma