Sistemas Operativos /2016 Support Document N o 1. Files, Pipes, FIFOs, I/O Redirection, and Unix Sockets

Similar documents
C File Processing: One-Page Summary

UNIX Kernel. UNIX History

ENG120. Misc. Topics

Chapter 12. Files (reference: Deitel s chap 11) chap8

Ch 11. C File Processing (review)

Lecture 8. Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

Chapter 11 File Processing

File Processing. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Process Management! Goals of this Lecture!

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

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

Lecture6 File Processing

Lecture 23: System-Level I/O

Accessing Files in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts

File I/O. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CS631 - Advanced Programming in the UNIX Environment Interprocess Communication II

Process Management 1

Process Management! Goals of this Lecture!

Interprocess Communication

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

Lecture 9: File Processing. Quazi Rahman

Programming with TCP/IP. Ram Dantu

CSE 410: Systems Programming

UNIX input and output

UNIX I/O. Computer Systems: A Programmer's Perspective, Randal E. Bryant and David R. O'Hallaron Prentice Hall, 3 rd edition, 2016, Chapter 10

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

Files and Directories

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

File Descriptors and Piping

File I/O. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

IPC and Unix Special Files

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

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

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

Session NM056. Programming TCP/IP with Sockets. Geoff Bryant Process software

Standard File Pointers

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

What Is Operating System? Operating Systems, System Calls, and Buffered I/O. Academic Computers in 1983 and Operating System

Workshop on Inter Process Communication Solutions

Contents. Programming Assignment 0 review & NOTICE. File IO & File IO exercise. What will be next project?

CSC Systems Programming Fall Lecture - XXIII Final Review. Tevfik Koşar. Louisiana State University

Using make. Using Variables in Makefiles. Implicit Rules. Static vs Dynamic Libraries. Libraries. Tevfik Ko!ar

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

C: How to Program. Week /June/18

Systems Programming. 08. Standard I/O Library. Alexander Holupirek

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

A Client-Server Exchange

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

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

ECE 650 Systems Programming & Engineering. Spring 2018

CSE 333 SECTION 3. POSIX I/O Functions

LAB 13 FILE PROCESSING

Socket Programming. CSIS0234A Computer and Communication Networks. Socket Programming in C

Input / Output Functions

read(2) There can be several cases where read returns less than the number of bytes requested:

UNIX System Programming

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files. Compiler vs.

System- Level I/O. Andrew Case. Slides adapted from Jinyang Li, Randy Bryant and Dave O Hallaron

Standard C Library Functions

LAB 13 FILE PROCESSING

Introduction to Computer Programming Lecture 18 Binary Files

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Operating System Labs. Yuanbin Wu

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

Lecture 7: file I/O, more Unix

Chapter 8 File Processing

Lecture 8: Structs & File I/O

CS 351 Week 15. Course Review

CSC209 Review. Yeah! We made it!

Light & NOS. Dan Li Tsinghua University

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

All the scoring jobs will be done by script

ECE 435 Network Engineering Lecture 2

System-Level I/O April 6, 2006

Computer Programming: Skills & Concepts (CP) Files in C

Outline. Relationship between file descriptors and open files

System-Level I/O. Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O

CptS 360 (System Programming) Unit 17: Network IPC (Sockets)

628 Lecture Notes Week 4

EECS122 Communications Networks Socket Programming. Jörn Altmann

Interprocess Communication. Interprocess Communication

Naked C Lecture 6. File Operations and System Calls

Lecture 7. Followup. Review. Communication Interface. Socket Communication. Client-Server Model. Socket Programming January 28, 2005

Inter-Process Communication

Mode Meaning r Opens the file for reading. If the file doesn't exist, fopen() returns NULL.

File I/O. Arash Rafiey. November 7, 2017

Unix File and I/O. Outline. Storing Information. File Systems. (USP Chapters 4 and 5) Instructor: Dr. Tongping Liu

Linux Kernel Application Interface

Operating Systems. Lecture 07. System Calls, Input/Output and Error Redirection, Inter-process Communication in Unix/Linux

Process Creation in UNIX

System-Level I/O Nov 14, 2002

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments

Sockets. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

The Fundamentals. Port Assignments. Common Protocols. Data Encapsulation. Protocol Communication. Tevfik Ko!ar

CSci 4061 Introduction to Operating Systems. Input/Output: High-level

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #47. File Handling

Design Overview of the FreeBSD Kernel CIS 657

Figure 1 Ring Structures

CS307 Operating Systems Processes

Transcription:

Universidade de Coimbra Departamento de Engenharia Electrotécnica e de Computadores Sistemas Operativos - 2015/2016 Support Document N o 1 Files, Pipes, FIFOs, I/O Redirection, and Unix Sockets 1 Objectives The objective of this document is to present information concerning files, pipes, I/O Redirection, and Unix Sockets. 2 Material for Preparation [Robbins e Robbins, 2003, Chapter 4, UNIX I/O ; Chapter 5, Files and Directories ; Chapter 6, UNIX Special Files ; Chapter 18, Connection-Oriented Communication ; Chapter 20, Connectionless Communication and Multicast ; Appendix B, Restart Library ]. [Marshall, 1999, Input and Output (I/O): stdio.h ; Interprocess Communication (IPC), Pipes ; IPC: Sockets ]. [Stevens, 1990, Section 3.4 Pipes (pp. 102-109); Section 3.5 FIFOs (pp. 110-115); Chapter 6 Berkeley Sockets ]. [Kernighan e Ritchie, 1988, Chapter 7, Input and Output (pp. 151-168); Chapter 8, The Unix System Interface (pp. 169-189); Appendix B, Standard Library (pp. 241-258)]. [Araújo, 2014, UNIX Programming: Files and Pipes ]. Students that do not have knowledge of programming in C should overcome this situation. Reading the book [Kernighan e Ritchie, 1988] (ANSI C) is strongly recommended to obtain knowledge about C programming, and in particular about pointers and the main standard C functions (mathematical, string manipulation, and file manipulation). 3 Files in C and Unix In C, files are referred either by pointers or by (file) descriptors. The ISO C standard library I/O functions such as fopen(), fscanf(), fprintf(), fread(), fwrite(), and fclose() use pointers. The UNIX I/O functions such as open(), read(), write(), close(), and ioctl() use file descriptors. A file descriptor is an integer value and is a pointer to a table of file descriptors (Figure 1) that exists in the memory space of each program. This table is accessed through the use of certain functions that use the descriptor (e.g. int fp = open("myfile.c", O_RDONLY); ). Each entry in the file descriptors Sistemas Operativos - 2015/2016 - Support Document N o 1 - pág. 1/6

Figure 1: File descriptor table, system file table, inode table. Figure 2: Diagram of a pointer to a file after a fopen(). table is a reference for the System File Table that maintains a list of all the references to all the files that are being presently referenced and the present write or read position in each of these references to files. Each element of this table makes a reference the a In-memory inode table that maintains a list of all the active files in the system. Two entries in the System File Table may reference the same element of the inodes table, because one or more programs may have open the same file for reading and may be reading from different positions. Typically, if two programs are writing to the same file there are no guarantees that they are not overwriting one over the other. A pointer to a file is a pointer to a structure (Figure 2) called FILE that resides in the memory area of the program that may be directly accessed by the user (contrary to the area where the descriptors table resides which may no be directly accessed by the user). The FILE structure contains a buffer and Sistemas Operativos - 2015/2016 - Support Document N o 1 - pág. 2/6

a file descriptor. It is this file descriptor that is used to write to (and read from) disk. In some way the pointer to a file is a handle for a handle. When a program calls for example a function fprintf(myfp, "This is a test");, a message This is a test is not immediately written to disk. It is writen in the buffer of the FILE structure. When this buffer is full the I/O subsystem calls function write() with the file descriptor of the file, and only in this instant are the data writen to disk. A call to function fflush() forces the system to empty the buffer and to write its contents to disk, even if the buffer is not full. The chmod command can be used to change file access permissions. The permissions of files can be seen using the ls -l command, for example. The following Linux system calls can be used for file manipulation (see the suggested literature and the man pages): creat() - create a file. open() - specifies a file to be manipulated. close() - release file descriptor. read() - input a block of information from a file. write() - output a block of information to a file. lseek() - reposition read/write file offset. ioctl() - control device. unlink() - delete a name and possibly the file it refers to. The following C library functions can be used for file manipulation: fopen(), fclose(), fread(), fwrite(),fprintf(),fscanf(),feof(),fflush(),fsync(),fseek(),ftell(),rewind(),fgetpos(), fsetpos(). Observe the following example concerning the use of the write and read functionalities using the standard C library. #include <stdio.h> /* write_to_file() */ int write_to_file(){ int account; /* account number */ char name[30]; /* account name */ double balance; /* account balance */ FILE *cfptr; /* cfptr = clients.dat file pointer */ /* fopen opens file. Exit program if unable to create file */ if ( ( cfptr = fopen("clients.dat","w") ) == NULL ) { printf("file could not be opened\n"); }else{ /* end if */ printf("enter the account, name, and balance.\n"); printf("enter EOF to end input.\n"); printf("? "); scanf("%d%s%lf",&account,name,&balance); /* write account, name and balance into file with fprintf */ while (!feof( stdin ) ){ Sistemas Operativos - 2015/2016 - Support Document N o 1 - pág. 3/6

} fprintf(cfptr,"%d %s %.2f\n",account,name,balance); printf("? "); scanf("%d%s%lf",&account,name,&balance); } /* end while */ fclose( cfptr ); /* fclose closes file */ } /* end else */ return 0; /* indicates successful termination */ /* read_from_file() */ int read_from_file(){ int account; /* account number */ char name[30]; /* account name */ double balance; /* account balance */ FILE *cfptr; /* cfptr = clients.dat file pointer */ /* fopen opens file; exits program if file cannot be opened */ if ( ( cfptr = fopen( "clients.dat", "r" ) ) == NULL ) { printf( "File could not be opened\n" ); }else{ /* end if */ /* read account, name and balance from file */ printf( "%-10s%-13s%s\n", "Account", "Name", "Balance" ); fscanf( cfptr, "%d%s%lf", &account, name, &balance ); /* while not end of file */ while (!feof( cfptr ) ) { printf( "%-10d%-13s%7.2f\n", account, name, balance ); fscanf( cfptr, "%d%s%lf", &account, name, &balance ); } /* end while */ fclose( cfptr ); /* fclose closes the file */ } /* end else */ return 0; /* indicates successful termination */ } 4 Pipes an FIFOs A pipe is a unidirectional data channel that can be used for interprocess communication. It is created using the pipe() system call (cf. man pipe). int pipe(int filedes[2]) returns two file descriptors fd[0] and fd[1]. The data written to fd[1] can be read from fd[0] on a first-in-first-out (FIFO) basis. In many aspects FIFOs are similar to pipes, but there are also differences - study the suggested literature and the man pages. In a C program, named pipes (FIFOs) can be created with themknod() or mkfifo() functions (cf. man 2 mknod, man 3 mkfifo). In the shell, the commands mknod, and mkfifo (cf. man 1 mknod, man 1 mkfifo) can be used to create FIFOs. To communicate, processes write and read to a pipe or to a FIFO using the write() and read() system calls, respectively. If fifo01 is a FIFO that has already been created, then, in the shell, the command echo Hello FIFO! >fifo01 will write the string Hello FIFO! (and an ending newline character) to the fifo01, provided that the permissions of the FIFO are appropriately set. 5 I/O Redirection If a process performs input or output operations to some file descriptor, it is possible to redirect such operations to other file descriptors. This is called I/O redirection. The dup(), and dup2() system calls Sistemas Operativos - 2015/2016 - Support Document N o 1 - pág. 4/6

create a copy of a file descriptor and are useful to perform I/O redirection: int dup(int oldfd) - returns the lowest numbered unused descriptor for the new descriptor. int dup2(int oldfd, int newfd) - makes newfd be the copy of oldfd, closing newfd first if necessary. dup() and dup2() create a copy of file descriptor oldfd. After successful return of dup() or dup2(), the old and new descriptors may be used interchangeably. They share locks, file position pointers and flags; for example, if the file position is modified by using lseek() on one of the descriptors, the position is also changed for the other. See usage examples on the suggested literature. 6 Unix Sockets Sockets provide a very versatile point-to-point, two-way communication between two processes [Stevens, 1990], [Robbins e Robbins, 2003], [Marshall, 1999]. Sockets are a basic component of interprocess and intersystem communication. A socket is an endpoint of communication to which a name can be bound. It has a type and one or more associated processes. Sockets exist in communication domains or protocol families. A socket domain is an abstraction that provides an addressing structure and a set of protocols. Sockets connect only with sockets in the same domain. There are several socket domains (see <sys/socket.h>). The most used domains are the UNIX (AF_UNIX) and Internet (AF_INET) domains. The UNIX domain provides a socket address space on a single system. UNIX domain sockets are named with UNIX paths. Sockets can also be used to communicate between processes on different systems. The mostly used socket address space between connected systems is called the Internet domain. Internet domain communication uses the TCP/IP Internet protocol suite. In this course we will only deal with sockets in the UNIX domain. There are several types of sockets. In this Operating Systems course we will deal with sockets of type stream (SOCK_STREAM). See man 2 socket for more information on socket domains (protocol families) and socket types. There are several functions that are used by user processes to send or receive packets and to do other socket operations. For more information see their respective manual pages. socket() creates a socket, connect() connects a socket to a remote socket address, the bind() function binds a socket to a local socket address, listen() tells the socket that new connections shall be accepted, and accept() is used to get a new socket with a new incoming connection. socketpair() returns two connected anonymous sockets (only implemented for a few local families like AF_UNIX). send(), sendto(), and sendmsg() send data over a socket, and recv(), recvfrom(), recvmsg() receive data from a socket. poll() and select() wait for arriving data or a readiness to send data. In addition, the standard I/O operations like write(), writev(), sendfile(), read(), and readv() can be used to read and write data. getsockname() returns the local socket address and getpeername() returns the remote socket address. getsockopt() and setsockopt() are used to get or set socket layer or protocol options. ioctl() can be used to set or read some other options. close() is used to close a socket. shutdown() closes parts of a full-duplex socket connection. See the suggested literature (Sec. 2) and the manual pages for more information and usage examples. References [Araújo, 2014] Rui Araújo. Operating Systems. DEEC-FCTUC, 2014. [Kernighan e Ritchie, 1988] Brian W. Kernighan e Dennis M. Ritchie. The C Programming Language. Prentice-Hall, Inc, Englewood Cliffs, New Jersey, USA, Second ed., 1988. [Marshall, 1999] A. Dave Marshall. Programming in C: UNIX System Calls and Subroutines Using C. Cardiff University, UK, 1999. [Online]. Available: http://www.cs.cf.ac.uk/dave/c/. Sistemas Operativos - 2015/2016 - Support Document N o 1 - pág. 5/6

[Robbins e Robbins, 2003] Kay A. Robbins e Steven Robbins. Unix Systems Programming: Communication Concurrency, and Threads. Prentice Hall, Inc, Upper Saddle River, NJ, USA, 2003. [Stevens, 1990] W. Richard Stevens. UNIX Network Programming. Prentice-Hall, Inc, Englewood Cliffs, New Jersey, USA, First ed., 1990. Sistemas Operativos - 2015/2016 - Support Document N o 1 - pág. 6/6