Process Synchronization and Communication

Slides:



Advertisements
Similar presentations
More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
Advertisements

CAS3SH3 Midterm Review. The midterm 50 min, Friday, Feb 27 th Materials through CPU scheduling closed book, closed note Types of questions: True & False,
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 6: Process Synchronization
Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
Engineering Open House Need students to demo their players on –Friday 3-4 –Saturday 10-2.
Cpr E 308 Spring 2004 Recap for Midterm Introductory Material What belongs in the OS, what doesn’t? Basic Understanding of Hardware, Memory Hierarchy.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Operating Systems ECE344 Ding Yuan Synchronization (I) -- Critical region and lock Lecture 5: Synchronization (I) -- Critical region and lock.
Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 91 Today’s class Mutual exclusion and synchronization 
Review: Chapters 1 – Chapter 1: OS is a layer between user and hardware to make life easier for user and use hardware efficiently Control program.
Big Picture Lab 4 Operating Systems Csaba Andras Moritz.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
CSE Fall Introduction - 1 What is an Embedded Systems  Its not a desktop system  Fixed or semi-fixed functionality (not user programmable)
Processes 1 CS502 Spring 2006 Processes Week 2 – CS 502.
OS Spring’03 Introduction Operating Systems Spring 2003.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Threads CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University
© 2004, D. J. Foreman 2-1 Concurrency, Processes and Threads.
CSE 466 – Fall Introduction - 1 Remainder of Syllabus  Lecture  RTOS  Maestro In Linux  Distributed Control Architecture – distributed state.
CSE 451: Operating Systems Autumn 2013 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Introduction to Embedded Systems
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 VxWorks 5.4 Group A3: Wafa’ Jaffal Kathryn Bean.
CSE 153 Design of Operating Systems Winter 2015 Midterm Review.
6.0 INTRODUCTION TO REAL-TIME OPERATING SYSTEMS (RTOS) 6.0 Introduction A more complex software architecture is needed to handle multiple tasks, coordination,
CSE 153 Design of Operating Systems Winter 2015 Lecture 5: Synchronization.
Operating Systems CMPSC 473 Signals, Introduction to mutual exclusion September 28, Lecture 9 Instructor: Bhuvan Urgaonkar.
CSCI1600: Embedded and Real Time Software Lecture 17: Concurrent Programming Steven Reiss, Fall 2015.
Where Testing Fails …. Problem Areas Stack Overflow Race Conditions Deadlock Timing Reentrancy.
Big Picture Lab 4 Operating Systems C Andras Moritz
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Tutorial 2: Homework 1 and Project 1
Processes and threads.
CSE 120 Principles of Operating
Advanced Operating Systems CIS 720
Multi-processor Scheduling
CS 6560: Operating Systems Design
Topics Covered What is Real Time Operating System (RTOS)
6.0 INTRODUCTION TO REAL-TIME OPERATING SYSTEMS (RTOS/RTK)
CSE 466 – Fall Introduction - 1
CSE 466 – Fall Introduction - 1
Concurrency: Mutual Exclusion and Synchronization
Synchronization Issues
Thread Implementation Issues
Lecture Topics: 11/1 General Operating System Concepts Processes
Dr. Mustafa Cem Kasapbaşı
CSCI1600: Embedded and Real Time Software
CSE 451: Operating Systems Autumn 2003 Lecture 2 Architectural Support for Operating Systems Hank Levy 596 Allen Center 1.
Concurrency, Processes and Threads
CSE 451: Operating Systems Autumn 2001 Lecture 2 Architectural Support for Operating Systems Brian Bershad 310 Sieg Hall 1.
February 5, 2004 Adrienne Noble
Basic Synchronization
CSE 451: Operating Systems Autumn 2004 Module 6 Synchronization
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2004 Module 6 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 2 Architectural Support for Operating Systems Hank Levy 412 Sieg Hall 1.
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
CSCI1600: Embedded and Real Time Software
CSE 153 Design of Operating Systems Winter 2019
CSE 153 Design of Operating Systems Winter 2019
Embedded System Development Lecture 12 4/4/2007
CSE 542: Operating Systems
CSE 542: Operating Systems
Lecture 12 Input/Output (programmer view)
Presentation transcript:

Process Synchronization and Communication How to we protect a critical section without disabling interrupts? CSE 466 – Fall 2000 - Introduction - 1

Process Synchronization critical section need a HW interlock to prevent simultaneity The term “semaphore” means signal. It comes from the railroads. A semaphore requires some form of mutual exclusion in hardware: like disabling interrupts. By making it an OS call, we leave implementation up to the OS/HW. Same system call on many HW platforms. CSE 466 – Fall 2000 - Introduction - 2

Semaphore Implementations Some processors have a Test-and-Set Instruction provides hardware level atomicity for synchronizing critical sections, just like using the memory mapped I/O system provides atomicity for device I/O (single instructions are atomic) example: bit flag … while (flag != false); flag = true; <excute code in critical section> If processor has a test and set operation, it could like like this in ass’y code loop: tst flag, loop; //sometimes they just skip the next instruction < execute critical section > But we still don’t want to rely on our compiler…so we use a system call semaphore s; // declare a semaphore while(!set(s)); // os_set(semaphore) is a system call…let OS worry about atomicity <execute critical section> bad time for an interrupt? CSE 466 – Fall 2000 - Introduction - 3

CSE 466 – Fall 2000 - Introduction - 4 A Better Semaphore Problem with first semaphore: busy-waiting. OS can’t tell the difference between busy waiting and doing real work? How can we solve this? Try a blocking semaphore struct sem { processQueue Q; int count; }; void os_init_sem(sem *s) { scount = MAX_PROC_IN_SECTION; // probably one } void os_wait(sem *s) { disable(); scount--; if (scount < 0) { block calling process and put it in squeue; start any process in the ready-to-run queue; } enable(); sem *cs1; …. os_wait(cs1); <execute critical section> os_signal(cs1); // tiny has wait and signal // but they are thread // specific Is there and opportunity for deadlock detection? void os_signal(sem *s) { disable(); scount++; if (scount < 0) { move proc. from s->queue to ready queue; } enable(); CSE 466 – Fall 2000 - Introduction - 4

Better than Semaphores They’re kinda like goto…makes your multithreaded system into spaghetti Hard to tell if you have created a deadlock situation. An alternative? The Monitor: you see this in Java with “synchronized” methods Only one thread can be executing in a monitor at a time. The compiler builds in all of the os_calls and semaphores needed to protect the critical section No chance of forgetting to signal when leaving! unless of course a process dies in the monitor! Basic idea…bury semaphores in the compiler class queue synchronized { Vector data; queue() { data = new Vector();} void put(Object x) { data.add(x); } Object get(Object x) { data.remove(0); class top { q = new queue(); Producer p = new Producer(q); Consumer c = new Consumer(q); p.run(); c.run(); } individual methods can also be synchronized CSE 466 – Fall 2000 - Introduction - 5

CSE 466 – Fall 2000 - Introduction - 6 Message Passing Use queues, or queues of buffers Use monitors/semaphores to protect the queues Doesn’t work if we are dealing with processes rather than threads…separate address space. Process a can’t just send process b a pointer! They can’t access the same queue! So instead, provide inter-process message passing system calls os_send(destination, message); os_receive(source, message); Maybe implemented as a critical section in the OS Design Consideration Efficiency: do we have to copy from address space to another Space: how much space is there for messages? Who allocates the space? Authentication End-to-End guarantees, protocol. We will see more of this in Linux, along with semaphores and other synchronization and communication primitives This also leads into networking…what if the processes are on different machines? CSE 466 – Fall 2000 - Introduction - 6

Back to Comparative Real Time OSes Compare to uClinux at ~400Kbytes. What is this? 38uS – 280uS actually 16 semaphores CSE 466 – Fall 2000 - Introduction - 7

CSE 466 – Fall 2000 - Introduction - 8 Threads and Stacks Process: entire address space is private (processes can be multi-threaded) Threads: heap is public, but stack is private. What if stack wasn’t private? DATA DATA stack stack task 1 context task 2 context ? task 1 context sp globals globals sp context swap OS OS regs regs CSE 466 – Fall 2000 - Introduction - 8

How TINY manages the stack worst case stack size is sum of worst case for each task, plus ISR’s. One stack/thread stack 3 stack 3 stack 3 stack 2 stack 2 stack 1 stack 2 stack 1 stack 1 sp globals sp globals sp globals OS OS OS regs regs regs CSE 466 – Fall 2000 - Introduction - 9

CSE 466 – Fall 2000 - Introduction - 10 Reentrant Stack re-stack 3 Why not use the regular (Hardware Stack) for reentrant stack frames? re-stack 2 re-stack 1 re-sp stack 3 stack 2 stack 1 globals hw-sp OS regs CSE 466 – Fall 2000 - Introduction - 10

CSE 466 – Fall 2000 - Introduction - 11 Lab 4 How are register banks used in the Tiny OS Where does Tiny store the registers of non-running tasks? Write a program using Tiny OS that causes a context switch to occur with a fair amount of data (calls) on the stack for at least two threads. Then use the simulator to trigger an interrupt. determine the latency to the start of the interrupt routine Are interrupts disabled for the entire context switch process? if so then explain the experiment you did to prove this…otherwise How is it possible to allow interrupts during context switch? Make sure your interrupt routine also has some subroutine calls so that you can see what happens to the stack. If one thread signals another thread then executes a wait before the end of the OS timeslice, does the signalled thread start running immediately or only at the beginning of the next timeslice? Turn in answers to the questions along with your test code. Include clear explanations for how you arrived at your answers. The Keil debugger has many utilities to help you: Dissassembler, memory window, timer window for timer 0, elapsed time in machine cycles and seconds, etc. CSE 466 – Fall 2000 - Introduction - 11

CSE 466 – Fall 2000 - Introduction - 12 Embedded System Types Data flow dominated Systems our music player a network router queues, messages, packets, routing Control Dominated Systems Software State machines, distributed state management/synchronization, e.g. power plant, autopilot, etc. CSE 466 – Fall 2000 - Introduction - 12

Mode (State) Diagram for the Player IDLE STREAM what is the difference between IDLE and PAUSED? Modes are like states in an FSM…but they are not static. What does that mean? There is an execution sequence associated with each mode. Mode determines response to events and choice of next mode Tx PAUSED CSE 466 – Fall 2000 - Introduction - 13

An Alternative: Is this equivalent? ‘S’/clearb() ‘R’ STREAM IDLE STOP STOP ‘T’/clearb() Tx CSE 466 – Fall 2000 - Introduction - 14

Communication and Synchronization Semaphores Queues: Messages, Mailboxes, and Pipes Events Rendezvous CSE 466 – Fall 2000 - Introduction - 15