Today’s topics Signals and how to control the program behavior in handling signals. Terminal I/O.

Slides:



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

Terminal I/O POSIX termios Two terminal I/O modes Canonical and non-canonical Getting and setting terminal attributes Terminal window size: struct winsize.
I/O Multiplexing Road Map: 1. Motivation 2. Description of I/O multiplexing 3. Scenarios to use I/O multiplexing 4. I/O Models  Blocking I/O  Non-blocking.
15-213, Fall 06 Outline Shell Lab Processes Signals.
System Programming Project 2 Due : 4/19...?. Foreground & Background Foreground job Foreground job Only one at moment Only one at moment Having user’s.
Terminal Control operating systems. Terminal Control may seem like an arcane subject, but … It illustrates the relationship between devices and files.
Page 1 Task Control: Signals and Alarms Havilland and Salama’s Unix Systems Programming B. Ramamurthy.
CPSC 451 Editors and Systems Calls1 Minix editors Mined - (mined) is a simple screen editor. Elle - (elle) is a clone of Emacs. Elvis - (elvis, ex, vi)
Process Process: the UNIX abstraction of a stand-along computer that manages resources (memory, CPU, I/O resources) comprising a running program. Processes.
CS Lecture 17 Outline Named pipes Signals Lecture 17
Signals Hua LiSystems ProgrammingCS2690Signals. Topics: Sending Signals -- kill(), raise() Signal Handling -- signal() sig_talk.c -- complete example.
Dynamic visualizations On ‘non-canonical’ keyboard-input and terminal escape-sequences for visualization effects.
Operating Systems Course Hebrew University Spring 2007 Signals & User Thread.
Slide 6-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Threads and Scheduling 6.
1 Signals COS Goals of Today’s Lecture Overview of signals  Notifications sent to a process  UNIX signal names and numbers  Ways to generate.
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.
CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
Chapter 5. TCP Client-Server Example. Contents –Introduction –TCP Echo Server –TCP Echo Client –Normal Startup and Termination –Posix Signal Handling.
Carnegie Mellon 1 Processes, Signals, I/O, Shell Lab : Introduction to Computer Systems Recitation 9: 10/21/2013 Tommy Klein Section B.
UNIX Signals Bach 7.2 Operating Systems Course The Hebrew University Spring 2010.
Signals & Timers CS241 Discussion Section Spring 2009 Week 6.
Interrupts & signals Operating Systems (234123) Spring 2013 Interrupts & signals Dan Tsafrir (20/5/2013) Partially based on slides by Hagit Attiya OS (234123)
1Reference “Introduction To Unix Signals Programming” in the reference material section Man page – sigprocmask, alarm “Understanding the Linux Kernel”
Lecture 4 Introduction to Unix Processes Introduction to Systems Programming: Processes and Signals.
1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 5: Signals, Time, Timers, and Token Rings.
* POSIX-Defined Signals * Signaling Processes * Signal Mask * sigaction * kill and sigaction * alarm Topics.
Agenda  Working with Processes: Purpose Running Programs within same process (execl, execlp, execle, execv, execvp, execve) “Spawning” other process (fork,
Scis.regis.edu ● CS 468: Advanced UNIX Class 5 Dr. Jesús Borrego Regis University 1.
Signals (Chap 10 in the book “Advanced Programming in the UNIX Environment”) Acknowledgement : Prof. Y. Moon at Kangwon Nat’l Univ.
UNIX Socket Programming CS 6378 Project Reference Book: Unix Network programming: Networking APIs: Sockets and XTI (2nd edition), Prentice Hall >> Threads.
Signals and Signal Processing CIS 370 Lab 7 Umass Dartmouth.
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.
Signals (Chap 10 in the book “Advanced Programming in the UNIX Environment”) Acknowledgement : Prof. Y. Moon at Kangwon Nat’l Univ.
NCHU System & Network Lab Lab #8 Signals Operating System Lab.
1 Signals (continued) CS 241 April 9, 2012 University of Illinois.
SignalsSignals. What is a Signal? A signal is a notification that some event has occurred. Usually a signal is sent to a process asynchronously and whatever.
UNIX Signals * POSIX-Defined Signals * Signaling Processes * Signal Mask * sigaction * kill and sigaction * alarm * Interval Timers * POSIX.1b Timers *
Interprocess Communication Mechanisms. IPC Signals Pipes System V IPC.
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.
CSC Advanced Unix Programming, Fall, 2008 Welcome back to UNIX System Programming! Monday, September 22, class 5.
1 UNIX System Programming Signals. 2 Overview 1. Definition 2. Signal Types 3. Generating a Signal 4. Responding to a Signal 5. Common Uses of Signals.
KUKUM Real Time System Module #3 POSIX programming Lecture 2.
© 숙대 창병모 1 제 10 장 신호 (Signal). © 숙대 창병모 2 Contents 1. Signal Concepts 2. signal() 3. Interrupted System Calls 4. kill() /raise() 5. alarm() pause() 6.
Today’s topic Environment variables Signal. The list of environment variables –try ‘env’ –Environment variables can be defined in shell setenv DISPLAY.
Carnegie Mellon 1 Processes, Signals, I/O, Shell Lab : Introduction to Computer Systems Recitation 9: Monday, Oct. 21, 2013 Marjorie Carlson Section.
Chapter 8 Signals Source: Robbins and Robbins, UNIX Systems Programming, Prentice Hall, 2003.
Signals & Message queue Inter process mechanism in Linux system 3/24/
ACCESS CONTROL. Components of a Process  Address space  Set of data structures within the kernel - process’s address space map - current status - execution.
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)
Signals and daemon processes Prepared by : Department of CSE Engineered for Tomorrow Course code: 10CS62.
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, Abdelzaher1 Signals.
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Signals.
Operating Systems Practical Session 3, Signals 1.
Operating Systems Summer Semester 2011 Practical Session 2, Signals 1.
Operating Systems Inter-Process Communication Signals Moti Geva
UNIX signals.
G.Jyostna.
Signals What is a Signal?
Chapter 5. TCP Client-Server Example
Task Control: Signals and Alarms Chapter 7 and 8
Linux/UNIX Programming
UNIT-IV Process and Signals
Chapter 5 (part 1) TCP Client /Server Example By: Lim Meng Hui.
CSC Advanced Unix Programming, Fall 2015
Advanced UNIX programming
Signals Tanzir Ahmed CSCE 313 Fall 2018.
Signals.
Presentation transcript:

Today’s topics Signals and how to control the program behavior in handling signals. Terminal I/O

Signal –A form of inter-process communication –tells a process that some event occurs the kill command –“kill –l” –“kill –s INT pid” Type “ctrl-C” when a program is running Memory violation, etc Divided by 0. A process dies (SIGHUP and SIGCHLD). a packet arrives ……

–Some commonly used signals: SIGABRT, SIGALRM, SIGCHLD, SIGHUP, SIGINT, SIGUSR1, SIGUSR2, SIGTERM, SIGKILL, SIGSTOP, SIGSEGV, SIGILL All defined in signal.h –more /usr/include/bits/signum.h on linprog to see all signals. Try ‘man –s 7 signal’ on linprog

Signal –When a process receives a signal, it has the following options: Block the signal –Do not disturb (no signal until allowed) – kernel delivers the signal only after the signal is unblocked by the process. Ignore the signal –I see the signal, I won’t do anything. Perform the default operation –Ignore the signal –exit Catch the signal (perform user defined operations).

Catch a signal: –similar to interrupt (software interrupt) –when a process receives a signal: stop execution call the signal handler routine resume execution –Signal can be received at any point in the program. Catching a signal is like inserting a signal handler routine call dynamically (at any point of the program).

Controlling the reaction to signals in a program: Block the signal: sigprocmask Ignore the signal: signal, sigaction Perform the default operation (do nothing): –Most likely ignore the signal, or quit Catch the signal (perform user-defined operations): signal, sigaction

ANSI C signal function: –syntax: #include void (*signal(int sig, void (*disp)(int)))(int); –semantic: sig -- signal (defined in signal.h) disp: SIG_IGN, SIG_DFL or the address of a signal handler routine. Handler may be erased after one invocation. –How to get continuous coverage? –Still have problems – may lose signals –See example1.c (lingprog and program): semantic not well defined Signal handler has a prototype of void handler(int signo)

Block/unblock signal: sigprocmask –Manipulate signal sets #include int sigemptyset(sigset_t *set); int sigfillset(sigset_t *set); int sigaddset(sigset_t *set, int signo); int sigdelset(sigset_t *set, int signo); int sigismember(const sigset_t *set, int signo); –Manipulate signal mask of a process Int sigprocmask(int how, const sigset_t *set, sigset_t *oset); –How: SIG_BLOCK, SIG_UNBLOCK, SIG_SETMASK See example2.c, example2a.c, example3.c

For a critical region where you don’t want certain signal to come, the program will look like: sigprocmask(SIG_BLOCK, &newmask, &oldmask); ……. /* critical region */ sigprocmask(SIG_SETMASK, &oldmask, NULL);

Sigaction –Supersedes the signal function + signal blocking –#include –Int sigaction(int signo, const struct sigaction * act, struct sigaction *oact) Struct sigaction { void (*sa_handler)(); /* signal handler */ sigset_t sa_mask; /*additional signal to be block */ int sa_flags; /* various options for handling signal */ }; –See example4.c (noted the program behavior with multiple signals).

Kill: –Send a signal to a process –#include –Int kill(pid_t pid, int signo); Pid > 0, normal Pid == 0, all processes whose group ID is the current process’s group ID. Pid <0, all processes whose group ID = |pid| –See example5.c

Alarm in a program #include Unsigned int alarm(unsigned int seconds) –Arrange to have a signal (SIGALRM) to be delivered to the process in seconds seconds. Stop running this program in 2 days. –See example6.c for the use of alarm.

Impact of signals on system calls –A system call may return prematurely –See example7.c –How to deal with this problem? Check the return value of the system call and act accordingly, and check errno to decide the error type (see man –a read)

Compiling C program with POSIX routines using ‘-Wall –pedantic –ansi’ –Try ‘gcc –Wall –pedantic –ansi example7.c’ –Do ‘man sigaction’ –‘more /usr/include/signal.h’ –‘more /usr/include/features.h’ –Specify the code is a POSIX code with flag ‘-D_POSIX_SOURCE’ ‘-D_POSIX_C_SOURCE=200112L’ ‘gcc –Wall –pedantic –ansi example7.c -D_POSIX_C_SOURCE=200112L’

Terminal I/O: –The semantics of an output operation is relatively simple. Input is rather messy. –Two input modes: Canonical mode: the default mode, input line by line Noncanonical mode: input characters are not assembled. –We will focus on noncanonical mode When do we use it? What should we expect when using this mode for input? Example: Which input mode does a regular shell use?

In POSIX.1, all the characteristics of a terminal device that we can examine and change are in a termios structure (termios.h) Struct termios { tcflag_t c_iflag; /* input flag */ tcflag_t c_oflag; /* output flag */ tcflag_t c_cflag; /* control flags */ tcflag_t c_lflag; /* local flags */ cc_t c_cc[NCCS]; /* control characters */ }

Functions to get and set the fields in the termios structure –tcgetattr and tcsetattr; –#include –int tcgetattr(int fildes, struct termios *termios_p) –int tcsetattr(int fildes, int optional_actions, const struct termios *termios_p) optional_actions: TCSANOW, TCSADRAIN, TCSAFLUSH

Turn on the noncanonical mode: –Unset the ICANON flag in c_lflag Myterm.c_lflag & = ~ICANON –When will a read return using the noncanonical mode for input? Number of characters(VMIN) Time (VTIME) Specified in the c_cc field –c_cc[VMIN] = ???, c_cc[VTIME] = ??? –VMIN > 0, VTIME > 0 –VMIN = 0, VTIME > 0 –VMIN > 0, VTIME = 0 –VMIN = 0, VTIME = 0

See example8.c and example9.c

Review Name a few commonly used signals. What is the typical behavior when a signal is received? Name different ways a program can deal with a signal Which routine can be used to block a signal? Which routine can be used to ignore a signal Which routine can be used to establish a user-defined signal handler? What to do to set up a code region that no-signals are allowed? Give two routines where signals are sent to a process What is the potential impact of signal on system calls? Is tcsh using canonical mode of input? How to turn on non-canonical mode of input? In non-canonical mode of input, how to decide when to return from a input routine?