Lab #9 Semaphores Operating System Lab.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Synchronization and Deadlocks
Chapter 6 Process Synchronization Bernard Chen Spring 2007.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
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.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Objectives Understand.
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail,
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
CS 241 Section Week #4 (2/19/09). Topics This Section  SMP2 Review  SMP3 Forward  Semaphores  Problems  Recap of Classical Synchronization Problems.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Chapter 6 Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Module 6: Process Synchronization.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
NCHU System & Network Lab Lab 11 Memory Mapped File.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 5: Process Synchronization.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
Synchronizing Threads with Semaphores
NCHU System & Network Lab Lab #8 Signals Operating System Lab.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 11: Thread-safe Data Structures, Semaphores.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 11: Synchronization (Chapter 6, cont)
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 12: Thread-safe Data Structures, Semaphores.
Operating Systems Lecture Notes Synchronization Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 22 Semaphores Classic.
NCHU System & Network Lab Lab #6 Thread Management Operating System Lab.
Process Synchronization. Objectives To introduce the critical-section problem, whose solutions can be used to ensure the consistency of shared data To.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Q: 請以實際例子說明 critical section 之意 ? 何謂 race condition? while (true) { /*
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
Web Server Architecture Client Main Thread for(j=0;j
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Synchronization Background The Critical-Section Problem Peterson’s.
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.
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
Chien-Chung Shen CIS/UD
Semaphores Reference text: Tanenbaum ch
Semaphores Synchronization tool (provided by the OS) that does not require busy waiting. Logically, a semaphore S is an integer variable that, apart from.
Process Synchronization
Chapter 5: Process Synchronization
Process Synchronization: Semaphores
PARALLEL PROGRAM CHALLENGES
Chapter 5: Process Synchronization – Part II
Chapter 5: Process Synchronization
Interprocess Communication (3)
סמפורים.
Pthreads – Create and Join
Chapter 6-7: Process Synchronization
Chapter 5: Process Synchronization
Topic 6 (Textbook - Chapter 5) Process Synchronization
Lab 5 Process Control Operating System Lab.
Synchronization and Semaphores
Concurrency: Mutual Exclusion and Process Synchronization
Synchronization Primitives – Semaphore and Mutex
Chapter 6 Synchronization Principles
Synchronization.
Chapter 6: Synchronization Tools
Lab 10 Paging.
Synchronization.
Semaphores Reference text: Tanenbaum ch
Presentation transcript:

Lab #9 Semaphores Operating System Lab

NCHU System & Network Lab Race Condition Race condition: A situation that several tasks access and manipulate the same data concurrently and the outcome of the execution depends on the particular order in which the access take place. Example: Suppose that the value of the variable counter = 5. Process 1 and process 2 execute the statements “counter++” and “counter--” concurrently. Following the execution of these two statements, the value of the variable counter may be 4, 5, or 6! NCHU System & Network Lab

NCHU System & Network Lab Race Condition (cont.) “counter++” is implemented as register1 = counter register1 = register1 + 1 counter = register1 “counter--” is implemented as register2 = counter register2 = register2 - 1 counter = register2 The concurrent execution of “counter++” and “counter--” is equivalent to a sequential execution where the low-level statements are interleaved in some arbitrary order. NCHU System & Network Lab

NCHU System & Network Lab Race Condition (cont.) Thread 1 register1 Thread 2 register2 counter register1 =counter 5 --- register1=register1 + 1 6 register2 =counter register2=register2 − 1 4 counter =register1 counter =register2 NCHU System & Network Lab

Process Synchronization To guard against the race condition mentioned above, we need to ensure that only one process at a time can be manipulating the variable counter. To make such a guarantee, we require some form of synchronization for the processes-semaphore Semaphore S: An integer variable that, apart from initialization, is accessed only through two standard atomic operations: wait and signal. NCHU System & Network Lab

Process Synchronization (cont.) A blocking implementation of semaphores. typedef struct { int value; struct process *L; }semaphore; void wait(semaphore S) { S.value--; if(S.value < 0) { add this process to S.L; block(); } void signal(semaphore S) { S.value++; if(S.value <= 0) { remove a process P from S.L; wakeup(P); } NCHU System & Network Lab

Process Synchronization (cont.) The following pseudocode protects a critical section if the semaphore variable S is initially 1. wait(&S); /* entry section */ <critical section> signal(&S); /* exit section */ <remainder section> NCHU System & Network Lab

NCHU System & Network Lab Normal Execution Critical Section Blocked on semaphore S.L S.value A B 1 wait(S) wait(S) Critical Section B -1 signal(S) Critical Section signal(S) 1 NCHU System & Network Lab

POSIX Semaphores Declare a semaphore variable called sem. Operations: Use <semaphore.h>

POSIX Semaphores sem_init: Initialize the semaphore referenced by sem to value. The semaphores must be initialized before they used. value: cannot be negative. pshared: A flag indicating whether or not the semaphore should be shared with forked processes. Pshared == 0 only threads of process creating semaphore can use semaphore.

POSIX Semaphores (cont.) sem_destroy: Destroy a previously initialized semaphore referenced by the sem parameter. sem_getvalue: Get the current value of sem and places it in the location pointed to by val NCHU System & Network Lab

Examples sem_init sem_destroy sem_getvalue

POSIX Semaphores (cont.) sem_post: Implement classic semaphore signaling. It increments the value of the semaphore and wakes up a blocked process waiting on the semaphore, if any. NCHU System & Network Lab

POSIX Semaphores (cont.) sem_wait: Implement the classic semaphore wait operation. NCHU System & Network Lab

Example #include <semaphore.h> #include <pthread.h> #include <stdio.h> void *PrintHello(void *arg); int count; int main (int argc, char *argv[]) { sem_t semaphore; ... /* call sem_init before creating any threads. */ if (sem_init(&semaphore, 0, 1) == -1) { printf(“Failed to initialize semaphore.”); exit(-1); } rc = pthread_create(&thread, NULL, PrintHello, (void *)t); rc = pthread_join(thread, NULL); return 0;

NCHU System & Network Lab Example (cont.) void *PrintHello(void *arg) { sem_wait(...); /* Entry section */ /* start of critical section */ ... count++; sem_post(...); /* Exit section */ pthread_exit(NULL); } NCHU System & Network Lab

Solution to the Race Condition Thread 1 register1 Thread 2 register2 counter sem_wait(&semA); --- 5 register1 =counter /* blocked */ register1=register1 + 1 6 counter =register1 sem_post(&semA); register2 =counter register2=register2 − 1 counter =register2

Lab I – Binary Semaphore Write programs using semaphores to solve the race condition occurred in your previous lab and observe the differences between these two outcomes. NCHU System & Network Lab

Lab II – Counting Semaphore Counting semaphores can be used to control access to a given resource consist of a finite number of instances. The semaphore is initialized to the number of resources available. Each process that wishes to use a resource performs a wait() on the semaphore. When a process releases a resource, it performs a signal(). When the count for the semaphore goes to 0, all resources are being used. NCHU System & Network Lab

Lab II – Counting Semaphore (cont.) Create 15 threads that share a buffer with four slots. Each thread sleeps for a random period of time and will attempt to insert their thread id into the buffer. When an insertion gets successful, print out the value of the buffer. Then, after a random period of time, it releases its slot to other threads who are waiting for using it. Print the value of the buffer if any change and count the number of the slots that have been used. NCHU System & Network Lab

NCHU System & Network Lab References Avi Silberschatz, Peter Baer Galvin and Greg Gagne, “Operating System Concepts,” John Wiley & Sons, 6th Edition, 2001 “Unix Systems Programming: Communication, Concurrency, and Threads” by Kay A. Robbins, Steven Robbins Neil Matthew and Richard Stones, “Beginning Linux Programming,” Wiley publishing, 3rd Edition, 2004 W. Richard Stevens, “Advanced Programming in the UNIX Environment,” Addison-Wesley, 1992 NCHU System & Network Lab

NCHU System & Network Lab References (cont.) IBM http://publib.boulder.ibm.com/infocenter/iseries/v5r3/index.jsp?topic=/rzahw/rzahwsemco.htm Synchronizing Threads with POSIX Semaphores http://www.csc.villanova.edu/~mdamian/threads/posixsem.html NCHU System & Network Lab