Kernel timing issues An introduction to the use of kernel timers and work queues.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

DEVICE DRIVER VINOD KAMATH CS691X PROJECT WORK. Introduction How to write/install device drivers Systems, Kernel Programming Character, Block and Network.
Ch 7 B.
Real-Time Library: RTX
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Module R2 CS450. Next Week R1 is due next Friday ▫Bring manuals in a binder - make sure to have a cover page with group number, module, and date. You.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
CS 6560 Operating System Design Lecture 7: Kernel Synchronization Kernel Time Management.
Another device-driver? Getting ready to program the network interface.
I/O Multiplexing The role of the ‘poll()’ method in Linux device-driver operations.
I/o multiplexing On adding a ‘poll()’ method to our character-mode device-driver for an 82573L network controller.
Read-Copy Update P. E. McKenney, J. Appavoo, A. Kleen, O. Krieger, R. Russell, D. Saram, M. Soni Ottawa Linux Symposium 2001 Presented by Bogdan Simion.
Concurrency, Race Conditions, Mutual Exclusion, Semaphores, Monitors, Deadlocks Chapters 2 and 6 Tanenbaum’s Modern OS.
On using ‘tasklets’ An example of the Linux kernel’s ‘tasklet’ mechanism for deferring some interrupt-handling work.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L19 (Chapter 24) Multithreading.
Timeout for some demos An instructive look at how the Linux Operating System sets up system data-structures.
A ‘ringbuffer’ application Introduction to process ‘blocking’ and the Linux kernel’s support for ‘sleeping’ and ‘waking’
Linux kernel timers How can a module cause a function to be invoked at some future moment in time?
Concurrency: Deadlock and Starvation Chapter 6. Revision Describe three necessary conditions for deadlock Which condition is the result of the three necessary.
CS533 Concepts of Operating Systems Class 4 Linux Kernel Locking Issues.
How to make a pseudo-file As a follow-up to our first lab, we examine the steps needed to create our own ‘/proc’ file.
03/09/2007CSCI 315 Operating Systems Design1 Memory Management Notice: The slides for this lecture have been largely based on those accompanying the textbook.
Kernel timing issues An introduction to the use of kernel timers and work queues.
Standard C Library Application Programming Interface to System-Calls.
Applying kernel timers Could we prevent a device-driver from consuming an excessive amount of processor time?
Concurrent Programming Introducing some principles of reentrancy, mutual exclusion and thread-synchronization.
Kernel Event-Timers Our first look at the Linux kernel’s mechanism for managing its dynamic timers.
Kernel timing issues An introduction to the use of kernel timers and work queues By Allan Cruise modified by sdc.
Task scheduling What are the goals of a modern operating system scheduler, and how does Linux achieve them?
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Multiplexing i/o A look at some alternatives under Linux for dealing concurrently with multiple sources of device-input.
Operating System Program 5 I/O System DMA Device Driver.
Multiprogramming. Readings r Silberschatz, Galvin, Gagne, “Operating System Concepts”, 8 th edition: Chapter 3.1, 3.2.
4P13 Week 3 Talking Points 1. Process State 2 Process Structure Catagories – Process identification: the PID and the parent PID – Signal state: signals.
Kernel Locking Techniques by Robert Love presented by Scott Price.
2001 Networking Operating Systems (CO32010) 1. Operating Systems 2. Processes and scheduling 4.
Overview Task State Diagram Task Priority Idle Hook AND Co-Routines
Multithreading Chapter Introduction Consider ability of human body to ___________ –Breathing, heartbeat, chew gum, walk … In many situations we.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1.
Preemptive Context Switching
Threads. Readings r Silberschatz et al : Chapter 4.
Project 3. “System Call and Synchronization” By Dongjin Kim
CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Synchronization.
Where Testing Fails …. Problem Areas Stack Overflow Race Conditions Deadlock Timing Reentrancy.
Ch6. Flow of Time Ch7. Getting Hold of Memory 홍원의.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Embedded Real-Time Systems Processing interrupts Lecturer Department University.
Timers and Time Management Ok-Kyun Ha
Linux Kernel Development - Robert Love
Chapter 5: Process Synchronization – Part 3
Chapter 5: Process Synchronization – Part II
Processes Chapter 3 These slides include text, figures, and information from Operating Systems Concepts, by Silberschatz, Galvin, and Gagne. They also.
Project 3. “System Call and Synchronization”
Multithreaded Programming in Java
An introduction to the use of kernel timers and work queues
Threads and Locks.
Multithreading Chapter 23.
Multithreading.
Chapter 7: Synchronization Examples
Chien-Chung Shen CIS/UD
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
NETWORK PROGRAMMING CNET 441
CSE 153 Design of Operating Systems Winter 19
CSE 542: Operating Systems
Chapter 3: Process Management
Presentation transcript:

Kernel timing issues An introduction to the use of kernel timers and work queues

Kernel semaphores Our ‘stash.c’ device-driver exhibited some ‘race conditions’ when we used it with two or more ‘reader’ processes (or with two or more ‘writer’ processes) We can eliminate such ‘races’ if our driver enforces a ‘one writer/one reader’ policy This is easy to do by using ‘semaphores’

Mutual-exclusion syntax Declare a semaphore: struct semaphore sem; To initialize this semaphore: init_MUTEX( &sem ); To acquire this semaphore: down_interruptible( &sem ); To release this semaphore: up( &sem);

‘open()’ uses file->f_fmode You can implement an ‘open()’ method in your device-driver that lets only one task at a time open your device for reading: { if ( file->f_fmode & FMODE_READ ) down_interruptible( &sem ); return 0;// success }

‘release()’ uses file->f_fmode You can implement a ‘release()’ method in your device-driver that lets a ‘reader’ task release its earlier acquired semaphore: { if ( file->f_fmode & FMODE_READ ) up( &sem ); return 0;// success }

struct file_operations struct file_operations my_fops = { owner:THIS_MODULE, read:my_read, write:my_write, open:my_open, release:my_release, };

‘newstash.c’ We wrote a new version of ‘stash.c’ that illustrates the use of two semaphores to restrict device-file access to one ‘writer’ and one ‘reader’ at a time Other tasks that want to ‘read’ or ‘write’ are put to sleep if they try to ‘open’ the device-file, but are woken up when the appropriate semaphore gets ‘released’

Kernel timers Another facility Linux offers lets drivers put a process to sleep until a fixed amount of time has elapsed (as measured in jiffies) When the timer expires, a driver-defined action will be performed, which can ‘wake up’ the process that was put to sleep, or could perform some alternative action (for example, the kernel timer could start over)

Programming syntax Declare a timer: struct timer_list mytimer; Initialize this timer: init_timer( &mytimer ); mytimer.func = mytimeraction; mytimer.data = (unsigned long)mydata; mytimer.expires = Install this timer: add_timer( &mytimer ); Modify this timer: mod_timer( &mytimer, ); Delete this timer: del_timer( &mytimer ); Delete it safely: del_timer_sync( &mytimer);

A kernel-timer caution A kernel timer’s timeout-action cannot do anything that could cause the current task to ‘sleep’ (such as copying data between user-space and kernel-space, or trying to allocate more kernel memory) However, to aid debugging, a timer CAN use ‘printk()’ within its timeout-routine

‘trytimer.c’ We have posted an example that shows how a Linux kernel timer can be used to perform a periodic action (such as using ‘printk()’ to issue a message every time the time expires, then restart the timer Notice that our demo is not able to issue messages directly to the console – its timer-function executes without a ‘tty’

Delaying work If a device-driver needs to perform actions that require using process resources (like a tty), or that may possibly ‘sleep’, then it can defer that work – using a ‘workqueue’

Programming syntax Declare: struct workqueue_struct *myqueue; struct work_struct thework; Define: void dowork( void *data ) { /* actions */ }; Initialize: myqueue = create_singlethread_workqueue( “mywork” ); INIT_WORK( &thework, dowork, ); Schedule: queue_dalayed_work( myqueue, &thework, ); Cleanup:if ( !cancel_delayed_work( &thework ) ) flush_workqueue( myqueue ); destroy_workqueue( myqueue );

‘tryworkq.c’ and ‘defermsg.c’ We have posted demo-modules that show the use of workqueues to perform actions later, either as soon as a ‘process context’ is available, or after a prescribed time Further details on the options for using an existing kernel workqueue or a workqueue of your own creation may be found in our textbook (Chapter 7 of LDD3)

Applying these ideas To demonstrate a programming situation in which using kernel timers is valuable, we created the ‘foo.c’ device-driver, plus an application that uses it (‘watchfoo.cpp’) You can compile and install the module, then execute the application: $ watchfoo But you will notice there are two ‘problems’ (excess cpu usage and loop-termination)

Reducing CPU’s usage The ‘watchfoo’ program rereads ‘/dev/foo’ constantly (numerous times per second), much faster than the human eye can see If you run the ‘top’ utility, you will see that up to 99-percent of the available CPU time is being consumed by ‘watchfoo’ You can add a kernel timer to the ‘foo.c’ driver to curtail this excessive reading

In-class exercise Modify ‘foo.c’ (call it ‘timedfoo.c’) as follows Create an integer flag-variable (‘ready’) as a global object in your module When your ‘read()’ function gets called, it should sleep until ‘ready’ equals TRUE; it should set ‘ready’ equal to FALSE when it awakens, but should set a timer to expire after 1/10 seconds Your timer’s action-function should set ‘ready’ back to TRUE, then wake up any sleeping tasks

Implementation hints You need a wait-queue (so your driver’s ‘reader’ tasks can sleep on it) You need a timer action-function You need to organize your timer-function’s data-items into a single structure (because the timer-function has only one argument) Your timer-function must do two things: –Change ‘ready’ to TRUE –Wake up any ‘sleepers’