UNIX System Programming Overview 1. Definition 2. Signal Types

Total Page:16

File Type:pdf, Size:1020Kb

UNIX System Programming Overview 1. Definition 2. Signal Types UNIX System Programming Overview Signals 1. Definition 2. Signal Types 3. Generating a Signal 4. Responding to a Signal Objectives 5. Common Uses of Signals – Introduce signals 6. Implementing a read() – Concentrate on sigaction() function Timeout continued 1730 UNIX System Programming: Signals Maria Hybinette 1 1730 UNIX System Programming: Signals Maria Hybinette 2 1. Definition 7. POSIX Signal Functions A signal is an asynchronous event which is 8. Interrupted System Calls delivered to a process. 9. System Calls inside Handlers 10. More Information Asynchronous means that the event can occur at any time – may be unrelated to the execution of the process – e.g. user types ctrl-C, or the modem hangs 1730 UNIX System Programming: Signals Maria Hybinette 3 1730 UNIX System Programming: Signals Maria Hybinette 4 2. Signal Types (31 in POSIX) Signal Sources terminal memory Name Description Default Action driver management shell command SIGINT Interrupt character typed terminate process SIGQUIT Quit character typed (^\) create core image SIGINT SIGHUP SIGKILL kill -9 terminate process SIGQUIT SIGSEGV Invalid memory reference create core image SIGKILL kernel SIGPIPE SIGPIPE Write on pipe but no reader terminate process SIGALRM alarm() clock ‘rings’ terminate process SIGUSR1 user-defined signal type terminate process SIGWINCH SIGALRM SIGUSR2 user-defined signal type terminate process window : manager a process SIGUSR1 See man 7 signal other user processes 1730 UNIX System Programming: Signals Maria Hybinette 5 1730 UNIX System Programming: Signals Maria Hybinette 6 UNIX: Signals Maria Hybinette 1 3. Generating a Signal kill() Use the UNIX command: $ kill -KILL 4481 Send a signal to a process (or group of processes). – send a SIGKILL signal to pid 4481 – check #include <signal.h> ps –l int kill( pid_t pid, int signo ); – to make sure process died kill is not a good name; send_signal Return 0 if ok, -1 on error. might be better. 1730 UNIX System Programming: Signals Maria Hybinette 7 1730 UNIX System Programming: Signals Maria Hybinette 8 4. Responding to a Signal Some pid Values A process can: – ignore/discard the signal (not possible with pid Meaning SIGKILL or SIGSTOP) > 0 send signal to process pid – execute a signal handler function, and then possibly resume execution or terminate == 0 send signal to all processes whose process group ID – carry out the default action for that signal equals the sender’s pgid. e.g. parent kills all children The choice is called the process’ signal disposition 1730 UNIX System Programming: Signals Maria Hybinette 9 1730 UNIX System Programming: Signals Maria Hybinette 10 signal(): library call Actual Prototype The actual prototype, listed in the “man” page is a bit Specify a signal handler function to deal with a signal type. perplexing but is an expansion of the Sigfunc type: #include <signal.h> void (*signal(int signo, void(*handler)(int)))(int); typedef void Sigfunc(int); /* my defn */ In Linux: Sigfunc *signal( int signo, Sigfunc *handler ); typedef void (*sighandler_t)(int); – signal returns a pointer to a function that returns an int (i.e. it sig_handler_t signal(int signo, sighandler_t handler); returns a pointer to Sigfunc Signal returns a pointer to a function that returns an int Returns previous signal disposition if ok, SIG_ERR on error. 1730 UNIX System Programming: Signals Maria Hybinette 11 1730 UNIX System Programming: Signals Maria Hybinette 12 UNIX: Signals Maria Hybinette 2 The signal function itself returnsSignal a pointer toHandling a function. Example The return type is the same as the function that is passedThe signal in, toThe be handler function int main() { i.e., aUse function the signal that handling takes caughtlibrary: an or ignored signal.hReceives a single integer signal( SIGINT, foo ); int and returns a void is given as argumentArgument and returns void : Then can use the signal call: sig /* do usual things until SIGINT */ #include <signal.h> return 0; } void (*signal( int sig, void (*handler)(int))) (int) ; void foo( int signo ) signal returns a pointer to the PREVIOUS signal { handler : /* deal with SIGINT signal */ The function to be called Signal #includeis a function <signal.h> when the specified signal that takestypedef two void Sigfunc(int); is /* received my defnThe */is returnedgiven as functiona return; /* return to program */ Sigfunc *signal( int signo, Sigfunc *handler ); } arguments: pointer totakes the function a integer sig and handler handler parameter. 1730 UNIX System Programming: Signals Maria Hybinette 13 1730 UNIX System Programming: Signals Maria Hybinette 14 sig_examp.c #include <stdio.h> #include <unistd.h> : #include <signal.h> while(1) { void sig_usr( int signo ); /* handles two signals */ printf( “%2d\n“, I ); pause(); int main() { /* pause until signal handler int i = 0; * has processed signal */ if( signal( SIGUSR1,sig_usr ) == SIG_ERR ) i++; printf( “Cannot catch SIGUSR1\n” ); } if( signal( SIGUSR2,sig_usr ) == SIG_ERR ) return 0; printf(“Cannot catch SIGUSR2\n”); } : continued continued 1730 UNIX System Programming: Signals Maria Hybinette 15 1730 UNIX System Programming: Signals Maria Hybinette 16 Usage void sig_usr( int signo ) /* argument is signal number */ { $ sig_examp & if( signo == SIGUSR1 ) [1] 4720 printf(“Received SIGUSR1\n”); 0 else if( signo == SIGUSR2 ) $ kill -USR1 4720 printf(“Received SIGUSR2\n”); Received SIGUSR1 else 1 printf(“Error: received signal $ kill -USR2 4720 %d\n”, signo); Received SIGUSR2 2 return; $ kill 4720 /* send SIGTERM */ } [1] + Terminated sig_examp & $ 1730 UNIX System Programming: Signals Maria Hybinette 17 1730 UNIX System Programming: Signals Maria Hybinette 18 UNIX: Signals Maria Hybinette 3 Special Sigfunc * Values Multiple Signals If many signals of the same type are waiting Value Meaning to be handled (e.g. two SIGINTs), then most UNIXs will only deliver one of them. SIG_IGN Ignore / discard the signal. SIG_IGN – the others are thrown away SIG_DFL Use default action to handle signal. SIG_ERR Returned by signal() as an error. If many signals of different types are waiting to be handled (e.g. a SIGINT, SIGSEGV, SIGUSR1), they are not delivered in any fixed order. 1730 UNIX System Programming: Signals Maria Hybinette 19 1730 UNIX System Programming: Signals Maria Hybinette 20 pause() The Reset Problem Suspend the calling process until a signal is caught. In Linux (and many other UNIXs), the #include <unistd.h> signal disposition in a process is reset to its int pause(void); signal disposition in a process is reset to its default action immediately after the signal Returns -1 with errno assigned EINTR. has been delivered. (Linux assigns it ERESTARTNOHAND). pause() only returns after a signal handler has Must call signal() again to reinstall the returned. signal handler function. 1730 UNIX System Programming: Signals Maria Hybinette 21 1730 UNIX System Programming: Signals Maria Hybinette 22 To keep catching Reset Problem the signal with this Reset Problem Example function, must call : the signal system int main() void ouch( int sig ) call again. { { signal(SIGINT, foo); printf( "OUCH! - I got signal %d\n", sig ); : (void) signal(SIGINT, ouch); /* do usual things until SIGINT */ } } Problem: from the time int main() that the interrupt function { starts to just before the void foo(int signo) signal handler is re-established (void) signal( SIGINT, ouch ); { the signal will not be while(1) handled. signal(SIGINT, foo); /* reinstall */ { : printf("Hello World!\n"); return; sleep(1); If another SIGINT signal is } } received during this time, default behavior will be done, } 1730 UNIX System Programming: Signals Maria Hybinette 23 1730 UNIX System Programming: Signals Maria Hybinette i.e., program will terminate.24 UNIX: Signals Maria Hybinette 4 Re-installation may be too 5. Common Uses of slow! Signals There is a (very) small time period in foo() 5.1. Ignore a Signal when a new SIGINT signal will cause the default action to be carried out -- process 5.2. Clean up and Terminate termination. 5.3. Dynamic Reconfiguration 5.4. Report Status With signal() there is no answer to this problem. 5.5. Turn Debugging on/off – POSIX signal functions solve it (and some 5.6. Restore Previous Handler other later UNIXs) 1730 UNIX System Programming: Signals Maria Hybinette 25 1730 UNIX System Programming: Signals Maria Hybinette 26 5.2. Clean up and 5.1. Ignore a Signal Terminate : int main() : { /* global variables */ signal(SIGINT, SIG_IGN); signal(SIGINT, SIG_IGN); int my_children_pids; signal(SIGQUIT, SIG_IGN); : : /* do work without interruptions */ void clean_up(int signo); } int main() Cannot ignore/handle SIGKILL or SIGSTOP { Should check for SIG_ERR signal(SIGINT, clean_up); : } continued 1730 UNIX System Programming: Signals Maria Hybinette 27 1730 UNIX System} Programming: Signals Maria Hybinette 28 Problems void clean_up(int signo) { If a program is run in the background then unlink(“/tmp/work-file”); the interrupt and quit signals (SIGINT, kill(my_children_pids, SIGTERM); SIGQUIT) are automatically ignored. wait((int *)0); fprintf(stderr, “Program terminated\n”); Your code should not override these exit(1); changes: } – check if the signal dispositions are SIG_IGN 1730 UNIX System Programming: Signals Maria Hybinette 29 1730 UNIX System Programming: Signals Maria Hybinette 30 UNIX: Signals Maria Hybinette 5 Checking the Disposition 5.3. Dynamic new disposition old disposition Reconfiguration : : if( signal(SIGINT, SIG_IGN ) != SIG_IGN ) void read_config(int signo);
Recommended publications
  • Distributed Programming I (Socket - Nov'09)
    Distributed programming I (socket - nov'09) Warning for programmers network programming is dangerously close to O.S. Network programming: sockets kernel, and therefore: It can easily hang the O.S. verify the results of every operation, without assuming anything as granted Antonio Lioy < [email protected] > APIs can vary in details that are minimal but important consider every possible situation to create english version created and modified by “portable” programs Marco D. Aime < [email protected] > we will try to use Posix 1.g Politecnico di Torino Dip. Automatica e Informatica ISO/OSI, TCP/IP, network programming Exercise – copying data copy the content of file F1 (first parameter on the application command line) into file F2 (second parameter on the 7. application details application command line) user 6. presentation (l6: XDR/XML/... process l5: RPC/SOAP/...) 5. session network programming 4. transport TCP UDP SCTP interface 3. network IPv4, IPv6 kernel communication 2. data link device driver details 1. physical and hardware OSI model IP suite ref. UNP Intro copyfile.c Error messages Error functions must contain at least: best to define standard error reporting functions [ PROG ] program name which accept: [ LEVEL ] error level (info, warning, error, bug) a format string for the error [ TEXT ] error signalling, the most specific as possible a list of parameters to be printed (e.g. input file name and line where the problem has UNP, appendix D.4 (D.3 in 3rd edition) occurred) errno? termination? log level [ ERRNO ] system error number and/or name (if applicable) err_msg no no LOG_INFO err_quit no exit(1) LOG_ERR suggested format: err_ret yes no LOG_INFO err_sys yes exit(1) LOG_ERR ( PROG ) LEVEL - TEXT : ERRNO err_dump yes abort( ) LOG_ERR errlib.h errlib.c © A.Lioy - Politecnico di Torino (2009) B-1 Distributed programming I (socket - nov'09) stdarg.h stdarg.h usage example variable list of arguments (ANSI C) create a function named my_printf declared with an ellipsis (.
    [Show full text]
  • Beej's Guide to Unix IPC
    Beej's Guide to Unix IPC Brian “Beej Jorgensen” Hall [email protected] Version 1.1.3 December 1, 2015 Copyright © 2015 Brian “Beej Jorgensen” Hall This guide is written in XML using the vim editor on a Slackware Linux box loaded with GNU tools. The cover “art” and diagrams are produced with Inkscape. The XML is converted into HTML and XSL-FO by custom Python scripts. The XSL-FO output is then munged by Apache FOP to produce PDF documents, using Liberation fonts. The toolchain is composed of 100% Free and Open Source Software. Unless otherwise mutually agreed by the parties in writing, the author offers the work as-is and makes no representations or warranties of any kind concerning the work, express, implied, statutory or otherwise, including, without limitation, warranties of title, merchantibility, fitness for a particular purpose, noninfringement, or the absence of latent or other defects, accuracy, or the presence of absence of errors, whether or not discoverable. Except to the extent required by applicable law, in no event will the author be liable to you on any legal theory for any special, incidental, consequential, punitive or exemplary damages arising out of the use of the work, even if the author has been advised of the possibility of such damages. This document is freely distributable under the terms of the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 License. See the Copyright and Distribution section for details. Copyright © 2015 Brian “Beej Jorgensen” Hall Contents 1. Intro................................................................................................................................................................1 1.1. Audience 1 1.2. Platform and Compiler 1 1.3.
    [Show full text]
  • POSIX Signals
    CSE 410: Systems Programming POSIX Signals Ethan Blanton Department of Computer Science and Engineering University at Buffalo Introduction Signals Blocking Concurrency Sending Signals Summary References POSIX Signals POSIX signals are another form of interprocess communication. They are also a way to create concurrency in programs. For these two reasons, they are rather complicated and subtle! Signals provide a simple message passing mechanism. © 2018 Ethan Blanton / CSE 410: Systems Programming Introduction Signals Blocking Concurrency Sending Signals Summary References Signals as Messages POSIX signals are asynchronous messages. Asynchronous means that their reception can occur at any time.1 The message is the reception of the signal itself. Each signal has a number, which is a small integer. POSIX signals carry no other data. 1Almost. We’ll see how to control it later. © 2018 Ethan Blanton / CSE 410: Systems Programming Introduction Signals Blocking Concurrency Sending Signals Summary References Signal Types There are two basic types of POSIX signals: Reliable signals Real-time signals Real-time signals are much more complicated. In particular, they can carry data. We will discuss only reliable signals in this lecture. © 2018 Ethan Blanton / CSE 410: Systems Programming Introduction Signals Blocking Concurrency Sending Signals Summary References Asynchronous Reception From the point of view of the application: Signals can be blocked or ignored Enabled signals may be received between any two processor instructions A received signal can run a user-defined function called a signal handler This means that enabled signals and program code must very carefully manipulate shared or global data! © 2018 Ethan Blanton / CSE 410: Systems Programming Introduction Signals Blocking Concurrency Sending Signals Summary References Signals POSIX defines a number of signals by name and number.
    [Show full text]
  • Programming with POSIX Threads II
    Programming with POSIX Threads II CS 167 IV–1 Copyright © 2008 Thomas W. Doeppner. All rights reserved. Global Variables int IOfunc( ) { extern int errno; ... if (write(fd, buffer, size) == –1) { if (errno == EIO) fprintf(stderr, "IO problems ...\n"); ... return(0); } ... } CS 167 IV–2 Copyright © 2008 Thomas W. Doeppner. All rights reserved. Unix was not designed with multithreaded programming in mind. A good example of the implications of this is the manner in which error codes for failed system calls are made available to a program: if a system call fails, it returns –1 and the error code is stored in the global variable errno. Though this is not all that bad for single-threaded programs, it is plain wrong for multithreaded programs. Coping • Fix Unix’s C/system-call interface • Make errno refer to a different location in each thread – e.g. #define errno __errno(thread_ID) CS 167 IV–3 Copyright © 2008 Thomas W. Doeppner. All rights reserved. The ideal way to solve the “errno problem” would be to redesign the C/system-call interface: system calls should return only an error code. Anything else to be returned should be returned via result parameters. (This is how things are done in Windows NT.) Unfortunately, this is not possible (it would break pretty much every Unix program in existence). So we are stuck with errno. What can we do to make errno coexist with multithreaded programming? What would help would be to arrange, somehow, that each thread has its own private copy of errno. I.e., whenever a thread refers to errno, it refers to a different location from any other thread when it refers to errno.
    [Show full text]
  • UNIT: 4 DISTRIBUTED COMPUTING Introduction to Distributed Programming
    UNIT: 4 DISTRIBUTED COMPUTING Introduction To Distributed Programming: • Distributed computing is a model in which components of a software system are shared among multiple computers. Even though the components are spread out across multiple computers, they are run as one system. This is done in order to improve efficiency and performance. • Distributed computing allows different users or computers to share information. Distributed computing can allow an application on one machine to leverage processing power, memory, or storage on another machine. Some applications, such as word processing, might not benefit from distribution at all. • In parallel computing, all processors may have access to a shared memory to exchange information between processors. In distributed computing, each processor has its own private memory (distributed memory). Information is exchanged by passing messages between the processors. • Distributed computing systems are omnipresent in today’s world. The rapid progress in the semiconductor and networking infrastructures have blurred the differentiation between parallel and distributed computing systems and made distributed computing a workable alternative to high- performance parallel architectures. • However attractive distributed computing may be, developing software for such systems is hardly a trivial task. Many different models and technologies have been proposed by academia and industry for developing robust distributed software systems. • Despite a large number of such systems, one fact is clear that the software
    [Show full text]
  • Introduction to POSIX Signals
    Introduction to POSIX Signals Michael Jantz Prasad Kulkarni Douglas Niehaus Introduction ● This lab is an introduction to signals in Unix systems. – In it, you will learn about some common uses for signals. – You will also construct a small program that uses signals. ● Unpack the starter code, then make and tag it: – bash> tar zxvf eecs678-signals-lab.tar.gz – cd signals/; make; ctags -R EECS 678 Signals Lab 2 Signals ● A signal is a short message that may be sent to a process or a group of processes. ● The only information given to a process is usually the number identifying the signal; there is no room in standard signals for arguments, a message or other accompanying information. ● Signals serve two main purposes: – To make a process aware that a specific event has occurred. – To cause a process to execute a signal handler function included in its code. EECS 678 Signals Lab 3 Interrupts vs. Signals ● Signals and interrupts are very similar in their behavior ● Important difference: interrupts are sent to the operating system by the hardware, signals are sent to the process by the operating system, or other processes through the OS ● Important similarity: both signals and interrupts associate handlers with asynchronous events which interrupt current processing, thus inserting the handler into current code path ● Signals can thus be thought of as an interrupt in software: – However, note that signals have nothing to do with Soft-IRQs. The name seems related, but these are a method for deferring much of the processing associated with a hardware-interrupt into a less restrictive execution context inside the OS.
    [Show full text]
  • POSIX Signal Handling in Java
    Technical Document Series POSIX Signal Handling in Java POSIX Signal Handling In Java Introduction POSIX signals inform a running process of external events, such as the user wishing to kill the process, or the operating system signaling an impending shutdown, or the process being suspended or reinstated; or the process may have violated a resource constraint, such as excessive CPU usage or attempts to access areas outside its permitted memory space, and is asked to shutdown. In short, POSIX signals serve many different purposes. Some are even up to interpretation, such as the HUP (HangUP) signal, which is commonly used to inform a process that something about its environment has changed and the process should adjust accordingly. Some programs may interpret this to mean that the configuration has changed and needs to be reloaded; or the log file has been moved for archiving purposes and a new one should be started. The use of signals is widespread, especially on Unix-based operating systems, but Java provides no standard interface for a Java application to hear and react to them. This document shows you how to get around this limitation. The Good, the Bad, and the Ugly The good news is that there is a way to intercept POSIX signals and react to them in Java. This would allow your Java program to avoid being killable with ^C (SIGINT), for example, even ignore termination requests from the operating system (SIGTERM). Neither of these is necessarily a good idea, of course, unless you know exactly why you would want to catch these signals and either handle them yourself or ignore them altogether.
    [Show full text]
  • Lecture 14: Paging
    Lecture 14: Paging Fall 2018 Jason Tang Slides based upon Operating System Concept slides, http://codex.cs.yale.edu/avi/os-book/OS9/slide-dir/index.html Copyright Silberschatz, Galvin, and Gagne, 2013 "1 Topics • Memory Mappings! • Page Table! • Translation Lookaside Bu$ers! • Page Protection "2 Memory Mapped • Logical address translated not to memory but some other location! • Memory-mapped I/O (MMIO): hardware redirects read/write of certain addresses to physical device! • For example, on x86, address 0x3F8 usually mapped to first serial port! • Memory-mapped file (mmap): OS redirects read/write of mapped memory region to file on disk! • Every call to read() / write() involves a system call! • Writing to a pointer faster, and OS can translate in the background (also see upcoming lecture) "3 Memory-Mapped File • In Linux, typical pattern is to:! • Open file, using open() function! • Optional: preallocate file size, using ftruncate()! • Create memory mapping, using mmap() function! • Do work, and then release mapping using munmap() function! • Kernel might not write data to disk until munmap() "4 mmap() function void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) • addr is target address of mapping, or NULL to let kernel decide! • length is number of bytes to map! • prot defines what mapping protection (read-only or read/write)! • flags sets other options! • fd is file descriptor that was returned by open()! • offset is o$set into file specified by fd "5 mmap() example part 1 • See man page for each of these functions
    [Show full text]
  • Interprocess Communication 1 Processes • Basic Concept to Build
    Interprocess Communication 1 Processes • Basic concept to build the OS, from old IBM mainframe OS to the most modern Windows • Used to express the requirements to be met by an OS – Interleave the execution of multiple processes, to maximize CPU utilization while providing good response time – Allocate resources to processes using a policy while avoiding deadlocks – Support interprocess communications and user creation of processes to help structuring applications • Background – Computer platform * Collection of hardware resources – CPU, memory, I/O modules, timers, storage devices – Computer applications * Developed to perform some task * Input, processing, output – Efficient to write applications for a given CPU * Common routines to access computer resources across platforms * CPU provides only limited support for multiprogramming; software manages sharing of CPU and other re- sources by multiple applications concurrently * Data and resources for multiple concurrent applications must be protected from other applications • Process – Abstraction of a running program – Unit of work in the system – Split into two abstractions in modern OS * Resource ownership (traditional process view) * Stream of instruction execution (thread) – Pseudoparallelism, or interleaved instructions – A process is traced by listing the sequence of instructions that execute for that process • Modeling sequential process/task – Program during execution – Program code – Current activity – Process stack * Function parameters * Return addresses * Temporary variables –
    [Show full text]
  • Dressing up Data For
    Dressing up data for Hannes Mühleisen DSC 2017 Problem? • People push large amounts of data into R • Databases, Parquet/Feather … • Need native SEXP for compatibility • R has no abstraction for data access • INTEGER(A)[i] * INTEGER(B)[j] etc. • Data possibly never actually used 2 Sometimes lucky • Perfectly compatible bits: • int my_int_arr[100]; • double my_dbl_arr[100]; • Doctor SEXP header in front of data and good to go • Implemented in MonetDBLite with custom allocator • Next version on CRAN will have this https://github.com/hannesmuehleisen/MonetDBLite 3 Zero-Copy in MonetDBLite Page 1 Page 2 Page 3 Page 4 Page 5 addr = mmap(col_file, len, NULL) col_file addr addr1 = mmap(NULL, len + PAGE_SIZE, NULL) addr2 = mmap(col_file, len, addr1 + 4096) addr3 = addr1 + PAGE_SIZE - sizeof(SEXPREC_ALIGN) SEXP res = allocVector3(INTSXP, len/sizeof(int), &allocator); Page 0 Page 1 Page 2 Page 3 Page 4 Page 5 col_file addr1 res & addr3 4 Demo 1 Stock R, MonetDBLite & zero-copy library(“DBI”) con <- dbConnect(MonetDBLite::MonetDBLite(), "/tmp/dscdemo") dbGetQuery(con, "SELECT COUNT(*) FROM onebillion”) # 1 1e+09 system.time(a <- dbGetQuery(con, "SELECT i FROM onebillion”)) # user system elapsed # 0.032 0.000 0.033 .Internal(inspect(a$i)) # @20126efd8 13 INTSXP g0c6 [NAM(2)] (len=1000000000, tl=0) 1,2,3,4,5,... Native R Vector w. zero-copy! 5 Not always so lucky • What if we have to actually convert? • Strings, TIMESTAMP to POSIXct etc. • NULL/NA mismatches • More involved data representations • compressed, batched, hybrid row/col, … • Need to convert all data before handing control over to R. • Can take forever, takes memory, non-obvious wait time 6 ALTREP • Luke Tierney, Gabe Becker & Tomas Kalibera • Abstract vectors, ELT()/GET_REGION() methods • Lazy conversion! static void monetdb_altrep_init_int(DllInfo *dll) { R_altrep_class_t cls = R_make_altinteger_class(/* .
    [Show full text]
  • Signals and Pipes
    Signals and Pipes Signals and Pipes 1/20 Learning Objectives Signals and Pipes I Understand synchronization issues with respect to signals I Learn about the client-server model using pipes and named pipes I Understand basic concepts of servers and daemons 2/20 More on Signals Signals and Pipes I Two main system calls that deal with signals: signal() and sigaction(). The sigaction() call is consistent across various systems whereas signal() is not necessarily consistent across systems but is simpler to use. See man page for details on both. I Examples: I signal-ex1.c I signal-ex2.c 3/20 Signal Handling after Exec Signals and When a program is exec’d the status of all signals is either default or Pipes ignore. The exec functions changes the disposition of any signals that are being caught to their default action (why?) and leaves the status of all other signals alone. For example: if (fork()==0) {// the child process if(signal(SIGINT, SIG_IGN) == SIG_ERR) err_ret ("failed␣to␣set␣SIGINT␣behavior"); if(signal(SIGTSTP, SIG_IGN) == SIG_ERR) err_ret ("failed␣to␣set␣SIGTSTP␣behavior"); execvp(program, argv); // the exec 'd program will ignore the // signalsSIGINT andSIGTSTP 4/20 Signal Handling for an Application Signals and How an application ought to set its signal handling: Pipes An application process should catch the signal only if the signal is not currently being ignored. int sig_int(), sig_quit(); if(signal(SIGINT, SIG_IGN) != SIG_IGN) signal(SIGINT, sig_int); if(signal(SIGQUIT, SIG_IGN) != SIG_IGN) signal(SIGQUIT, sig_quit); See code example: signal-ex3.c 5/20 Other Signal Issues Signals and Pipes I Signals are set to their default after being caught (under Linux and System V UNIX semantics).
    [Show full text]
  • 6.828: Virtual Memory for User Programs
    6.828: Virtual Memory for User Programs Adam Belay <[email protected]> Plan for today • Previously: Discussed using virtual memory tricks to optimize the kernel • This lecture is about virtual memory for user programs: • Concurrent garbage collection • Generational garbage collection • Concurrent checkpointing • Data-compression paging • Persistent stores What primitives do we need? • Trap: handle page-fault traps in usermode • Prot1: decrease the accessibility of a page • ProtN: decrease the accessibility of N pages • Unprot: increase the accessibility of a page • Dirty: returns a list of dirtied pages since previous call • Map2: map the same physical page at two different virtual addresses, at different levels of protection, in the same address space What about UNIX? • Processes manage virtual memory through higher- level abstractions • An address space consists of a non-overlapping list of Virtual Memory Areas (VMAs) and a page table • Each VMA is a contiguous range of virtual addresses that shares the same permissions and is backed by the same object (e.g. a file or anonymous memory) • VMAs help the kernel decide how to handle page faults Unix: mmap() • Maps memory into the address space • Many flags and options • Example: mapping a file mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, offset); • Example: mapping anonymous memory mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); Unix: mprotect() • Changes the permissions of a mapping • PROT_READ, PROT_WRITE, and PROT_EXEC • Example: make mapping read-only mprotect(addr, len, PROT_READ); • Example: make mapping trap on any acCess mprotect(addr, len, PROT_NONE); Unix: munmap() • Removes a mapping • Example: munmap(addr, len); Unix: sigaction() • Configures a signal handler • Example: get signals for memory access violations act.sa_sigaction = handle_sigsegv; act.sa_flags = SA_SIGINFO; sigemptyset(&act.sa_mask); sigaction(SIGSEGV, &act, NULL); Unix: Modern implementations are very complex e.g.
    [Show full text]