0% found this document useful (0 votes)
3 views

Exp03_os_04

The document outlines Experiment No. 3 for the Operating System Lab course, focusing on demonstrating read, write, and open system calls in Linux. It includes the experiment's aim, objectives, theoretical background, program examples, and evaluation criteria. The evaluation section details performance indicators with maximum marks and marks obtained by the student, Advay Joshi, for the experiment conducted on February 5, 2025, and submitted on February 12, 2025.

Uploaded by

advay.234486101
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Exp03_os_04

The document outlines Experiment No. 3 for the Operating System Lab course, focusing on demonstrating read, write, and open system calls in Linux. It includes the experiment's aim, objectives, theoretical background, program examples, and evaluation criteria. The evaluation section details performance indicators with maximum marks and marks obtained by the student, Advay Joshi, for the experiment conducted on February 5, 2025, and submitted on February 12, 2025.

Uploaded by

advay.234486101
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

EXPERIMENT ASSESSMENT

ACADEMIC YEAR 2024-25


Course: Operating System Lab
Course code: CSL403
Year: SE Sem: IV

Experiment No.: 3

Aim: - Program to demonstrate read, write and open system calls in Linux.

Name: ADVAY JOSHI

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

Exceed Expectations Meet Expectations Below Expectations


Performance Indicator
(EE) (ME) (BE)
Performance 5 3 2
Understanding 5 3 2
Journal work and
10 8 4
timely submission.

Checked by

Name of Faculty​ : Odilia Gonsalves

Signature ​ ​ :

CSL403- Operating System Lab


Date ​ ​ ​ :

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.

Theory A system call is a request from computer software to an operating system's


kernel. The Application Program Interface (API) connects the operating system's
functions to user programs. It acts as a link between the operating system and a
process, allowing user-level programs to request operating system services.

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.

But, normally we would either read from a user-created file or write to a


user-created file. Hence, the question comes that how do know the file descriptor

CSL403- Operating System Lab


of these files because to read or to write the first parameter in the read ()/write ()
system calls is the file descriptor. We can use the open() system call to get the
file descriptor of any file.

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

The open() system call has two syntax.

Syntax 1:

int open(const char *pathname, int flags);

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.

int open(const char *pathname, int flags, mode_t mode);

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

CSL403- Operating System Lab


Program 1: Write a program using open() system call to read the first 10 characters of an
existing file “test.txt” and print them on screen.

//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);
}

CSL403- Operating System Lab


Output 1.
Step1: create the file test.txt and write some text into it​
Step2: compile the program​
$gcc open.c​
Step3: run​
$./a.out

2.

Conclusion

CSL403- Operating System Lab

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy