System Calls for I/O
The way that programs talk to the operating system is via ``system calls.'' -- it is a request to the operating system to perform some activity.
There are 5 basic system calls that Unix provides for file I/O.
1. int open(char *path, int flags [ , int mode ] );
2. int close(int fd);
3. int read(int fd, char *buf, int size);
4. int write(int fd, char *buf, int size);
5. off_t lseek(int fd, off_t offset, int whence);
Open makes a request to the operating system to use a file
Close() tells the operating system that you are done with a file descriptor
Read() tells the operating system to read "size" bytes from the file opened in file descriptor "fd", and to put those bytes into the location pointed to by "buf".
Write() is just like read(), only it writes the bytes instead of reading them. It returns the number of bytes actually written, which is almost invariably "size".
/*
I/O Operations: U may be thinking What the hell this I/O operation is rite?????
Following are the five services provided by an operating systems to the convenience of the users.
- Program Execution
- I/O Operations
- File System Manipulation
- Communications
- Error Detection
yeah!!!!!11 U got it now I/O is one of the services provided by the OS.The operating systems by providing I/O makes it convenient for the users to run programs. Becoz as Each program requires an input The same way it produces output also,so without an an i/o service how will u see ur o/p.?
This giving the input and taking o/p from computers ivloves lot of jagularies inside which we are not aware as os is providing it as a service to us. The reason the operating system controls I/O is for safety -- the computer must ensure that if my program has a bug in it, then it doesn't crash the system, and it doesn't mess up other people's programs that may be running at the same time or later. Thus, whenever you do disk or screen or network I/O, you must go through the operating system and use system calls.For efficiently and protection users cannot control I/O so this service cannot be provided by user-level programs.
*/
Open
Open makes a request to the operating system to use a file. The 'path' argument specifies what file you would like to use, and the 'flags' and 'mode' arguments specify how you would like to use it. If the operating system approves your request, it will return a ``file descriptor'' to you. This is a non-negative integer. If it returns -1, then you have been denied access, and you have to check the value of the variable "errno" to determine why.
All actions that you will perform on files will be done through the operating system. Whenever you want to do file I/O, you specify the file by its file descriptor. Thus, whenever you want to do file I/O on a specific file, you must first open that file to get a file descriptor.
Close
Close() tells the operating system that you are done with a file descriptor. The OS can then reuse that file descriptor. The file c1.c (say)shows some examples with opening and closing the file in1. You should look at it carefully, as it opens the file multiple times without closing it, which is perfectly legal in Unix.