MMAP AND PIPE. UNIX Programming 2015 Fall by Euiseong Seo

Similar documents
Advanced Unix Programming Module 06 Raju Alluri spurthi.com

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

Programmation Systèmes Cours 8 Synchronization & File Locking

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

ISA 563: Fundamentals of Systems Programming

OPERATING SYSTEMS: Lesson 2: Operating System Services

ECE 650 Systems Programming & Engineering. Spring 2018

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

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

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

Shared Memory Memory mapped files

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

Operating systems. Lecture 9

File I/0. Advanced Programming in the UNIX Environment

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Information Sciences and Engineering

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

프로세스간통신 (Interprocess communication) i 숙명여대창병모

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

UNIX System Calls. Sys Calls versus Library Func

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

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

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

Outline. 1 Details of paging. 2 The user-level perspective. 3 Case study: 4.4 BSD 1 / 19

Virtual Memory: Systems

Page Which had internal designation P5

ECEN 449 Microprocessor System Design. Review of C Programming

P6 memory system P6/Linux Memory System October 31, Overview of P6 address translation. Review of abbreviations. Topics. Symbols: ...

Programmation Système Cours 5 Memory Mapping

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

VIRTUAL FILE SYSTEM AND FILE SYSTEM CONCEPTS Operating Systems Design Euiseong Seo

Operating System Labs. Yuanbin Wu

Signal Example 1. Signal Example 2

Intel P The course that gives CMU its Zip! P6/Linux Memory System November 1, P6 memory system. Review of abbreviations

Lab 09 - Virtual Memory

UNIX System Programming

Preview. Process Control. What is process? Process identifier The fork() System Call File Sharing Race Condition. COSC350 System Software, Fall

CSCI 4061: Virtual Memory

Process Creation in UNIX

Inter-Process Communication. Disclaimer: some slides are adopted from the book authors slides with permission 1

CSE 333 SECTION 3. POSIX I/O Functions

everything is a file main.c a.out /dev/sda1 /dev/tty2 /proc/cpuinfo file descriptor int

CSC 271 Software I: Utilities and Internals

Applications of. Virtual Memory in. OS Design

The course that gives CMU its Zip! I/O Nov 15, 2001

FILE SYSTEMS. Jo, Heeseung

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

CS240: Programming in C

Interprocess Communication E. Im

Intel P6 (Bob Colwell s Chip, CMU Alumni) The course that gives CMU its Zip! Memory System Case Studies November 7, 2007.

Process Management 1

CS 25200: Systems Programming. Lecture 14: Files, Fork, and Pipes

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

CSC209H Lecture 7. Dan Zingaro. February 25, 2015

Files. Eric McCreath

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

Virtual Memory: Systems

Memory management. Single process. Multiple processes. How to: All memory assigned to the process Addresses defined at compile time

Operating systems. Lecture 7

Introduction to File Systems. CSE 120 Winter 2001

System Calls and I/O Appendix. Copyright : University of Illinois CS 241 Staff 1

CSE 410: Systems Programming

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

Operating Systems 2230

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

Recitation 8: Tshlab + VM

Memory System Case Studies Oct. 13, 2008

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

Input and Output System Calls

Naked C Lecture 6. File Operations and System Calls

Pentium/Linux Memory System March 17, 2005

12: Filesystems: The User View

Foundations of Computer Systems

Interacting with Unix

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

What is a Process. Preview. What is a Process. What is a Process. Process Instruction Cycle. Process Instruction Cycle 3/14/2018.

Virtual Memory. Alan L. Cox Some slides adapted from CMU slides

Processes COMPSCI 386

Process Management! Goals of this Lecture!

Important Dates. October 27 th Homework 2 Due. October 29 th Midterm

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

CSE 333 SECTION 3. POSIX I/O Functions

CSC 1600 Unix Processes. Goals of This Lecture

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

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

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

Process Management! Goals of this Lecture!

CS240: Programming in C

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

File Descriptors and Piping

OS COMPONENTS OVERVIEW OF UNIX FILE I/O. CS124 Operating Systems Fall , Lecture 2

2009 S2 COMP File Operations

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

Multi-Process Programming in C

Matt Ramsay CS 375 EXAM 2 Part 1

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

CS240: Programming in C

CS 201. Files and I/O. Gerson Robboy Portland State University

Princeton University Computer Science 217: Introduction to Programming Systems. I/O Management

Princeton University. Computer Science 217: Introduction to Programming Systems. I/O Management

CSC209H Lecture 6. Dan Zingaro. February 11, 2015

Transcription:

MMAP AND PIPE UNIX Programming 2015 Fall by Euiseong Seo

Memory Mapping mmap(2) system call allows mapping of a file into process address space Instead of using read() and write(), just write to memory to transfer data You cannot extend a mapped file without use of other calls such as truncate(2)

mmap Call Prototype #include <sys/mman.h> void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset); int munmap(void *addr, size_t length); addr - requested address, NULL to let system choose len - length of mapped area prot- PROT_READ for read, PROT_WRITE for write access flags - MAP_SHARED save changes to disk, MAP_PRIVATE does not fd - file descriptor of opened file to be mapped off - offset from beginning of file to start mapping

Memory Mapping Address space after memory mapping of a file high address stack len memory-mapped portion of file start addr heap uninitialized data (bss) initialized data low address text file: memory-mapped portion of file off len

Memory Mapping Example #include <sys/types.h> #include <sys/mman.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> main(int argc, char *argv[]) { int fd; caddr_t addr; struct stat statbuf; if(argc!= 2) { fprintf(stderr, "Usage: mycat2 filename\n"); exit(1); if(stat(argv[1], &statbuf) == -1) { perror("open"); exit(1); fd = open(argv[1], O_RDONLY); if(fd == -1) {perror("open"); exit(1); addr = mmap((caddr_t)null, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, (off_t) 0); if(addr == (caddr_t)-1) { perror("mmap"); exit(1); /* no longer need fd */ close(fd); write(1, addr, statbuf.st_size); return 0;

Extension of a Mapped File You cannot extend a mapped file If you want to extend a mapped file, increase file size by truncate(2) or ftruncate(3), and mmap() the newly increased region

Extension of a Mapped File main() { int fd; int pagesize = sysconf(_sc_pagesize); caddr_t addr; printf("page size is %d bytes.\n", pagesize); fd=open("mapfile", O_RDWR O_CREAT O_TRUNC, 0666); if(fd == -1) { perror("open"); exit(1); if(ftruncate(fd,(off_t)(6*pagesize )) == -1) { perror("ftruncate"); exit(1); system("ls -l mapfile"); addr = mmap((caddr_t) NULL, pagesize, PROT_READ PROT_WRITE, MAP_SHARED, fd, (off_t)0); if(addr == (caddr_t)-1) { perror("mmap"); exit(1); close(fd); (void)strcpy(addr, "Test string.\n"); system("head -1 mapfile"); return 0;

Advising System on Access Patterns If you know how program will be accessing mmaped data, you may be able to improve performance by informing VM system with madvise(3) Prototype #include <sys/mman.h> int madvise(void *addr, size_t length, int advice);

Advising System on Access Patterns advice is one of followings MADV_NORMAL MADV_RANDOM n No read-ahead MADV_SEQUENTIAL n Read-ahead and drop-behind MADV_WILLNEED n Read in now MADV_DONTNEED n Free immediately

File Locking You can lock a portion of a file or an entire file for exclusive access by using lockf(3) This is an advisory lock An advisory lock requires all programs to use lockf() to enforce locking Prototype #include <unistd.h> int lockf(int fd, int cmd, off_t len); Locked section ranges from current offset to len If len is 0 then rest of file will be locked

File Locking cmd can be one of followings F_LOCK F_ULOCK F_TEST F_TLOCK

Using fcntl() to Lock Files fcntl(2) can do many things Duplicating a FD Changing FD flags Advisory locking Mandatory locking (Non-POSIX) Prototype #include <unistd.h> #include <fcntl.h> int fcntl(int fd, int cmd,... /* arg */ );

Using fcntl() to Lock Files cmd parameter may be F_GETLK F_SETLK (non-block) F_SETLKW (block) struct flock struct flock {... short l_type; ; /* Type of lock: F_RDLCK, F_WRLCK, F_UNLCK */ short l_whence; /* How to interpret l_start: SEEK_SET, SEEK_CUR, SEEK_END */ off_t l_start; /* Starting offset for lock */ off_t l_len; /* Number of bytes to lock */ pid_t l_pid;... /* PID of process blocking our lock (F_GETLK only) */

Anonymous Pipes Pipes are a one-way byte-stream connection between two processes Frequently they are used to connect STDIO of one process to STDOUT of another The easiest way to handle pipes is with popen(3) and pclose(3) Pipes are anonymous because they have no name in file system

Anonymous Pipes Prototype #include <stdio.h> FILE *popen(const char *command, const char *type); int pclose(file *stream);

Anonymous Pipes Example #include <stdio.h> main() { FILE *fp; int m; /* write your screen output through "more" */ fp = popen("more", "w"); if(fp==null) {fprintf(stderr, "popen failed.\n"); return 1; for(m = 1 ; m<= 90; m++) fprintf(fp, "Lots and lots of stuff.\n"); pclose(fp); return 0;

Anonymous Pipes A somewhat harder but more powerful way is to use pipe(2) pipe() fills in an array with two descriptors p[0] is read-end p[1] is write-end Prototype #include <unistd.h> int pipe(int pipefd[2]);

Anonymous Pipes After calling pipe(), process will fork() to create another process When a process forks, all file descriptors are duplicated Thus child inherits connections to both sides of pipe Child and parent must close sides of pipe that they are not interested in Writes to a pipe of less than PIPE_BUF size are guarnateed to be atomic

Anonymous Pipes int p[2]; pid_t pid; if(pipe(p) == -1) { perror("pipe"); exit(1); switch(pid = fork()) { case -1: perror("fork"); exit(1); break; case 0: /* child */ close(p[1]); if(p[0]!= 0) { dup2(p[0],0); close(p[0]); execlp("cat", (char *)NULL); perror("execlp"); exit(1); break; default: /* parent */ break; close(p[0]); write(p[1], "This is a message\n", 18); close(p[1]); if(waitpid(pid, (int *)NULL, 0) == -1) { perror("waitpid"); exit(1);