Systems Programming: 5. File I/O Using System Calls
Systems Programming: 5. File I/O Using System Calls
System Programming
Systems Programming mainly refers to the architecture and design of the applications and services that make up an operating system. How these all fit together, when they are invoked, what restraints are there on resource use and what APIs(application programming interface) and utilities etc are provided for users- are all aspects of Systems Programming.
System calls
In this chapter, we will study how to make use of system calls to obtain services directly from the kernel(OS). Direct entry points, through which an active process can obtain services from the kernel-are termed as system calls.
Let us discuss here some of the terms required in our further discussion..
Program
A program
Is an executable file. Created by a link editor Resides on a disk file. The only way that a program can be executed by the UNIX/Linux system is by the exec() system call.
Process(task)
System Calls
A Unix/Linux kernel provides a limited number of direct entry points (system calls) through which an active process can obtain services from the kernel. The standard Unix/Linux C library provides a C interface to each system call. This makes the actual system calls appear as normal C functions to the programmer.
System Calls
Most system calls return(-1) if an error occurs and a (>=0) if everything is ok.
C interface also provides a global integer variable errno to obtain the system error number if an error occurs.
Header file <error.h> contains the name and values of these error numbers.
Some system calls returns a structure of information or pointer to structures in addition to this integer value.
The UNIX/Linux operating system provides its services through a set of system calls (which are in effect functions within the operating system that may be called by user programs.)
C helps to employ system calls for maximum efficiency, or to access some facilities that may not be in the library.
There are 2 methods available under Unix/Linux for doing Input /Output.
The standard I/O library.
Functions that provide higher level interface between a process and the kernel providing features such as line by line input /output.
Open a file
Most implementations define O_RDONLY as 0, O_WRONLY as 1 and O_RDWR as 2 for compatibility with older programs/compilers. The third argument mode, is optional and is required only if O_CREAT is specified. Function flags for mode can also be as : S_IWRITE
S_IREAD
S_IWRITE | S_IREAD
If the read is successful, the number of bytes read is returned this can be less than the count that was requested. If the end of the file is encountered, zero is returned. If an error is encountered, -1 is returned.
write
When a process terminates, all open files are automatically closed by the kernel.
#include<sys/stat.h>
#include<unistd.h> #define BUFFSIZE 2*1024
main()
{ char *buffer; char source_filename[50], target_filename[50]; int source_handle, target_handle; int number_read; buffer=(char*)malloc(BUFFSIZE);
Exercise 1
if(buffer = =NULL) { printf("Memory not allocated\n"); exit(1);
}
printf("Source file:"); scanf("%s",source_filename);
printf("Target file:");
scanf("%s",target_filename);
Exercise 1
source_handle=open(source_filename, O_RDONLY);
if(source_handle = = -1) { printf("Could not open the source file"); exit(1); }
target_handle =open ( target_filename, O_CREAT|O_WRONLY, S_IWRITE|S_IREAD);
if(target_handle = = -1) { printf ("Could not open the target file"); exit(1); }
Exercise 1
number_read = read (source_handle, buffer, BUFFSIZE); while(number_read) { write(target_handle,buffer,number_read); // while(number_read >0)
OUTPUT
user@user-laptop:~$ cat testfile.txt hai how are u hope keeping fine. user@user-laptop:~$ gcc unixfile1.c user@user-laptop:~$ ./a.out
Exercise 2: Rewrite the above program using command line arguments. The output of the program should be as follows: user@user-laptop:~$ gcc -o copier unixfile2.c
user@user-laptop:~$ ./copier testfile.txt testcopy2.txt
user@user-laptop:~$ cat testcopy2.txt hai how are u hope keeping fine. user@user-laptop:~$
Thank You