* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download D00_Files
Security-focused operating system wikipedia , lookup
Library (computing) wikipedia , lookup
MTS system architecture wikipedia , lookup
Plan 9 from Bell Labs wikipedia , lookup
Windows NT startup process wikipedia , lookup
Commodore DOS wikipedia , lookup
Burroughs MCP wikipedia , lookup
Spring (operating system) wikipedia , lookup
Computer file wikipedia , lookup
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 is the part of an operating system which 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() Read more about these functions in the Solaris close() System Interface Guide at http://docs.sun.com/app/docs/doc/806-4750 read() write() ioctl() 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 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 Open File Table a file is opened, the OS: In the picture to the right, the open call returns 3 for "My_file" 0 stdin 1 stdout 2 stderr 3 My_file 4 5 6 7 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: 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 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 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);}