Download presentation
Presentation is loading. Please wait.
Published byLester O’Brien’ Modified over 9 years ago
1
POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O
2
POSIX - Portable Operating System Interface u POSIX is a popular standard for Unix-like operating systems u POSIX is actually a collection of standards that cover system calls, libraries, applications and more… u POSIX 1003.1 defines the C language interface to a Unix-like kernel A kernel is the part of an operating system which is always resident in memory handles low level resource management tasks
3
POSIX and Unix u Most current Unix-like operating systems (OS) are POSIX compliant (or nearly so): Linux, BSD, Solaris, AIX, IRIX u While an understanding of each operating system’s design is necessary to fully utilize its API, most functions work almost identically on any compliant operating system u We will develop for the Solaris operating system The code we write should be portable to any POSIX OS
4
The Basic File I/O API u While various programming languages provide specific means of accessing files, every POSIX compliant OS must implement the following basic file I/O functions creat() open() close() read() write() ioctl() Read more about these functions in the Solaris System Interface Guide at http://docs.sun.com/app/docs/doc/806-4750 http://docs.sun.com/app/docs/doc/806-4750
5
Finding Files in Unix u To use the Basic File I/O API, we must be able to uniquely identify the file to be opened. We use A relative path, describing a path through the file system tree starting with the current working directory OR An absolute path, describing a path starting at the root directory of the file system When ever a path is mentioned in the discussion that follows, it can be either relative or absolute
6
Opening a file u When a file is opened, the OS: Checks to make sure the opener has permission to access the file Adds an entry in the process' open file table for the given file This entry contains information about where the file is stored on disk, it's size, where the file pointer is currently located, etc. Returns the offset in the open file table associated with that file In the picture to the right, the open call returns 3 for "My_file" stdin stdout stderr My_file 0123456701234567 Open File Table
7
The open() call #include int open(const char *path, int flags, mode_t mode); u char *path: contains the path name of the file to be opened This is a C-string, not a C++ string object u int flags: specifies the method of access O_RDONLY, O_RDWR, O_WRONLY The flags are defined as constants in fcntl.h
8
The open() call #include int open(const char *path, int flags, mode_t mode); u mode_t mode: this optional parameter is used to set the access permissions upon file creation the mode_t struct is defined in types.h Mode constants are defined in stat.h u The return value is the index in the open file table of the newly opened file. All subsequent interaction with that file (read, write, close) is specified using this integer
9
read() and write() #include ssize_t read(int fd, void *buffer, size_t n); ssize_t write(int fd, const void *buffer, size_t n); u int fd: file descriptor that has been obtained from open() u void *buffer: In read, a pointer to the address in memory where the data read from the file will be stored In write, a pointer to the address in memory where the data written to the file is currently stored u size_t n: the maximum number of bytes that are to be read/written from/to the file u ssize_t is a data type (typedef long) defined in types.h This return value is the actual number of bytes read, or written u size_t is a data type (typedef unsigned long) defined in types.h
10
The close() call u Please make it a habit to close all files that you program has used as soon as you are done using them #include int close(int filedes); u Note that filedes is the offset in the open file table of the file you want closed It is the integer returned by the open system call when the file was first opened u Remember, closing resources timely can improve system performance and prevent deadlocks from happening
11
A simple example: #include /*controls file attributes*/ #include main() { int fd; /* a file descriptor */ ssize_t nread;/* number of bytes read */ char buf[1024];/* data buffer */ /* open the file "data" for reading */ fd = open("data", O_RDONLY); /* read in the data */ nread = read(fd, buf, 1024); /* close the file */ close(fd);}
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.