Download presentation
Presentation is loading. Please wait.
Published byFlora Francis Modified over 8 years ago
1
Files in UNIX u UNIX deals with two different classes of files: Special Files Regular Files u Regular files are just ordinary data files on disk - something you have used all along when you studied programming! u Special files are abstractions of devices. UNIX deals with devices as if they were regular files. u The interface between the file system and the device is implemented through a device driver - a program that hides the details of the actual device.
2
Special files u UNIX distinguishes two types of special files: Block Special Files represent a device with characteristics similar to a disk. The device driver transfers chunks or blocks of data between the operating system and the device. Character Special Files represent devices with characteristics similar to a keyboard. The device is abstracted by a stream of bytes that can only be accessed in sequential order.
3
Access Primitives u UNIX provides access to files and devices through a (very) small set of basic system calls (primitives) creat() open() close() read() write() ioctl()
4
the open() call #include int open(const char *path, int flags, [mode_t mode]); u char *path: is a string that contains the fully qualified filename of the file to be opened. u int flags: specifies the method of access i.e. read_only, write_only read_and_write. u mode_t mode: optional parameter used to set the access permissions upon file creation.
5
read() and write() #include ssize_t read(int filedes, void *buffer, size_t n); ssize_t write(int filedes, const void *buffer, size_t n); u int filedes: file descriptor that has been obtained though an open() or create() call. u void *buffer: pointer to an array that will hold the data that is read or holds the data to be written. u size_t n: the number of bytes that are to be read/written from/to the file.
6
A close() call u Although all open files are closed by the OS upon completion of the program, it is good programming style to “clean up” after you are done with any system resource. u Please make it a habit to close all files that you program has used as soon as you don’t need them anymore! #include int close(int filedes); u Remember, closing resources timely can improve system performance and prevent deadlocks from happening (more later)
7
A rudimentary 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);}
8
Pipes u File-like constructs which are memory resident conduits of information sent from one process to another u Unnamed pipes are created by using the pipe() routine int pipe( int fd[]) u The array fd contains two elements fd[0] is a descriptor for the read end of the pipe fd[1] is a descriptor for the write end of the pipe
9
Working with pipes u The pipe descriptor can be treated much like a file descriptor Closing a pipe puts eof in the data stream Reading from a closed pipe returns zero Writing to a closed pipe may cause process termination u Unnamed pipes are generally only useful for communication between parent and child processes which continue to run the same program
10
A pipe example #include char msg[100]; char crecv[100]; int fd[2], numbytes, bytesread; main(){ pipe(fd); printf("Enter message: "); scanf("%s",msg); numbytes = strlen(msg);
11
A pipe example if(fork() == 0) {close(fd[0]); printf("In child, sending to parent...\n"); write(fd[1], msg, numbytes + 1); close(fd[1]); } else {close(fd[1]); bytesread = read(fd[0], crecv, 100); printf(Received: %s-%d bytes\n", crecv, bytesread); close(fd[0]); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.