File Structure Related system calls

Slides:



Advertisements
Similar presentations
© Original by Donald Acton; Changes by George Tsiknis Unix I/O Related Text Sections: 2nd Ed: and st Ed: and
Advertisements

Operating system services Program execution I/O operations File-system manipulation Communications Error detection Resource allocation Accounting Protection.
NCHU System & Network Lab Lab 15 Record Locking. NCHU System & Network Lab Record Locking (1/4) What happens when two process attempt to edit the same.
January 13, Csci 2111: Data and File Structures Week1, Lecture 2 Basic File Processing Operations.
1 Files and Directories Hua LiSystems ProgrammingCS2690Files and Directories.
CS 311 – Lecture 09 Outline Introduction to Systems programming – System calls – Categories of system calls Error Management System calls File Handling.
Files. System Calls for File System Accessing files –Open, read, write, lseek, close Creating files –Create, mknod.
1 Advanced programming in UNIX 1 File I/O Hua LiSystems ProgrammingCS2690File I/O.
6/24/2015B.RamamurthyPage 1 File System B. Ramamurthy.
Files and Directories Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003Page 1 of 60.
7/15/2015B.RamamurthyPage 1 File System B. Ramamurthy.
CS 311 – Lecture 12 Outline File management system calls Stat() Directory Information  Opendir()  Readdir()  Closedir() Truncate() and remove() Lecture.
1 Unix File System API Operating System Hebrew University Spring 2007.
POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.
Chapter 39 Virtsualization of Storage: File and Directory Chien-Chung Shen CIS, UD
Week 12 - Friday.  What did we talk about last time?  Exam 2 post mortem  Low level file I/O.
1 UNIX System Programming v Objectives –look at how to program with directories –briefly describe the UNIX file system Directories and File System.
1 UNIX System Programming Files and Directories. 2 File Pointer Both read() and write() will change the file pointer. The pointer will be incremented.
File System Review bottomupcs.com J. Kubiatowicz, UC Berkeley.
UNIX Files File organization and a few primitives.
Files & File system. A Possible File System Layout Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved
Files and Directories File types stat functions for file information
Week 12 - Wednesday.  What did we talk about last time?  File I/O  Binary trees  Lab 11.
Project 6 Unix File System. Administrative No Design Review – A design document instead 2-3 pages max No collaboration with peers – Piazza is for clarifications.
Advanced Programming in the Unix Environment
Today’s topic Access and manipulate meta data for files –File type, ownership, access permissions, access time, etc How to determine if a file is not there?
CSCI 330 UNIX and Network Programming Unit VII: I/O Management I.
Laface 2007 File system 2.1 Operating System Design Filesystem system calls buffer allocation algorithms getblk brelse bread breada bwrite iget iput bmap.
1 Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library  5. Files and Directories 6. System Data.
NCHU System & Network Lab Lab 14 File and Directory.
CSE333 SECTION 3. Important Dates Jan 27 th – Homework 1 Due Feb 6 th – Midterm.
January 7, 2003Serguei Mokhov, 1 File I/O System Calls Reference COMP 229, 444, 5201 Revision 1.2 Date: July 21, 2004.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Tarek Abdelzaher Vikram Adve CS241 Systems Programming System Calls and I/O.
Library Functions The UNIX system provides a large number of C functions as libraries. Some of these implement frequently used operations, while others.
File I/O open close lseek read and write – unbuffered I/O dup and dup2.
CSCI 330 UNIX and Network Programming Unit VIII: I/O Management II.
OS interface: file and I/O system calls File operations in C/C++? –fopen(), fread(), fwrite(), fclose(), fseek() in C f.open(…), f.close(…) in C++ I/O.
File Subsystem in Linux by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Files Oct. 28, 2008 Topics Mapping file offsets to disk blocks File system buffering and you The directory hierarchy lecture-18.ppt “The course.
CSC 271 – Software I: Utilities and Internals An Introduction to File I/O in Linux Credited to Dr. Robert Siegfried and Beginning Linux Programming by.
Week 12 - Friday.  What did we talk about last time?  Exam 2 post mortem.
File table: a list of opened files Each entry contains: – Index: file descriptors – Pointer to the file in memory – Access mode File descriptor is a positive.
File System Design David E. Culler CS162 – Operating Systems and Systems Programming Lecture 23 October 22, 2014 Reading: A&D a HW 4 out Proj 2 out.
Week 12 - Thursday CS222.
Unix Programming Environment
Unix Files Course code: 10CS62 Prepared by Department of CSE
Practical Session 5.
Today topics: File System Implementation
Advanced Unix Programming
Systems Programming.
Week 12 - Wednesday CS222.
Chapter 4: System calls for the file system
LINUX programming Unit-3 PPT Slides
UNIX FILES
Operating System Hebrew University Spring 2004
An overview of the kernel structure
Unix Directories unix etc home pro dev motd snt unix ... slide1 slide2
File System B. Ramamurthy B.Ramamurthy 11/27/2018.
CSE 333 – Section 3 POSIX I/O Functions.
“The course that gives CMU its Zip!”
File I/O (1) Prof. Ikjun Yeom TA – Mugyo
File I/O (1) Prof. Ikjun Yeom TA – Hoyoun
Advanced UNIX progamming
CSE 333 – Section 3 POSIX I/O Functions.
Programming with UNIX File Systems (Chap 3, 4
Chien-Chung Shen CIS, UD
FILE I/O File Descriptors I/O Efficiency File Sharing
Operating System Hebrew University Spring 2009
Week 12 - Wednesday CS222.
Unix Directories unix etc home pro dev motd snt unix ... slide1 slide2
Presentation transcript:

File Structure Related system calls The file structure related system calls available in the UNIX system. let you create, open, and close files, read and write files, randomly access files, alias and remove files, get information about files, check the accessibility of files, change protections, owner, and group of files, and control devices.

These operations either use a character string that defines the absolute or relative path name of a file, or a small integer called a file descriptor that identifies the I/O channel. The kernel presents and accepts data from the channel as a process reads and writes that channel.

File Descriptors Unix processes name I/O and IPC objects by integers known as file descriptors. A file descriptor is a small integer that is used to identify a file that has been opened for I/O. Each process is allowed a fixed number of file descriptors. Many programs associate the file descriptors 0, 1 & 2 with the standard input, standard output and standard error of a process. (i.e.) -File descriptor 0 is standard input. -File descriptor 1 is standard output. -File descriptor 2 is standard error.

CONSTANT DESCRIPTION O_RDONLY -Open for Reading only O_WRONLY -Open for Writing only O_RDWR -Open for Reading and Writing O_APPEND -Append to the end of file on each write O_CREAT -Create the file if it doesn’t exist O_EXCL -Generate an error if O_CREAT function is specified and the file exists O_TRUNC -If file exists, successfully opened for R or W, truncate its length to 0

Initial Permissions: When we create a file using the O_CREAT flag with open, we must use the three parameter form. Filename, mode, the third parameter, is made from a bitwise OR of the flags defined in the header file sys/stat.h , These are: S_IRUSR Read permission, owner. S_IWUSR Write permission, owner. S_IXUSR Execute permission, owner. S_IRGRP Read permission, group. S_IWGRP Write permission, group. S_IXGRP Execute permission, group. S_IROTH Read permission, others. S_IWOTH Write permission, others. S_IXOTH Execute permission, others

open Function A file is opened or created by calling the open function.

creat Function A new file can also be created by calling the creat function.

Close function An open file is closed by calling the close function.

How to use create():

Lseek function All open files have a "file pointer" associated with them to record the current position for the next file operation When the file is opened, the file pointer points to the beginning of the file. After reading/write m bytes, the file pointer moves m bytes forward . The lseek system call sets the read/write pointer of a file descriptor, fd, i.e. you can use it to set where in the file the next read or write will occur. We can set the pointer to an absolute location in the file or to a position relative to the current position or the end of file.

The 'whence' variable of lseek specifies how the seek is to be done from the beginning of the file from the current value of the pointer, or from the end of the file The return value is the offset of the pointer after the lseek The offset parameter is used to specify the position and the whence parameter specifies how the offset is used. whence can be one of the following: SEEK_SET   offset is an absolute position SEEK_CUR   offset is relative to the current position SEEK_END   offset is relative to the end of the file lseek returns the offset measured in bytes from the beginning of the file that the file pointer is set to, or -1 on failure. The type off_t, used for the offset in seek operations, is an implementation-dependent type defined in sys/types.h.

How to work lseek()

Read function Data is read from an open file with the read function.

To read "size" bytes from the file specified by "fd“ into the memory location pointed to by "buf". It returns many bytes were actually read (why?) 0 : at end of the file < size : fewer bytes are read to the buffer (why?) == size : read the specified # of bytes Things to be careful about buf needs to point to a valid memory location with length not smaller than the specified size Otherwise, what could happen? fd should be a valid file descriptor returned from open() to perform read operation

write Function Data is written to an open file with the write function.

int write(int fd, char *buf, int size) writes the bytes stored in buf to the file specified by fd It returns the number of bytes actually written, which is usually “size” unless there is an error Things to be careful about buf needs to be at least as long as specified by “size”The file needs to be opened for write operations.

how to use open(), lseek(),read()and write():

dup and dup2 Functions An existing file descriptor is duplicated by either of the following functions.

The new file descriptor returned by dup is guaranteed to be the lowest-numbered available filedescriptor. With dup2, we specify the value of the new descriptor with the filedes2 argument. If filedes2 is already open, it is first closed. If filedes equals filedes2, then dup2 returns filedes2 without closing it.

Another way to duplicate a descriptor is with the fcntl function Another way to duplicate a descriptor is with the fcntl function. Indeed, the call dup(filedes); is equivalent to fcntl(filedes, F_DUPFD, 0); Similarly, the call dup2(filedes, filedes2); close(filedes2); fcntl(filedes, F_DUPFD, filedes2);

sync, fsync, and fdatasync Functions Traditional implementations of the UNIX System have a buffer cache or page cache in the kernel through which most disk I/O passes. When we write data to a file, the data is normally copied by the kernel into one of its buffers and queued for writing to disk at some later time. This is called delayed write.

To ensure consistency of the file system on disk with the contents of the buffer cache, the sync, fsync, and fdatasync functions are provided.

fcntl Function The fcntl function can change the properties of a file that is already open.

The fcntl function is used for five different purposes. 1. Duplicate an existing descriptor (cmd = F_DUPFD) 2. Get/set file descriptor flags (cmd = F_GETFD or F_SETFD) 3. Get/set file status flags (cmd = F_GETFL or F_SETFL) 4. Get/set asynchronous I/O ownership (cmd = F_GETOWN or F_SETOWN) 5.Get/set record locks (cmd = F_GETLK, F_SETLK, or F_SETLKW)

stat, fstat, and lstat Functions There are three stat functions available.

The fstat system call returns status information about the file associated with an open file descriptor. The information is written to a structure,buf, the address of which is passed as a parameter. The related functions stat and lstat return status information for a named file. They produce the same results, except when the file is a symbolic link. lstat returns information about the link itself, while stat returns information about the file that the link refers to.

struct stat { mode_t st_mode; /. file type & mode (permissions) struct stat { mode_t st_mode; /* file type & mode (permissions) */ ino_t st_ino; /* i-node number (serial number) */ dev_t st_dev; /* device number (file system) */ dev_t st_rdev; /* device number for special files */ nlink_t st_nlink; /* number of links */ uid_t st_uid; /* user ID of owner */ gid_t st_gid; /* group ID of owner */ off_t st_size; /* size in bytes, for regular files */ time_t st_atime; /* time of last access */ time_t st_mtime; /* time of last modification */ time_t st_ctime; /* time of last file status change */ blksize_t st_blksize; /* best I/O block size */ blkcnt_t st_blocks; /* number of disk blocks allocated */ };

access Function When we open a file, the kernel performs its access tests based on the effective user and group IDs. There are times when a process wants to test accessibility based on the real user and group IDs.

umask function There are nine permission bits associated with every file, we can describe the file mode creation mask that is associated with every process. The umask function sets the file mode creation mask for the process and returns the previous value.

#include <sys/stat.h> mode_t umask(mode_t cmask); Returns: previous file mode creation mask st_mode mask Meaning S_IRUSR user-read S_IWUSR user-write S_IXUSR user-execute S_IRGRP group-read S_IWGRP group-write S_IXGRP group-execute S_IROTH other-read

chmod and fchmod Functions These two functions allow us to change the file access permissions for an existing file.

Stick bit(S_ISVTX) the sticky bit is an access-right flag that can be assigned to files and directories on Unix systems When set, it instructed the operating system to retain the text segment of the program in swap space after the process exited. This speeds up subsequent executions by allowing the kernel to make a single operation of moving the program from swap to real memory. Thus, frequently-used programs like editors would load noticeably faster.

SYMBOLIC LINKS A symbolic link is an indirect pointer to a file, unlike hard links. No file system limitations on a symbolic link. Any user can create a symbolic link, unlike hard links where superuser can only create them. Symbolic links were introduced with 4.2BSD and subsequently supported by SVR4.

symlink and readlink Functions

File Times Three time fields are maintained for each file. Their purpose is summarized in below table.

utime Function The access time and the modification time of a file can be changed with the utime function.

The structure used by this function is struct utimbuf { time_t actime; /* access time */ time_t modtime; /* modification time */ } The two time values in the structure are calendar times, which count seconds. The operation of this function, and the privileges required to execute it, depend on whether the times argument is NULL.

DIRECTORY HANDLING SYSTEM CALLS mkdir and rmdir Functions Directories are created with the mkdir function and deleted with the rmdir function.

A common mistake is to specify the same mode as for a file: read and write permissions only. But for a directory, we normally want at least one of the execute bits enabled, to allow access to filenames within the directory. The user ID and group ID of the new directory are established according to the rules .

An empty directory is deleted with the rmdir function. Recall that an empty directory is one that contains entries only for dot and dot-dot.

Reading Directories Directories can be read by anyone who has access permission to read the directory. But only the kernel can write to a directory, to preserve file system . The actual format of a directory. depends on the UNIX System implementation and the design of the file system.

The dirent structure defined in the file <dirent The dirent structure defined in the file <dirent.h> is implementation dependent. Implementations define the structure to contain at least the following two members: struct dirent { ino_t d_ino; /* i-node number */ char d_name[NAME_MAX + 1]; /* null-terminated filename */ }

chdir, fchdir, and getcwd Functions Every process has a current working directory When a user logs in to a UNIX system, the current working directory normally starts at the directory specified by the sixth fieldin the /etc/passwd filethe user's home directory. The current working directory is an attribute of a process; the home directory is an attribute of a login name. We can change the current working directory of the calling process by calling the chdir or fchdir functions.

DEVICE SPECIAL FILES The two fields st_dev and st_rdev are often confused. Every file system is known by its major and minor device numbers, which are encoded in the primitive system data type dev_t. The major number identifies the device driver and sometimes encodes which peripheral board to communicate with; the minor number identifies the specific subdevice.

We can usually access the major and minor device numbers through two macros defined by most implementations: major and minor The st_dev value for every filename on a system is the device number of the file system containing that filename and its corresponding i-node. •Only character special files and block special files have an st_rdev value. This value contains the device number for the actual device.