Preview. The pause() System Call. The pause() System Call. The signal() System Call 10/18/2017

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

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

CSC209H Lecture 8. Dan Zingaro. March 4, 2015

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

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

Signals and Signal Handling - Part 2

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

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

Unix System Programming - Chapter 8

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

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

CS240: Programming in C

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

CSCI 4061: Signals and Signal Handlers

CS 3733 Operating Systems

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

Signals. Joseph Cordina

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

Chapter 5. TCP Client-Server

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

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

Embedded Systems Programming

Lecture 4. Introduction to Unix Processes. Introduction to Systems Programming: Processes and Signals

COM324 System Programming. Midterm Exam

Exceptions, Processes and Signals

Operating Systems 2010/2011

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

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

Systems Programming/ C and UNIX

Basic POSIX signal concepts

Exceptional Control Flow: Signals

系統程式 郭大維教授 / 施吉昇教授臺灣大學資訊工程系

Linux Signals and Daemons

Recitation 8 Processes, Signals, Tshlab

Recitation Processes, Signals,UNIX error handling

System Programming 熊博安國立中正大學資訊工程學系

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

Processes, Signals, I/O, Shell Lab : Introduc>on to Computer Systems Recita>on 9: Monday, Oct. 21, 2013 Marjorie Carlson Sec>on A

System Programming. Signals II

UNIVERSITY OF TORONTO SCARBOROUGH Computer and Mathematical Sciences. APRIL 2016 EXAMINATIONS CSCB09H3S Software Tools & Systems Programming

Goals of this Lecture

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

Advanced Unix/Linux System Program. Instructor: William W.Y. Hsu

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

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 19

Process Management. Outline

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

CSE 421: Introduction to Operating Systems

Workshop on Inter Process Communication Solutions

Lecture 4. Introduction to make Debugging with gdb and ddd Introduction to Systems Programming: Processes and Signals

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

Introduction to Computer Systems. Exam 2. April 11, Notes and calculators are permitted, but not computers.

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

POSIX Condition Variables ADT

CS631 - Advanced Programming in the UNIX Environment. Process Groups, Sessions, Signals

System Programming. Signals I

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

Week 13. Final exam review

Advanced UNIX Programming. Student Workbook

CS170 Operating Systems. Discussion Section Week 4 Project 2 - Thread

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

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

PVPSIDDHARTHA INSTITUTE OF TECHNOLOGY

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

#include <dirent.h> int connect(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen); DIR *opendir(const char *name);

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

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

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

CS 110 Summer 2018 Midterm Review Session

Department of Computer Application SRM University MC0618 Unix and Network Programming

IC221: Systems Programming 12-Week Written Exam [SOLUTIONS]

UNIT 6 SIGNALS AND DAEMON PROCESSES

Signals. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CS 33. Architecture and the OS. CS33 Intro to Computer Systems XIX 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Introduction. Interprocess communication. Terminology. Shared Memory versus Message Passing

Parameter Passing in C. Pointer Arithmetic. Parameter Passing in C. Pointer Arithmetic. Pointer Arithmetic. Tevfik Ko!ar

Processes. Processes (cont d)

Operating Systems. No. 5 ศร ณย อ นทโกส ม Sarun Intakosum

CS 33. Architecture and the OS. CS33 Intro to Computer Systems XIX 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Chapter 9. Signals and Signal Processing

Other Interprocess communication (Chapter 2.3.8, Tanenbaum)

F28HS Hardware-Software Interface: Systems Programming

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

Inter Process Communication

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

Discussion of Assignments 2. Line buffered vs. full buffered I/O. Some often encountered issues in the submissions.

Unix-Linux 2. Unix is supposed to leave room in the process table for a superuser process that could be used to kill errant processes.

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

ACSC 372 Systems Programming. exec() Functions. Outline

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 18

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

DRIVER AND LIBRARY FOR DB67 DSP BOARD DRIVER AND LIBRARY FOR DB67 DSP BOARD

Final Exam. Spring Semester 2017 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

Foundations of Computer Systems

COMP 2400 UNIX Tools

Computer Science 330 Operating Systems Siena College Spring Lab 5: Unix Systems Programming Due: 4:00 PM, Wednesday, February 29, 2012

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

Week 2 Intro to the Shell with Fork, Exec, Wait. Sarah Diesburg Operating Systems CS 3430

Transcription:

Preview The pause() System Call The pause() System call The signal() system call Signal set The sigprocmask() system call The sigaction() system call The sigsuspend() system call The abort() system call The pause() system call suspends the calling process until a signal is caught int pause (void); Return -1 with error 1 2 The pause() System Call The signal() System Call /* alarm1.c: demonstrate a signal system call*/ void ding (int sig) printf("alarm fired signal number = %d\n", sig); void (*signal(int signo, void (*func(int)))(int); signo: name of signal SIG func: pointer to siglal handler function printf("set alram 5 second for a process\n"); alarm(5); signal (SIGALRM, ding); exit(0); Function signal accept two arguments and return a pointer to a function that returns nothing. Second argument is pointer to a function that take a single integer argument and return nothing. 3 4 /* sig_talk2.c: demonstrate a signal system call*/ static int alarm_fired = 0; /* sig_talk1.c */ /* a process keep running waiting for a signal */ void ding (int sig) alarm_fired = 1; pid_t pid; printf("alarm application start \n"); if ((pid = fork() <0)) perror ("fork error"); exit (1); else if (pid == 0) /* child process */ sleep(5); kill(getppid(), SIGALRM); exit(0); else /* parent process */ printf("waiting for alarm \n"); signal (SIGALRM, ding); if (alarm_fired) printf("ding\n"); printf("done\n"); exit (0); 5 static void sig_usr(int); /* signal handler */ int main(void) if (signal(sigusr1, sig_usr) == SIG_ERR) printf("can't catch SIGUSR1"); exit(1); if (signal(sigusr2, sig_usr) == SIG_ERR) printf("can't catch SIGUSR2"); exit(1); for ( ; ; ) /* can wait for signal */ /* sinal handler must have one single integer */ static void sig_usr(int signo) if (signo == SIGUSR1) printf("received SIGUSR1\n"); else if (signo == SIGUSR2) printf("received SIGUSR2\n"); else printf("received not SIGUSR1 or SIGUSR2\n"); 6 1

The signal() System Call Signal Set /*signal.c send signal to a process */ int pid =11294; int i; for (i =1; i<=20 i++) if ( i % 2 == 1) kill (pid, SIGUSR1); else kill(pid, SIGUSR2); POSIX define the data type sigset_t to contain a signal set. There are five functions to manipulate signal set. int sigemptyset(sigset_t *set); /*set all signals are excluded */ int sigfillset(sigset_t *set); /*set all signals are included */ int sigaddset (sigset_t *set, int signo) /* set a signal set */ int sigdelset (sigset_t *set, int signo) /* reset a signal */ int sigismember (const sigset_t *set, int signo) /*check membership */ sleep(1); return 0; 7 8 Signal Set Either sigemptyset() or sigfillset() must be called for every object of type sigset_t before any other use of the object. The sigemptyset() function initializes a signal set to be empty. The sigfillset() function initializes a signal set to contain all signals. The sigaddset() function adds the specified signal signo to the signal set. The sigdelset() function deletes the specified signal signo from the sig- nal set. The sigismember() function returns whether a specified signal signo is contained in the signal set. The sigprocmask() System Call Signal mask of a process is the set of signals currently blocked from delivery to that process. The sigprocmask() function examines, or changes, or both examines and changes the signal mask of the calling process. int sigprocmask( int how, const sigset_t *set, sigset_t *oset ); 9 10 The sigprocmask() System Call int sigprocmask(int how, const sigset_t *set, sigset_t *oset ); return 0 for ok, -1 for error how (Input) The way in which the signal set is changed. The possible value of how are SIG_BLOCK:Indicates that the set of signals given by set should be blocked SIG_UNBLOCK:Indicates that the set of signals given by set should not be blocked SIG_SETMASK:the set of signals given by set should replace the old set of signals being blocked *set (Input) A pointer to a set of signals to be used to change the currently blocked set. May be NULL. *oset (Output) A pointer to the space where the previous signal mask is stored. May be NULL. The sigaction() System Call We can modify or examine the action associated with a particular signal by using system call sigaction(). int sigaction( int signo, const struct sigaction *act, struct sigaction *oact); struct segaction void (*sa_handler)(int)/*pointer to signal handler */ sigset_t sa_mask; /*additional set of signals to block */ int sa_flag; /*signal options */ void (*sa_sigaction)(int siginfo_t *, void *);/* alternate handler */ 11 12 2

The sigaction() System Call int sigaction( int signo, const struct sigaction *act, struct sigaction *oact); signo (Input) A signal from the list *act (Input) A pointer to the sigaction structure that describes the action to be taken for the signal. Can be NULL. If act is a NULL pointer, signal handling is unchanged. sigaction() can be used to inquire about the current handling of signal sig. If act is not NULL, the action specified in the sigaction structure becomes the new action associated with sig. *oact (Output) A pointer to a storage location where sigaction() can store a sigaction structure. This structure contains the action currently associated with signo. Can be NULL. If oact is a NULL pointer, sigaction() does not store this information. /** This program (signalset.c )blocks SIGINT signal for 10 seconds using sigprocmask(2) After that the signal is unblocked and signal is handled. */ #include <string.h> void hdl (int sig) printf("got signal Cnt-C\n");; int main (int argc, char *argv[]) sigset_t mask; sigset_t orig_mask; struct sigaction act; memset (&act, 0, sizeof(act)); //clear structure act.sa_handler = hdl; // set signal handler as hd1 // set signlal handler for SIGINT if (sigaction(sigint, &act, 0)) perror ("sigaction"); sigemptyset (&mask); // clear signal set sigaddset (&mask, SIGINT); // add SIGINT // SIGINT is blocked if (sigprocmask(sig_block, &mask, &orig_mask) < 0) perror ("sigprocmask"); // SIGINT is unbloked if (sigprocmask(sig_setmask, &orig_mask, NULL) < 0) perror ("sigprocmask"); 13 return 0; 14 /* sigset1.c: demonstrate block two signal with signal set */ /* regarding set of signal */ sigset_t new_set, old_set; // signal set time_t start, finish; sigemptyset( &new_set );//clear signal sets : block all signal sigaddset( &new_set, SIGTSTP );// add signal for Cnt-z sigaddset(&new_set, SIGINT); // add signal for Cnt-C sigprocmask( SIG_BLOCK, &new_set, &old_set); /*two signals are blocked */ time( &start ); printf( "SIGTSTP and SIGINT are blocked at %s\n", ctime(&start)); /* sigset.c: demonstrate several system calls */ /* regarding set of signal */ void catcher( int sig ) printf( "The SIGALRM signal is handled well\n" ); time_t start, finish; struct sigaction sact; /*for sigaction function */ sigset_t new_set, old_set; /* signal set */ double diff; sigemptyset( &sact.sa_mask ); /* clear signaction structure mask */ sact.sa_flags = 0; /* flag is set 0 to ignore option */ sact.sa_handler = catcher; /* set signal action hander as function catcher */ sigaction( SIGALRM, &sact, NULL ); /*change the action change for signal SIGALRM */ sigemptyset( &new_set );/*clear signal sets : block all signal*/ sigaddset( &new_set, SIGALRM );/*make unblock for a SIGALRM */ sigprocmask( SIG_BLOCK, &new_set, &old_set); /*signal SIGALRM is blocked */ time( &start ); printf( "SIGALRM signals blocked at %s\n", ctime(&start) ); alarm( 1 ); /* SIGALRM will be sent in 1 second */ printf( "SIGALRM is fired but it is blocked.\n"); time( &finish ); do printf( "SIGTSTP and SIGINT are unblocked at %s\n", ctime(&finish)); time( &finish ); sigprocmask( SIG_SETMASK, &old_set, NULL ); // unblock two signals printf( "Current time is %s\n", ctime(&finish) ); diff = difftime( finish, start ); while (diff < 10); while (1) sigprocmask( SIG_SETMASK, &old_set, NULL ); /* signal set are reset with old set */ ; printf( "SIGALRM signals unblocked at %s\n", ctime(&finish) ); 15 16 Three issues in interprocess communication 1. How one process can pass information to another 2. How to make sure two or more processes do not get into the critical region (mutual exclusion) 3. Proper sequencing when dependencies are present (ex. Producer-Consumer: a producer produce outputs and save into a buffer, a consumer consume outputs from the buffer) Critical section (critical region) The part of program where the shared memory is accessed. If we could arrange matters such that no two processes were ever in their critical regions at the same time, we can avoid races condition. 17 18 3

We can change the signal mask for a process to block and unblock selected signal by sequence of system calls. It might be possible to use this technique to protect critical region (critical section). sigset_t newmask, oldmask; sigemptyset(&newmask); sigaddset(&newmask, SIGINT); /*block SIGINT and save current signal mask */ if (sigprocmask(sig_block, &newmask, &oldmask)<0) error_sys(" SIG_BLOCK ERROR "); /**** Critical Region of code ********/ /*reset signal mask, which unblocks SIGINT */ if (sigprocmask(sig_setmask, &oldmask, NULL)<0) error_sys(" SIG_BLOCK ERROR "); /* hole */ 19 20 The sigsuspend() System Call If a signal is sent to the process while it is blocked, the signal delivery will be differed until the signal is unblocked. If a signal does occur between the unblocking and the pause, the signal can be lost. The result is pause forever!!! The sigsuspend() system guarantee both reset and put a process to sleep. int sigsuspend(const sigset_t *sigmask); The sigsuspend() function replaces the current signal mask of a process with the signal set given by *sigmask and then suspends processing of the calling process. The process does not resume running until a signal is delivered 21 22 /*sigsuspend.c */ The sigsuspend() System Call sigset_t newmask, oldmask; sigemptyset(&newmask); sigaddset(&newmask, SIGINT); /*block SIGINT and save current signal mask */ if (sigprocmask(sig_block, &newmask, &oldmask)<0) error_sys( SIG_BLOCK ERROR ); /**** Critical Region of code ********/ /*reset signal mask, which unblocks SIGINT */ if (sigsuspend(&oldmask)<0) error_sys( SIG_SUSPEND ERROR ); void catcher( int sig ) printf( "inside catcher() function\n" ); void timestamp( char *str ) time_t t; time( &t ); printf( "%s the time is %s\n", str, ctime(&t) ); struct sigaction sigact; /* for sigaction call */ sigset_t block_set; sigfillset( &block_set ); /*set all signals are included */ sigdelset( &block_set, SIGALRM ); /* exclude SIGALRM from signal set*/ sigact.sa_handler = catcher; /* set signal hadler*/ sigaction( SIGALRM, &sigact, NULL ); /* set sigaction for SIGALRM signal */ timestamp( "before sigsuspend()" ); alarm( 5 ); sigsuspend( &block_set ); /* replaces the current signal mask with block_set */ timestamp( "after sigsuspend()" ); 23 24 4

The abort() System Call The abort() system call cause abnormal program termination. The abort() system call send SIGABRT signal to caller process #include <stdlib.h> void abort(void); 25 5