Exp03_os_04
Exp03_os_04
Experiment No.: 3
Aim: - Program to demonstrate read, write and open system calls in Linux.
Roll Number: 04
Date of Performance:05/02/2025
Date of Submission:12/02/2025
Evaluation
Performance Indicator Max. Marks Marks Obtained
Performance 5
Understanding 5
Journal work and timely submission. 10
Total 20
Checked by
Signature :
Experiment No. 3
Aim Write a program to demonstrate read, write and open system calls in Linux.
Objective System calls enable processes to request device access, perform read or write
operations on these devices, and release them afterward.
1.write () system call is used to write to a file descriptor. In other words write()
can be used to write to any file (all hardware are also referred as file in Linux) in
the system but rather than specifying the file name, you need to specify its file
descriptor.
Syntax:
#include<unistd.h>
ssize_t write(int fd, const void *buf, size_t count);
The first parameter (fd) is the file descriptor where you want to write. The data
that is to be written is specified in the second parameter. Finally, the third
parameter is the total bytes that are to be written.
2. read()
The use of read() system call is to read from a file descriptor. The working is
same as write(), the only difference is read() will read the data from file pointed
to by file descriptor.
Syntax:
#include<unistd.h>
ssize_t read(int fd, const void *buf, size_t count);
The first parameter is the file descriptor. The second parameter is the buffer
where the read data will be saved. Lastly, the third parameter is the number of
bytes that you want to read.
3. Open ()
In read/write system call we learned how to read from the standard input device
and how to write to a standard output device.
Syntax
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
Syntax 1:
The first parameter is the name of the file that you want to open for
reading/writing. The second parameter is the mode in which to open the file i.e.,
for reading or for writing. For reading from a file, the flag used is O_RDONLY,
for writing O_WRONLY and for both reading and writing O_RDWR.
On success, the open() system call return the file descriptor of the file. This
descriptor now can be used in read()/write() system call for further processing.
The value of the file descriptor will always be a positive number greater than 2.
Whereas on failure it returns -1.
Syntax 2:
The second syntax is used when the file involved does not already exist in the
system and you want to create it on the go.
The first parameter is the file name. The second parameter is the mode
(read/write). In this case apart from writing O_RDONLY, O_WRONLY and
O_RDWR you also need to write O_CREAT, to create the file. The third
parameter secifies the permissions on the created file (read/write/execute).
//open.c
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdio.h>
int main()
{
int n,fd;
char buff[50];
fd=open("test.txt",O_RDONLY); //opens test.txt in read mode and the file
descriptor is saved in integer fd.
printf("The file descriptor of the file is: %d\n",fd); // the value of the file
descriptor is printed.
n=read(fd,buff,10);//read 10 characters from the file pointed to by file descriptor
fd and save them in buffer (buff)
write(1,buff,n); //write on the screen from the buffer
}
2: To read 10 characters from file “test.txt” and write them into non-existing file
“towrite.txt”
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
int main()
{
int n,fd,fd1;
char buff[50];
fd=open("test.txt",O_RDONLY);
n=read(fd,buff,10);
fd1=open("towrite1.txt",O_WRONLY|O_CREAT,0642);//use the pipe symbol (|)
to separate O_WRONLY and O_CREAT
write(fd1,buff,n);
}
2.
Conclusion