CS4101 Introduction to Embedded Systems Lab 13: Task Synchronization

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Ch 7 B.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 6: 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.
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Objectives Understand.
Synchronization Principles. Race Conditions Race Conditions: An Example spooler directory out in 4 7 somefile.txt list.c scores.txt Process.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Concurrency 1 CS502 Spring 2006 Thought experiment static int y = 0; int main(int argc, char **argv) { extern int y; y = y + 1; return 0; }
CS4101 嵌入式系統概論 RTOS and MQX Prof. Chung-Ta King Department of Computer Science National Tsing Hua University, Taiwan ( Materials from Freescale; Prof.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Outline Introduction to MQX Initializing and starting MQX
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Background Concurrent.
LAB 11: Task Synchronization Chung-Ta King National Tsing Hua University CS 4101 Introduction to Embedded Systems.
6.3 Peterson’s Solution The two processes share two variables: Int turn; Boolean flag[2] The variable turn indicates whose turn it is to enter the critical.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 14: October 14, 2010 Instructor: Bhuvan Urgaonkar.
1 Using Semaphores CS 241 March 14, 2012 University of Illinois Slides adapted in part from material accompanying Bryant & O’Hallaron, “Computer Systems:
Operating Systems CSE 411 CPU Management Oct Lecture 14 Instructor: Bhuvan Urgaonkar.
LAB 12: Timer and Interrupt Chung-Ta King National Tsing Hua University CS 4101 Introduction to Embedded Systems.
Chap 6 Synchronization. Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms.
Chapter 6: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Synchronization Background The Critical-Section.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Chapter 6: Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware.
Synchronization III: Summary CPE Operating Systems
LAB 13: IO Driver Chung-Ta King National Tsing Hua University CS 4101 Introduction to Embedded Systems.
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
CSCI1600: Embedded and Real Time Software Lecture 17: Concurrent Programming Steven Reiss, Fall 2015.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Big Picture Lab 4 Operating Systems C Andras Moritz
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
Introduction In this lab , we will learn
MQX GPIO.
Lab 7 Basic 1: Game of Memory
Introduction In this lab , we will learn
Outline Introduction to task synchronization MQX events MQX mutexs
Interprocess Communication Race Conditions
Outline Introduction Centralized shared-memory architectures (Sec. 5.2) Distributed shared-memory and directory-based coherence (Sec. 5.4) Synchronization:
CS4101 Introduction to Embedded Systems Lab 8: Tower System
CS4101 Introduction to Embedded Systems Lab 11: Task Synchronization
Chapter 6: Process Synchronization
Process Synchronization: Semaphores
Auburn University COMP 3500 Introduction to Operating Systems Synchronization: Part 4 Classical Synchronization Problems.
PARALLEL PROGRAM CHALLENGES
Background on the need for Synchronization
Chapter 5: Process Synchronization – Part II
CS4101 Introduction to Embedded Systems Lab 12: Task Synchronization
CS4101 Introduction to Embedded Systems Lab 11: Sensors
CS4101 Introduction to Embedded Systems Lab 1: General Purpose IO
National Tsing Hua University CS4101 Introduction to Embedded Systems Lab 2: Timer and Clock Prof. Chung-Ta King Department of Computer Science National.
National Tsing Hua University CS4101 Introduction to Embedded Systems Lab 3: Interrupt Prof. Chung-Ta King Department of Computer Science National Tsing.
National Tsing Hua University CS4101 Introduction to Embedded Systems Lab 6: Serial Communication Prof. Chung-Ta King Department of Computer Science National.
CS4101 嵌入式系統概論 I/O Drivers Prof. Chung-Ta King
Semaphore Originally called P() and V() wait (S) { while S <= 0
Process Synchronization
Synchronization Hank Levy 1.
Computer Science & Engineering Electrical Engineering
Basic Synchronization
Synchronization Hank Levy 1.
CSE 153 Design of Operating Systems Winter 19
CS4101 Introduction to Embedded Systems Lab 2: Basic IO and Timer
Process Synchronization
Process Synchronization
CS4101 嵌入式系統概論 Device Drivers
Process/Thread Synchronization (Part 2)
Presentation transcript:

CS4101 Introduction to Embedded Systems Lab 13: Task Synchronization Prof. Chung-Ta King Department of Computer Science National Tsing Hua University, Taiwan

Introduction In this lab, we will learn To synchronize tasks using synchronization primitives of MQX

Sample Code: Reader-Writer NUM_BUFFERS buffers NUM_WRITERS writer tasks: Read data from 3-axis accelerometer Fill the first empty buffer with the received data 1 reader task: Find the first filled buffer and empty it Synchronization among writers and reader: each buffer is given three flags Assigned = 1: buffer is assigned to a writer task for writing Filled = 1: buffer is filled with data and is ready for read Emptied = 1: buffer is empty and is ready for write

Tasks and Buffers k writer tasks (read 3-axis data) … Suppose k > n semaphore (count = n) Flags of buffers: Assigned Filled Emptied … n buffers 1 reader task

Writer and Reader Task Writer task: Reader task: Read data from 3-axis accelerometer Find the first unassigned buffer and mark it “assigned” Wait until the buffer is empty and mark it not “emptied” Fill the buffer with data Mark the buffer “filled” and “unassigned” Reader task: Find the first “filled” buffer and mark it not “filled” Read the data from the buffer Mark the buffer “emptied” Semaphore index_sem to control at most NUM_BUFFERS writer tasks in filling the buffers

Sample Code: Main #include <mqx.h> #include <bsp.h> #include <sem.h> #include <fio.h> uchar buffer[NUM_BUFFERS][ARRAY_SIZE]; int Assigned[NUM_BUFFERS]; int Filled[NUM_BUFFERS]; int Emptied[NUM_BUFFERS]; const TASK_TEMPLATE_STRUCT MQX_template_list[] = { { MAIN_TASK, main_task, 2000, 8, "main", MQX_AUTO_START_TASK, 0, 0 }, { WRITE_TASK, write_task, 2000, 8, "write", MQX_TIME_SLICE_TASK, 0, 10 }, { READ_TASK, read_task, 2000, 8, "read", MQX_TIME_SLICE_TASK, 0, 10 }, { 0 } };

Sample Code: Main void main_task(uint_32 initial_data) { _task_id task_id; /* Initialize the flags */ for (i = 0; i < NUM_BUFFERS; i++) { Assigned[i] = 0; Filled[i] = 0; Emptied[i] = 1; } /* Create the semaphores */ if(_sem_create_component(1,1,6) != MQX_OK) { ... } if(_sem_create("sem.index",NUM_BUFFERS,0)!= MQX_OK){} /* Create the tasks */ for (i = 0; i < NUM_WRITERS; i++) { task_id = _task_create(0, WRITE_TASK, (uint_32)i);} task_id = _task_create(0,READ_TASK, 0); _task_block();

Sample Code: Writer CS void void write_task(uint_32 initial_data){ pointer index_sem; if(_sem_open("sem.index",&index_sem) != MQX_OK) {...} while (TRUE) { if(_sem_wait(index_sem, 0) != MQX_OK) {...} for(i = 0; i < NUM_BUFFERS; i++){ if(Assigned[i] == 0) { Assigned[i] = 1; while(Emptied[i] == 0){ ... /* wait */ } Emptied[i] = 0; for(j = 0; j < ARRAY_SIZE; j++){ buffer[i][j] = ... /* fill the buffer */ } Assigned[i] = 0; Filled[i] = 1; break;} } _sem_post(index_sem); } } CS

Sample Code: Reader void read_task(uint_32 initial_data) { pointer index_sem; int i; while (TRUE) { for(i = 0; i < NUM_BUFFERS; i++){ if(Filled[i] == 1){ Filled[i] = 0; ... /* read and empty the buffer */ Emptied[i] = 1; break; }

Semaphores & RoundRobin: user_config.h 1. Add the definition in bsp_twrk60d100m/twrk60d100m/user_config.h #define MQX_USE_SEMAPHORES 1 #define MQX_HAS_TIME_SLICE 1 2. Rebuild the bsp and psp library 2 1 2 9 9

Basic Lab There is a race condition in the sample code. Use semaphores or mutex to protect the critical data structures. [Hint]: First check whether there is a race condition between writer and reader tasks by examining their common variables. Next, do the same between writer tasks. Modify the writer code to minimize the time spend in the critical section, i.e., move all non-critical operations out of the critical section.

Bonus Lab In the original sample code, the writer task, after grabbing a buffer by finding the first unassigned buffer, waits for Emptied[i] == 1 using a while-loop. This is basically a spin-wait, which wastes CPU. Use Events to let the task blocked wait.