System Calls & Signals. CS449 Spring 2016

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

Signals. Joseph Cordina

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

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

Fortran Signal Handling

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

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

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

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

System Programming. Signals I

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

Chapter 4 Multithreaded Programming

ECE 650 Systems Programming & Engineering. Spring 2018

PVPSIDDHARTHA INSTITUTE OF TECHNOLOGY

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

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 18

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

Goals of this Lecture

Interprocess Communication

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

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

Operating Systems 2010/2011

CS240: Programming in C

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

Technical Document Series. POSIX Signal Handling in Java

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

Chapter 1 Introduction

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

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

CMSC 216 Introduction to Computer Systems Lecture 17 Process Control and System-Level I/O

Operating Systemss and Multicore Programming (1DT089)

CSE 421: Introduction to Operating Systems

Process Control. Philipp Koehn. 23 April 2018

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

Concurrency Problems Signals & Synchronization Semaphore Mutual Exclusion Critical Section Monitors

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

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

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

Embedded Systems Programming

Lecture 4 Threads. (chapter 4)

Fork() System Call and Processes. CS449 Fall 2017

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 19

Chapter 4: Threads. Operating System Concepts 9 th Edition

Operating Systemss and Multicore Programming (1DT089)

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

COSC243 Part 2: Operating Systems

Signals. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

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

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

Inter Process Communication

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

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

Operating Systems Grado en Informática. Course

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

COMP 2400 UNIX Tools

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

Lecture 8: Unix Pipes and Signals (Feb 10, 2005) Yap

HW 1: Shell. Contents CS 162. Due: September 18, Getting started 2. 2 Add support for cd and pwd 2. 3 Program execution 2. 4 Path resolution 3

Lecture 9: Real-Time Software Design Issues

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

File and Console I/O. CS449 Spring 2016

Processes. Processes (cont d)

Solutions to the first midterm. COSC 4330/6310 Summer 2013

Preview. Interprocess Communication with Pipe. Pipe from the Parent to the child Pipe from the child to the parent FIFO popen() with r Popen() with w

Unix Processes. What is a Process?

Processes. CS439: Principles of Computer Systems January 30, 2019

Lecture 6. User-Mode Linux. Jeff Dike. 9 November, Advanced Operating Systems. SOA/OS Lecture 6, UML 1/33

Processes COMPSCI 386

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

Signal Example 1. Signal Example 2

Contents. IPC (Inter-Process Communication) Representation of open files in kernel I/O redirection Anonymous Pipe Named Pipe (FIFO)

Table of Contents. Unix Systems: Some more useful commands

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

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

Advanced Unix Concepts. Satyajit Rai

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

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

Systems Programming/ C and UNIX

CSC209H Lecture 8. Dan Zingaro. March 4, 2015

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

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

Lecture 24: Multitasking and Signals

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

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

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

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

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

Operating systems. Lecture 9

Killing Zombies, Working, Sleeping, and Spawning Children

Exceptional Control Flow Part II

UNIT III- INTERPROCESS COMMUNICATION

Layers in a UNIX System. Create a new process. Processes in UNIX. fildescriptors streams pipe(2) labinstructions

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

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

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

System Programming. Introduction to Unix

Exceptional Control Flow Part I

CS 31: Intro to Systems Processes. Kevin Webb Swarthmore College March 31, 2016

Transcription:

System Calls & Signals CS449 Spring 2016

Operating system OS a layer of software interposed between the application program and the hardware Application programs Operating system Processor Main memory I/O devices Software Hardware Two primary goal Manage hardware resources Abstract details about hardware I/O devices abstracted by files Main memory abstracted by virtual memory Processors abstracted by processes 2

What is an OS Pedantically it is just a Kernel A program that implements the core OS functions (process scheduling, memory/hw management) More commonly it refers to the basic environment Libraries, user interface, foundational programs A Kernel is a program, but it is not an application Doesn t actually execute on its own (except at boot time) Only reacts to events and requests System calls, exceptions, and interrupts

x86 Privilege Levels

OS Interactions System Calls Allow an application to request the OS to do something Read a file, print to console, etc Signals OS notifies an application of something SEGFAULT, Killed, etc Exceptions Application does something it wasn t supposed to Access invalid memory address Interrupts Processor receives a hardware signal on an interrupt pin Mouse move or keyboard type

Signals Notifications sent to a program by OS Indicate special events Allows for asynchronous notification rather than polling Polling to explicitly ask if something occurred, usually repeatedly

kill -l (returns a complete list of signals for your system) SIGHUP SIGINT SIGQUIT SIGILL SIGTRAP SIGABRT SIGBUS SIGFPE SIGKILL SIGUSR1 SIGSEGV SIGUSR2 SIGPIPE SIGALRM SIGTERM SIGCHLD SIGCONT SIGSTOP SIGTSTP SIGTTIN SIGTTOU SIGURG SIGXCPU SIGXFSZ SIGVTALRM SIGPROF SIGWINCH SIGIO SIGPWR SIGSYS SIGRTMIN SIGRTMIN+1 SIGRTMIN+2 SIGRTMIN+3 SIGRTMIN+4 SIGRTMIN+5 SIGRTMIN+6 SIGRTMIN+7 SIGRTMIN+8 SIGRTMIN+9 SIGRTMIN+10 SIGRTMIN+11 SIGRTMIN+12 SIGRTMIN+13 SIGRTMIN+14 SIGRTMIN+15 SIGRTMAX-14 SIGRTMAX-13 SIGRTMAX-12 SIGRTMAX-11 SIGRTMAX-10 SIGRTMAX-9 SIGRTMAX-8 SIGRTMAX-7 SIGRTMAX-6 SIGRTMAX-5 SIGRTMAX-4 SIGRTMAX-3 SIGRTMAX-2 SIGRTMAX-1 SIGRTMAX

Common Error Signals SIGILL Illegal Instruction SIGBUS Bus Error, usually caused by bad data alignment or a bad address SIGFPE Floating Point Exception SIGSEGV Segmentation violation, i.e., a bad address

Termination Signals SIGINT Interrupt, or what happens when you hit CTRL + C SIGTERM Ask nicely for a program to end (can be caught) SIGKILL Ask meanly for a program to end (cannot be caught) SIGABRT, SIGQUIT End a program with a core dump

kill From the shell in UNIX you can send signals to a program. Use ps to get a process ID kill it! (1) thot $ ps -af UID PID PPID C STIME TTY TIME CMD jrmst106 27500 27470 0 07:13??? 00:00:00 crashed_program jrmst106 27507 27474 0 07:13 pts/5 00:00:00 ps -af kill 27500 Sends SIGTERM kill -9 27500 Sends SIGKILL

kill kill() is the system call that can send a process a signal (any signal, not just SIGKILL) #include <unistd.h> #include <sys/types.h> #include <signal.h> int main() { pid_t my_pid = getpid(); kill(my_pid, SIGSTOP); return 0; }

Catching Signals Most signals can be caught Like exceptions in Java Do some cleanup, then exit Generally bad to try to continue, the machine might be in a corrupt state Some signals can be caught safely though

SIGALRM #include <unistd.h> #include <signal.h> int timer = 10; void catch_alarm(int sig_num) { printf("%d\n",timer--); alarm(1); } int main() { signal(sigalrm, catch_alarm); >>./a.out 10 9 8 7 6 5 4 3 2 1 } alarm(1); while(timer > 0) ; alarm(0); return 0;

Exceptions You ve seen these in Java Exceptions are generated by unexpected errors Hardware exceptions exist for the same purpose Hardware has to tell OS that a program tried to do something illegal Executed an illegal instruction, violated memory protection OS then has to respond to the violation Handle the exception let program continue to run (e.g. page fault) Page Fault: exception that happens when accessing page in disk Notify the program that something happened (via signal) Kill the program

Exception Handling Each exception has an entry in a dispatch table An array of function pointers to specific handlers Interrupt Vector Table Appropriate privilege given on jump to handler Registers must be saved to/restored from kernel stack Hardware interrupts handled by same dispatch table System calls often implemented using exceptions x86 versions uses a trap instruction: int 0x80 Trap: an intentional exception to invoke a specific handler Jumps to entry 0x80 in dispatch table

System Call User space 4 2 Return to caller Trap to kernel 3 Trap code in register 8 Library (printf call) 1 Call printf Push arguments 7 User code Kernel space (OS) Dispatch 5 6 Sys call handler

strace./hello fstat(1, {st_mode=s_ifchr 0600, st_rdev=makedev (136, 7),...}) = 0 mmap(null, 4096, PROT_READ PROT_WRITE, MAP_PRIVATE MAP_ANONYMOUS, -1, 0) = 0x2a95557000 write(1, "Hello world!\n", 13Hello world!) = 13 exit_group(0)

Linux Syscalls 325 syscall slots reserved in system call table (2.6.23.1 kernel) -- Not all are used exit fork read write open close creat Causes a process to terminate Creates a new process, identical to the current one Reads data from a file or device Writes data to a file or device Opens a file Closes a file Creates a file

Using Syscalls #include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main() { int fd; char buffer[100]; strcpy(buffer, "Hello, World!\n"); fd = open("hello.txt", O_WRONLY O_CREAT); write(fd, buffer, strlen(buffer)); close(fd); exit(0); } return 0;

OR-ing Flags Define constants as powers of 2 Bitwise OR to combine Bitwise AND to test #define O_RDONLY 0 #define O_WRONLY 1 #define O_RDWR 2 #define O_CREAT 16

File Descriptors Integer identifying a unique open file Similar to FILE * FILE struct has a file descriptor member File descriptors index into a file descriptor table OS maintains additional information about the file to do things such as clean up on process termination Three standard file descriptors opened automatically: 0 stdin 1 stdout 2 stderr

Dumping the File Descriptor Table ls -l /proc/self/fd (listing default file descriptors) (84) thot $ ls -l /proc/self/fd total 0 lrwx------ 1 wahn UNKNOWN1 64 Sep 11 13:37 0 -> /dev/pts/0 lrwx------ 1 wahn UNKNOWN1 64 Sep 11 13:37 1 -> /dev/pts/0 lrwx------ 1 wahn UNKNOWN1 64 Sep 11 13:37 2 -> /dev/pts/0 lr-x------ 1 wahn UNKNOWN1 64 Sep 11 13:37 3 -> /proc/16970/fd ls -l /proc/self/fd > /tmp/ls.out (listing with redirect) (85) thot $ ls -l /proc/self/fd > /tmp/ls.out && cat /tmp/ls.out total 0 lrwx------ 1 wahn UNKNOWN1 64 Sep 11 13:41 0 -> /dev/pts/0 l-wx------ 1 wahn UNKNOWN1 64 Sep 11 13:41 1 -> /tmp/ls.out lrwx------ 1 wahn UNKNOWN1 64 Sep 11 13:41 2 -> /dev/pts/0 lr-x------ 1 wahn UNKNOWN1 64 Sep 11 13:41 3 -> /proc/17025/fd