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

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

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

System Programming. Signals I

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 18

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

Signals. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Process. Signal #8. Signals are software interrupts from unexpected events. a power failure. an alarm clock. the death of a child process

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

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

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

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

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

Signals. Joseph Cordina

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

Exceptional Control Flow Part II

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

System Calls & Signals. CS449 Spring 2016

Giving credit where credit is due

Computer Science & Engineering Department I. I. T. Kharagpur

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

Lecture 24: Multitasking and Signals

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

Preview. Process Termination. wait and waitpid() System Call. wait and waitpid() System Call. waitpid() System Call 10/23/2018

Process Control. Philipp Koehn. 23 April 2018

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

ECE 650 Systems Programming & Engineering. Spring 2018

Goals of this Lecture

Exceptional Control Flow Part II Nov. 2, 2009"

Exceptions, Processes and Signals

System Programming. Signals II

ACSC 372 Systems Programming. exec() Functions. Outline

CS240: Programming in C

Exceptional Control Flow: Signals and Nonlocal Jumps

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 19

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

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

CSE 421: Introduction to Operating Systems

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

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

So far, we know how to create processes. How do we communicate with them? Primary mechanism: signals

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

Fortran Signal Handling

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

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

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

Lecture 4 Threads. (chapter 4)

CSE 410: Computer Systems Spring Processes. John Zahorjan Allen Center 534

COSC243 Part 2: Operating Systems

Inter Process Communication - I

CSE 451: Operating Systems Winter Module 4 Processes. Mark Zbikowski Allen Center 476

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

Systems Programming/ C and UNIX

Operating Systems 2010/2011

Chapter 4 Multithreaded Programming

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

ECF Exists at All Levels of a System

Signals and Signal Handling - Part 2

Operating Systemss and Multicore Programming (1DT089)

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

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

Acontecimentos assíncronos (POSIX signals) Sincronização com múltiplos acontecimentos

PVPSIDDHARTHA INSTITUTE OF TECHNOLOGY

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

CMPSCI 230 Computer Systems Principles. Processes

Last Class. System Calls, Kernel Mode, and Process Implementation. Processes. Today. Operating Systems 9/11/2018 CSC 256/456 1

Processes. Processes (cont d)

COP4020 Programming Languages. Exception Handling Prof. Robert van Engelen

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

Interprocess Communication

Exceptional Control Flow: Signals

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

signals Communicating with the OS System call (last lecture) Signal (this lecture) User Process Operating System

Processes & Threads. (Chapter 3) CS 4410 Operating Systems. [R. Agarwal, L. Alvisi, A. Bracy, M. George, E. Sirer, R. Van Renesse]

APPLIED INFORMATICS Processes. Bash characteristics. Command type. Aliases.

Computer Science & Engineering Department I. I. T. Kharagpur

Signals and Inter-Process Communica.on

CSE 506: Opera.ng Systems Signals and Inter-Process Communica.on

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

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

1 Requesting for Memory

CSC209H Lecture 8. Dan Zingaro. March 4, 2015

UNIX System Programming. Overview. 2. Signal Types (31 in POSIX) Signal Sources. Signals. 1. Definition

Course Syllabus. Operating Systems, Spring 2016, Meni Adler, Danny Hendler & Amnon Meisels

Process Management forks, bombs, zombies, and daemons! Lecture 5, Hands-On Unix System Administration DeCal

Programs. Program: Set of commands stored in a file Stored on disk Starting a program creates a process static Process: Program loaded in RAM dynamic

F28HS Hardware-Software Interface: Systems Programming

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

Final Precept: Ish. Slides Originally Prepared by: Wonho Kim

Lecture 9: Real-Time Software Design Issues

Operating Systems Components. What is an Operating System? Process Management. Memory Management. Operating Systems 9/7/2018 CSC 256/456 1

COMP 2400 UNIX Tools

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

Kernel and processes

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

n Unconditionally kill process 8371 n Interactive interrupt key n See /usr/include/bits/signum.h (e.g., SIGKILL 9) 2 -Ken Wong, Sep 2008

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

Today. Exceptional Control Flow Processes. Exceptions and Processes. Control Flow. Altering the Control Flow

Today. Introduction to Computer Systems /18 243, Fall th Lecture. Control Flow. Altering the Control Flow.

Introduction to Processes

CS 355 Operating Systems. Keeping Track of Processes. When are processes created? Process States 1/26/18. Processes, Unix Processes and System Calls

Transcription:

Computer Science & Engineering Department I. I. T. Kharagpur Operating System: CS33007 3rd Year CSE: 5th Semester (Autumn 2006-2007) Lecture VII Goutam Biswas Date: 4th September, 2006 1 Signals Signals are software version of interrupt or exceptions, triggered by some event. The normal execution of the process is suspended and it is forced to handle the signal. When the <Ctrl-C> is pressed by the user, the INT signal (SIGINT) is sent to the current process. A signal may be raised by some error condition e.g. divide-by-zero (SIGFPE), memory protection violation (SIGSEGV), illegal instruction instruction (SIGILL). Each signal is represented by a small positive integer (it has a a symbolic name). When a signal is sent to a process, its normal execution is suspended and it is forced to call a function called signal handler. Different signals available in a system can be viewed by the command kill -l. It is also available from the man page of the syatem call signal (man 7 signal). A few of them are - SIGINT - interrupt from the keyboard, terminates the process (default). SIGILL - illegal instruction, terminate and core dupm. SIGFPE - floating-point exception, terminate and core dump. SIGKILL - kill signal, terminate, cannot be caught or ignored. SIGSEGV - invalid memory reference, terminate and core dump. SIGALRM - alarm clock, timer signal - terminate. SIGUSR1 and SIGUSR2 - user defined signals. SIGSTOP - stop executing, cannot be caught or ignored. SIGCHLD - child process may be stopped, ignored by default. SIGCONT - continue if stopped. There are a few signals e.g. SIGKILL (9), SIGSTOP etc. that the process cannot catch. 1

1.1 How to Send Signal Signal can be sent from the keyboard e.g. Ctrl-C for SIGINT, Ctrl-Z (SIGTSTP) to suspend a running process, CTRL-\ is very similar to Ctrl-C. /************************************** * Ctrl-C terminates the current * process: * $./a.out * Press Ctrl-C to terminate * Execute again * Press Ctrl-Z to suspend * $ fg to restart * Try Ctrl-\ **************************************/ int main(){ while(1) printf("what next!\n") ; Signal can be sent by executing the comand kill from the command line. $ ps -A... 29348 pts/2 00:00:00 vim 29350 pts/4 00:00:00 a.out 29352 pts/6 00:00:00 ps $ kill -SIGINT 29350 $ ps -A 29348 pts/2 00:00:00 vim 29353 pts/6 00:00:00 ps $ We already have seen the command fg. Signal can be sent using system call kill() (37). /**************************************** * Sending SIGINT signal to a process: * * $ cc -Wall killprog1.c -o killprog1 * * **************************************/ 2

int pid ; printf("enter a process ID:") ; scanf("%d", &pid) ; if(kill(pid, SIGINT) == -1) perror("kill fails\n") ; A child process can send signal to the parent! /********************************************* * Send SIGINT to parent: killprog2.c * * *******************************************/ if(fork() > 0) { // Parent while(1) { printf("\t\tin parent: Infinite Loop!\n") ; sleep(1) ; else { // Child int ppid ; printf("parent ID is: %d\n", ppid = getppid()) ; printf("sending SIGINT to the parent\n") ; kill(ppid, SIGINT) ; sleep(2) ; printf("parent ID is: %d\n", getppid()) ; Stop, continue and kill a child. /************************************************* * Stop a child and then continue 3

* $ cc -Wall sigstopcont.c * ***********************************************/ int chpid ; if((chpid = fork())!= 0) { // Parent printf("\tinside parent\n") ; printf("\tchild Stopped\n") ; kill(chpid, SIGSTOP) ; printf("\tchild Continue\n") ; kill(chpid, SIGCONT) ; kill(chpid, SIGKILL) ; else { // Child while(1) { printf("\t\tinside Child\n") ; sleep(1) ; 1.2 Catch and Handle When a signal is sent to a process, it is registered in the PCB (process table slot). The process is forced to react to the signal in three different ways - 1. The signal may be ignored. 2. There may be default action associated to the signal predefined by the kernel. The default action depends on the signal type. 3. The signal may be caught and a user defined function to handle the signal, known as the signal handler, can be invoked. There are some signals that cannot be ignored or caught e.g. SIGKILL, SIGSTOP etc. are of that type. But there are some signals that can be caught by a process, it can be ignored or a user defined handler can be written for it. Ignoring SIGINT - the system call signal() (48) 4

/************************************************** * SIGINT is sent to the process if <ctrl-c> is * * pressed. It may be ignored: SIGINTprog1.c * * ************************************************/ // signal(sigint, SIG_IGN) ; // Ignore <Ctrl-C> while(1) printf("what next?\n") ;; A new signal handler can be written for SIGINT. /************************************************** * SIGINT is sent to the process if <ctrl-c> is * * pressed. There may be a different signal * * handler: SIGINTprog2.c * * ************************************************/ void mysignalhandler(int n) { signal(sigint, SIG_DFL) ; // <ctrl-c> default printf("\t\tinside mysignalhandler()\n") ; signal(sigint, mysignalhandler) ; // <Ctrl-C> mysignalhandler() while(1) { printf("what next?\n") ; sleep(1); Handler for segmentation violation SIGSEGV. /******************************************* * SIGSEGV handler * SIGSEGVprog1.c *******************************************/ void mysegvhandler(int n){ signal(sigsegv, mysegvhandler) ; // SEGV default 5

printf("\t\tinside mysignalhandler()\n") ; _exit(0) ; int *p = (int *)100 ; // signal(sigsegv, mysegvhandler) ; // SEGV mysegvhandler() *p = 10 ; printf("not printed\n") ; By-pass the segmentation fault using setjmp and longjmp. /******************************************* * SIGSEGV handler - setjmp, longjmp * SIGSEGVprog2.c *******************************************/ #include <setjmp.h> jmp_buf env ; int fact(int n){ if(n == 0) return 1 ; return n*fact(n-1) ; void mysegvhandler(int n){ signal(sigsegv, mysegvhandler) ; // SEGV default printf("\t\tinside mysignalhandler()\n") ; longjmp(env, 1) ; int *p = (int *)100 ; // signal(sigsegv, mysegvhandler) ; // SEGV mysegvhandler() if(setjmp(env) == 0) *p = 10 ; printf("print after exception: %d! = %d\n", 5, fact(5)) ; The function call pause() () makes the process wait for a signal 6

/************************************************ * Sending an alarm signal in future * $ cc -Wall alarmclock.c * **********************************************/ void ringthebell(int n){ printf("\t\t\talarm signal received\n") ; int chpid ; if((chpid = fork())!= 0) { // Parent printf("\tinside parent\n") ; kill(chpid, SIGALRM) ; else { // Child signal(sigalrm, ringthebell) ; printf("\t\tinside child\n") ; printf("\t\tno alarm yet\n") ; pause() ; // wait for a signal Floating-point Exception: SIGFPE /************************************************** * SIGFPE is sent to the process if there is an * * arithmetic error: SIGFPEprog1.c * * ************************************************/ #include <stdlib.h> void mysigfpehandler(int signum) { printf("\t\tdivide-by-zero\n") ; exit(0) ; 7

int n ; signal(sigfpe, mysigfpehandler) ; printf("enter an integer:") ; scanf("%d", &n) ; printf("1/n = %d\n", 1/n) ; Two processes can communicate by sending signals. /************************************************ * Communicating between parent and child * using SIGUSR1 * SIGUSR1prog1.c ************************************************/ void sigusr1handler(int n){ signal(sigusr1, sigusr1handler) ; printf("---> Parent receives\n") ; void sigusr2handler(int n){ signal(sigusr2, sigusr2handler) ; printf("\t\tchild receives ---> ") ; int main() { int chpid, count = 0 ; if((chpid = fork())!= 0) { // Parent signal(sigusr1, sigusr1handler) ; while(1){ printf("\t%d: P --> C ", ++count) ; // sleep(1) ; kill(chpid, SIGUSR2) ; pause() ; else { // Child int ppid = getppid() ; signal(sigusr2, sigusr2handler) ; 8

while(1){ pause() ; printf("%d: C --> P\n", ++count) ; sleep(1) ; kill(ppid, SIGUSR1) ; References [1] http://users.actcom.co.il/ choo/lupg/tutorials/signals/signals-programming.html 9