Experiment 5 (OSII)
Experiment 5 (OSII)
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
2. Read():
Def
Syntax
3. Write():
Def
Syntax
Explain syntax in brief.
4. Close():
Def
Syntax
Conclusion
1) Open():
// C program to illustrate
// open system call
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
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);
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;
close(fd);
}
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.
1. Process control
2. File management
3. Device management
4. Information maintenance
5. Communication
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.
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.
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.
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.
*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.