CS 241 Section Week #4 (2/19/09). Topics This Section  SMP2 Review  SMP3 Forward  Semaphores  Problems  Recap of Classical Synchronization Problems.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Ch 7 B.
CS Lecture 4 Programming with Posix Threads and Java Threads George Mason University Fall 2009.
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Interprocess Communication
Enforcing Mutual Exclusion, Semaphores. Four different approaches Hardware support Disable interrupts Special instructions Software-defined approaches.
Computer Architecture II 1 Computer architecture II Programming: POSIX Threads OpenMP.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Objectives Understand.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
SMP threads an Introduction to Posix Threads. Technical Definition 1.Independent stream of instructions that can be scheduled to run by an operating system.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Semaphores Questions answered in this lecture: Why are semaphores necessary? How are semaphores used for mutual exclusion? How are semaphores used for.
Condition Variables Revisited Copyright ©: University of Illinois CS 241 Staff1.
Pthread (continue) General pthread program structure –Encapsulate parallel parts (can be almost the whole program) in functions. –Use function arguments.
Introduction to Threads CS240 Programming in C. Introduction to Threads A thread is a path execution By default, a C/C++ program has one thread called.
The University of Adelaide, School of Computer Science
Operating Systems CMPSC 473 Mutual Exclusion Lecture 14: October 14, 2010 Instructor: Bhuvan Urgaonkar.
5.1 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Problems with Semaphores Incorrect use of semaphore operations:
Multi-threaded Programming with POSIX Threads CSE331 Operating Systems Design.
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,
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Semaphore and Mutex Operations.
1 Using Semaphores CS 241 March 14, 2012 University of Illinois Slides adapted in part from material accompanying Bryant & O’Hallaron, “Computer Systems:
Threads CSCE Thread Motivation Processes are expensive to create. Context switch between processes is expensive Communication between processes.
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
Pthreads.
Threads Tutorial #7 CPSC 261. A thread is a virtual processor Each thread is provided the illusion that it owns a core – Copy of the registers – It is.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
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.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
Chapter 6 P-Threads. Names The naming convention for a method/function/operation is: – pthread_thing_operation(..) – Where thing is the object used (such.
CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.
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)
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Module 6: Process Synchronization.
Semaphores Reference –text: Tanenbaum ch
1 Carnegie Mellon Synchronization : Introduction to Computer Systems Recitation 14: November 25, 2013 Pratik Shah (pcshah) Section C.
Working with Pthreads. Operations on Threads int pthread_create (pthread_t *thread, const pthread_attr_t *attr, void * (*routine)(void*), void* arg) Creates.
Web Server Architecture Client Main Thread for(j=0;j
pThread synchronization
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
Chapter 5: Process Synchronization – Part 3
Chapter 5: Process Synchronization – Part II
PThreads.
Pthreads – Create and Join
Threads Threads.
Thread synchronization
Recitation 14: Proxy Lab Part 2
Semaphore Originally called P() and V() wait (S) { while S <= 0
CS510 Operating System Foundations
Jonathan Walpole Computer Science Portland State University
CMPT 300: Operating Systems I
Synchronization Primitives – Semaphore and Mutex
Synchronization.
CS 144 Advanced C++ Programming May 7 Class Meeting
Lab #9 Semaphores Operating System Lab.
Presentation transcript:

CS 241 Section Week #4 (2/19/09)

Topics This Section  SMP2 Review  SMP3 Forward  Semaphores  Problems  Recap of Classical Synchronization Problems

SMP2

SMP2 Review  qsort()  Define your own comparison function  int compare (const void *e1, const void *e2) { return (*(int*)e1 - *(int*)e2); }  Merge  You need to remove duplicates  How do you do that?  Pthreads  pthread_create and pthread_join  You DO NOT want to do this: for ( …) { pthread_create(…); … pthread_join(…); }

SMP3

SMP3 Forward  The scenario: Request: line Response: # client_0 Server file0 Read a line client_1 file1 Read a line client_2 file2 Read a line …

SMP3 Forward  Communication is done through shared memories  queue  client_result[client_id]  You need to deal with three semaphores  queue_semaphore  To protect access to the queue  server_notify_semaphore  Client notify the server when data is added to the queue  client_notify_semaphore  Array of semaphores  Client ID is used to access the semaphores  Server notify individual client a response is ready

Semaphores

Example (machex1.c) int N = ; int x = 0; int main(int argc, char** argv) { pthread_t threadCountUp, threadCountDown; pthread_create(&threadCountUp, NULL, countUp, NULL); pthread_create(&threadCountDown, NULL, countDown, NULL); }

Example void* countUp() { int i; for (i = 0; i < N; i++) { int c = x; c++; x = c; } void* countDown() { int i; for (i = 0; i < N; i++) { int c = x; c--; x = c; }

Semaphores  Thread1 did ‘ x++ ’ N times.  Thread2 did ‘ x-- ’ N times.  Ideal result: ‘ x ’ is at its initial value.  Please try to compile machex1.c and run it with different N values: N= 1000, N = , etc …  Actual result?

Semaphores  To fix this:  A thread must read, update, and write ‘ x ’ back to memory without any other threads interacting with ‘ x ’.  This concept is an atomic operation.

Semaphores  Conceptually, we would want an ‘ atomic ’ scope:  void* countUp() { atomic { int c = x; c++; x = c; } // But this doesn ’ t exist … }

Semaphores  Semaphores provides a locking mechanism to give us atomicity.  void* countUp() { sem_wait(&sema); int c = x; c++; x = c; sem_post(&sema); }

Semaphores  Semaphores provides a locking mechanism to give us atomicity.  void* countUp() { sem_wait(&sema); LOCKS int c = x; c++; x = c; sem_post(&sema); UNLOCKS }

Semaphores  To use a semaphore, you have to define it. Three steps to defining a semaphore:  1. Include the header file:  #include

Semaphores  To use a semaphore, you have to define it. Three steps to defining a semaphore:  2. Declare the semaphore:  sem_t sema; (Declare this in a global scope.)

Semaphores  To use a semaphore, you have to define it. Three steps to defining a semaphore:  3. Initialize the semaphore:  sem_init(&sema, 0, 1);

Semaphores  sem_init(&sema, 0, 1);  &sema: Your declared sem_t.  0 : 0 := Thread Sync  1 : Total of one thread inside a ‘ locked ’ section of code.

Semaphores  Three steps to starting them:  Include: #include  Define: sem_t sema;  Init: sem_init(&sema, 0, 1);

Semaphores  Two functions to use them:  Acquiring the lock: sem_wait(&sema);  Releasing the lock: sem_post(&sema);

Semaphores  Example Revisited: sem_wait() read x x++ context sw  sema_wait() write x  thread blocked sem_post() unlocked  /* … */

Problems

Problem 1 - Use semaphores to ensure order  machex2.c creates two threads to print out “ Hello World ”.  Use semaphores to ensure that that “ World\nHello “ is never printed instead of “ Hello World ”. void *hello_thread(void *arg) { sleep(2); fprintf(stderr, "Hello "); } void *world_thread(void *arg) { sleep(1); fprintf(stderr, "World!\n"); } int main(int argc, char **argv) { pthread_t hello, world; pthread_create(&hello, NULL, hello_thread, NULL); pthread_create(&world, NULL, world_thread, NULL); pthread_join(hello, NULL); pthread_join(world, NULL); return 0; }

Problem 1 - Use semaphores to ensure order void *hello_thread(void *arg) { sleep(2); fprintf(stderr, "Hello "); sem_post(&sem); } void *world_thread(void *arg) { sleep(1); sem_wait(&sem); fprintf(stderr, "World!\n"); sem_post(&sem); } sem_t sem; int main(int argc, char **argv) { pthread_t hello, world; sem_init(&sem, 0, 0); pthread_create(&hello, NULL, hello_thread, NULL); pthread_create(&world, NULL, world_thread, NULL); pthread_join(hello, NULL); pthread_join(world, NULL); return 0;

Problem 2 – Two semaphores  machex3.c creates two threads that both want access to two semaphores.  If you run this program, the program appears to stop running after a bit. Why? sem_t sem1, sem2; int main(int argc, char **argv) { pthread_t t1, t2; sem_init(&sem1, 0, 1); sem_init(&sem2, 0, 1); pthread_create(&t1, NULL, p1, NULL); pthread_create(&t2, NULL, p2, NULL); pthread_join(t1, NULL); pthread_join(t2, NULL); return 0; }

Problem 2 – Two semaphores void *p1(void *arg) { while (1) { printf("p1: sem_wait(&sem1);\n"); sem_wait(&sem1); printf("p1: sem_wait(&sem2);\n"); sem_wait(&sem2); printf("p1: locked\n"); printf("p1: sem_post(&sem2);\n"); sem_post(&sem2); printf("p1: sem_post(&sem1);\n"); sem_post(&sem1); printf("p1: unlocked\n"); } return NULL; } void *p2(void *arg) { while (1) { printf("p2: sem_wait(&sem2);\n"); sem_wait(&sem2); printf("p2: sem_wait(&sem1);\n"); sem_wait(&sem1); printf("p2: locked\n"); printf("p2:sem_post(&sem1);\n"); sem_post(&sem1); printf("p2: sem_post(&sem2);\n"); sem_post(&sem2); printf("p2: unlocked\n"); } return NULL;

Problem 2 – Two semaphores sem1_wait() sem2_wait() sem1_wait() sem2_wait() DEADLOCK! How to fix it??

Classical Synchronization Problems

Producer – consumer problem  Producers insert items  Consumers remove items  Shared bounded buffer  Three semaphores  Slots: initialize to N, to prevent buffer overflow  Items: initialize to 0, to prevent buffer underflow  Mutex to protect accesses to shared buffer & pointers.

Reader – writer problem  Multiple readers can read the data simultaneously  Only one writer can write the data at any time  A reader and a writer cannot in critical section together.  One global reader counter and 2 semaphores  Semaphore wrt to enforce mutual exclusion  Semaphore x to protect access to the global reader counter