POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide:...
-
Upload
lester-obrien -
Category
Documents
-
view
215 -
download
0
Transcript of POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide:...
POSIX: Files
Introduction to Operating Systems: Discussion 1
Read Solaris System Interface Guide:
Ch. 5.1 Basic File I/O
POSIX - Portable Operating System Interface
POSIX is a popular standard for Unix-like operating systems
POSIX is actually a collection of standards that cover system calls, libraries, applications and more…
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
POSIX and Unix
Most current Unix-like operating systems (OS) are POSIX compliant (or nearly so):
Linux, BSD, Solaris, AIX, IRIX 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
We will develop for the Solaris operating system The code we write should be portable to any POSIX OS
The Basic File I/O API
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
Finding Files in Unix
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
Opening a file
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
0
1
2
3
4
5
6
7
Open File Table
The open() call
#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>int open(const char *path, int flags, mode_t mode); char *path: contains the path name of the file to be opened
This is a C-string, not a C++ string object int flags: specifies the method of access
O_RDONLY, O_RDWR, O_WRONLY The flags are defined as constants in fcntl.h
The open() call
#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>int open(const char *path, int flags, mode_t mode); 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
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
read() and write()#include <fcntl.h>ssize_t read(int fd, void *buffer, size_t n);ssize_t write(int fd, const void *buffer, size_t n);
int fd: file descriptor that has been obtained from open() 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
size_t n: the maximum number of bytes that are to be read/written from/to the file
ssize_t is a data type (typedef long) defined in types.h This return value is the actual number of bytes read, or written
size_t is a data type (typedef unsigned long) defined in types.h
The close() call
Please make it a habit to close all files that you program has used as soon as you are done using them
#include <fcntl.h>
int close(int filedes); 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
Remember, closing resources timely can improve system performance and prevent deadlocks from happening
A simple example:#include <fcntl.h> /*controls file attributes*/#include <unistd.h>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);}