C programming language Chapter 8 The UNIX System























- Slides: 23

C programming language Chapter 8 : The UNIX System Interface Presenters: Do Van Quyen, Le Thi Hien, Do Tien Thanh Internship in DASAN networks 21 Feb 2012

Contents 1. 2. 3. 4. 5. 6. 7. System Calls File Descriptors Low Level I/O - Read and Write Open, Creat, Close, Unlink Random Access – Lseek Listing Directories A Storage Allocator dovanquyen. vn@gmail. com 2/24

System Calls (Library calls) § Linux OS Root filesystem User space Linux OS System Call Interface Process Management Virtual file system Kernel. Network Stack Memory Management Arch Kernel space Device Driver Hardware Platform dovanquyen. vn@gmail. com 3/24

File Descriptors § A file descriptor is a low positive integer which indicates for accessing a file Integer value Name 0 Standard Input (stdin) 1 Standard Output (stdout) 2 Standard Error (stderr) § The user program refers to the file only by the file descriptor(Each file is referenced by a file descriptor ) dovanquyen. vn@gmail. com 4/24

Low Level I/O - Read and Write int n_read = read(int fd, char *buf, int n); int n_written = write(int fd, char *buf, int n); § The first argument is a file descriptor § The second argument is a character array in your program where the data is to go to or to come from § The third argument is the number of bytes to be transferred dovanquyen. vn@gmail. com 5/24

Open, Creat, Close, Unlink § Open () #include <fcntl. h> int fd; int open(char *name, int flags, int perms); fd = open(name, flags, perms); § The name argument is a character string containing the filename § The second argument, flags, is an int § O_RDONLY open for reading only § O_WRONLY open for writing only § O_RDWR open for both reading and writing § Perms is specify permissions if using O_CREAT § To open an existing file for reading: fd = open(name, O_RDONLY, 0); dovanquyen. vn@gmail. com 6/24

Open, Creat, Close, Unlink § Creat () #include <fcntl. h> int fd; int creat(char *name, int perms); fd = creat(name, perms); § Returns a file descriptor if it was able to create the file, and -1 if not § If the file already exists, creat will truncate it to zero length § If the file does not already exist, creates it with the permissions specified by the perms argument dovanquyen. vn@gmail. com 7/24

Open, Creat, Close, Unlink § Unlink() unlink(char *name) § The function unlink removes the file name from the file system. It corresponds to the standard library function remove dovanquyen. vn@gmail. com 8/24

Random Access – Lseek § The system call lseek provides a way to move around in a file without reading or writing any data long lseek(int fd, long offset, int origin); § Sets the current position in the file whose descriptor is fd to offset § Offset is taken relative to the location specified by origin § Origin can be 0, 1, or 2 to specify that offset is to be measured from the beginning, from the current position, or from the end of the file respectively dovanquyen. vn@gmail. com 9/24

Listing Directories § File is a collection of organized data, is managed by operating system. § Ex : a text file, a programing, … § Directory is a file which contains a list of filenames and some indication of where they are located. § Inode contains all file’s informations except filename. § A directory include : § Inode number #define NAME_MAX 14 typedef struct { § File name long ino; /* inode number */ char name[NAME_MAX+1]; /* name + '