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

OS Lab 3

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

OS Lab 3

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

20CS2036 – Operating System Lab URK22CS1198

Ex. No. 3 I/O System Call

Date of Exercise 08/01/2024

Aim

To create and execute five types of system call using C programming on klab.karunya.edu.

Description
1. create:
The create() function is used to create a new empty file in C. We can specify the permission and
the name of the file which we want to create using the create() function. It is defined
inside <unistd.h> header file and the flags that are passed as arguments are defined
inside <fcntl.h> header file.

Syntax of create() in C
int create(char *filename, mode_t mode);

Parameter
● filename: name of the file which you want to create
● mode: indicates permissions of the new file.

Return Value
● return first unused file descriptor (generally 3 when first creating use in the process because
0, 1, 2 fd are reserved)
● return -1 when an error

2. open:
The open() function in C is used to open the file for reading, writing, or both. It is also capable of
creating the file if it does not exist. It is defined inside <unistd.h> header file and the flags that
are passed as arguments are defined inside <fcntl.h> header file.

Syntax of open() in C
int open (const char* Path, int flags);

Parameters
● Path: Path to the file which we want to open.
● Use the absolute path beginning with “/” when you are not working in the same
directory as the C source file.
● Use relative path which is only the file name with extension, when you
are working in the same directory as the C source file.
● flags: It is used to specify how you want to open the file. We can use the following flags.

19
20CS2036 – Operating System Lab URK22CS1198

Flags Description

O_RDONLY Opens the file in read-only mode.

O_WRONLY Opens the file in write-only mode.

O_RDWR Opens the file in read and write mode.

O_CREAT Create a file if it doesn’t exist.

O_EXCL Prevent creation if it already exists.

O_ APPEND Opens the file and places the cursor at the end of the contents.

O_ASYNC Enable input and output control by signal.

O_CLOEXEC Enable close-on-exec mode on the open file.

O_NONBLOCK Disables blocking of the file opened.

O_TMPFILE Create an unnamed temporary file at the specified path.

3. close:
The close() function in C tells the operating system that you are done with a file descriptor and
closes the file pointed by the file descriptor. It is defined inside <unistd.h> header file.

Syntax of close() in C
int close(int fd);

Parameter
● fd: File descriptor of the file that you want to close.

Return Value
● 0 on success.
● -1 on error.

20
20CS2036 – Operating System Lab URK22CS1198

4. read:
From the file indicated by the file descriptor fd, the read() function reads the specified amount of
bytes cnt of input into the memory area indicated by buf. A successful read() updates the access
time for the file. The read() function is also defined inside the <unistd.h> header file.

Syntax of read() in C
size_t read (int fd, void* buf, size_t cnt);

Parameters
● fd: file descriptor of the file from which data is to be read.
● buf: buffer to read data from
● cnt: length of the buffer

Return Value
● return Number of bytes read on success
● return 0 on reaching the end of file
● return -1 on error
● return -1 on signal interrupt

Important Points
● buf needs to point to a valid memory location with a length not smaller than the specified
size because of overflow.
● fd should be a valid file descriptor returned from open() to perform the read
operation because if fd is NULL then the read should generate an error.
● cnt is the requested number of bytes read, while the return value is the actual number of
bytes read. Also, some times read system call should read fewer bytes than cnt.

5. write:
Writes cnt bytes from buf to the file or socket associated with fd. cnt should not be greater than
INT_MAX (defined in the limits.h header file). If cnt is zero, write() simply returns 0 without
attempting any other action.
The write() is also defined inside <unistd.h> header file.

Syntax of write() in C
size_t write (int fd, void* buf, size_t cnt);

Parameters
● fd: file descriptor
● buf: buffer to write data from.
● cnt: length of the buffer.

21
20CS2036 – Operating System Lab URK22CS1198

Return Value
● returns the number of bytes written on success.
● return 0 on reaching the End of File.
● return -1 on error.
● return -1 on signal interrupts.

Important Points about C write


● The file needs to be opened for write operations
● buf needs to be at least as long as specified by cnt because if buf size is less than the cnt then
buf will lead to the overflow condition.
● cnt is the requested number of bytes to write, while the return value is the actual number of
bytes written. This happens when fd has a less number of bytes to write than cnt.
● If write() is interrupted by a signal, the effect is one of the following:
● If write() has not written any data yet, it returns -1 and sets errno to EINTR.
If write() has successfully written some data, it returns the number of bytes it wrote before it was
interrupted.
Questions:
1. Write a C program to open a new file using Unix system call.

#include<stdio.h>
#include<fcntl.h>
int main()
{
int fd= creat("xyz.txt", S_IRWXU);
if( fd == -1)
printf("failed\n");
else
printf("success\n");
}

22
20CS2036 – Operating System Lab URK22CS1198

2. Write a C program to copy the content of one file to another file using Unix system call.

#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
int main() {
int sourceFile = open("xyz.txt", O_RDONLY);
int destinationFile = open("pqr.txt", O_CREAT, O_WRONLY);
char buffer[4096];
ssize_t bytesRead;
while ((bytesRead = read(sourceFile, buffer, sizeof(buffer))) > 0)
{ write(destinationFile, buffer, bytesRead);
}
}

3. Write a C program to concatenate two files and write it into third file using Unix system
call.

#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
int main() {
int file1 = open("xyz.txt", O_RDONLY);
int file2 = open("pqr.txt", O_RDONLY);
int outputFile = open("abc.txt", O_CREAT, O_WRONLY);

23
20CS2036 – Operating System Lab URK22CS1198

char buffer[4096];
ssize_t bytesRead;
while ((bytesRead = read(file1, buffer, sizeof(buffer))) > 0)
{ write(outputFile, buffer, bytesRead);
} while ((bytesRead = read(file2, buffer, sizeof(buffer))) > 0)
{ write(outputFile, buffer, bytesRead);
}
}

4. Write a C program to append some data to an existing file using Unix system call.

#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
int main() {
int file = open("xyz.txt", O_WRONLY | O_APPEND);
if (file == -1) {
perror("Error opening file");
return 1;
}
char data[] = "Additional data to append.";
write(file, data, sizeof(data) - 1);
}

24
20CS2036 – Operating System Lab URK22CS1198

5. Write a C program to randomly access a file using Unix system call.

#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
int main() {
int file = open("hij.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
if (file == -1) {
perror("Error opening file");
return 1;
}
lseek(file, 100, SEEK_SET);
const char *data = "Hello, Random Access!";
write(file, data, sizeof(data) - 1);
lseek(file, 0, SEEK_SET);
char buffer[100];
read(file, buffer, sizeof(buffer));
printf("Data read from file: %s\n", buffer);
}

Conclusion:
Thus the I/O system calls are executed successfully.

25

You might also like