Date post: | 11-Dec-2015 |
Category: |
Documents |
Upload: | don-baraka-daniel |
View: | 213 times |
Download: | 0 times |
Types of files File I/O related calls File statistic related calls File permission related calls
ICS 2305 May-Aug 2013 2
A non negative number – identifies file to the kernel
File descriptor table – stores fd’s for various files opened by a process
A process must identify a file via a for any kernel service i.e. read/write
ICS 2305 May-Aug 2013 3
Anything that can be written/read from should be kernel identifiable
Processes have three standard file descriptors
0 standard input stream of a process 1 standard output stream of a process 2 standard error stream of a process
File descriptors may refer to any UNIX file type
Everything that can be written/read from is treated like a file
Regular file –contains data Directory – contains listing of files,
written to by kernel Block file – block devices Character file – special devices Socket – network communication file Named pipe (fifo) – interprocess
communication file Symbolic link – points to another file
ICS 2305 May-Aug 2013 7
Process will receive a FD from the kernel when: 1. It opens a file 2. It creates a file 3. Creates/opens any file type e.g.
establishing a network connection, creating a pipe
include<fcntl.h> defines arguments passed to the open and fcntl functions
Receives at least two parameters: int open( const char *pathname, int oflag, ... /* mode_t mode */ );
ICS 2305 May-Aug 2013 12
1. const char *pathname - path to file
2. int oflag – open flag, indicates one of three open modes O_RDONLY – open for reading O_WRONLY open for writing O_RDWR open for read and write
The function requires at least two arguments
ICS 2305 May-Aug 2013 14
O_APPEND append O_CREAT creates file (if !exist) O_TRUNC truncates O_EXCL generates error if
O_CREAT is specified and the file exists
O_NONBLOCK open in nonblocking mode
ICS 2305 May-Aug 2013 15
int fileDescriptor; char* path =
“/home/jamesmm/docs/list.txt”; fileDescriptor = open(path,
O_RDONLY);
ICS 2305 May-Aug 2013 16
Receives file descriptor of file to close
int close(fd); Returns 0 if ok -1 if error Releases locks on the file
include <unistd.h> defines symbolic constants and types
ssize_t read(int filedes, void *buf, size_t nbytes);
ICS 2305 May-Aug 2013 18
If read was successful - returns number of bytes read
If at end of file (eof) – returns zero
If an error was encountered – returns one
Receives three parameters 1. A file descriptor 2. A character buffer – to store
bytes read 3. Number of bytes to read from file
Declare necessary variables • char buffer[256]; • ssize_t bytesRead; • size_t numBytes; numBytes =
sizeof(buffer);
bytes_read = read(fd, buffer, numBytes);
ICS 2305 May-Aug 2013 21
#include<unistd.h> ssize_t write(int fd, const void
*buff, size_t bytes); If successful - returns bytes
written Returns -1 if unsuccessful
ICS 2305 May-Aug 2013 22
System programs may need to get file attributes • File owner • Permissions • Date created • Date last modified etc
Stat functions -get file attributes The ls command – implemented using
stat functions
ICS 2305 May-Aug 2013 25
Can be done via three related system calls
stat fstat lstat All require include of <sys/types.h> <sys/stat.h>
When file is a symbolic link it will return information about the link
int lstat(const char *pathname, struct stat *buf);