16.05.2015 Views

Working with the Unix OS

Working with the Unix OS

Working with the Unix OS

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Processes I<br />

System V provides times system call<br />

#include <br />

#include <br />

long times(struct tms *ptr);<br />

struct tms (<br />

time_t tms_utime; /* user time */<br />

time_t tms_stime; /* system time */<br />

time_t tms_cutime; /* user time, children */<br />

time_t tms_cstime; /* system time, children */<br />

};<br />

long time(long *ptr); /* seconds since 00:00:00 GMT, 1 Jan 1970 */<br />

! Input and Output<br />

- <strong>Unix</strong> system calls for I/O<br />

open, read, write etc<br />

direct entry points into kernel<br />

- Standard I/O library<br />

higher level interface between process and kernel features: buffering, line-by-line input, formatted output<br />

! System Calls<br />

#include <br />

int open(char pathname int oflag[, int mode]);<br />

returns a file descriptor if successful, else -1<br />

oflag<br />

O_RDONLY<br />

O_WRONLY<br />

O_RDWR<br />

O_NDELAY<br />

O_APPEND<br />

O_CREAT<br />

O_TRUNC<br />

O_EXCL<br />

open for reading only<br />

open for writing only<br />

open for reading and writing<br />

do no block on open or read or write<br />

append to end of file on each write<br />

create <strong>the</strong> file is it does not exist<br />

if file exist, truncate to zero length<br />

error if O_CREAT & file already exist<br />

int creat(char *pathname, int mode);<br />

int close{int fildes);<br />

int read{int fildes, char *buff, unsigned int nbytes);<br />

returns number of bytes read if successful, else -1<br />

int write{int fildes, char *buff, unsigned int nbytes);<br />

returns actual number of bytes written<br />

long lseek{int fildes, long offset, int whence);<br />

whence<br />

0 position offset from beginning of file current<br />

1 position plus offset<br />

2 position set to size of file plus offset<br />

int dup{int fildes);<br />

returns a new file descriptor - same file position<br />

int fcntl{int fildes, int cmd, int arg);<br />

used to change <strong>the</strong> properties of an open file cmd<br />

F_DUPFD duplicate file descriptor<br />

F_SETFD set <strong>the</strong> close-on-exec flag via arg<br />

F_GETFD return <strong>the</strong> close-on-exec flag via arg<br />

115

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!