CS631 - Advanced Programming in the UNIX Environment. Dæmon processes, System Logging, Advanced I/O

Similar documents
MMAP AND PIPE. UNIX Programming 2015 Fall by Euiseong Seo

ISA 563: Fundamentals of Systems Programming

Programmation Systèmes Cours 8 Synchronization & File Locking

ADVANCED I/O. ISA 563: Fundamentals of Systems Programming

Advanced Unix Programming Module 06 Raju Alluri spurthi.com

File I/0. Advanced Programming in the UNIX Environment

Outline. OS Interface to Devices. System Input/Output. CSCI 4061 Introduction to Operating Systems. System I/O and Files. Instructor: Abhishek Chandra

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Operating System Labs. Yuanbin Wu

which maintain a name to inode mapping which is convenient for people to use. All le objects are

CS 33. Files Part 2. CS33 Intro to Computer Systems XXI 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Overview. Daemon processes and advanced I/O. Source: Chapters 13&14 of Stevens book

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

File I/O - Filesystems from a user s perspective

I/O OPERATIONS. UNIX Programming 2014 Fall by Euiseong Seo

Lecture 10 Overview!

CS631 - Advanced Programming in the UNIX Environment Interprocess Communication II

I/O OPERATIONS. UNIX Programming 2014 Fall by Euiseong Seo

UNIX System Calls. Sys Calls versus Library Func

Lecture files in /home/hwang/cs375/lecture05 on csserver.

CS631 - Advanced Programming in the UNIX Environment Interprocess Communication I

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes

POSIX Shared Memory. Linux/UNIX IPC Programming. Outline. Michael Kerrisk, man7.org c 2017 November 2017

IPC and Unix Special Files

Overview. Last Lecture. This Lecture. Daemon processes and advanced I/O functions

ECEN 449 Microprocessor System Design. Review of C Programming

St. MARTIN S ENGINEERING COLLEGE Dhulapally,Secunderabad DEPARTMENT OF INFORMATION TECHNOLOGY Academic year

File Descriptors and Piping

Outline. Relationship between file descriptors and open files

ECE 650 Systems Programming & Engineering. Spring 2018

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

Systems Programming. COSC Software Tools. Systems Programming. High-Level vs. Low-Level. High-Level vs. Low-Level.

ECEN 449 Microprocessor System Design. Review of C Programming. Texas A&M University

CS631 - Advanced Programming in the UNIX Environment

Chapter 3. File I/O. System Programming 熊博安國立中正大學資訊工程學系

Never Lose a Syslog Message

Pipes and FIFOs. Woo-Yeong Jeong Computer Systems Laboratory Sungkyunkwan University

CS 350 : COMPUTER SYSTEM CONCEPTS SAMPLE TEST 2 (OPERATING SYSTEMS PART) Student s Name: MAXIMUM MARK: 100 Time allowed: 70 minutes

Overview. Administrative. * HW 2 Grades. * HW 3 Due. Topics: * What are Threads? * Motivating Example : Async. Read() * POSIX Threads

Processes often need to communicate. CSCB09: Software Tools and Systems Programming. Solution: Pipes. Recall: I/O mechanisms in C

Motivation of VPN! Overview! VPN addressing and routing! Two basic techniques for VPN! ! How to guarantee privacy of network traffic?!

Lecture 17. Log into Linux. Copy two subdirectories in /home/hwang/cs375/lecture17/ $ cp r /home/hwang/cs375/lecture17/*.

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

Process Management 1

Processes. Operating System CS 217. Supports virtual machines. Provides services: User Process. User Process. OS Kernel. Hardware

Operating Systems. Lecture 06. System Calls (Exec, Open, Read, Write) Inter-process Communication in Unix/Linux (PIPE), Use of PIPE on command line

UNIX System Programming

Maria Hybinette, UGA. ! One easy way to communicate is to use files. ! File descriptors. 3 Maria Hybinette, UGA. ! Simple example: who sort

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

Process Management! Goals of this Lecture!

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes

Parents and Children

Processes COMPSCI 386

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes

Shared Memory Memory mapped files

we are here Page 1 Recall: How do we Hide I/O Latency? I/O & Storage Layers Recall: C Low level I/O

CSE 410: Systems Programming

Secure Software Programming and Vulnerability Analysis

Introduction to File Systems. CSE 120 Winter 2001

INTRODUCTION TO THE UNIX FILE SYSTEM 1)

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

Lecture 5 Overview! Last Lecture! This Lecture! Next Lecture! I/O multiplexing! Source: Chapter 6 of Stevens book!

CS 3733 Operating Systems

CMPS 105 Systems Programming. Prof. Darrell Long E2.371

Process Management! Goals of this Lecture!

Page Which had internal designation P5

Operating System Labs. Yuanbin Wu

Operating systems. Lecture 9

Naked C Lecture 6. File Operations and System Calls

Files and Directories

Files. Eric McCreath

경희대학교컴퓨터공학과 조진성. UNIX System Programming

Signal Example 1. Signal Example 2

Inter-Process Communication

CS240: Programming in C

PROCESSES. Jo, Heeseung

Processes. Jo, Heeseung

CSCI 4061: Virtual Memory

File Systems Overview. Jin-Soo Kim ( Computer Systems Laboratory Sungkyunkwan University

Files and the Filesystems. Linux Files

UNIT III- INTER PROCESS COMMUNICATIONS Part A

Operating Systems. Lecture 05

Pipes. Pipes Implement a FIFO. Pipes (cont d) SWE 545. Pipes. A FIFO (First In, First Out) buffer is like a. Pipes are uni-directional

OPERATING SYSTEMS: Lesson 2: Operating System Services

we are here I/O & Storage Layers Recall: C Low level I/O Recall: C Low Level Operations CS162 Operating Systems and Systems Programming Lecture 18

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

Project 2: User Programs

UNIX System Programming. Overview. 1. A UNIX System. 2. Processes (review) 2.1. Context. Pipes/FIFOs

Contents. PA1 review and introduction to PA2. IPC (Inter-Process Communication) Exercise. I/O redirection Pipes FIFOs

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Inter-process communication (IPC)

Lecture 3. Introduction to Unix Systems Programming: Unix File I/O System Calls

Noorul Islam College Of Engineering, Kumaracoil MCA Degree Model Examination (October 2007) 5 th Semester MC1642 UNIX Internals 2 mark Questions

FILE SYSTEMS. Jo, Heeseung

COM324 System Programming. Midterm Exam

Advanced Programming in the UNIX Environment W. Richard Stevens

Memory-Mapped Files. generic interface: vaddr mmap(file descriptor,fileoffset,length) munmap(vaddr,length)

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

Creating a Shell or Command Interperter Program CSCI411 Lab

Fall 2017 :: CSE 306. File Systems Basics. Nima Honarmand

Transcription:

CS631 - Advanced Programming in the UNIX Environment Slide 1 CS631 - Advanced Programming in the UNIX Environment Dæmon processes, System Logging, Advanced I/O Department of Computer Science Stevens Institute of Technology Jan Schaumann jschauma@stevens.edu https://www.cs.stevens.edu/~jschauma/631/

Dæmon processes CS631 - Advanced Programming in the UNIX Environment Slide 2 So... what s a dæmon process anyway?

Dæmon characteristics CS631 - Advanced Programming in the UNIX Environment Slide 3 Commonly, dæmon processes are created to offer a specific service. Dæmon processes usually live for a long time are started at boot time terminate only during shutdown have no controlling terminal

Dæmon characteristics CS631 - Advanced Programming in the UNIX Environment Slide 4 The previously listed characteristics have certain implications: do one thing, and one thing only no (or only limited) user-interaction possible resource leaks eventually surface consider current working directory how to create (debugging) output

Writing a dæmon CS631 - Advanced Programming in the UNIX Environment Slide 5 fork off the parent process change file mode mask (umask) create a unique Session ID (SID) change the current working directory to a safe place close (or redirect) standard file descriptors open any logs for writing enter actual dæmon code

Writing a dæmon CS631 - Advanced Programming in the UNIX Environment Slide 6 int daemon(int nochdir, int noclose) { int fd; switch (fork()) { case -1: return (-1); case 0: break; default: _exit(0); } if (setsid() == -1) return (-1); if (!nochdir) (void)chdir("/"); } if (!noclose && (fd = open(_path_devnull, O_RDWR, 0))!= -1) { (void)dup2(fd, STDIN_FILENO); (void)dup2(fd, STDOUT_FILENO); (void)dup2(fd, STDERR_FILENO); if (fd > STDERR_FILENO) (void)close(fd); } return (0);

Dæmon conventions CS631 - Advanced Programming in the UNIX Environment Slide 7 prevent against multiple instances via a lockfile allow for easy determination of PID via a pidfile configuration file convention /etc/name.conf include a system initialization script (for /etc/rc.d/ or /etc/init.d/) re-read configuration file upon SIGHUP relay information via event logging

A central logging facility CS631 - Advanced Programming in the UNIX Environment Slide 8 There are three ways to generate log messages: via the kernel routine log(9) via the userland routine syslog(3) via UDP messages to port 514

A central logging facility CS631 - Advanced Programming in the UNIX Environment Slide 9

syslog(3) CS631 - Advanced Programming in the UNIX Environment Slide 10 #include <syslog.h> void openlog(const char *ident, int logopt, int facility); void syslog(int priority, const char *message,...); openlog(3) allows us to set specific options when logging: prepend ident to each message specify logging options (LOG CONS LOG NDELAY LOG PERRO LOG PID) specify a facility (such as LOG DAEMON, LOG MAIL etc.) syslog(3) writes a message to the system message logger, tagged with priority. A priority is a combination of a facility (as above) and a level (such as LOG DEBUG, LOG WARNING or LOG EMERG).

Nonblocking I/O CS631 - Advanced Programming in the UNIX Environment Slide 11 Recall from our lecture on signals that certain system calls can block forever: read(2) from a particular file, if data isn t present (pipes, terminals, network devices) write(2) to the same kind of file open(2) of a particular file until a specific condition occurs read(2) and write(2) of files that have mandatory locking enabled certain ioctls(2) some IPC functions (such as sendto(2) or recv(2)) See eintr.c from that lecture.

Nonblocking I/O CS631 - Advanced Programming in the UNIX Environment Slide 12 Recall from our lecture on signals that certain system calls can block forever: read(2) from a particular file, if data isn t present (pipes, terminals, network devices) write(2) to the same kind of file open(2) of a particular file until a specific condition occurs read(2) and write(2) of files that have mandatory locking enabled certain ioctls(2) some IPC functions (such as sendto(2) or recv(2)) Nonblocking I/O lets us issue an I/O operation and not have it block forever. If the operation cannot be completed, return is made immediately with an error noting that the operating would have blocked (EWOULDBLOCK or EAGAIN).

Nonblocking I/O CS631 - Advanced Programming in the UNIX Environment Slide 13 Ways to specify nonblocking mode: pass O NONBLOCK to open(2): open(path, O RDRW O NONBLOCK); set O NONBLOCK via fcntl(2): flags = fcntl(fd, F GETFL, 0); fcntl(fd, F SETFL, flags O NONBLOCK);

Nonblocking I/O CS631 - Advanced Programming in the UNIX Environment Slide 14 $ cc -Wall nonblock.c -o block $ cc -DNONBLOCK -Wall nonblock.c -o nonblock $./nonblock >/dev/null wrote 100000 bytes [...] $./block ( sleep 3; cat >/dev/null ) [...] $./nonblock ( sleep 3; cat >/dev/null ) [...] $ (./nonblock cat >/dev/null ) 2>&1 more [...] $ nc -l 8080 >/dev/null & $./nonblock nc hostname 8080 [...]

Resource Locking CS631 - Advanced Programming in the UNIX Environment Slide 15 Ways we have learned so far to ensure only one process has exclusive access to a resource: open file using O CREAT O EXCL, then immediately unlink(2) it create a lockfile if file exists, somebody else is using the resource use of a semaphore What are some problems with each of these?

Advisory Locking CS631 - Advanced Programming in the UNIX Environment Slide 16 #include <fcntl.h> int flock(int fd,int operation); Returns: 0 if OK, -1 otherwise applies or removes an advisory lock on the file associated with the file descriptor fd operation can be LOCK NB and any one of: LOCK SH LOCK EX LOCK UN locks entire file

Advisory Locking CS631 - Advanced Programming in the UNIX Environment Slide 17 $ cc -Wall flock.c 1$./a.out Shared lock established - sleeping for 10 seconds. [...] Giving up all locks. 2$./a.out Shared lock established - sleeping for 10 seconds. Now trying to get an exclusive lock. Unable to get an exclusive lock. [...] Exclusive lock established. 1$./a.out [blocks until the other process terminates]

Advisory Record Locking CS631 - Advanced Programming in the UNIX Environment Slide 18 Record locking is done using fcntl(2), using one of F GETLK, F SETLK or F SETLKW and passing a struct flock { short l_type; /* F_RDLCK, F_WRLCK, or F_UNLCK */ off_t l_start; /* offset in bytes from l_whence */ short l_whence; /* SEEK_SET, SEEK_CUR, or SEEK_END */ off_t l_len; /* length, in bytes; 0 means "lock to EOF" */ pid_t l_pid; /* returned by F_GETLK */ } Lock types are: F RDLCK Non-exclusive (read) lock; fails if write lock exists. F WRLCK Exclusive (write) lock; fails if any lock exists. F UNLCK Releases our lock on specified range.

CS631 - Advanced Programming in the UNIX Environment Slide 19 Advisory Record locking #include <unistd.h> int lockf(int fd, int value, off t size); Returns: 0 on success, -1 on error value can be: F ULOCK unlock locked sections F LOCK lock a section for exclusive use F TLOCK test and lock a section for exclusive use F TEST test a section for locks by other processes

Advisory Record locking CS631 - Advanced Programming in the UNIX Environment Slide 20 Locks are: released if a process terminates released if a filedescriptor is closed (!) not inherited across fork(2) inherited across exec(2) released upon exec(2) if close-on-exec is set

Advisory Record locking CS631 - Advanced Programming in the UNIX Environment Slide 21 Locks are associated with a file and process pair, not with a filedescriptor!

Mandatory locking CS631 - Advanced Programming in the UNIX Environment Slide 22 not implemented on all UNIX flavors chmod g+s,g-x file possible to be circumvented: $ mandatory-lock /tmp/file & $ echo foo > /tmp/file2 $ rm /tmp/file $ mv /tmp/file2 /tmp/file

Asynchronous I/O CS631 - Advanced Programming in the UNIX Environment Slide 23

Synchronous blocking I/O CS631 - Advanced Programming in the UNIX Environment Slide 24

Synchronous non-blocking I/O CS631 - Advanced Programming in the UNIX Environment Slide 25

Asynchronous blocking I/O CS631 - Advanced Programming in the UNIX Environment Slide 26

Asynchronous non-blocking I/O CS631 - Advanced Programming in the UNIX Environment Slide 27

Asynchronous I/O CS631 - Advanced Programming in the UNIX Environment Slide 28 System V derived async I/O limited to STREAMS enabled via ioctl(2) uses SIGPOLL BSD derived async I/O limited to terminals and networks enabled via fcntl(2) (O ASYNC, F SETOWN) uses SIGIO and SIGURG POSIX aio(3) kernel process manages queued I/O requests notification of calling process via signal or sigevent callback function calling process can still choose to block/wait

CS631 - Advanced Programming in the UNIX Environment Slide 29 Memory Mapped I/O #include <sys/types.h> #include <sys/mman.h> void *mmap(void *addr, size t len, int prot, int flags, int fd, off t offset); Returns: pointer to mapped region if OK Protection specified for a region: PROT READ region can be read PROT WRITE region can be written PROT EXEC region can be executed PROT NONE region can not be accessed flag needs to be one of MAP SHARED MAP PRIVATE MAP COPY which may be OR d with other flags (see mmap(2) for details).

Memory Mapped I/O CS631 - Advanced Programming in the UNIX Environment Slide 30

Memory Mapped I/O CS631 - Advanced Programming in the UNIX Environment Slide 31 Exercise: write a program that benchmarks this performance and run it on the systems you have access to.

Memory Mapped I/O CS631 - Advanced Programming in the UNIX Environment Slide 32 http://cvsweb.netbsd.org/bsdweb.cgi/src/bin/cp/utils.c?rev=head

Reading CS631 - Advanced Programming in the UNIX Environment Slide 33 https://www.ibm.com/developerworks/linux/library/l-async/index.html http://menehune.opt.wfu.edu/kokua/more_sgi/007-2478-008/sgi_html/ch08.html http://lse.sourceforge.net/io/aionotes.txt https://en.wikipedia.org/wiki/streams