Presentation is loading. Please wait.

Presentation is loading. Please wait.

File table: a list of opened files Each entry contains: – Index: file descriptors – Pointer to the file in memory – Access mode File descriptor is a positive.

Similar presentations


Presentation on theme: "File table: a list of opened files Each entry contains: – Index: file descriptors – Pointer to the file in memory – Access mode File descriptor is a positive."— Presentation transcript:

1

2 File table: a list of opened files Each entry contains: – Index: file descriptors – Pointer to the file in memory – Access mode File descriptor is a positive integer starting from 0 and has a max value (negative values are not allowed) Each process has at least 3 file descriptors: – 0 (stdin)STDIN_FILENO (in C) – 1 (stdout)STDOUT_FILENO (in C) – 2 (stderr)STDERR_FILENO (in C)

3 System calls: read(), write(), open(), creat(), close() #include #include int open (const char *name, int flags); int open (const char *name, int flags, mode_t mode); int fd = open ("/home/ceng360/test", O_RDONLY); Int fd = open ("/home/teach/pearl", O_WRONLY | O_TRUNC); #include #include ssize_t write (int fd, const void *buf, size_t count); Flags: O_CREAT: If the file does not exist, the kernel will create it. O_TRUNC: If the file exists, open and truncate to 0 length. O_TRUNC: If the file exists, open and truncate to 0 length. S_IRWXU S_IWUSR S_IRGRP What about mode? S_IRWXU S_IWUSR S_IRGRP Refer to Linux, System programming for other flags and mods… Headers needed Open command Without e Example

4 Write a program test.c that opens a new file and write few lines in it. – To compile: gcc -c test.c – To link: gcc test.o -o test – To run:./test #include #include int main() { int fd = open(“myfile”, O_CREAT | O_WRONLY, 0700); int fd = open(“myfile”, O_CREAT | O_WRONLY, 0700); const char *buf = “Hello \n This is my file”; write (fd, buf, strlen(buf)); close (fd); return 0; }

5 NOTE: we will use errno in order to communicate errors…. creat() int creat (const char *name, mode_t mode); Note: you need to open after creating the file Note: creat() and open() calls return fd on success or -1 on failure read() ssize_t read (int fd, void *buf, size_t len); Note: creat() call returns: – Length of what we read – Greater than 0 and less than the requested len  EOF reached – 0  EOF and nothing was read – -1  errorerrno set to EINTR or EAGAIN  reissue the read command, otherwise …problems.

6 Operating systems implement delayed write via buffers. The Linux kernel, like any modern operating system kernel, implements a complex layer of caching, buffering, and I/O management between devices and applications. include include int fsync (int fd); int fsync (int fd); Used to ensure that data are written to the disk. Or int fdatasync (int fd); Or void sync (void); void sync (void); No return value and no parameters. See O_DIRECT for more details about this topic.

7 Iseek system call: set the file position to a given value #include #include off_t lseek (int fd, off_t pos, int origin); Origin can be: SEEK_CUR The current file position is set to its current value plus pos, which can be negative, zero, or positive. SEEK_END The current file position is set to the current length of the file plus pos, which can be negative, zero, or positive. A pos of zero sets the offset to the end of the file. SEEK_SET The current file position is set to pos. A pos of zero sets the offset to the beginning of the file.

8 off_t ret; ret = lseek (fd, (off_t) 1825, SEEK_SET); if (ret == (off_t) -1) /* error */ Set the file position to 1825 (byte) off_t ret; ret = lseek (fd, 0, SEEK_END); if (ret == (off_t) -1) /* error */ Set the file position to the file end int pos; pos = lseek (fd, 0, SEEK_CUR); if (pos == (off_t) -1) /* error */ else /* 'pos' is the current position of fd */ Find out the current file position

9 Build a file using stdout redirection. Example: echo “Hello, my name is Sam” > myfile echo “This is the second line” >> &myfile Write a C program that creates a new file elifym and write in this file the contents of myfile but in reverse order. myfile should look like: enil dnoces eht si siht maS si eman ym,olleH

10 #include #include int main() { int fd1 = open("myfile", O_RDONLY); int fd2 = open("elifym", O_CREAT | O_WRONLY, 0700); char buf[2]; int pos = lseek(fd1, 0, SEEK_END); int size = 0; while(pos >= 0) { read(fd1, buf, 1); buf[1]='\0'; write(fd2, buf, strlen(buf)); size = size-1; pos = lseek(fd1,(off_t) size,SEEK_END); }close(fd1);close(fd2); return 0; }

11


Download ppt "File table: a list of opened files Each entry contains: – Index: file descriptors – Pointer to the file in memory – Access mode File descriptor is a positive."

Similar presentations


Ads by Google