CS4101 Introduction to Embedded Systems Lab 12: Task Synchronization

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Ch 7 B.
1 Implementations: User-level Kernel-level User-level threads package each u.process defines its own thread policies! flexible mgt, scheduling etc…kernel.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
5.6.2 Thread Synchronization with Semaphores Semaphores can be used to notify other threads that events have occurred –Producer-consumer relationship Producer.
Synchronization Principles. Race Conditions Race Conditions: An Example spooler directory out in 4 7 somefile.txt list.c scores.txt Process.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
1 Race Conditions/Mutual Exclusion Segment of code of a process where a shared resource is accessed (changing global variables, writing files etc) is called.
Semaphores Questions answered in this lecture: Why are semaphores necessary? How are semaphores used for mutual exclusion? How are semaphores used for.
Outline Introduction to MQX Initializing and starting MQX
CS 149: Operating Systems February 17 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak
LAB 11: Task Synchronization Chung-Ta King National Tsing Hua University CS 4101 Introduction to Embedded Systems.
LAB 8: Program Design Pattern and Software Architecture
Semaphores, Locks and Monitors By Samah Ibrahim And Dena Missak.
FreeRTOS.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
Lecture 8 Task Utilization. Theoretical Analysis.
Case Study: Pthread Synchronization Dr. Yingwu Zhu.
Chien-Chung Shen CIS/UD
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 Queues of FreeRTOS
Outline Introduction to task synchronization MQX events MQX mutexs
Interprocess Communication Race Conditions
CS703 - Advanced Operating Systems
REAL-TIME OPERATING SYSTEMS
Outline Introduction Centralized shared-memory architectures (Sec. 5.2) Distributed shared-memory and directory-based coherence (Sec. 5.4) Synchronization:
Outline Analog to digital conversion (ADC) of NuMaker TRIO
CS4101 Introduction to Embedded Systems Lab 10: Tasks and Scheduling
CS4101 Introduction to Embedded Systems Lab 11: Task Synchronization
CS4101 嵌入式系統概論 Real-Time Operating System
COT 4600 Operating Systems Fall 2009
Background on the need for Synchronization
CS4101 Introduction to Embedded Systems Lab 11: Sensors
CS4101 Introduction to Embedded Systems Lab 1: General Purpose IO
CS4101 Introduction to Embedded Systems Lab 13: Task Synchronization
Prof. Chung-Ta King Department of Computer Science
CS4101 Introduction to Embedded Systems Lab 9: NuMaker ADC and LCD
An Embedded Software Primer
CS4101 嵌入式系統概論 Tasks and Scheduling
CS510 Operating System Foundations
CS4101 嵌入式系統概論 RT Scheduling & Synchronization
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.
Realtime System Fundamentals : Scheduling and Priority-based scheduling B. Ramamurthy cse321-fall2014 9/20/2018.
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
Realtime System Fundamentals : Scheduling and Priority-based scheduling B. Ramamurthy cse321-fall /27/2018.
Semaphore Originally called P() and V() wait (S) { while S <= 0
Process Synchronization
UNIVERSITY of WISCONSIN-MADISON Computer Sciences Department
Realtime System Fundamentals
Realtime System Fundamentals : Scheduling and Priority-based scheduling B. Ramamurthy Amrita-UB-MSES /11/2013.
Computer Science & Engineering Electrical Engineering
CSCI1600: Embedded and Real Time Software
Chapter 6 Synchronization Principles
Thread Synchronization including Mutual Exclusion
CSE 153 Design of Operating Systems Winter 19
CS4101 Introduction to Embedded Systems Lab 2: Basic IO and Timer
Chapter 6: Synchronization Tools
Prof. Chung-Ta King Department of Computer Science
Process Synchronization
CSCI1600: Embedded and Real Time Software
CS 144 Advanced C++ Programming May 7 Class Meeting
Process Synchronization
Introduction to FreeRTOS
Lab #9 Semaphores Operating System Lab.
Presentation transcript:

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

Introduction In this lab , we will learn How to implement interrupt-driven controls in FreeRTOS using binary semaphores How to use Mutex to ensure exclusive accesses to system resources How to use counting semaphores to communicate data between tasks

Binary Semaphore Example vSemaphoreHandle binary_sem; //Global handler void one_sec_isr(void){ // an ISR xSemaphoreGiveFromISR(binary_sem, NULL); } void sem_task(void *p){ while(1) if(xSemaphoreTake(binary_sem,999999)) puts("Tick!"); int main(void){ vSemaphoreCreateBinary(binary_sem); xTaskCreate(sem_task, (signed char*)) "t1", 2048, NULL, 1, NULL); vTaskStartScheduler(); return 0;

Mutex Example (1/3) Two tasks attempt to gain exclusive accesses to a precious resource xSemaphoreHandle gatekeeper = 0; //Global handler int main(void){ gatekeeper = xSemaphoreCreateMutex(); /*Create tasks with priority 1 for both users*/ xTaskCreate(user_1, (signed char*)) "t1", 1024, NULL, 1, NULL); xTaskCreate(user_2, (signed char*)) "t2", 1024, vTaskStartScheduler(); return 0; }

Mutex Example (2/3) void user_1(void *p){ while(1){ if(xSemaphoreTake(gatekeeper, 1000)){ puts("User 1 got access"); access_precious_resource(); // critical section xSemaphoreGive(gatekeeper); } else{ puts("User 1 failed to get access within 1000ms"); } vTaskDelay(1000); // or do other works /*Without delay, user 1 will get key immediately after releasing the key */

Mutex Example (3/3) void user_2(void *p){ while(1){ if(xSemaphoreTake(gatekeeper, 1000)){ puts("User 2 got access"); access_precious_resource(); // critical section xSemaphoreGive(gatekeeper); } else{ puts("User 2 failed to get access within 1000ms"); } vTaskDelay(1000); /*Without delay, user 2 will get key immediately after releasing the key */

Counting Semaphore Example (1/2) xSemaphoreHandle count_sem; //Global Handler int main(void){ /**parameter for uxMaxCount, uxInitialCount/ count_sem = xSemaphoreCreateCounting(2, 2); /*Create tasks with priority 1 for both users*/ xTaskCreate(task1, (signed char*)) “t1", 1024, NULL, 1, NULL); xTaskCreate(task2, (signed char*)) “t2", vTaskStartScheduler(); return 0; }

Counting Semaphore Example (2/2) void task1(void *p){ while(1){ if(xSemaphoreTake(count_sem, portMAX_DELAY)){ xSemaphoreGive(count_sem); } vTaskDelay(3000); void task2(void *p){ if(xSemaphoreTake(count_sem), portMAX_DELAY){

Lab: Basic 1 Modify Basic 2 of Lab 11 so that Task 3 is interrupt- driven Whenever light changes (from bright to dark and vice versa) on NuMaker TRIO, an interrupt is triggered If the light changes from bright to dark, then a FreeRTOS task will print “Dark” on the display If the light changes from dark to bright, then a FreeRTOS task will print “Bright” on the display Remember to clear the display before you print

Lab: Basic 2 Extend Basic 1 to allow exclusive accesses of the tasks to the LCD display Create a Mutex to guard the LCD so that only one task can print texts to the LCD display at any time Note: Texts should be displayed at the same position of the LCD You may like to delay the tasks for a while after they printed to the LCD so that the race condition is obvious What happen if the light is blocked very swiftly? Will your program miss the light change events?

Lab: Basic 3 Create 3 sender tasks, 1 receiver task and 2 buffers Each buffer is an array that can hold 10 characters Sender task 1 will fill an empty buffer with “aaaaa” and “bbbbb” alternatively; sender task 2 with texts “ccccc” and “ddddd” alternatively; and sender task 3 with “eeeee” and “fffff” alternatively The receiver task will read the text from the filled buffer and print it on the LCD display Use counting semaphores to allow data communicated between sender and receiver tasks