Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Semaphore and Mutex Operations.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

CS Lecture 4 Programming with Posix Threads and Java Threads George Mason University Fall 2009.
Pthreads & Concurrency. Acknowledgements  The material in this tutorial is based in part on: POSIX Threads Programming, by Blaise Barney.
Enforcing Mutual Exclusion, Semaphores. Four different approaches Hardware support Disable interrupts Special instructions Software-defined approaches.
Critical Sections and Semaphores A critical section is code that contains access to shared resources that can accessed by multiple processes. Critical.
Computer Architecture II 1 Computer architecture II Programming: POSIX Threads OpenMP.
CS444/CS544 Operating Systems Synchronization 2/19/2007 Prof. Searleman
Unix Synchronization * Mutexes * Semaphores - named - unnamed.
CS Introduction to Operating Systems
CS 241 Section Week #4 (2/19/09). Topics This Section  SMP2 Review  SMP3 Forward  Semaphores  Problems  Recap of Classical Synchronization Problems.
Pthread (continue) General pthread program structure –Encapsulate parallel parts (can be almost the whole program) in functions. –Use function arguments.
Introduction to Pthreads. Pthreads Pthreads is a POSIX standard for describing a thread model, it specifies the API and the semantics of the calls. Model.
Atomic Operations David Monismith cs550 Operating Systems.
Multi-threaded Programming with POSIX Threads CSE331 Operating Systems Design.
POSIX Threads Nezer J. Zaidenberg. References  Advanced programming for the UNIX environment (2nd edition chapter This material does not exist.
Threads and Thread Control Thread Concepts Pthread Creation and Termination Pthread synchronization Threads and Signals.
Copyright ©: University of Illinois CS 241 Staff1 Synchronization and Semaphores.
ICS 145B -- L. Bic1 Project: Process/Thread Synchronization Textbook: pages ICS 145B L. Bic.
CS345 Operating Systems Threads Assignment 3. Process vs. Thread process: an address space with 1 or more threads executing within that address space,
What is a thread? process: an address space with 1 or more threads executing within that address space, and the required system resources for those threads.
Programming with POSIX* Threads Intel Software College.
Linux Programming –Threads CS Threads Review Threads in the same address space –share everything in the address space –lighter than process –no.
Lecture 10 Locks.
Pthreads: A shared memory programming model
CSC Advanced Unix Programming, Fall, 2008 Monday, November 24 POSIX threads (pthreads)
S -1 Posix Threads. S -2 Thread Concepts Threads are "lightweight processes" –10 to 100 times faster than fork() Threads share: –process instructions,
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
Thread API Xiaohua Lu Office : CS 3310 Tel. : Office hours: 11-12,3-5 T,TR.
POSIX Synchronization Introduction to Operating Systems: Discussion Module 5.
Synchronizing Threads with Semaphores
IT 325 Operating systems Chapter6.  Threads can greatly simplify writing elegant and efficient programs.  However, there are problems when multiple.
2.3 interprocess communcation (IPC) (especially via shared memory & controlling access to it)
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 11: Thread-safe Data Structures, Semaphores.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Software Systems Advanced Synchronization Emery Berger and Mark Corner University.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 12: Thread-safe Data Structures, Semaphores.
1 Synchronization Threads communicate to ensure consistency If not: race condition (non-deterministic result) Accomplished by synchronization operations.
Chapter 6 P-Threads. Names The naming convention for a method/function/operation is: – pthread_thing_operation(..) – Where thing is the object used (such.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 13: Condition Variable, Read/Write Lock, and Deadlock.
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Page 1 threads CS 360, WSU Vancouver POSIX Threads 1. Background 2. Threads vs. Processes 3. Thread Synchronization 4. Mutex Variables 5. Condition Variables.
PThread Synchronization. Thread Mechanisms Birrell identifies four mechanisms commonly used in threading systems –Thread creation –Mutual exclusion (mutex)
Thread Basic Thread operations include thread creation, termination, synchronization, data management Threads in the same process share:  Process address.
Working with Pthreads. Operations on Threads int pthread_create (pthread_t *thread, const pthread_attr_t *attr, void * (*routine)(void*), void* arg) Creates.
1 COMP 3500 Introduction to Operating Systems Project 3 – Synchronization Overview Dr. Xiao Qin Auburn University
Web Server Architecture Client Main Thread for(j=0;j
pThread synchronization
回到第一頁 What are threads n Threads are often called "lightweight processes” n In the UNIX environment a thread: u Exists within a process and uses the process.
CS 311/350/550 Semaphores. Semaphores – General Idea Allows two or more concurrent threads to coordinate through signaling/waiting Has four main operations.
Case Study: Pthread Synchronization Dr. Yingwu Zhu.
Chien-Chung Shen CIS/UD
Synchronization and Semaphores
CS 537 – Introduction to Operating Systems
Linux Thread Programming
Process Synchronization
Principles of Operating Systems Lecture 11
סמפורים.
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Thread synchronization
Lecture 14: Pthreads Mutex and Condition Variables
BOOM! count is [ ], should be
Synchronization and Semaphores
Pthread Prof. Ikjun Yeom TA – Mugyo
Synchronization Primitives – Semaphore and Mutex
Lecture 14: Pthreads Mutex and Condition Variables
Synchronization.
Synchronization.
Lecture 20: Synchronization
Lecture 12 CV and Semaphores
POSIX Threads(pthreads)
Lab #9 Semaphores Operating System Lab.
Presentation transcript:

Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Semaphore and Mutex Operations

Copyright ©: Nahrstedt, Angrave, Abdelzaher 2 CS241 Administrative SMP2 due tomorrow by 10pm SMP3 is out today at 10pm

Copyright ©: Nahrstedt, Angrave, Abdelzaher 3 Review: Basic Operations wait (sem_t *sp) if (sp->value >0) sp->value--; else { Add process to sp->list; } signal (sem_t *sp) if (sp->list != NULL) remove next process from sp->list; else sp->value++;

Copyright ©: Nahrstedt, Angrave, Abdelzaher 4 Review: Implementation of Semaphores in POSIX POSIX:SEM semaphore is variable of type sem_t Use Atomic Operations: int sem_init(sem_t *sem, int pshared, unsigned value); int sem_destroy(sem_t *sem); int sem_post(sem_t *sem); int sem_trywait(sem_t *sem); int sem_wait(sem_t *sem);

Copyright ©: Nahrstedt, Angrave, Abdelzaher 5 Example 1 on Semaphore We want a shared variable shared (critical section) to be protected by semaphore to allow for two functions decshared – is a function that decrements the current value of the shared variable shared incshared – is a function that increments the shared variable.

Copyright ©: Nahrstedt, Angrave, Abdelzaher 6 Example, creating shared variable #include static int shared = 0; static sem_t sharedsem; int initshared(int val) { if (sem_init(&sharedsem, 0, 1) == -1) return -1; shared = val; return 0; }

Copyright ©: Nahrstedt, Angrave, Abdelzaher 7 Example – shared variable int decshared() { while (sem_wait(&sharedsem) == -1) if (errno != EINTR) return -1; shared--; return sem_post(&sharedsem); } int incshared() { while (sem_wait(&sharedsem) == -1) if (errno != EINTR) return -1; shared++; return sem_post(&sharedsem); }

Copyright ©: Nahrstedt, Angrave, Abdelzaher 8 Mutex Simplest and most efficient thread synchronization mechanism A special variable that can be either in locked state: a distinguished thread that holds or owns the mutex; or unlocked state: no thread holds the mutex When several threads compete for a mutex, the losers block at that call The mutex also has a queue of threads that are waiting to hold the mutex. POSIX does not require that this queue be accessed FIFO.

Copyright ©: Nahrstedt, Angrave, Abdelzaher 9 POSIX Mutex-related Functions int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr); Also see PTHREAD_MUTEX_INITIALIZER int pthread_mutex_destroy(pthread_mutex_t *mutex); int pthread_mutex_lock(pthread_mutex_t *mutex); int pthread_mutex_trylock(pthread_mutex_t *mutex); int pthread_mutex_unlock(pthread_mutex_t *mutex);

Copyright ©: Nahrstedt, Angrave, Abdelzaher 10 Mutex and Shared Variables Mutex locks are usually used to protect access to a shared variable. The idea: lock the mutex critical section unlock the mutex Unlike a semaphore, a mutex does not have a value, it has states (locked and unlocked). Only the owner of the mutex should unlock the mutex. Do not lock a mutex that is already locked. Do not unlock a mutex that is not already locked.

Copyright ©: Nahrstedt, Angrave, Abdelzaher 11 Example #include static pthread_mutex_t my_lock = PTHREAD_MUTEX_INITIALIZER; void *mythread(void *ptr) { long int i,j; while (1) { pthread_mutex_lock (&my_lock); for (i=0; i<10; i++) { printf ("Thread %d\n", (int) ptr); for (j=0; j< ; j++); } pthread_mutex_unlock (&my_lock); for (j=0; j< ; j++); } int main (int argc, char *argv[]) { pthread_t thread[2]; pthread_create(&thread[0], NULL, mythread, (void *)0); pthread_create(&thread[1], NULL, mythread, (void *)1); getchar(); }