1 / 13

การทำงานกับ Files

การทำงานกับ Files. ธวัชชัย เอี่ยมไพโรจน์. UNIX File Structure. ดูรายละเอียดได้ที่ http://lecture.compsci.buu.ac.th/~f44404/doc/file.ppt บน UNIX ทุกๆ อย่างเป็น File ตั้งแต่ disk files, serial ports, printers และอุปกรณ์อื่นๆ สามารถใช้ basic functions ต่อไปนี้จัดการได้หมด

Download Presentation

การทำงานกับ Files

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. การทำงานกับ Files ธวัชชัย เอี่ยมไพโรจน์

  2. UNIX File Structure • ดูรายละเอียดได้ที่ http://lecture.compsci.buu.ac.th/~f44404/doc/file.ppt • บน UNIX ทุกๆ อย่างเป็น File ตั้งแต่ disk files, serial ports, printers และอุปกรณ์อื่นๆ • สามารถใช้ basic functions ต่อไปนี้จัดการได้หมด open, close, read, write, ioctl

  3. คำสั่งที่ควรศึกษา • od -c <filename> • od -d <filename> • คำถาม คำสั่งทั้ง 2 ทำอะไรกับ File • inode คืออะไร ?

  4. Files / Devices • /dev/console • /dev/tty • /dev/null • system calls : เป็นบริการจาก UNIX และติดต่อโดยตรงกับ OS • device drivers: เป็น low level interfaces สำหรับควบคุม Hardware

  5. System called สำหรับ device drivers • Open a file or device. : open • Read from an open file or device. : read • Write to a file or device. : write • Close the file or device. : close • Specific control the device. : ioctl ioctl( ) ของแต่ละเครื่องอาจแตกต่างกันได้

  6. ประสิทธิภาพ • UNIX ต้อง switch จากโปรแกรม แล้วไป run kernel code แล้วต้อง switch กลับมาอีก • Hardware limit เกี่ยวกับขนาดของ data blocks ซึ่ง low level function call สามารถใช้ได้มีขนาดจำกัด • นิยมใช้ Library Functions เช่น stdio.h แทน • แต่ยังมีที่จำเป็นต้องใช้อีกมาก

  7. Low-level File Access • 0 : standard input 1: standard output 2 : standard error • write ดูโปรแกรมตัวอย่างที่ http://lecture.compsci.buu.ac.th/~f44213/samples/jun26/write.c • read ดูโปรแกรมตัวอย่างที่ http://lecture.compsci.buu.ac.th/~f44213/samples/jun26/read.c • open • close • ioctl

  8. A File Copy Program • ศึกษาได้จาก http://lecture.compsci.buu.ac.th/~f44213/samples/jun26/system.c

  9. lseek • เป็น low level system call ในการหาตำแหน่งใน file SEEK_SET SEEK_CUR SEEK_END • man lseek

  10. get file status system call • stat : fstat : lstat struct stat { dev_t st_dev; /* device */ ino_t st_ino; /* inode */ ….. }; เป็น structure ที่ต้องศึกษาให้เข้าใจ

  11. dup และ dup2 • duplicate a file descriptor #include <unistd.h> int dup(int oldfd); int dup2(int oldfd, int newfd);

  12. File Maintenance (man 2 <fname>) • chmod int chmod(const char *path, mode_t mode); • chown int chown(const char *path, uid_t owner, gid_t group); • link int link(const char *oldpath, const char *newpath); • unlink int unlink(const char *pathname);

  13. Directories Maintenance (man 2 <fname>) • mkdir int mkdir(const char *pathname, mode_t mode); • rmdir int rmdir(const char *pathname); • chdir int chdir(const char *path); • getcwd [man getcwd] char *getcwd(char *buf, size_t size);

More Related