Computer Science & Engineering Department I. I. T. Kharagpur

Similar documents
Computer Science & Engineering Department I. I. T. Kharagpur

Lecture 23: System-Level I/O

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

All the scoring jobs will be done by script

UNIX System Calls. Sys Calls versus Library Func

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

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

All the scoring jobs will be done by script

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

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

1 Requesting for Memory

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

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

Operating System Labs. Yuanbin Wu

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

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

CSC 271 Software I: Utilities and Internals

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

Process Creation in UNIX

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

CS 33. Shells and Files. CS33 Intro to Computer Systems XX 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

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

CSC209H Lecture 6. Dan Zingaro. February 11, 2015

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

Contents. NOTICE & Programming Assignment #1. QnA about last exercise. File IO exercise

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

File Descriptors and Piping

Process Management 1

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

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

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

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

628 Lecture Notes Week 4

Process Management! Goals of this Lecture!

CS 3113 Introduction to Operating Systems Midterm October 11, 2018

CS 3113 Introduction to Operating Systems Midterm October 11, 2018

15-213/18-243, Spring 2011 Exam 2

Inter-Process Communication

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

Recitation 8: Tshlab + VM

Input and Output System Calls

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

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

File I/0. Advanced Programming in the UNIX Environment

Process Management! Goals of this Lecture!

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

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING SOFTWARE ENGINEERING DEPARTMENT

File Input and Output (I/O)

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

Week 2 Intro to the Shell with Fork, Exec, Wait. Sarah Diesburg Operating Systems CS 3430

ESE 333 Real-Time Operating Systems 2 What is an operating system? Two views: 1. Top-down view: Extended machine ffl Covers the details of the hardwar

Processes. Processes (cont d)

Final Precept: Ish. Slides Originally Prepared by: Wonho Kim

System- Level I/O. CS 485 Systems Programming Fall Instructor: James Griffioen

Operating System Labs. Yuanbin Wu

CSC209H Lecture 7. Dan Zingaro. February 25, 2015

CS240: Programming in C

Chapter 3 Processes we will completely ignore threads today

COE518 Lecture Notes Week 2 (Sept. 12, 2011)

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

CSE 333 SECTION 3. POSIX I/O Functions

Preview. System Call. System Call. System Call. System Call. Library Functions 9/20/2018. System Call

Operating Systems 2230

Figure 1 Ring Structures

UNIX System Programming

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

Interacting with Unix

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

Introduction to Computer Systems , fall th Lecture, Oct. 19 th

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

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

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

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

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

Goals of this Lecture

Matt Ramsay CS 375 EXAM 2 Part 1

I/O Management! Goals of this Lecture!

CS Operating system Summer Midterm I -- July 11, 2017 You have 115 min (10:00am-11:55pm). Good Luck!

Processes COMPSCI 386

I/O Management! Goals of this Lecture!

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

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

Interprocess Communication E. Im

Naked C Lecture 6. File Operations and System Calls

CSI 402 Lecture 2 Working with Files (Text and Binary)

System Programming. Pipes I

Outline. Relationship between file descriptors and open files

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

1 Architectural Support

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

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

15213 Recitation Section C

518 Lecture Notes Week 3

CSC 1600 Unix Processes. Goals of This Lecture

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

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

CS240: Programming in C

Logistics. Recitation 11: I/O Problems. Today s Plan. Why Use Robust I/O. Andrew Faulring Section A 18 November 2002

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

CSE 333 SECTION 3. POSIX I/O Functions

Transcription:

Computer Science & Engineering Department I. I. T. Kharagpur Operating System: CS33007 3rd Year CSE: 5th Semester (Autumn 2006-2007) Lecture III (Linux System Calls II) Goutam Biswas Date: 1st-7th August, 2006 1 File Access Calls 1. In Unix like systems I/O devices are viewed as files (abstraction). Both files and devices can be accessed using system calls related to file I/O. 2. Whenever a process is created, three standard files are opened for it. They are stdin, stdout and stderr. 3. Three data structures are of interest for open files of a process on Linux (Unix like system). These are file descriptor table (one per process), file table (global for the whole system), in-core inode 1 table (global table, one entry per file). 4. Open files are accessed in the process using the index of the file descriptor table. The indices for the standard files are 0 (stdin), 1 (stdout) and 2 (stderr). 5. A new entry is made, in the lowest available slot of the file descriptor table, when a file is opened. A new entry is also made in the file table. If there is already an entry in the inode table for the file, its usage count is increased; otherwise a new entry is made and loaded with data from the disk inode of the file. 6. The file descriptor table of the parent process is copied to the child process when it is created. 7. open() - opens the file specified by the path and returns the file descriptor, the index of the lowest available slot in the file descriptor table. /*************************************** * open() system call ***************************************/ 1 Inode or index node is the object by which a file is identified in a Unix like system. The inode for a file resides on the disk. When a file is opened, it is copied to the memory. The memory copy of the inode has other fields than the disk copy e.g. usage count etc. 1

printf("stdin: %d\n", STDIN_FILENO) ; printf("stdout: %d\n", STDOUT_FILENO) ; printf("stderr: %d\n", STDERR_FILENO) ; fd = open("./outfile", O_WRONLY O_CREAT, 0666) ; printf("./outfile: %d\n", fd) ; 8. Code with the software interrupt. /*************************************** * open() with software interrupt ***************************************/ printf("stdin: %d\n", STDIN_FILENO) ; printf("stdout: %d\n", STDOUT_FILENO) ; printf("stderr: %d\n", STDERR_FILENO) ; asm volatile ( "movl $3, %%eax\n\t" "int $0x80\n\t" :"=a" (fd) :"b" ("./outfile"), "c" (O_WRONLY O_CREAT), "d" (0666) ) // fd = open("./outfile", O_WRONLY O_CREAT, 0666) ; printf("./outfile: %d\n", fd) ; 9. close() - closes an open file descriptor. After close the descriptor does not refer to any file and can be reused. /********************************** 2

* close() **********************************/ close(stdin_fileno) ; // Closes stdin perror("close") ; // System error message fd = open("./infile", O_RDONLY, 0) ; // Opens infile in printf("./infile: %d\n", fd) ; // the slot of stdin 10. read() - reads the number of bytes specified (maximum) from an open file specified by the file descriptor. /************************************** * read() system call **************************************/ #define MAX 101 int charread ; charread = read(stdin_fileno, buff, MAX-1) ; buff[charread] = \0 ; printf("stream read: %s\n", buff) ; 11. Close stdin, open a file, and use scanf to read from the file. /************************************** * close() system call **************************************/ 3

#define MAX 101 close(stdin_fileno) ; perror("close") ; fd = open("./infile", O_RDONLY, 0) ; perror("open") ; printf("\t\tfile descriptor: %d\n", fd) ; scanf("%[^\n]", buff); printf("stream read:\n%s\n", buff) ; 12. When a child process is created, the file descriptor table of the parent is copied to the file descriptor table of the child. Both are pointing to the same entry of the global file table. Both the process see the same offset values in the file. /**************************************************** * The file table of the parent is copied to the * * child: cc -Wall filedescch.c * * **************************************************/ #define MAX 1000 int main() { int chpid, fd ; fd = open("./infile", O_RDONLY, 0) ; if((chpid = fork())!= 0) { // Parent read(fd, buff, 10) ; buff[10] = \0 ; printf("parent: %s\n", buff) ; else { // Child 4

read(fd, buff, 15) ; buff[15] = \0 ; printf("child: %s\n", buff) ; 13. lseek() - sets the read/write offset in the open file (specified by the file descriptor). /*********************************** * lseek() system call ***********************************/ #define MAX 100 int fd, offset ; fd = open("./infile", O_RDONLY, 0) ; perror("open") ; read(fd, buff, 40) ; buff[40] = \0 ; printf("1st line./infile: %s\n", buff) ; offset = lseek(fd, 10, SEEK_SET) ; // From the beginning perror("lseek") ; read(fd, buff, 40) ; buff[40] = \0 ; printf("after offset: %s\n", buff) ; 14. One can open the same file more than once, simultaneously in a program. There will be two descriptors and two entries in the global file table. The usage count of the in-memory inode table entry will be 2. /*********************************** 5

* opening a file more than once ***********************************/ #define MAX 100 int fd1, fd2, offset ; fd1 = open("./infile", O_RDONLY, 0) ; perror("open") ; fd2 = open("./infile", O_RDONLY, 0) ; perror("open") ; printf("\t\t\tfd1: %d, fd2: %d\n", fd1, fd2) ; read(fd1, buff, 20) ; buff[20] = \0 ; printf("\nfd1: 1st read from./infile: %s\n", buff) ; offset = lseek(fd2, 41, SEEK_CUR) ; // From current pos perror("lseek") ; read(fd2, buff, 10) ; buff[10] = \0 ; printf("\n\t\t\tfd2: 1st read: %s\n", buff) ; read(fd1, buff, 10) ; buff[10] = \0 ; printf("\nfd1: 2nd read from./infile: %s\n", buff) ; 15. The maximum offset that the system call lseek() can produce is ±(2 31 1). This is insufficient for a disk of size more than 4-GB. A long long version of the function llseek() is provided. /******************************************* * llseek() can produce long long offset * Only root can execute this code *******************************************/ #include <linux/unistd.h> 6

#define SECT 512 long long unsigned offset = 0x123456789ABCDEF0LLU; int fd, i, dat ; char buff[sect] ; fd = open("/dev/hda", O_RDONLY, 0) ; read(fd, buff, SECT) ; for(i=0; i<sect; ++i) { if(i%20 == 0) printf("\n") ; dat = 0x000000FF & (int)buff[i] ; printf("%x ", dat) ; llseek(fd, offset, SEEK_CUR) ; printf("\noffset: %llu\n", offset) ; 16. dup(), dup2() - duplicates a file descriptor of an open file. Both the descriptors points to the same entry of global file table. Change in offset in one descriptor also reflected in the copied one. /********************************************* * dup() system call *********************************************/ #include <stdlib.h> #define MAX 10 char buff[max+1] ; fd = dup(stdin_fileno) ; perror("dup") ; printf("new fd for stdin: %d\n", fd) ; printf("\t\tenter an integer: ") ; fflush(stdout) ; read(fd, buff, MAX) ; // Read from STDIN 7

buff[max] = \0 ; printf("\t\tdata: %d\n", atoi(buff)) ; 17. File can be used for communication between processes: /*********************************** * File descriptor (open file) * after execve * cc -Wall fdexec1.c *./a.out factorial ***********************************/ #include <stdlib.h> #include <sys/wait.h> #define MAX 10 int main(int argc, char *argv[], char *envp[]) { int n, chpid, status, fd ; char buff[max], **agv = argv+1; printf("enter a +ve integer: ") ; scanf("%d", &n) ; sprintf(buff, "%d", n) ; fd = open("./fdexec1_io", O_WRONLY O_CREAT, 0666) ; perror("open W") ; write(fd, buff, MAX) ; close(fd) ; perror("close") ; fd = open("./fdexec1_io", O_RDONLY, 0) ; perror("open R") ; if((chpid = fork())!= 0) { // Parent printf("\t\tinside Parent\n") ; close(fd) ; perror("close P") ; waitpid(chpid, &status, 0) ; else execve(agv[0], agv, envp) ; 8

The executable required for exec - /************************************************ * Factorial: cc -Wall factorial.c -o factorial * * **********************************************/ #include <stdlib.h> #define MAX 10 int main() { int n, i, fact = 1 ; read(3, buff, MAX) ; perror("\t\t\tread in factorial") ; n = atoi(buff) ; for(i=1; i<=n; ++i) fact *=i ; printf("\t\t\t%d! = %d\n", n, fact) ; 18. File status can be accessed using the system call fstat() /********************************************** * File meta-data: fstat1.c * * cc -Wall fstat1.c * *./a.out <filename> * * ********************************************/ #include <stdlib.h> #include <time.h> int main(int count, char *vect[]) { struct stat buff ; struct tm *ltmp ; 9

if(count < 2) { printf("file name not there\n") ; exit(0) ; fd = open(vect[1], O_RDONLY) ; fstat(fd, &buff) ; printf("\tdevice: (%d, %d)\n", major(buff.st_dev), minor(buff.st_dev)) ; printf("\tinode: %d\n", (int) buff.st_ino) ; printf("\tprotection: %o\n", (int) buff.st_mode) ; printf("\thard Links: %d\n", (int) buff.st_nlink) ; printf("\tuser ID: %d\n", (int) buff.st_uid) ; printf("\tgroup ID: %d\n", (int) buff.st_gid) ; printf("\tfile Size: %d\n", (int) buff.st_size) ; printf("\tnumber of Blocks: %d\n", (int) buff.st_blocks) ; ltmp = localtime(&buff.st_atime) ; printf("\tdate & time: %2d/%2d/%4d:%2d:%2d:%2d\n", ltmp -> tm_mday, ltmp -> tm_mon, 1900 + ltmp -> tm_year, ltmp -> tm_hour, ltmp -> tm_min, ltmp -> tm_sec) ; References [1] http://asm.sourceforge.net//syscall.htmli#3 [2] http://www-128.ibm.com/developerworks/library/l-ia.html#h1 10