+ All Categories
Home > Documents > Lecture 07

Lecture 07

Date post: 11-Dec-2015
Category:
Upload: don-baraka-daniel
View: 213 times
Download: 0 times
Share this document with a friend
Description:
system programming lecture
Popular Tags:
32
ICS 2105 Lecture 7 By James Murithi
Transcript

ICS 2105 Lecture 7 By

James Murithi

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

Reading/writing to a file – you must identify file to kernel by its FD

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

Open() Create() Socket() Pipe() … etc

Read() Write() Chown() Chmod()

dirfd – get fd for directory stream fileno – get fd for a file Fyi - do man on these system calls

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

If successful – returns the file descriptor

Returns -1 if unsuccessful

ICS 2305 May-Aug 2013 13

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

int fd – file descriptor const void *buff – message to write size_t bytes – bytes to write

char msg[] = “Some text"; ssize_t bytesWritten; bytesWritten =

write(fd,buff,sizeof(msg));

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>

All three functions return 0 if successful -1if unsuccessful

All three functions receive a pointer to a structure

The structure is filled with file attributes

Requires a path to the file int stat(const char *path, struct

stat *buf);

ICS 2305 May-Aug 2013 29

When file is a symbolic link it will return information about the link

int lstat(const char *pathname, struct stat *buf);

Requires file to be open – receives a file descriptor

int fstat(int filedes, struct stat *buf);

ICS 2305 May-Aug 2013 32


Recommended