Signals, Synchronization CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.

Slides:



Advertisements
Similar presentations
Recitation 8: 10/28/02 Outline Processes Signals –Racing Hazard –Reaping Children Annie Luo Office Hours: Thursday 6:00.
Advertisements

More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
Scheduling CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
Exceptional Control Flow Processes Today. Control Flow Processors do only one thing: From startup to shutdown, a CPU simply reads and executes (interprets)
Operating Systems Process Scheduling (Ch 3.2, )
A. Frank - P. Weisberg Operating Systems Advanced CPU Scheduling.
CS591 (Spring 2001) The Linux Kernel: Signals & Interrupts.
Chapter 3: CPU Scheduling
Introduction to Operating Systems – Windows process and thread management In this lecture we will cover Threads and processes in Windows Thread priority.
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.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
Slide 6-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Threads and Scheduling 6.
3.5 Interprocess Communication
Advanced OS Chapter 3p2 Sections 3.4 / 3.5. Interrupts These enable software to respond to signals from hardware. The set of instructions to be executed.
CS Lecture 16 Outline Inter-process Communication (IPC) – Pipes – Signals Lecture 161CS Operating Systems 1.
Signal Signal : - is a notification sent to a process to notify it of some event - interrupts whatever the process is doing and force it to handle a signal.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
Section A (March 14) Outline Exceptions Process Signals Non-local jumps Reminders Lab4: Due Next Thursday TA: Kun Gao Shamelessly Modified from Minglong.
Operating Systems Process Scheduling (Ch 4.2, )
CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
Slide 6-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Threads and Scheduling 6.
1Chapter 05, Fall 2008 CPU Scheduling The CPU scheduler (sometimes called the dispatcher or short-term scheduler): Selects a process from the ready queue.
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.
Chapter 6: CPU Scheduling
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
Operating System Examples - Scheduling
1Reference “Introduction To Unix Signals Programming” in the reference material section Man page – sigprocmask, alarm “Understanding the Linux Kernel”
Operating Systems Chapter 2
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Scheduling Example 3 (1) Assume: FIFO Job Scheduling 100 K Main Memory Processor Sharing Process Scheduling (Cont…)
Lecture 7: Scheduling preemptive/non-preemptive scheduler CPU bursts
Outline for Today Objectives –Finish discussion of Birrell –UNIX Signals –Eraser Administrative –Spider talk after class.
1 Signals (continued) CS 241 April 9, 2012 University of Illinois.
CE Operating Systems Lecture 2 Low level hardware support for operating systems.
UNIX Signals * POSIX-Defined Signals * Signaling Processes * Signal Mask * sigaction * kill and sigaction * alarm * Interval Timers * POSIX.1b Timers *
Operating Systems Recitation 4, April th, 2002 Signals.
Signals. Introduction r A signal is a mechanism for notifying a process that an event has occurred. m When a signal is sent to a process is normal execution.
CE Operating Systems Lecture 2 Low level hardware support for operating systems.
Processes. Process Concept Process Scheduling Operations on Processes Interprocess Communication Communication in Client-Server Systems.
Signals & Message queue Inter process mechanism in Linux system 3/24/
Operating Systems CMPSC 473 Signals, Introduction to mutual exclusion September 28, Lecture 9 Instructor: Bhuvan Urgaonkar.
S -1 Processes. S -2 wait and waitpid (11.2) Recall from a previous slide: pid_t wait( int *status ) wait() can: (a) block; (b) return with status; (c)
Synchronization CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
Operating Systems Unit 2: – Process Context switch Interrupt Interprocess communication – Thread Thread models Operating Systems.
1 Lecture 19: Unix signals and Terminal management n what is a signal n signal handling u kernel u user n signal generation n signal example usage n terminal.
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Signals.
CE Operating Systems Lecture 8 Process Scheduling continued and an introduction to process synchronisation.
Operating System Concepts
Interrupts and Exception Handling. Execution We are quite aware of the Fetch, Execute process of the control unit of the CPU –Fetch and instruction as.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Overview: Using Hardware.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Advanced Operating Systems CS6025 Spring 2016 Processes and Threads (Chapter 2)
1 Module 3: Processes Reading: Chapter Next Module: –Inter-process Communication –Process Scheduling –Reading: Chapter 4.5, 6.1 – 6.3.
G.Jyostna.
Recitation 7 (Oct. 25) Outline Minglong Shao Office hours: Reminders
Processes and threads.
Threads CSSE 332 Operating Systems Rose-Hulman Institute of Technology
Threads and Cooperation
Chapter 5: CPU Scheduling
Exceptional Control Flow Part II
Process management Information maintained by OS for process management
Exceptional Control Flow
Signals Tanzir Ahmed CSCE 313 Fall 2018.
Exceptional Control Flow
Outline Chapter 2 (cont) Chapter 3: Processes Virtual machines
Signals.
Outline Chapter 3: Processes Chapter 4: Threads So far - Next -
Chapter 3: Process Concept
Presentation transcript:

Signals, Synchronization CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

Announcements Program Assignment #1 due Tuesday Feb. 15 at 11:55 pm –TA will explain parts b-d in recitation Read chapters 7 and 8 Today: –Finish RR and multi-level scheduling, –cover Signals (helpful for PA #1), and –begin Ch. 8 Synchronization

Round Robin Scheduling (cont.) Weighted Round Robin - each process is given some number of time slices, not just one per round this is a way to provide preferences or priorities even with preemptive time slicing

Multi-level Queue Scheduling Partitions ready queue into several queues –different processes have different needs, e.g. foreground and background –so don’t apply the same scheduling policy to every process, e.g. foreground gets RR, background gets FCFS Queues can be organized by priority, or each given a percentage of CPU, or a hybrid combination

Multi-level Feedback Queues Allows processes to move between queues Criteria for movement could depend upon: –age of a process: old processes move to higher priority queues –behavior of a process: could be CPU-bound processes move down the hierarchy of queues, allowing interactive and I/O-bound processes to move up assign a time slice to each queue, with smaller time slices higher up if a process doesn’t finish by its time slice, it is moved down to the next lowest queue over time, a process gravitates towards the time slice that typically describes its average local CPU burst

Signals Allows one process to interrupt another process using OS signaling mechanisms A signal is an indication that notifies a process that some event has occurred 30 types of signals on Linux systems Each signal corresponds to some kind of system event

Signals Without signals, low-level hardware exceptions are processed by the kernel’s exception handlers only, and are not normally visible to user processes Signals expose occurrences of such low-level exceptions to user processes If a process attempts to divide by zero, kernel sends a SIGFPE signal (number 8) If a process makes an illegal memory reference, the kernel sends it a SIGSEGV signal (number 11) If you type a ctrl-C, this sends a SIGINT to foreground process A process can terminate another process by sending it a SIGKILL signal (#9)

Signals NumberName/TypeEvent 2SIGINTInterrupt from keyboard 11SIGSEGVinvalid memory ref (seg fault) 14SIGALRMTimer signal from alarm function 29SIGIOI/O now possible on descriptor

Signals Kernel sends a signal to a destination process by updating some state in the context of the destination process A destination process receives a signal when it is forced by the kernel to react in some way to the delivery of the signal: –can ignore the signal –terminate / exit (this is the usual default) –catch the signal by executing a user-defined function called the signal handler

Signals A signal that has been sent but not yet received is called a pending signal –At any point in time, there can be at most one pending signal of a particular type (signal number) –A pending signal is received at most once a process can selectively block the receipt of certain signals –when a signal is blocked, it can be delivered, but the resulting pending signal will not be received until the process unblocks the signal For each process, the kernel maintains the set of pending signals in the pending bit vector, and the set of blocked signals in the blocked bit vector

Signals Default action is to terminate a process Sending signals with kill function –kill -9 PID at command line, or can call kill from within a process A process can send SIGALRM signals to itself by calling the alarm function –alarm(T seconds) arranges for kernel to send a SIGALRM signal to calling process in T seconds –see code example next slide #include uses signal function to install a signal handler function that is called asynchronously, interrupting the infinite while loop in main, whenever the process receives a SIGALRM signal When handler returns, control passes back to main, which picks up where it was interrupted by the arrival of the signal, namely in its infinite loop

Signals #include int beeps=0; void handler(int sig) { if (beeps<5) { alarm(3); beeps++; } else { printf(“DONE\n”); exit(0); } int main() { signal(SIGALRM, handler); alarm(3); while(1) { ; } exit(0); } Signal handler cause next SIGALRM to be sent to this process in 3 seconds register signal handler cause first SIGALRM to be sent to this process in 3 seconds infinite loop that gets interrupted by signal handling

Signals Receiving signals: –when a kernel is returning from some exception handler, it checks to see if there are any pending signals for a process before passing control to the process –default action is typically termination – signal(signum, handler) function is used to change the action associated with a signal if handler is SIG_IGN, then signals of type signum are ignored if handler is SIG_DFL, then revert to default action otherwise handler is user-defined function call the signal handler. This installs or registers the signal handler. –Invocation of the signal handler is called catching the signal –Execution of signal handler is called handling the signal

Signals When a process catches a signal of type signum=k, the handler installed for signal k is invoked with a single integer argument set to k This argument allows the same handler function to catch different types of signals When handler executes its return statement (finishes), control usually passes back to the instruction where the process was interrupted

Signals Handling multiple signals –choose to handle the lower number signals first –pending signals are blocked, e.g. if a 2 nd SIGINT is received while handling 1 st SIGINT, the 2 nd SIGINT becomes pending and won’t be received until after the handler returns –pending signals are not queued there can be at most one pending signal of type k, e.g. if a 3 rd SIGINT arrives while the 1 st SIGINT is being handled and the 2 nd SIGINT is already pending, then the 3 rd SIGINT is dropped –system calls can be interrupted slow system calls that are interrupted by signal handling may not resume in some systems, and may return immediately with an error

Signals Portable signal handling: sigaction() is a standard POSIX API for signal handling –allows users on Posix-compliant systems such as Linux and Solaris to specify the signal-handling semantics they want, e.g. whether sys call is aborted or restarted sigaction(signum, struct sigaction*act, struct sigaction *oldact) each struct sigaction can define a handler, e.g. action.sa_handler = handler; In part iv of PA #1, –use sigaction() to define the handler, e.g. reschedule() –Also need to set up the timer - use setitimer instead of alarm. A SIGALRM is delivered when timer expires.

Chapter 8: Synchronization Protect access to shared common resources, e.g. buffers, variables, files, devices, etc., by using some type of synchronization Examples: –processes P1 and P2 use IPC to establish a shared memory buffer between them, and have to arbitrate access to avoid writing to the same memory location at the same time –Threads T1 and T2 shared some global variables, and have to synchronize to avoid writing to the same memory location at the same time Producer-Consumer example, next slide

Synchronization a Producer process P1 and a Consumer process P2 share some memory, say a bounded buffer Producer writes data into shared memory, while Consumer reads data In order to indicate that there is new data (produced), or that existing data has been read (consumed), then define a variable counter –keeps track of how much new data is in the buffer that has not yet been read –if counter==0, then consumer shouldn’t read from the buffer –if counter==MAX_BUFF_SIZE, then producer can’t write to the buffer

Synchronization while(1) { while(counter==MAX); buffer[in] = nextdata; in = (in+1) % MAX; counter++; } Data Bounded Buffer counter Producer Process Consumer Process while(1) { while(counter==0); getdata = buffer[out]; out = (out+1) % MAX; counter--; } Producer writes new data into buffer and increments counter Consumer reads new data from buffer and decrements counter counter updates can conflict! buffer[0] buffer[MAX]

Synchronization counter++; can translate into several machine language instructions, e.g. reg1 = counter; reg1 = reg1 + 1; counter = reg1; counter--; can translate into several machine language instructions, e.g. reg2 = counter; reg2 = reg2 - 1; counter = reg2; If these low-level instructions are interleaved, e.g. the Producer process is context-switched out, and the Consumer process is context-switched in, and vice versa, then the results of counter’s value can be unpredictable

Synchronization // counter++ reg1 = counter; reg1 = reg1 + 1; counter = reg1; // counter--; reg2 = counter; reg2 = reg2 - 1; counter = reg2; ( 1) [5] ( 3) [6] ( 2) [5] ( 4) [4] ( 5) [6] ( 6) [4] Suppose we have the following sequence of interleaving, where the brackets [value] denote the local value of counter in either the producer or consumer’s process. Let counter=5 initially. At the end, counter = 4. But if steps (5) and (6) were reversed, then counter=6 !!! Value of shared variable counter changes depending upon (an arbitrary) order of writes - very undesirable!