Intertask Communication

Similar documents
Real Time Operating System: Inter-Process Communication (IPC)

Tasks. Task Implementation and management

Dealing with Issues for Interprocess Communication

Concurrency: Deadlock and Starvation. Chapter 6

Process & Thread Management II. Queues. Sleep() and Sleep Queues CIS 657

Process & Thread Management II CIS 657

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017

What s An OS? Cyclic Executive. Interrupts. Advantages Simple implementation Low overhead Very predictable

Deadlock and Monitors. CS439: Principles of Computer Systems September 24, 2018

Concurrency. Chapter 5

CSE 153 Design of Operating Systems

Process Management And Synchronization

Real-Time Programming

Device-Functionality Progression

Chapter 12: I/O Systems. I/O Hardware

Deadlock. Only one process can use the resource at a time but once it s done it can give it back for use by another process.

Lecture 9: Midterm Review

PROCESS SYNCHRONIZATION

Lecture 4: Real Time Semaphores

Module 12: I/O Systems

Deadlock and Monitors. CS439: Principles of Computer Systems February 7, 2018

The University of Texas at Arlington

Lecture Topics. Announcements. Today: Concurrency (Stallings, chapter , 5.7) Next: Exam #1. Self-Study Exercise #5. Project #3 (due 9/28)

Experiment #3 Semaphores

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430

Micrium µc/os II RTOS Introduction EE J. E. Lumpp

2 Threads vs. Processes

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

Chapter 13: I/O Systems

Semaphore. Originally called P() and V() wait (S) { while S <= 0 ; // no-op S--; } signal (S) { S++; }

Midterm Exam #2 Solutions October 25, 2016 CS162 Operating Systems

Threads. Concurrency. What it is. Lecture Notes Week 2. Figure 1: Multi-Threading. Figure 2: Multi-Threading

CSE 451 Midterm 1. Name:

CMSC 330: Organization of Programming Languages. The Dining Philosophers Problem

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 8: Semaphores, Monitors, & Condition Variables

SAMPLE MIDTERM QUESTIONS

The University of Texas at Arlington

C09: Process Synchronization

Concurrency: Deadlock and Starvation

Midterm Exam Amy Murphy 19 March 2003

EECS 482 Introduction to Operating Systems

CS 153 Design of Operating Systems Winter 2016

What is the Race Condition? And what is its solution? What is a critical section? And what is the critical section problem?

Operating Systems ECE344. Ding Yuan

CSE 4/521 Introduction to Operating Systems. Lecture 24 I/O Systems (Overview, Application I/O Interface, Kernel I/O Subsystem) Summer 2018

I/O Systems. 04/16/2007 CSCI 315 Operating Systems Design 1

Chapter 13: I/O Systems

Chapter 13: I/O Systems. Chapter 13: I/O Systems. Objectives. I/O Hardware. A Typical PC Bus Structure. Device I/O Port Locations on PCs (partial)

SYNCHRONIZATION M O D E R N O P E R A T I N G S Y S T E M S R E A D 2. 3 E X C E P T A N D S P R I N G 2018

Process Synchronization

EECS 482 Introduction to Operating Systems

Review for Midterm. Starring Ari and Tyler

CS A331 Programming Language Concepts

Last Class: Deadlocks. Today

Deadlock and Starvation

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

Real-Time Component Software. slide credits: H. Kopetz, P. Puschner

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Chapters 5 and 6 Concurrency

Condition Variables & Semaphores

Process Management. Deadlock. Process Synchronization. Management Management. Starvation

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

Module 12: I/O Systems

CS 318 Principles of Operating Systems

MULTITHREADING AND SYNCHRONIZATION. CS124 Operating Systems Fall , Lecture 10

Chapter 13: I/O Systems

Deadlock and Starvation

Concurrency: Principles of Deadlock. Processes and resources. Concurrency and deadlocks. Operating Systems Fall Processes need resources to run

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

Process Synchronisation (contd.) Operating Systems. Autumn CS4023

EE458 - Embedded Systems Lecture 8 Semaphores

Introduction to OS Synchronization MOS 2.3

Midterm Exam. October 20th, Thursday NSC

Part I: Communication and Networking

Chapter 6: Synchronization. Operating System Concepts 8 th Edition,

IT 540 Operating Systems ECE519 Advanced Operating Systems

CSCC 69H3 2/1/13. Today: Deadlock. Remember example from last week? Example of Deadlock. Example: dining philosophers: Not just an OS Problem!

Chapter 13: I/O Systems

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Deadlocks: Part I Prevention and Avoidance Yi Shi Fall 2017 Xi an Jiaotong University

CS A320 Operating Systems for Engineers

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

Operating Systems. Thread Synchronization Primitives. Thomas Ropars.

The control of I/O devices is a major concern for OS designers

Lecture 5: Synchronization w/locks

CS-537: Midterm Exam (Spring 2001)

November 13, Networking/Interprocess Communication (25 pts)

Today: Synchronization. Recap: Synchronization

Introduction to Real-Time Operating Systems

Midterm on next week Tuesday May 4. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 9

Concept of a process

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

College of Computer & Information Science Spring 2010 Northeastern University 26 January 2010

Synchronization Principles

Chapter 6 Concurrency: Deadlock and Starvation

CSE Traditional Operating Systems deal with typical system software designed to be:

VI. Deadlocks. What is a Deadlock? Intended Schedule. Deadlock Problem (1) ???

VI. Deadlocks Operating Systems Prof. Dr. Marc H. Scholl DBIS U KN Summer Term

IV. Process Synchronisation

ENGR 3950U / CSCI 3020U UOIT, Fall 2012 Quiz on Process Synchronization SOLUTIONS

Transcription:

Inter-task Communication 04/27/01 Lecture # 29 16.070 Task state diagram (single processor) Intertask Communication Global variables Buffering data Critical regions Synchronization Semaphores Mailboxes and Queues Deadlock Readings: Chapter 7 in Laplante Smith 4/27/01 1

Task state diagram A process goes through several states during its life in a multitasking system. Running Tasks are moved from one state to another in response to the stimuli marked on the arrows. Wait Queue Blocked (waiting for I/O or other resource) READY Ready Queue (This is an interrupt, too. The CPU must stop what it s doing and mark the blocked task as ready ) Smith 4/27/01 2

State Diagram description Any tasks that are ready to run sit on the ready queue. This queue may be prioritized so the most important task runs next. When the scheduler decides the current task has had enough time on the CPU, either because it finished or its time slice is up, the Running task is moved to the Ready queue. Then the first task on the Ready queue is selected for Running. If the Running task needs I/O or needs a resource that is currently unavailable, it is put on the Blocked queue. When its resource becomes available, it goes back to Ready. Smith 4/27/01 3

Tasks don t work in isolation from each other. They often need to share data or modify it in series GPS translation Navigation Package Inertial Nav interpreter Position Data Instrument Interfaces Display Subsystem Smith 4/27/01 4

Inter-task communication examples Since only one task can be running at one time (remember the book analogy), there must be mechanisms for tasks to communicate with one another A task is reading data from a sensor at 15 hz. It stores 1024 bytes of data and then needs to signal a processing task to take and process the data so it has room to write more. A task is determining the state of a system- i.e. Normal Mode, Urgent Mode, Sleeping, Disabled. It needs to inform all other tasks in the system of a change in status. A user is communicating to another user across a network. The network receive task has to deliver messages to the terminal program, and the terminal program has to deliver messages to the network transmit task. Smith 4/27/01 5

Inter-task Communication Regular operating systems have many options for passing messages between processes, but most involve significant overhead and aren t deterministic. Pipes, message queues, semaphores, Remote Procedure Calls, Sockets, Datagrams, etc. In a RTOS, tasks generally have direct access to a common memory space, and the fastest way to share data is by sharing memory. In ordinary OS s, tasks are usually prevented from accessing another task s memory, and for good reason. Smith 4/27/01 6

int finished = 0; main() spawn( task1 ); spawn( task2 ); spawn( task3 ); while( finished!=3) ; printf( done ); Global Variables: an example in pseudocode void task1 (void) compute_pi_to_a zillion_places(); finished++; void task2 (void) solve_world_hunger(); finished++; void task3 (void) find_out_why_white_shirts_give_you_ black_belly_button_lint(); finished++; Smith 4/27/01 7

Mailboxes Post() - write operation- puts data in mailbox Pend() - read operation- gets data from mailbox Just like using a buffer or shared memory, except: If no data is available, pend() task is suspended Mutual exclusion built in: if somebody is posting, pend() has to wait. No processor time is wasted on polling the mailbox, to see if anything is there yet. Pend might have a timeout, just in case Smith 4/27/01 8

Buffering Data If you have a producer and a consumer that work at different rates, a buffer can keep things running smoothly As long as buffer isn t full, producer can write As long as buffer isn t empty, consumer can read Consumer Producer Smith 4/27/01 9

Shared Memory and Data Corruption Shared memory can be as simple as a global variable in a C program, or an OS-supplied block of common memory. In a single-task program, you know only one function will try to access the variable at a time. Write() Write() Let s look at a navigation system: We use a laser-based rangefinder to get altitude readings as available (approx. once every 5 seconds). We add a redundant system, an inertial navigation system, to update the altitude once a second: Smith 4/27/01 10

2 tasks sharing the same data INS Input Altitude 90 80 70 60 48 38 28 Laser Input 90 m 48 m 23 m Smith 4/27/01 11

Shared memory conflict: The INS executes several instructions while updating the altitude: Get stored altitude Subtract _ altitude Replace altitude with result One task may interrupt another at an arbitrary (possibly Bad ) point. INS Task Retrieve altitude 50m Subtract _ from altitude. 40m Replace altitude 40m Altitude: 50m Altitude: 42m Altitude: 40m Laser Task Get alt. from sensor 42m Store alt. in memory 42m Smith 4/27/01 12

Timing Problem: INS Input Altitude 60 50 40 30 20 10 0 Laser Input 60 m 43 m 26 m 9 m Smith 4/27/01 13

We need to be careful in multi-tasked systems, especially when modifying shared data. We want to make sure that in certain critical sections of the code, no two processes have access to data at the same time. Avoiding Conflict Smith 4/27/01 14

Mutual Exclusion If we set a flag (memory is busy, please hold), we can run into the same problem as the previous example: Flag set? Flag set? Set flag Set flag Write to Write to memory memory Unset Flag Smith 4/27/01 15

Atomic Operations An operating system that supports multiple tasks will also support atomic semaphores. The names of the functions that implement semaphores vary from system to system ( test-set/release; lock/unlock; wait/signal; P()/V() ) The idea: You check a lock before entering a critical section. If it s set, you wait. If it isn t, you go through the lock and unset it on your way out. The word atomic means checking the lock and setting it only takes one operation- it can t be interrupted. Smith 4/27/01 16

Semaphore Example TelescopeImageUpdate() if( time_is_now() ) lock( image_map ); update( curr_image[] ); unlock( image_map ); ImageTransmit() if( command == XMIT ) lock( transmitter ); lock( image_map ); broadcast( curr_image[] ); unlock( image_map ); unlock( transmitter ); Smith 4/27/01 17

Deadlock! ImageTransmit() if( command == XMIT ) lock( transmitter ); lock( image_map ); broadcast( curr_image[] ); unlock( image_map ); unlock( transmitter ); Waiting on image_map Process_Image_Weights() lock( image_map ); Waiting on transmitter color_map( curr_image[] ); lock( transmitter ); broadcast(colored_image[]); unlock( transmitter ); unlock( image_map ); Smith 4/27/01 18

Deadlock: detection and avoidance Cannot always be found in testing Four conditions necessary Area of mutual exclusion Circular wait Hold and wait No preemption Some well-known solutions exist Make all resources sharable Impose ordering on resources, and enforce it Force a task to get all of its resources at the same time or wait on all of them Allow priority preemption Smith 4/27/01 19

Other ways around synchronization problems Avoidance: Only write single-task programs or programs that don t use shared memory Ostrich method: Ignore the problem completely, assuming it won t happen often, or at least not often enough for your customers to sue you Brute force: Disable interrupts completely during critical section operations Smith 4/27/01 20

Summary Buffering data can smooth out the interaction of a producer that generates data at one rate and a consumer that eats at another. Intertask communication can be tricky- if your operating system supports high-level communication protocols, and they are appropriate for your task, use them! If you use a flag to indicate a resource is being used, understand why checking and setting the flag needs to be atomic. For Next time: Read sections 11.1, 11.2 (intro section only) 11.3, 11.4 Smith 4/27/01 21