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

Similar documents
CSE 421: Introduction to Operating Systems

CS213. Exceptional Control Flow Part II. Topics Process Hierarchy Signals

Processes & Signals. System Runs Many Processes Concurrently. State consists of memory image + register values + program counter

Signals. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Operating Systems 2010/2011

Lecture 2 Process Management

Multitasking. Programmer s model of multitasking. fork() spawns new process. exit() terminates own process

Shell and Signals. Computer Organization 3/17/2015. CSC252 - Spring The World of Multiprogramming or Multitasking. Unix Process Hierarchy

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 18

CSci 4061 Introduction to Operating Systems. (Advanced Control Signals)

Programming Assignments will be.. All the PAs are continuous 3 major factors that you should consider

Signals. CSC209: Software Tools and Systems Programming. Furkan Alaca & Paul Vrbik

Overview. Administrative. * HW 1 grades. * HW 2 Due. Topics. * 5.1 What is a Signal? * Dealing with Signals - masks, handlers

Concurrent Processes. CS 475, Spring 2018 Concurrent & Distributed Systems

Embedded Systems Programming

A read or write being atomic means that its effect is as if it happens instantaneously.

Signals. POSIX defines a variety of signal types, each for a particular

Lecture 7: Signals and Events. CSC 469H1F Fall 2006 Angela Demke Brown

ECE 650 Systems Programming & Engineering. Spring 2018

Signals are a kernel-supported mechanism for reporting events to user code and forcing a response to them. There are actually two sorts of such

Signals and Session Management. Signals. Mechanism to notify processes of system events

Interprocess Communication

CHAPTER 2: PROCESS MANAGEMENT

OPERATING SYSTEMS. UNIT II Sections A, B & D. An operating system executes a variety of programs:

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

Signals: Management and Implementation. Sanjiv K. Bhatia Univ. of Missouri St. Louis

Shell Execution of Programs. Process Groups, Session and Signals 1

Operating Systems. Threads and Signals. Amir Ghavam Winter Winter Amir Ghavam

Exceptions, Processes and Signals

IMPLEMENTATION OF SIGNAL HANDLING. CS124 Operating Systems Fall , Lecture 15

Signals. Joseph Cordina

Princeton University. Computer Science 217: Introduction to Programming Systems. Signals

KING FAHD UNIVERSITY OF PETROLEUM AND MINERALS Information and Computer Science Department ICS 431 Operating Systems Lab # 6

CSC Operating Systems Spring Lecture - XII Midterm Review. Tevfik Ko!ar. Louisiana State University. March 4 th, 2008.

Chapter 4 Multithreaded Programming

Operating Systems 9/6/ SIGINT Terminate Interrupt from keyboard (ctl-c) 9 SIGKILL Terminate Kill program (cannot override or ignore)

Signals. Goals of this Lecture. Help you learn about: Sending signals Handling signals

Linux Signals and Daemons

CS370 Operating Systems

F28HS Hardware-Software Interface: Systems Programming

Lecture 24: Multitasking and Signals

Processes. CS 475, Spring 2018 Concurrent & Distributed Systems

System Calls & Signals. CS449 Spring 2016

SMD149 - Operating Systems

Chapter 5: CPU Scheduling

CS370 Operating Systems

Chapter 6: CPU Scheduling

Lesson 3. The func procedure allows a user to choose the action upon receipt of a signal.

Computer Science & Engineering Department I. I. T. Kharagpur. Operating System: CS rd Year CSE: 5th Semester (Autumn ) Lecture VII

Problem Set: Processes

CSCI 4061: Signals and Signal Handlers

ECE 7650 Scalable and Secure Internet Services and Architecture ---- A Systems Perspective. Part I: Operating system overview: Processes and threads

System Programming. Signals I

Overview. POSIX signals. Generation of signals. Handling signals. Some predefined signals. Real-time Systems D0003E 3/3/2009

Signal Example 1. Signal Example 2

* What are the different states for a task in an OS?

Start of Lecture on January 20, Chapter 3: Processes

CSC209H Lecture 8. Dan Zingaro. March 4, 2015

CSE410 Operating Systems Spring 2018 Project 1: Introduction to Unix/Linux Signals

Problem Set: Processes

THE PROCESS ABSTRACTION. CS124 Operating Systems Winter , Lecture 7

What Is A Process? Process States. Process Concept. Process Control Block (PCB) Process State Transition Diagram 9/6/2013. Process Fundamentals

Concurrency Problems Signals & Synchronization Semaphore Mutual Exclusion Critical Section Monitors

Processes Prof. James L. Frankel Harvard University. Version of 6:16 PM 10-Feb-2017 Copyright 2017, 2015 James L. Frankel. All rights reserved.

Exceptional Control Flow Exists at All Levels of a System. Systemprogrammering 2007 Föreläsning 3 Exceptional Control Flow Part II

Processes and Threads

Announcements. Reading. Project #1 due in 1 week at 5:00 pm Scheduling Chapter 6 (6 th ed) or Chapter 5 (8 th ed) CMSC 412 S14 (lect 5)

Killing Zombies, Working, Sleeping, and Spawning Children

Course Syllabus. Operating Systems

Chapter 5 CPU scheduling

Operating Systems. VI. Threads. Eurecom. Processes and Threads Multithreading Models

CPU Scheduling. Daniel Mosse. (Most slides are from Sherif Khattab and Silberschatz, Galvin and Gagne 2013)

ECF Exists at All Levels of a System Exceptional Control Flow Part II Oct. 22, 2002 Topics! Process Hierarchy! Shells! Signals!

Operating System Concepts Ch. 5: Scheduling

Unix System Programming - Chapter 8

8: Scheduling. Scheduling. Mark Handley

Goals of this Lecture

Roadmap. Tevfik Ko!ar. CSC Operating Systems Fall Lecture - VII CPU Scheduling - II. Louisiana State University

Roadmap. Multilevel Queue Scheduling. Multilevel Queue. Example of Multilevel Feedback Queue. Multilevel Feedback Queue. Tevfik Ko!

System Calls and Signals: Communication with the OS. System Call. strace./hello. Kernel. Context Switch

Implementing Lightweight Threads

Announcements. Program #1. Program #0. Reading. Is due at 9:00 AM on Thursday. Re-grade requests are due by Monday at 11:59:59 PM.

Announcements. Program #1. Reading. Due 2/15 at 5:00 pm. Finish scheduling Process Synchronization: Chapter 6 (8 th Ed) or Chapter 7 (6 th Ed)

What is an Operating System? Signals, Processes, & Threads. Resource Sharing. Resource Abstraction ... Operating Systems 10/20/2010

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

Properties of Processes

Lecture 5 / Chapter 6 (CPU Scheduling) Basic Concepts. Scheduling Criteria Scheduling Algorithms

Notice: This set of slides is based on the notes by Professor Perrone of Bucknell and the textbook authors Silberschatz, Galvin, and Gagne

CPU Scheduling. Operating Systems (Fall/Winter 2018) Yajin Zhou ( Zhejiang University

OPERATING SYSTEMS CS3502 Spring Processor Scheduling. Chapter 5

CSCE Operating Systems Interrupts, Exceptions, and Signals. Qiang Zeng, Ph.D. Fall 2018

CPU Scheduling. Basic Concepts. Histogram of CPU-burst Times. Dispatcher. CPU Scheduler. Alternating Sequence of CPU and I/O Bursts

o Reality The CPU switches between each process rapidly (multiprogramming) Only one program is active at a given time

CS 33. Signals Part 1. CS33 Intro to Computer Systems XXII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Processes. Processes (cont d)

CPU Scheduling. Rab Nawaz Jadoon. Assistant Professor DCS. Pakistan. COMSATS, Lahore. Department of Computer Science

Process management. What s in a process? What is a process? The OS s process namespace. A process s address space (idealized)

CS240: Programming in C

Lecture Topics. Announcements. Today: Uniprocessor Scheduling (Stallings, chapter ) Next: Advanced Scheduling (Stallings, chapter

CS3733: Operating Systems

Transcription:

, 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

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

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)

Number Name/Type Event 2 SIGINT Interrupt from keyboard 11 SIGSEGV invalid memory ref (seg fault) 14 SIGALRM Timer signal from alarm function 29 SIGIO I/O now possible on descriptor

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

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

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<signal.h> 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

#include <signal.h> 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

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

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

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

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

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

// counter++ reg1 = counter; reg1 = reg1 + 1; counter = reg1; Synchronization 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. (1) [5] (3) [6] // counter--; reg2 = counter; (2) [5] reg2 = reg2-1; (4) [4] counter = reg2; (5) [6] (6) [4] 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!