CSC320 chapter4 - CSC 320 Systems Programming Chapter 4 -...

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

View Full Document Right Arrow Icon
1 CSC 320 Systems Programming Chapter 4 -File I/O Updated by: Dr. Safwan Qasem – Spring 2010 Original version created by: Dr. Mohamed El Bachir Menai
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Introduction Functions available for file I/O (open, read, write …) Most file I/O can be performed using only: open, read, write, lseek, and close Unbuffered I/O Invoke a system call in the kernel (part of ISO C) Centered around file descriptor Standard I/O routines (standard I/O library) Centered around streams (specified by the ISO C standard)
Background image of page 2
3 UNIX files A UNIX file is a sequence of m bytes: All I/O devices are represented as files: /dev/sda2 (/usr disk partition) /dev/tty2 (terminal) The kernel is also represented as a file: /dev/kmem (kernel memory image) /proc (kernel data structures) B 0 ,B 1 ... B m 1
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 UNIX file types Regular files: binary or text files Directory files: files containing names and locations of other files Character special and block special files: Terminals (character special) Disks (block special) FIFO (pipe) File type used for interprocess communication Socket File type used for network communication between processes
Background image of page 4
5 UNIX I/O The mapping of files to devices allows the kernel to export a simple interface called UNIX I/O. File descriptor: To the kernel all opened files are referred to by file descriptors – a non-negative integer. open an existing file or creat a new file: The kernel returns a file descriptor to the process. read or write a file: The file is identified with the file descriptor returned by open or creat .
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 UNIX I/O, cont. UNIX shells File descripor 0: standard input File descriptor 1: standard output File descriptor 2: standard error In POSIX-compliant applications: 0: STDIN_FILENO 1: STDOUT_FILENO 2: STDERR_FILENO These constants are defined in the <unistd.h> File descriptors range from 0 through OPEN_MAX Early UNIX implementations: maximun of 20 open files per process FreeBSD 5.2.1, Mac OS X 10.3, Solaris 9: infinite limit Linux 2.4.22: limit of 1,048,576
Background image of page 6
7 Basic I/O functions Opening and closing files: open () , close () Changing the current file position: lseek () Reading and writing a file: read () , write ()
Background image of page 7

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

View Full DocumentRight Arrow Icon
8 Opening files A file is opened or created by calling the open function: #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int open (const char *pathname, int oflag, … /*mode_t mode */); Returns: file descriptor if OK, -1 on error
Background image of page 8
9 Opening files, cont. pathname: name of the file to open or create oflag: O_RDONLY: open for reading only O_WRONLY: open for writing only O_RDWR: open for reading and writing One and only one of these three constants must be specified.
Background image of page 9

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

View Full DocumentRight Arrow Icon
10 Opening files, cont.
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/28/2011 for the course COMPUTER S 320 taught by Professor Dr.safwanqasem during the Spring '11 term at King Saud University.

Page1 / 56

CSC320 chapter4 - CSC 320 Systems Programming Chapter 4 -...

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

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