POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide:...

11
POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O

Transcript of POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide:...

Page 1: POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.

POSIX: Files

Introduction to Operating Systems: Discussion 1

Read Solaris System Interface Guide:

Ch. 5.1 Basic File I/O

Page 2: 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

Page 3: POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.

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

Page 4: POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.

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

Page 5: POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.

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

Page 6: POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.

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

Page 7: POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.

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

Page 8: POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.

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

Page 9: POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.

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

Page 10: POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.

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

Page 11: POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.

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);}