Project 2 kthreads, concurrency, shuttle. Highlevel overview User space –program wants to control the shuttle simulation by sending requests. –start_shuttle()

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling.
1 Operating Systems, 122 Practical Session 5, Synchronization 1.
1 Lecture 18 Further Threading Overview  Case Study: Cooperating Threads  Producer/ Consumer model threads  Waiting for Synchronized Data  Busy Waiting.
Operating Systems ECE344 Ding Yuan Synchronization (I) -- Critical region and lock Lecture 5: Synchronization (I) -- Critical region and lock.
CS444/CS544 Operating Systems Synchronization 2/16/2006 Prof. Searleman
1 CS318 Project #3 Preemptive Kernel. 2 Continuing from Project 2 Project 2 involved: Context Switch Stack Manipulation Saving State Moving between threads,
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Concurrent Processes Lecture 5. Introduction Modern operating systems can handle more than one process at a time System scheduler manages processes and.
Monitors CSCI 444/544 Operating Systems Fall 2008.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Concurrency: Deadlock and Starvation Chapter 6.
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
CS 241 Section Week #4 (2/19/09). Topics This Section  SMP2 Review  SMP3 Forward  Semaphores  Problems  Recap of Classical Synchronization Problems.
Multithreaded Web Server.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
1 Concurrent Languages – Part 1 COMP 640 Programming Languages.
1 Confidential Enterprise Solutions Group Process and Threads.
4061 Session 23 (4/10). Today Reader/Writer Locks and Semaphores Lock Files.
Semaphores, Locks and Monitors By Samah Ibrahim And Dena Missak.
CS425 /CSE424/ECE428 – Distributed Systems – Fall 2011 Material derived from slides by I. Gupta, M. Harandi, J. Hou, S. Mitra, K. Nahrstedt, N. Vaidya.
Threads and Thread Control Thread Concepts Pthread Creation and Termination Pthread synchronization Threads and Signals.
COMP 111 Threads and concurrency Sept 28, Tufts University Computer Science2 Who is this guy? I am not Prof. Couch Obvious? Sam Guyer New assistant.
11/18/20151 Operating Systems Design (CS 423) Elsa L Gunter 2112 SC, UIUC Based on slides by Roy Campbell, Sam.
1 Program5 Due Friday, March Prog4 user_thread... amount = … invoke delegate transact (amount)... mainThread... Total + = amount … user_thread...
CS252: Systems Programming
IT 325 Operating systems Chapter6.  Threads can greatly simplify writing elegant and efficient programs.  However, there are problems when multiple.
Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1.
Week 3 January 22, 2004 Adrienne Noble. Today CVS – a great tool to use with your groups Threads – basic thread operations Intro to synchronization Hand.
Lecture 12 CV. Last lecture Controlling interrupts Test and set (atomic exchange) Compare and swap Load linked and store conditional Fetch and add and.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
1 Previous Lecture Overview  semaphores provide the first high-level synchronization abstraction that is possible to implement efficiently in OS. This.
Condition Variables Condition variables support three operations: Wait – add calling thread to the condition variable’s queue and put the thread to sleep.
Dining Philosophers & Monitors Questions answered in this lecture: How to synchronize dining philosophers? What are monitors and condition variables? What.
Implementing Lock. From the Previous Lecture  The “too much milk” example shows that writing concurrent programs directly with load and store instructions.
Implementing Mutual Exclusion Andy Wang Operating Systems COP 4610 / CGS 5765.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores.
pThread synchronization
Case Study: Pthread Synchronization Dr. Yingwu Zhu.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Kernel Synchronization David Ferry, Chris Gill CSE 522S - Advanced Operating Systems Washington University in St. Louis St. Louis, MO
Tutorial 2: Homework 1 and Project 1
Multi Threading.
PARALLEL PROGRAM CHALLENGES
Background on the need for Synchronization
Thread synchronization
Kthreads, Mutexes, and Debugging
Review and Q/A.
Background and Motivation
CSE 451 Autumn 2003 Section 3 October 16.
Implementing Mutual Exclusion
Concurrency: Mutual Exclusion and Process Synchronization
Synchronization Primitives – Semaphore and Mutex
Implementing Mutual Exclusion
Userspace Synchronization
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
Lecture 12 CV and Semaphores
CSE 451 Section 1/27/2000.
Kthreads, Mutexes, and Debugging
EECE.4810/EECE.5730 Operating Systems
Presentation transcript:

Project 2 kthreads, concurrency, shuttle

Highlevel overview User space –program wants to control the shuttle simulation by sending requests. –start_shuttle() –issue_request() –stop_shuttle() Kernel space program wants to simulate the shuttle according those requests. Need to use a thread to simulate the shuttle running.

Kthreads Thread: basic executable unit. Run the shuttle in its own kthread, not the thread that calls the module_init function.

kthreads Kernel Threads Kthread_run(threadfn, data, name,...) –Creates a new thread of execution that starts with the function pointed to by threadfn –threadfn: fuction pointer –data: data passed to threadfn –name: name of the thread

while shuttle_running if users want get off at current terminal Unload (release space) if users want get on at current terminal and have space (..other performance logic) Load (consume space) Move to next desired terminal (*) endwhile

Module (1) Start a new kernel thread, performing a infinite loop. (2) Simulating the shuttle (load, unload, move to next) (3) Finally you may use kthread_stop to stop that thread.

Structure static int yourshuttle_init(void) { //create an proc entry //set function pointers } static void yourshuttle_exit(void) { //reset function pointers to null //remove module data file module_init(yourshuttle_init); module_exit(yourshuttle_exit);

Structure int yourstartshuttle(void) { //initilization //kthread_run } int yourissuerequest(pass_type, initial_terminal, dest_terminal) { //add pass } int yourstopshuttle(void) { //release resources

Add one shuttle

Good shuttle

Remove one shuttle

Good shuttle

Simultaneously shuttle

12345

Bad shuttle

That is correct shuttle

Why Two processes(threads) are accessing the same data. The order of doing things matters. Potential problem, unpredictable.

Concurrency problem? User space: –start_taxi, issue_request, stop_taxi Kernel space: –simulate the shuttle and the queues Bad thing happens if they are accessing the same queue.

Protection There can not be multiple threads accessing the same data, simultaneously. User space –add one passenger to the queue Kernel space –remove one passenger from the queue

"Lock" The solution is to use lock. When a thread wants to access the queue, it first check if it is locked: –if yes, then it waits until it becomes unlocked –if no, then it is fine to move on.

Add one lock before accessing the data; Remove the lock after accessing the data; What is a lock –mutex –semaphore –atom –......

Mutex Semaphone is old... do not use semaphone. Use mutex. Put a pair of mutex lock before and after the critical section.

mutex struct mutex lock; mutex_init(&lock); mutex_lock(&lock); { } mutex_unlock(&lock);

Mutex Kernel Module (Prepares Lock) struct mutex m; mutex_init(&m); User-space Code (Adds Alice) Kernel Module (Removes Bob) mutex_lock(&m) list_item->data = Alice list_item->next = NULL list_item->prev = bob_item Queue->last = list_item mutex_unlock(&m); mutex_lock(&m) newlast = list_item->last->prev newlast->next = NULL delete Queue->last Queue->last = newlast mutex_unlock(&m)

Mutex Kernel Module (Prepares Lock) struct mutex m; mutex_init(&m); User-space Code (Adds Alice) Kernel Module (Removes Bob) mutex_lock(&m) list_item->data = Alice list_item->next = NULL list_item->prev = bob_item Queue->last = list_item mutex_unlock(&m); mutex_lock(&m) newlast = list_item->last->prev newlast->next = NULL delete Queue->last Queue->last = newlast mutex_unlock(&m) Requests lock Receives it

Mutex Kernel Module (Prepares Lock) struct mutex m; mutex_init(&m); User-space Code (Adds Alice) Kernel Module (Removes Bob) mutex_lock(&m) list_item->data = Alice list_item->next = NULL list_item->prev = bob_item Queue->last = list_item mutex_unlock(&m); mutex_lock(&m) newlast = list_item->last->prev newlast->next = NULL delete Queue->last Queue->last = newlast mutex_unlock(&m) Doing stuff

Mutex Kernel Module (Prepares Lock) struct mutex m; mutex_init(&m); User-space Code (Adds Alice) Kernel Module (Removes Bob) mutex_lock(&m) list_item->data = Alice list_item->next = NULL list_item->prev = bob_item Queue->last = list_item mutex_unlock(&m); mutex_lock(&m) newlast = list_item->last->prev newlast->next = NULL delete Queue->last Queue->last = newlast mutex_unlock(&m) Requests lock Already locked Waits...

Mutex Kernel Module (Prepares Lock) struct mutex m; mutex_init(&m); User-space Code (Adds Alice) Kernel Module (Removes Bob) mutex_lock(&m) list_item->data = Alice list_item->next = NULL list_item->prev = bob_item Queue->last = list_item mutex_unlock(&m); mutex_lock(&m) newlast = list_item->last->prev newlast->next = NULL delete Queue->last Queue->last = newlast mutex_unlock(&m) Releases lock Notifies threads

Mutex Kernel Module (Prepares Lock) struct mutex m; mutex_init(&m); User-space Code (Adds Alice) Kernel Module (Removes Bob) mutex_lock(&m) list_item->data = Alice list_item->next = NULL list_item->prev = bob_item Queue->last = list_item mutex_unlock(&m); mutex_lock(&m) newlast = list_item->last->prev newlast->next = NULL delete Queue->last Queue->last = newlast mutex_unlock(&m) Wakes up Requests lock Gets lock

Q&A