CS Lecture 17 Outline Named pipes Signals Lecture 17

Slides:



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

R4 Dynamically loading processes. Overview R4 is closely related to R3, much of what you have written for R3 applies to R4 In R3, we executed procedures.
1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
15-213, Fall 06 Outline Shell Lab Processes Signals.
CS591 (Spring 2001) The Linux Kernel: Signals & Interrupts.
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
CS 311 – Lecture 14 Outline Process management system calls Introduction System calls  fork()  getpid()  getppid()  wait()  exit() Orphan process.
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)
Signals Hua LiSystems ProgrammingCS2690Signals. Topics: Sending Signals -- kill(), raise() Signal Handling -- signal() sig_talk.c -- complete example.
Operating Systems Course Hebrew University Spring 2007 Signals & User Thread.
UNIX IPC CSE 121 Spring 2003 Keith Marzullo. CSE 121 Spring 2003Review of Concurrency2 Creating a UNIX process A process is created by making an exact.
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.
CSSE Operating Systems
CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
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.
UNIX Signals Bach 7.2 Operating Systems Course The Hebrew University Spring 2010.
Signals & Timers CS241 Discussion Section Spring 2009 Week 6.
Introduction to Processes CS Intoduction to Operating Systems.
1Reference “Introduction To Unix Signals Programming” in the reference material section Man page – sigprocmask, alarm “Understanding the Linux Kernel”
Recitation 9: Section L (1:30pm - 2:20pm) Monday, October 22, 2012 Processes, Signals and Shell Lab Siddharth Dhulipalla.
Chapter 41 Processes Chapter 4. 2 Processes  Multiprogramming operating systems are built around the concept of process (also called task).  A process.
CY2003 Computer Systems Lecture 06 Interprocess Communication Monitors.
Operating Systems Chapter 2
* 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,
System calls for Process management
IPC Programming. Process Model Processes can be organized into a parent-child hierarchy. Consider the following example code: /* */
Washington WASHINGTON UNIVERSITY IN ST LOUIS Core Inter-Process Communication Mechanisms (Historically Important) Fred Kuhns
UNIX Socket Programming CS 6378 Project Reference Book: Unix Network programming: Networking APIs: Sockets and XTI (2nd edition), Prentice Hall >> Threads.
Concurrent Processes Processes can concurrently run same program. Processes can concurrently run same program. Processes can start other processes. Processes.
Concurrency & Context Switching Process Control Block What's in it and why? How is it used? Who sees it? 5 State Process Model State Labels. Causes of.
Operating Systems Process Creation
Signals and Signal Processing CIS 370 Lab 7 Umass Dartmouth.
Outline for Today Objectives –Finish discussion of Birrell –UNIX Signals –Eraser Administrative –Spider talk after class.
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 *
Operating Systems Recitation 4, April th, 2002 Signals.
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.
Chapter 8 Signals Source: Robbins and Robbins, UNIX Systems Programming, Prentice Hall, 2003.
Today’s topics Signals and how to control the program behavior in handling signals. Terminal I/O.
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)
CSCI 330 UNIX and Network Programming Unit XVII: Socket Programming Detail.
Signals and daemon processes Prepared by : Department of CSE Engineered for Tomorrow Course code: 10CS62.
CS 241 Section Week #10 (04/01/10) ‏. Topics This Section  MP5 Overview  Signals.
Unix System Programming
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
G.Jyostna.
Signals What is a Signal?
Chapter 5. TCP Client-Server Example
Unix Process Management
Threads and Cooperation
UNIT-IV Process and Signals
Chapter 5 (part 1) TCP Client /Server Example By: Lim Meng Hui.
CSC Advanced Unix Programming, Fall 2015
Signals.
Presentation transcript:

CS 311 - Lecture 17 Outline Named pipes Signals Lecture 17 CS 311 - Operating Systems 1

Named pipes Named pipes are physically available files in the file system. Any two totally unrelated processes in the same machine can make use of the named pipe for communication. They act as a buffer between two process Created using mkfifo utility. mkfifo {filename}* System call prototype int mkfifo(const char *pathname, mode_t mode); Lecture 17 CS 311 - Operating Systems 1

Named pipes When two processes use the named pipes One acts as a writer which writes into the pipe Other acts as a reader which reads from the pipe How is named pipe different from temporary files? Temporary files occupy space in the filesystem while named pipes does not. Accessing temporary files are slower Advantages of named pipes Can be used by totally unrelated processes The pipe physically exists in the file system Lecture 17 CS 311 - Operating Systems 1

Signals To override default actions taken by the process, signal() system call can also be used void (*signal (int sigCode, void (*func)(int))) (int) Sigcode is the signal to be handled Second arg is a pointer to a function which handles the signal or SIG_IGN/SIG_DFL Lecture 17 CS 311 - Operating Systems 1

sigaction() First parameter is the signal type of interest int sigaction(int signo, struct sigaction *act, struct sigaction *act) First parameter is the signal type of interest The second parameter is a pointer to a special sigaction structure Describes the action to be taken upon receipt The third parameter is a pointer to a another sigaction structure The sigaction() function will use this pointer to write out what the setting were before the change was requested Lecture 17 CS 311 - Operating Systems 1

The sigaction structure struct sigaction { void (*sa_handler)(int); sigset_t sa_mask; int sa_flags; void (*sa_sigaction)(int, siginfo_t *, void *); }; Lecture 17 CS 311 - Operating Systems 1

The sigaction structure struct sigaction { void (*sa_handler)(int); sigset_t sa_mask; int sa_flags; void (*sa_sigaction)(int, siginfo_t *, void *); }; The first attribute is set to one of three values: SIG_DFL - take the default action SIG_IGN - ignore the signal A pointer to a function that should be called when this signal is received Lecture 17 CS 311 - Operating Systems 1

The sigaction structure struct sigaction { void (*sa_handler)(int); sigset_t sa_mask; int sa_flags; void (*sa_sigaction)(int, siginfo_t *, void *); }; The sa_mask attribute includes what signals should be blocked while the signal handler is executing Blocked means that the signals are put on hold until your signal handler is done executing You need to pass this a signal set Make sure that you set sa_flags to 0 if you aren't planning to set any flags Lecture 17 CS 311 - Operating Systems 1

Signal Sets A signal set is simply a list of signal types A signal set is defined using the special type sigset_t defined in <signal.h> Lecture 17 CS 311 - Operating Systems 1

Signal Sets To define a signal set sigset_t my_signal_set; To initialize or reset the signal set to have no signal types: sigemptyset(&my_signal_set); To initialize or reset the signal set to have all types of signals sigfillset(&my_signal_set) To add a single signal type to the set sigaddset(&my_signal_set, SIGINT) To remove a single single type from the set sigdelset(&my_signal_set, SIGQUIT) Lecture 17 CS 311 - Operating Systems 1

The sigaction structure struct sigaction { void (*sa_handler)(int); sigset_t sa_mask; int sa_flags; void (*sa_sigaction)(int, siginfo_t *, void *); }; The third attribute of the sigaction provides additional instructions (flags) SA_RESTHAND - resets the signal handler to SIG_DFL (default action) after the first signal has been received and handled SA_SIGINFO - tells the kernel to call the function specified in the fourth attribute (sa_sigaction), instead of the first attribute (sa_handler). More detailed information is passed to this function Lecture 17 CS 311 - Operating Systems 1

The sigaction structure struct sigaction { void (*sa_handler)(int); sigset_t sa_mask; int sa_flags; void (*sa_sigaction)(int, siginfo_t *, void *); }; sa_sigaction specifies an alternative signal handler function to be called. This attribute will only be used if the SA_SIGINFO flag is set in sa_flags. The siginfo_t structure contains information such as which process sent you the signal. Most of the time you will use sa_handler and not sa_sigaction Lecture 17 CS 311 - Operating Systems 1

Catching SIGINT #include <signal.h> void catchint(int signo) { printf("Caught an interrupt: %d\n", signo); } main() { struct sigaction act; act.sa_handler = catchint; act.sa_flags = 0; sigfillset(&(act.sa_mask)); sigaction(SIGINT, &act, NULL); while (1) { printf("sleeping…\n"); sleep(1); exit(0); Lecture 17 CS 311 - Operating Systems 1

Temporarily changing a signal handler Sometimes you only want to change a signal handler temporarily Afterwards you need to change it back to what it used to be So how do you save the original signal handler and restore it when you are done? Use the third parameter to sigaction() Lecture 17 CS 311 - Operating Systems 1

#include <signal.h> void catchint(int signo) { printf("Caught an interrupt: %d\n", signo); } int main() { struct sigaction original_act, new_act; int counter = 0; new_act.sa_handler = catchint; new_act.sa_flags = 0; sigfillset(&(new_act.sa_mask)); sigaction(SIGINT, &new_act, &original_act); while (1) { counter++; if (counter == 10) { printf("resetting the signal action\n"); sigaction(SIGINT, &original_act, NULL); printf("sleeping.\n"); sleep(1); exit(0); Lecture 17 CS 311 - Operating Systems 1

Blocking signals It is also possible to block signals from occurring during your program execution Blocking a signal simply means that it is delayed until you unblock the signal This could be useful if you have code where it is extremely critical that you don't get interrupted Blocking is done using: sigprocmask() Not used very frequently Lecture 17 CS 311 - Operating Systems 1

Waiting for a signal pause() system call temporarily suspends a process and returns when a signal is received. Prototype: int pause (void) If a signal is ignored, then pause() will ignore it If a signal causes a termination, pause() does nothing If a signal is caught, the appropriate signal handler function will be called. After the signal handler function is done, pause returns -1 and sets errno to EINTR. Lecture 17 CS 311 - Operating Systems 1

Signals and system calls Signals can arrive any time including in the middle of a system call! System calls are savvy about signals and prevent data loss and corruption from occuring They also prevent partial actions from happening How should signals in "long running" system calls be handled For example: write(), read(), and pause Normally system call returns an error: EINTR Can arrange for system calls to automatically restart by setting SA_RESTART in the sa_flags variable Lecture 17 CS 311 - Operating Systems 1

Sending signals to others kill() sends any signal In spite of the name, the signal may not actually cause the termination of the signaled process Prototype: int kill (pid_t pid, int sigCode) The first parameter affects who the signal is sent to: If pid > 0, then the signal will be sent to the process with a process id of pid If pid == 0, then the signal is sent to all processes in the same process group as the sender if pid == -1, the signal is sent to all processes with real user-ids equal to the effective user-id of the sender if pid < 0, then then signal will be sent to all processes with a process group id equal to pid Lecture 17 CS 311 - Operating Systems 1

Sending signals to yourself You can send yourself a specified signal Prototype: int raise(int sig) The alarm() function will also result in your process being sent a signal, although at a later time Prototype: unsigned int alarm(unsigned int seconds); Note that alarm() will return immediately, unlike sleep() You can only have one alarm active at any time Lecture 17 CS 311 - Operating Systems 1