260 likes | 430 Views
C programming language. Presenters: Do Van Quyen, Le Thi Hien, Do Tien Thanh. Chapter 8 : The UNIX System Interface. 21 Feb 2012. Contents. System Calls File Descriptors Low Level I/O - Read and Write Open, Creat, Close, Unlink Random Access – Lseek Listing Directories
E N D
C programming language Presenters: Do Van Quyen, Le Thi Hien, Do Tien Thanh Chapter 8 : The UNIX System Interface 21 Feb 2012
Contents • 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
System Calls (Library calls) • Linux OS Root filesystem User space Linux OS Kernel System Call Interface Process Management Virtual file system Memory Management NetworkStack Kernel space Arch Device Driver Hardware Platform dovanquyen.vn@gmail.com
File Descriptors • A file descriptor is a low positive integer which indicates for accessing a file • The user program refers to the file only by the file descriptor(Each file is referenced by a file descriptor ) dovanquyen.vn@gmail.com
Low Level I/O - Read and Write intn_read = read(intfd, char *buf, intn); intn_written = write(intfd, 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
Open, Creat, Close, Unlink • Open () #include<fcntl.h> intfd; 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
Open, Creat, Close, Unlink • Creat () #include<fcntl.h> intfd; intcreat(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, creat creates it with the permissions specified by the perms argument dovanquyen.vn@gmail.com
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
Random Access – Lseek • The system call lseek provides a way to move around in a file without reading or writing any data longlseek(intfd, 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
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 • File name #define NAME_MAX 14 typedefstruct { long ino; /* inode number */ char name[NAME_MAX+1]; /* name + '\0' terminator */ } Dirent; Lethihien.fet8@gmail.com
Listing Directories • “dirent.h” typedefstruct{ intfd; Dirent d; } DIR; Lethihien.fet8@gmail.com
Listing Directories • “stat.h” • Calls system : • int stat(char*, struct stat *); • int fstat(int fd, struct stat *); Lethihien.fet8@gmail.com
Listing Directories • void dirwalk(char *dir, void (*fcn)(char *)) { … dfd = opendir(dir); while (NULL !=(dp = readdir(dfd))) { if (0 ==strcmp(dp->name, ".")|| strcmp(dp->name, "..")) continue; /* skip self and parent */ else { sprintf(name, "%s/%s", dir, dp->name); (*fcn)(name); } } closedir(dfd); } • void fsize(char *name) { struct stat stbuf; stat(name, &stbuf; if ((stbuf.st_mode & S_IFMT) == S_IFDIR) dirwalk(name, fsize); printf("%8ld %s\n", stbuf.st_size, name); } Lethihien.fet8@gmail.com
A Storage Allocator • Memory map • .text area: it is read-only memory which has executing file. • .data area : this is area where global variable which are declared the value are allocated • .bss area( below stack section): this is area where global variable which are not declared the value are allocated • Heap area : is used to allocate dynamic variable (C use malloc() and free() to allocate and free memory) • Stack are : is used to allocate automatic (local) variable and parameter of each function when it is called • ENV area : is used to allocate environment variable Dotienthanh.bkhn@gmail.com
A Storage Allocator • Free list • Free list is a collection of free blocks. • 1 block contain 1 pointer to next free block, a size and free space of itself. (value of size fields = size of free space + size of header) • The blocks are kept in order of increasing storage address, and the last block has the highest addr • The last block has pointer to the first block Dotienthanh.bkhn@gmail.com
A Storage Allocator • Storage location base on Free list Dotienthanh.bkhn@gmail.com
A Storage Allocator • Case 1: Found 1 block having size = nbytes + header Dotienthanh.bkhn@gmail.com
A Storage Allocator • Case 2: Found 1 block having size > nbytes + header Dotienthanh.bkhn@gmail.com
A Storage Allocator • Case 3: require OS locates a new block • OS will try to locate 1 block. (Size of block >> nbytes is require) • New block is either between 2 exiting blocks or at the end/start of list • Block is needed to attach to free list • (if new block is adjacent to a exiting block, it will be coalesced into a single block) • Remaining work is the same case 2 Dotienthanh.bkhn@gmail.com
A Storage Allocator • Case 3: require OS locates a new block Dotienthanh.bkhn@gmail.com
A Storage Allocator • Case 3: require OS locates a new block • Coalesce new block into exiting block between 2 exiting blocks Dotienthanh.bkhn@gmail.com
A Storage Allocator • Case 3: require OS locates a new block • Coalesce new block into exiting block between 2 exiting blocks Dotienthanh.bkhn@gmail.com