C programming language Chapter 8 The UNIX System

  • Slides: 23
Download presentation
C programming language Chapter 8 : The UNIX System Interface Presenters: Do Van Quyen,

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

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

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

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,

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

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

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

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

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

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 + '' terminator */ } Dirent; Lethihien. fet 8@gmail. com 10/24

Listing Directories § “dirent. h” typedef struct{ int fd; Dirent d; } DIR; Lethihien.

Listing Directories § “dirent. h” typedef struct{ int fd; Dirent d; } DIR; Lethihien. fet 8@gmail. com Function Explain DIR *opendir(char Open directory *dirname); -Is directory : pointer to drectory information. -Fail or not directory : NULL Dirent *readdir(DIR *dfd); Read directory -Return a pointer point to next file in directory - No more file, return NULL void closedir(DIR *dfd); Close dirctory and free memory. 11/24

Listing Directories § “stat. h” § Calls system : § int stat(char*, struct stat

Listing Directories § “stat. h” § Calls system : § int stat(char*, struct stat *); § int fstat(int fd, struct stat *); Lethihien. fet 8@gmail. com 12/24

Listing Directories • void dirwalk(char *dir, void (*fcn)(char *)) { … dfd = opendir(dir);

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); } Lethihien. fet 8@gmail. com • { void fsize(char *name) struct stat stbuf; stat(name, &stbuf; if ((stbuf. st_mode & S_IFMT) == S_IFDIR) dirwalk(name, fsize); printf("%8 ld %sn", stbuf. st_size, name); } 13/24

A Storage Allocator § Memory map §. text area: it is read-only memory which

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 14/24

A Storage Allocator § Free list is a collection of free blocks. § 1

A Storage Allocator § 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 15/24

A Storage Allocator § Storage location base on Free list Dotienthanh. bkhn@gmail. com 16/24

A Storage Allocator § Storage location base on Free list Dotienthanh. bkhn@gmail. com 16/24

A Storage Allocator § Case 1: Found 1 block having size = nbytes +

A Storage Allocator § Case 1: Found 1 block having size = nbytes + header Dotienthanh. bkhn@gmail. com 17/24

A Storage Allocator § Case 2: Found 1 block having size > nbytes +

A Storage Allocator § Case 2: Found 1 block having size > nbytes + header Dotienthanh. bkhn@gmail. com 18/24

A Storage Allocator § Case 3: require OS locates a new block § OS

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 19/24

A Storage Allocator § Case 3: require OS locates a new block Dotienthanh. bkhn@gmail.

A Storage Allocator § Case 3: require OS locates a new block Dotienthanh. bkhn@gmail. com 20/24

A Storage Allocator § Case 3: require OS locates a new block § Coalesce

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 21/24

A Storage Allocator § Case 3: require OS locates a new block § Coalesce

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 22/24

Thank you! 23/24

Thank you! 23/24