1a - ELEC 2602 Object Oriented & Systems Programming...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
ELEC 2602 – Object Oriented & Systems Programming Module 1A – Unix O.S. Support 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Module 1A: the Unix Operating System Support Outline: UNIX File I/O Files and Directories Standard I/O Library System Data Files and Information Acknowledgments and Disclaimer: The materials presented in this module are adopted from many textbooks, in particular, those several textbooks listed in the References contained in the course website. The instructor disclaims any copyright ownership of the materials presented in this module. The materials in this module are expected to be used by the students for personal studying purposes. 2
Background image of page 2
Who provides the abstraction for developing applications ?? Operating systems: files, system data, system calls to network protocols, etc. (as in Unix) Programming paradigm and language constructs: object-oriented, distributed objects, object brokers, components, etc. (as in Java) Compilers, interpreters: scripts Physical Layer MAC/Datalink IP TCP UDP Basic Services HTTP Components, Scripts Application Services Files Database 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Unix O.S. Support – (1) File I/O: Overview • Can be done using 5 functions: – open, read, write, lseek, and close • Unbuffered I/O: – each read or write invokes a system call in the kernel • Files sharing • Other functions: – dup, fcntl, and ioctl 4
Background image of page 4
File I/O: *File Descriptors • All open files are referred to by file descriptors • Non-negative integers, kernel assigned through a call: open or creat • Use it as an argument to other functions: read, write • Conventions: – 0: standard input – 1: standard output – 2: standard error 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
File I/O: open Function #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int open (const char *pathname, int oflag , . ../* mode_t mode */); •o f l a g can be: –O _RDON L Y : open for reading only O_WRONLY : open for writing only –O _RDWR : open for reading and writing There are other flags (see Stevens’ APUE book) open() returns an integer (by the kernel) as the file descriptor
Background image of page 6
File I/O: creat Function #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int creat(const char *pathname, mode_t mode); • This function is equivalent to: open(pathname, O_WRONLY | O_CREAT | O_TRUNC, mode); • open a file for writing only 7
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
File I/O: close Function #include <unistd.h> int close(int filedescriptor); • Closing a file also releases all record locks (will elaborate) • When a process terminates, all open files are automatically closed by the kernel 8
Background image of page 8
File I/O: lseek Function #include <sys/types.h> #include <unistd.h> off_t lseek(int filedescriptor, off_t offset , int whence ); Every open file has an associated “ current file offset ” (a file is a sequential list of bytes), kept in the kernel The current file offset is initialized to 0 when the file is opened (unless O_APPEND is used)
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 07/05/2008 for the course EEE ELEC2602 taught by Professor Dr.vincenttam during the Winter '08 term at HKU.

Page1 / 34

1a - ELEC 2602 Object Oriented &amp; Systems Programming...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online