Program 1: Create a C program to demonstrate open(), read(),
write(),close() system calls.
A system call is just what its name implies—a request for the operating system
to do something on behalf of the user‘s program
1. open()
2. read()
3. write()
4. close()
1. open()
#include <fcntl.h>
int open( char *filename, int access, int permission );
The available access modes are
O_RDONLY O_WRONLY O_RDWR O_APPEND O_BINARY O_TEXT
The permissions are
S_IWRITE S_IREAD S_IWRITE | S_IREAD
The open() function returns an integer value, which is used to refer to the file. If
unsuccessful, it returns -1, and sets the global variable errno to indicate the error
type.
2. read()
#include <fcntl.h>
int read( int handle, void *buffer, int nbyte );
The read() function attempts to read nbytes from the file associated with handle,
and places the characters read into buffer. If the file is opened using O_TEXT, it
removes carriage returns and detects the end of the file. The function returns
the number of bytes read. On end-of-file, 0 is returned, on error it returns -1,
setting errno to indicate the type of error that occurred.
3. write()
#include <fcntl.h>
int write( int handle, void *buffer, int nbyte );
The write() function attempts to write nbytes from buffer to the file associated
with handle. On text files, it expands each LF to a CR/LF. The function returns
the number of bytes written to the file. A return value of -1 indicates an error,
with errno set appropriately.
4. close()
#include <fcntl.h>
int close( int handle );
The close() function closes the file associated with handle. The
function returns 0 if successful, - 1 to indicate an error, with errno
set appropriately.
lseek()
#include <unistd.h>
#include <fcntl.h>
off_t lseek(int fd, off_t offset, int whence);
● fd: This is the file descriptor of the open file whose offset is to be changed.
● offset: This is the number of bytes by which to move the file offset. It can be a positive or
negative value.
● whence: This specifies the reference point for the offset. It can take one of the following
values:
a) SEEK_SET: The offset is set to offset bytes from the beginning of the file.
b) SEEK_CUR: The offset is set to its current location plus offset bytes.
c) SEEK_END: The offset is set to the size of the file plus offset bytes (i.e., offset bytes
from the end of the file).
Step-by-Step Algorithm
1. Start the program.
2. Declare variables:
o fd file descriptor
o buffer[80] buffer for reading file data
o message[] data to write
o nbytes number of bytes read/written
3. Open the file "[Link]" in read/write mode.
o Use O_RDWR | O_CREAT flags.
o If opening fails, print error and exit.
4. Print message: "[Link] opened with read/write access".
5. Write the message to the file using write().
o If writing fails, print error, close file, and exit.
6. Move the file pointer to the beginning of the file using lseek().
o If seeking fails, print error, close file, and exit.
7. Read data from the file into buffer using read().
o If reading fails, print error, close file, and exit.
8. Null-terminate the buffer: buffer[nbytes] = '\0'.
9. Print the data read from the file.
10. Close the file using close(fd).
11. End the program.
Implementation of open(), read(), write() and close() functions
student@ubuntu:~$ gedit os1.c
#include <stdio.h>
#include <fcntl.h>
#include<unistd.h>
int main()
{
int fd;
char buffer[80];
static char message[]="Welcome to Unix System Calls\
n"; fd=open("[Link]",O_RDWR);
if (fd != -1)
{
printf("[Link] opened with read/write access\n");
write(fd,message,sizeof(message));
lseek(fd,0,0);
read(fd,buffer,sizeof(message
));
printf("%s — was written to [Link] \
n",buffer); close(fd);
}
}
Note: First create empty file with [Link] before running the
commands.
Commands:
student@ubuntu:~$ gedit [Link]
student@ubuntu:~$ gcc os1.c
student@ubuntu:~$ ./[Link]
[Link] opened with read/write access
Welcome to Unix System Calls — was written to [Link]
student@ubuntu:~$ cat [Link]
Welcome to Unix System
Calls student@ubuntu:~$
Modifications /Extension of the Program
1. Modify the open() call to create the file if it does not exist using O_CREAT.
2. Set different File permission and check file attributes using ls-l
3. Append the data to the file instead of overwriting
4. Move the file pointer 10 bytes from the beginning and write a new string. (using
lseek())