0% found this document useful (0 votes)
23 views9 pages

Experiment 5 (OSII)

The document outlines an experiment focused on file management using low-level file access system calls, specifically detailing the functions open(), read(), write(), and close(). It includes definitions, syntax, and example C programs for each system call, demonstrating their usage in file operations. The conclusion emphasizes the importance of these system calls in systems programming and software development.

Uploaded by

devab52664
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)
23 views9 pages

Experiment 5 (OSII)

The document outlines an experiment focused on file management using low-level file access system calls, specifically detailing the functions open(), read(), write(), and close(). It includes definitions, syntax, and example C programs for each system call, demonstrating their usage in file operations. The conclusion emphasizes the importance of these system calls in systems programming and software development.

Uploaded by

devab52664
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/ 9

Experiment 5

Title : Study & Implement File Management using Low Level file Access System calls.
Aim: To Learn about File Management using low level file access system calls.
Theory: write theory about low level file access system calls.
 Def of System Call
 System Calls are divided in 5 categories:

1. Process control
2. File management
3. Device management
4. Information maintenance
5. Communication
 File management system calls:

1. Open():
 Def

 Syntax

 Explain syntax in brief.

2. Read():
 Def

 Syntax

 Explain syntax in brief.

3. Write():
 Def

 Syntax
Explain syntax in brief.
4. Close():
 Def

 Syntax

 Explain syntax in brief.

Conclusion

1) Open():
// C program to illustrate
// open system call
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

extern int errno;

int main()
{
// if file does not have in directory
// then file foo.txt is created.
int fd = open("foo.txt", O_RDONLY | O_CREAT);
printf("fd = %d\n", fd);

if (fd == -1) {
// print which type of error have in a code
printf("Error Number % d\n", errno);

// print program detail "Success or failure"


perror("Program");
}
return 0;
}
2) Close():
// C program to illustrate close system Call
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

int main()
{
int fd1 = open("foo.txt", O_RDONLY);
if (fd1 < 0) {
perror("c1");
exit(1);
}
printf("opened the fd = % d\n", fd1);
// Using close system Call
if (close(fd1) < 0) {
perror("c1");
exit(1);
}
printf("closed the fd.\n");
}

3) Read():
// C program to illustrate
// read system Call
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

int main()
{
int fd, sz;
char* c = (char*)calloc(100, sizeof(char));

fd = open("foo.txt", O_RDONLY);
if (fd < 0) {
perror("r1");
exit(1);
}

sz = read(fd, c, 10);
printf("called read(% d, c, 10). returned that"
" %d bytes were read.\n",
fd, sz);
c[sz] = '\0';
printf("Those bytes are as follows: % s\n", c);

return 0;
}
4) Write():
// C program to illustrate
// write system Call
#include<stdio.h>
#include <fcntl.h>
main()
{
int sz;

int fd = open("foo.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);


if (fd < 0)
{
perror("r1");
exit(1);
}

sz = write(fd, "hello geeks\n", strlen("hello geeks\n"));

printf("called write(% d, \"hello geeks\\n\", %d)."


" It returned %d\n", fd, strlen("hello geeks\n"), sz);

close(fd);
}

*Theory of Low-Level File Access System Calls*

*Definition of System Call*

A system call is a request from a program to the operating system (OS) to perform a
specific service or function. System calls provide a way for programs to interact with the
OS and access hardware resources.

*Categories of System Calls*

System calls are divided into five categories:

1. Process control
2. File management
3. Device management
4. Information maintenance
5. Communication

*File Management System Calls*

File management system calls are used to interact with files and directories. The following
are some common file management system calls:

*Open()*
*Definition:* The `open()` system call is used to open a file and return a file descriptor.

*Syntax:* `int open(const char *pathname, int flags);`

- `pathname`: the path to the file to be opened


- `flags`: a set of flags that specify the mode in which the file should be opened (e.g., read
-only, write-only, read-write)

The `open()` system call returns a file descriptor, which is a unique integer that identifies
the open file.

*Read()*
*Definition:* The `read()` system call is used to read data from an open file.

*Syntax:* `ssize_t read(int fd, void *buf, size_t count);`


- `fd`: the file descriptor of the open file
- `buf`: a buffer to store the read data
- `count`: the number of bytes to read

The `read()` system call returns the number of bytes actually read, which may be less than
the requested `count`.

*Write()*
*Definition:* The `write()` system call is used to write data to an open file.

*Syntax:* `ssize_t write(int fd, const void *buf, size_t count);`

- `fd`: the file descriptor of the open file


- `buf`: a buffer containing the data to be written
- `count`: the number of bytes to write

The `write()` system call returns the number of bytes actually written, which may be less
than the requested `count`.

*Close()*
*Definition:* The `close()` system call is used to close an open file.

*Syntax:* `int close(int fd);`

- `fd`: the file descriptor of the open file


The `close()` system call returns 0 on success, or -1 on error.

*Conclusion*

In conclusion, the `open()`, `read()`, `write()`, and `close()` system calls provide a way for
programs to interact with files and directories at a low level. These system calls are
essential for file management and are used by many programs and libraries.
Understanding how these system calls work is important for systems programming and
software development.

You might also like