Concurrency Aspects of Project 2

Similar documents
Kthreads, Mutexes, and Debugging. Sarah Diesburg CS 3430 Operating Systems

Week 8 Locking, Linked Lists, Scheduling Algorithms. Classes COP4610 / CGS5765 Florida State University

NPTEL Course Jan K. Gopinath Indian Institute of Science

NPTEL Course Jan K. Gopinath Indian Institute of Science

1 #include <linux/module.h> 2 #include <linux/sched.h> 3 #include <linux/interrupt.h> 4 #include <linux/init.h> 5 #include <linux/kernel.

High Performance Computing Course Notes Shared Memory Parallel Programming

Synchronization for Concurrent Tasks

7.4 Simple example of Linux drivers

Linux drivers - Exercise

Finish up OS topics Group plans

/dev/hello_world: A Simple Introduction to Device Drivers under Linux

Synchronization. CSE 2431: Introduction to Operating Systems Reading: Chapter 5, [OSC] (except Section 5.10)

Virtual File System (VFS) Implementation in Linux. Tushar B. Kute,

Systems Programming/ C and UNIX

Kernel Modules. Kartik Gopalan

Concurrency: a crash course

High-level Synchronization

Synchronization Spinlocks - Semaphores

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Chapter 7: Process Synchronization!

CS 423 Operating System Design: Introduction to Linux Kernel Programming (MP1 Q&A)

CSC 1600: Chapter 6. Synchronizing Threads. Semaphores " Review: Multi-Threaded Processes"

Synchronization Principles I

Dept. of CSE, York Univ. 1

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

Systèmes d Exploitation Avancés

Chapter 6: Process Synchronization

Synchronization I. Jo, Heeseung

Linux Kernel Development (LKD)

Character Device Drivers One Module - Multiple Devices

User Space Multithreading. Computer Science, University of Warwick

CS5460/6460: Operating Systems. Lecture 24: Device drivers. Anton Burtsev April, 2014

REVISION HISTORY NUMBER DATE DESCRIPTION NAME

COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang

Synchronization. Disclaimer: some slides are adopted from the book authors slides with permission 1

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

C09: Process Synchronization

Achieving Synchronization or How to Build a Semaphore

Toolbox Kernel Korner

Loadable Kernel Module

Unix (Linux) Device Drivers

Synchronization and Semaphores. Copyright : University of Illinois CS 241 Staff 1

CMSC 412 Project #3 Threads & Synchronization Due March 17 th, 2017, at 5:00pm

Processes often need to communicate. CSCB09: Software Tools and Systems Programming. Solution: Pipes. Recall: I/O mechanisms in C

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Steve Gribble. Synchronization. Threads cooperate in multithreaded programs

Recap: Thread. What is it? What does it need (thread private)? What for? How to implement? Independent flow of control. Stack

Prof. Dr. Hasan Hüseyin

CS 153 Lab4 and 5. Kishore Kumar Pusukuri. Kishore Kumar Pusukuri CS 153 Lab4 and 5

Operating Systems: William Stallings. Starvation. Patricia Roy Manatee Community College, Venice, FL 2008, Prentice Hall

POSIX Threads: a first step toward parallel programming. George Bosilca

CS 378 (Spring 2003)

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

Process Synchronization Mechanisms

Introduction to OS Synchronization MOS 2.3

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

Synchronization COMPSCI 386

Lecture. DM510 - Operating Systems, Weekly Notes, Week 11/12, 2018

RCU. ò Walk through two system calls in some detail. ò Open and read. ò Too much code to cover all FS system calls. ò 3 Cases for a dentry:

Learning Outcomes. Concurrency and Synchronisation. Textbook. Concurrency Example. Inter- Thread and Process Communication. Sections & 2.

VFS, Continued. Don Porter CSE 506

Problem 1: Concepts (Please be concise)

Threads. lightweight processes

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

CS 153 Design of Operating Systems Winter 2016

Concurrency and Synchronisation

Concurrency and Synchronisation

CSE 333 SECTION 3. POSIX I/O Functions

Introduction to PThreads and Basic Synchronization

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Hank Levy 412 Sieg Hall

Step Motor. Step Motor Device Driver. Step Motor. Step Motor (2) Step Motor. Step Motor. source. open loop,

Concurrency, Thread. Dongkun Shin, SKKU

l12 handout.txt l12 handout.txt Printed by Michael Walfish Feb 24, 11 12:57 Page 1/5 Feb 24, 11 12:57 Page 2/5

Background. The Critical-Section Problem Synchronisation Hardware Inefficient Spinning Semaphores Semaphore Examples Scheduling.

Linux Device Drivers

Operating systems for embedded systems. Embedded Operating Systems

USB. Development of a USB device driver working on Linux and Control Interface. Takeshi Fukutani, Shoji Kodani and Tomokazu Takahashi

PROCESS SYNCHRONIZATION

Synchronization. Race Condition. The Critical-Section Problem Solution. The Synchronization Problem. Typical Process P i. Peterson s Solution

Concurrency. On multiprocessors, several threads can execute simultaneously, one on each processor.

Process Synchronisation (contd.) Operating Systems. Autumn CS4023

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

G52CON: Concepts of Concurrency

Introduction Reading Writing scull. Linux Device Drivers - char driver

Process Synchronization

CMSC421: Principles of Operating Systems

Synchronization I. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Process Coordination

Operating systems for embedded systems

CS 333 Introduction to Operating Systems. Class 4 Concurrent Programming and Synchronization Primitives

Reading compiler errors

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430

Agenda. Highlight issues with multi threaded programming Introduce thread synchronization primitives Introduce thread safe collections

CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio

CS 453: Operating Systems Programming Project 5 (100 points) Linux Device Driver

Last Class: Synchronization

Device Drivers. CS449 Fall 2017

Concept of a process

MS Windows Concurrency Mechanisms Prepared By SUFIAN MUSSQAA AL-MAJMAIE

Project 2 : User Level Thread Library Operating Systems September 19, 2003

Linux Kernel Development (LKD)

Transcription:

Locking

Concurrency Aspects of Project 2 Multiple producers, one consumer Multiple users can issue system calls at the same time Need to protect all shared data Examples Passengers may appear on a floor at the same time the elevator does /proc/elevator might be read at the same time that you're updating the backing data How do you guarantee correctness?

Global vs Local Global data is Declared outside of the functions Before any function that uses it Often required for kernel programming Very sensitive to concurrency issues

Global vs Local Local data is Declared within a functions Sensitive to concurrency when It depends on global data Parallel access to the function is possible Carefully consider whether they need to be synchronized

Synchronization Primitives Atomic functions Spin locks Semaphores Mutexes

Mutexes MUTual Exclusion Based on semaphores States Locked Unlocked Only one thread may hold the lock at a given time

Structure #include <linux/mutex.h> struct mutex my_mutex mutex_init(&my_mutex)

Locking mutex_lock (&my_mutex) Waits indefinitely mutex_lock_interruptible(&my_mutex) Locks so long as it is not interrupted Returns 0 if succeeded Returns <0 if interrupted Preferred

Unlocking mutex_unlock(&my_mutex) Guarantees that the mutex is unlocked

Example Compete module Uses counter module as a template Two kthreads compete for a single variable /proc/compete shows which thread last wrote to it

Headers #include <linux/init.h> #include <linux/module.h> #include <linux/proc_fs.h> #include <linux/slab.h> #include <linux/string.h> #include <linux/kthread.h> #include <linux/mutex.h> #include <linux/delay.h> #include <asm-generic/uaccess.h> MODULE_LICENSE("GPL"); MODULE_AUTHOR("Britton"); MODULE_DESCRIPTION("Simple module featuring proc read");

Globals #define ENTRY_NAME "compete" #define PERMS 0644 #define PARENT NULL static struct file_operations fops; #define BUFFER_SIZE 20 #define KTHREAD_STRING_1 "kthread 1" #define KTHREAD_STRING_2 "kthread 2" static struct task_struct *kthread1; static struct task_struct *kthread2; static struct mutex shared_data_mutex; static char *shared_data; static char *message; static int read_p;

Kthread Run int my_run(void *data) { char *name = (char*)data; mutex_lock_interruptible(&shared_data_mutex); while (!kthread_should_stop()) { strcpy(shared_data, name); strcat(shared_data, "\n"); mutex_unlock(&shared_data_mutex); schedule(); } mutex_lock_interruptible(&shared_data_mutex); } mutex_unlock(&shared_data_mutex); printk("the %s has terminated\n", name); return 0;

Proc Open int compete_proc_open(struct inode *sp_inode, struct file *sp_file) { } printk("proc called open\n"); read_p = 1; message = kmalloc(sizeof(char) * BUFFER_SIZE, GFP_WAIT GFP_IO GFP_FS); if (message == NULL) { } printk("error, counter_proc_open"); return -ENOMEM; mutex_lock_interruptible(&shared_data_mutex); strcpy(message, shared_data); mutex_unlock(&shared_data_mutex); return 0;

Proc Read ssize_t compete_proc_read(struct file *sp_file, char user *buf, size_t size, loff_t *offset) { int len = strlen(message); read_p =!read_p; if (read_p) { } return 0; } printk("proc called read\n"); copy_to_user(buf, message, len); return len;

Proc Close int compete_proc_release(struct inode *sp_inode, struct file *sp_file) { } printk("proc called release\n"); kfree(message); return 0;

Proc Init static int compete_init(void) { printk("/proc/%s create\n", ENTRY_NAME); mutex_init(&shared_data_mutex); shared_data = kmalloc(sizeof(char) * BUFFER_SIZE, GFP_WAIT GFP_IO GFP_FS); kthread1 = kthread_run(my_run, (void*)kthread_string_1, KTHREAD_STRING_1); if (IS_ERR(kthread1)) { } printk("error! kthread_run, %s\n", KTHREAD_STRING_1); return PTR_ERR(kthread1); kthread2 = kthread_run(my_run, (void*)kthread_string_2, KTHREAD_STRING_2); if (IS_ERR(kthread2)) { } printk("error! kthread_run, %s\n", KTHREAD_STRING_2); return PTR_ERR(kthread2);

Proc Init fops.open = compete_proc_open; fops.read = compete_proc_read; fops.release = compete_proc_release; if (!proc_create(entry_name, PERMS, NULL, &fops)) { printk("error! proc_create\n"); remove_proc_entry(entry_name, NULL); return -ENOMEM; } return 0; } module_init(compete_init);

Proc Exit static void compete_exit(void) { } int ret; ret = kthread_stop(kthread1); if (ret!= -EINTR) printk("%s has stopped\n", KTHREAD_STRING_1); ret = kthread_stop(kthread2); if (ret!= -EINTR) printk("%s has stopped\n", KTHREAD_STRING_2); kfree(shared_data); remove_proc_entry(entry_name, NULL); printk("removing /proc/%s.\n", ENTRY_NAME); module_exit(compete_exit);