[go: up one dir, main page]

0% found this document useful (0 votes)
69 views24 pages

Systems Programming: 5. File I/O Using System Calls

This document discusses file input/output (I/O) using system calls in Linux/Unix systems. It covers: 1) The standard C library contains functions that either correspond directly to system calls for file I/O (like read() and write()), or provide higher-level interfaces. 2) System calls provide direct entry points for processes to request services from the kernel, like creating new processes or opening/reading/writing files. 3) An example C program is given that uses system calls like open(), read(), write() to copy the contents of one file to another.

Uploaded by

Kibru Kawso
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views24 pages

Systems Programming: 5. File I/O Using System Calls

This document discusses file input/output (I/O) using system calls in Linux/Unix systems. It covers: 1) The standard C library contains functions that either correspond directly to system calls for file I/O (like read() and write()), or provide higher-level interfaces. 2) System calls provide direct entry points for processes to request services from the kernel, like creating new processes or opening/reading/writing files. 3) An example C program is given that uses system calls like open(), read(), write() to copy the contents of one file to another.

Uploaded by

Kibru Kawso
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

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.

The Linux/Unix Model

Standard Unix/Linux C library contains two sets of functions:


those that should be provided with any C compiler (the standard I/O library eg: malloc ) those that correspond ( eg: read(),write(), etc...) to Unix/Linux 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)

Is an instance of a program that is being executed by the operating system.


The only way a new process can be created by the UNIX/LINUX system is by issuing the fork() system call. More than one process can be executed at a time. A single program can execute as many different processes at the same time.

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 System Interface

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.

UNIX System calls for I/O

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.

The Unix/Linux system call.


The Unix/Linux system calls for I/O are direct entry points to the kernel.

O _APPEND : Append to end of file on each write.

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

int read(int fd, char * buff, unsigned int count);

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.

int write(int fd, char * buff, unsigned int count);

write

When a process terminates, all open files are automatically closed by the kernel.

Exercise 1 To copy the contents of one file into another


#include<stdio.h> #include<fcntl.h>

#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)

number_read = read (source_handle, buffer, BUFFSIZE);


} close(source_handle); close(target_handle); return 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

Source file: testfile.txt


Target file: testcopy.txt user@user-laptop:~$ cat testcopy.txt

hai how are u


hope keeping fine. user@user-laptop:~$

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

You might also like