Winter Semester 2026–27 · VIT-AP · BACSE106 · Operating Systems · Dr. Krithika L.B

Lab 15: File-system operations using system calls

Theory link: 4.7 File concepts · 1.3 System calls

Experiment 15 of 15 Module 4 2 hours

Video walkthrough

Lab 15 walkthrough — File-system operations using system calls

Video coming soon

Lab 15 walkthrough — File-system operations using system calls

Recorded by Dr. Krithika L.B. Slot lab15-walkthrough

Aim

Aim. To create, write, read, reposition, append, copy and delete files using the system calls open, read, write, lseek, close and unlink.

Explanation

These are the file-management system calls of Session 1.3, operating on the open-file tables of Session 4.7.

CallPurpose
open(path, flags, mode)Open/create; returns a file descriptor. Flags: O_RDONLY, O_WRONLY, O_RDWR, O_CREAT, O_TRUNC, O_APPEND.
read(fd, buf, n)Read up to n bytes at the file pointer; returns bytes read (0 at end of file).
write(fd, buf, n)Write n bytes at the file pointer.
lseek(fd, offset, whence)Move the file pointer (SEEK_SET, SEEK_CUR, SEEK_END); returns the new position — direct access.
close(fd)Release the descriptor.
unlink(path)Delete a directory entry (the file is removed when no links remain).

Code

fileops.c
/* fileops.c - file-system operations using system calls (BACSE106 Lab 15) */
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>

int main(void)
{
    char buf[100];
    int fd, n;

    /* 1. create a file and write to it */
    fd = open("demo.txt", O_CREAT | O_WRONLY | O_TRUNC, 0644);
    if (fd < 0) { perror("open"); return 1; }
    write(fd, "Operating Systems\n", 18);
    write(fd, "File system calls\n", 18);
    close(fd);
    printf("Created demo.txt and wrote 36 bytes\n");

    /* 2. open it again and read everything */
    fd = open("demo.txt", O_RDONLY);
    n = read(fd, buf, sizeof(buf) - 1);
    buf[n] = '\0';
    printf("Read %d bytes:\n%s", n, buf);

    /* 3. reposition with lseek and read part of the file */
    lseek(fd, 10, SEEK_SET);                 /* jump to byte 10 */
    n = read(fd, buf, 7);
    buf[n] = '\0';
    printf("After lseek(fd, 10, SEEK_SET), read 7 bytes: \"%s\"\n", buf);
    printf("File size using lseek(fd, 0, SEEK_END) = %ld bytes\n", (long)lseek(fd, 0, SEEK_END));
    close(fd);

    /* 4. append to the file */
    fd = open("demo.txt", O_WRONLY | O_APPEND);
    write(fd, "Appended line\n", 14);
    close(fd);
    printf("Appended 14 bytes\n");

    /* 5. copy demo.txt to copy.txt block by block */
    int in = open("demo.txt", O_RDONLY);
    int out = open("copy.txt", O_CREAT | O_WRONLY | O_TRUNC, 0644);
    while ((n = read(in, buf, sizeof(buf))) > 0)
        write(out, buf, n);
    close(in);
    close(out);
    printf("Copied demo.txt to copy.txt\n");

    /* 6. delete the copy */
    if (unlink("copy.txt") == 0)
        printf("Deleted copy.txt\n");
    return 0;
}

Download fileops.c

How to run

  1. gcc fileops.c -o fileops
  2. ./fileops
  3. cat demo.txt and ls -l demo.txt to check the result.
  4. Optional: strace -e trace=openat,read,write,lseek,close,unlink ./fileops to watch the system calls.

Sample input & output

fileops on Linux

Input
(none)
Output
Output
$ ./fileops
Created demo.txt and wrote 36 bytes
Read 36 bytes:
Operating Systems
File system calls
After lseek(fd, 10, SEEK_SET), read 7 bytes: "Systems"
File size using lseek(fd, 0, SEEK_END) = 36 bytes
Appended 14 bytes
Copied demo.txt to copy.txt
Deleted copy.txt
$ cat demo.txt
Operating Systems
File system calls
Appended line

Try it yourself

  1. Write mycp src dst that copies any file given on the command line.
  2. Write mycat that prints a file using only read() and write() to fd 1.
  3. Use stat() to print a file's size, inode number and permissions.
  4. Count the lines in a file by reading it in 64-byte blocks.

Viva questions

Prepare these for the lab evaluation.

Revealed 0
What is a file descriptor?

A small integer index into the process's open-file table; 0, 1 and 2 are stdin, stdout and stderr.

Difference between read() and fread()?

read() is a system call; fread() is a buffered C-library function that calls read() underneath.

What does lseek(fd, 0, SEEK_END) return?

The size of the file (the offset of its end).

Which access method does lseek provide?

Direct (random) access.

What does O_APPEND guarantee?

Every write goes to the current end of the file.