Controlling concurrency A look at some techniques for process synchronization in the Linux environmemt.

Slides:



Advertisements
Similar presentations
Process Management.
Advertisements

3.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Process An operating system executes a variety of programs: Batch system.
CSCC69: Operating Systems
Concurrent Programming Introducing the principles of reentrancy, mutual exclusion and thread-synchronication.
Readers and Writers An introduction to the Linux programming interface for using UNIX semaphores.
OPERATING SYSTEMS Threads
Threads. Objectives To introduce the notion of a thread — a fundamental unit of CPU utilization that forms the basis of multithreaded computer systems.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
I/o multiplexing On adding a ‘poll()’ method to our character-mode device-driver for an 82573L network controller.
Computer Systems/Operating Systems - Class 8
The ‘thread’ abstraction A look at the distinction between the idea of a ‘process’ and the concept of a ‘thread’
Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)
15-213/ Intro to Computer Systems by btan with reference to Spring 10’s slides.
Operating Systems, Spring 2002 Ittai Abraham, Zinovi Rabinovich (recitation)
Chapter 3: Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Objectives Understand Process concept Process scheduling Creating.
Concurrent TCP connections A look at design-changes which permit a TCP server to handle multiple clients without delays.
3.5 Interprocess Communication Many operating systems provide mechanisms for interprocess communication (IPC) –Processes must communicate with one another.
Operating Systems Course Hebrew University Spring 2007 Signals & User Thread.
Inter Process Communication:  It is an essential aspect of process management. By allowing processes to communicate with each other: 1.We can synchronize.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
3.5 Interprocess Communication
Concurrent Programming Introducing some principles of reentrancy, mutual exclusion and thread-synchronization.
CMPT 300: Operating Systems I Ch 3: Processes Dr. Mohamed Hefeeda
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
The ‘wait4()’ function Looking at how the various ‘wait()’ system-calls rely on ‘wait4()’ in the x86_64 Linux kernel.
Some server “stress” tests Can our TCP concurrent server handle many successive and/or simultaneous connections?
Multiplexing i/o A look at some alternatives under Linux for dealing concurrently with multiple sources of device-input.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
CS 3013 & CS 502 Summer 2006 Threads1 CS-3013 & CS-502 Summer 2006.
Chapter 4: Threads READ 4.1 & 4.2 NOT RESPONSIBLE FOR 4.3 &
Processes Part I Processes & Threads* *Referred to slides by Dr. Sanjeev Setia at George Mason University Chapter 3.
Operating Systems CSE 411 CPU Management Sept Lecture 11 Instructor: Bhuvan Urgaonkar.
UNIX Signals Bach 7.2 Operating Systems Course The Hebrew University Spring 2010.
Signals & Timers CS241 Discussion Section Spring 2009 Week 6.
Chapter 4: Threads. 4.2CSCI 380 Operating Systems Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux.
Silberschatz, Galvin and Gagne ©2011Operating System Concepts Essentials – 8 th Edition Chapter 4: Threads.
1Reference “Introduction To Unix Signals Programming” in the reference material section Man page – sigprocmask, alarm “Understanding the Linux Kernel”
1 Logging in to a UNIX System init ( Process ID 1 created by the kernel at bootstrap ) spawns getty for every terminal device invokes our login shell terminal.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
Threads and Thread Control Thread Concepts Pthread Creation and Termination Pthread synchronization Threads and Signals.
Florida State UniversityCOP5570 – Advanced Unix Programming Today’s topics System V Interprocess communication (IPC) mechanisms –Message Queues –Semaphores.
Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.
LINUX System : Lecture 7 Bong-Soo Sohn Lecture notes acknowledgement : The design of UNIX Operating System.
Linux Processes Travis Willey Jeff Mihalik. What is a process? A process is a program in execution A process includes: –program counter –stack –data section.
UNIX Socket Programming CS 6378 Project Reference Book: Unix Network programming: Networking APIs: Sockets and XTI (2nd edition), Prentice Hall >> Threads.
Signals and Signal Handling. Signals A predefined message sent between two processes or from the kernel to a process, or by a user to a process A software.
NCHU System & Network Lab Lab #8 Signals Operating System Lab.
1 Signals (continued) CS 241 April 9, 2012 University of Illinois.
UNIX Signals * POSIX-Defined Signals * Signaling Processes * Signal Mask * sigaction * kill and sigaction * alarm * Interval Timers * POSIX.1b Timers *
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Department of Computer Science and Software Engineering
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Lecture 3 Threads Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
CS 241 Section Week #10 (04/01/10) ‏. Topics This Section  MP5 Overview  Signals.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Signals.
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Signals.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Chapter 4: Threads Modified by Dr. Neerja Mhaskar for CS 3SH3.
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
Chapter 4: Threads.
Chapter 4: Threads.
Threads Chapter 5 2/17/2019 B.Ramamurthy.
Threads Chapter 5 2/23/2019 B.Ramamurthy.
Still Chapter 2 (Based on Silberchatz’s text and Nachos Roadmap.)
Chapter 3: Processes.
Signals.
Lab #9 Semaphores Operating System Lab.
Presentation transcript:

Controlling concurrency A look at some techniques for process synchronization in the Linux environmemt

What is a ‘race condition’ ? Without any ‘synchronization’ mechanism, multiprogramming is vulnerable to ‘races’ in which programs produce unpredictable and erroneous results, due to the relative timing of instruction-execution in separate threads or processes An example program demonstrates this phenomenon (see our ‘racedemo.cpp’)

Two tasks write to one terminal A parent-process forks a child-process, and both write messages to the screen, but without coordinating their efforts The operating system’s task-scheduler repeatedly preempts each of them The result is incomprehensible gibberish! Programs like this one are said to contain a ‘race condition’

The cure is communication What’s needed is some way for the tasks to be made aware of each other’s actions Various mechanisms for this exist in Linux One of the simplest ways is by ‘signaling’ (i.e., one task can wait for an ‘all clear’ signal to be sent to it by the other task) But ‘busy-waiting’ needs to be avoided, since it wastes CPU time and degrades the overall efficiency of the system

The ‘signal mask’ Each process has a ‘signal mask’ that can be used to ‘block’ certain specific signals The signal mask for each process is kept in its process control block (in the kernel) But a process can inspect and modify its signal mask by using special system-calls

The ‘sigset_t’ type Process Control Block struct task_struct blocked sigset_t The signal mask is a collection of flag-bits that indicates which signals are to be ‘blocked’

How to inspect ‘signal mask’ 1. Include the header-file: #include 2. Declare a ‘sigset_t’ object: sigset_t sigmask; 3. Call the ‘sigprocmask()’ library-function: sigprocmask( 0, NULL, &sigmask );

How to modify ‘signal mask’ 1. Declare two ‘sigset_t’ objects: sigset_tnset, oset; 2. Initialize the ‘new’ signal-set: sigemptyset( &nset ); sigaddset( &nset, SIGUSR1 ) sigaddset( &nset, SIGUSR2 ) 3. Call the ‘sigprocmask()’ library-function: sigprocmask( SIG_BLOCK, &nset, &oset );

How to wait for a signal 1. Declare and initialize a global variable: int done = 0; 2 Define your signal-handling function: void upon_signal( int signum ) { done = 1; }. 3. Install your signal-handler function: signal( SIGUSR1, upon_signal ); signal( SIGUSR2, upon_signal ); 4. Declare and initialize a ‘sigset_t’ object: sigset_t zeromask; sigemptyset( &zeromask ); 5. Then use ‘sigsuspend()’ to wait for your signal: while ( done == 0 ) sigsuspend( &zeromask );

Our ‘racecure.cpp’ demo These signal-handling library-functions are used by this demo-program to remove the ‘race condition’ without doing busy-waiting It’s based on ideas of W. Richard Stevens from his classic: “Advanced Programming in the UNIX Environment” (1993).

How it works done signal-mask signal-handler parent-process write-message; TELL_CHILD; WAIT_CHILD; done signal-mask signal-handler child-process WAIT_PARENT; write-message; TELL_PARENT; user kernel SIGUSR1 SIGUSR2

In-class exercises Modify ‘racedemo’ so that the parent forks twice (i.e., two child-processes), with three processes all writing to the ‘stdout’ stream Then add synchronization functions which will eliminate the race conditions and allow the parent-process to finish writing before either of the child-processes begins