Lab 15 walkthrough — File-system operations using system calls
Lab 15 walkthrough — File-system operations using system calls
Recorded by Dr. Krithika L.B. Slot lab15-walkthrough
Theory link: 4.7 File concepts · 1.3 System calls
Lab 15 walkthrough — File-system operations using system calls
Recorded by Dr. Krithika L.B. Slot lab15-walkthrough
Aim. To create, write, read, reposition, append, copy and delete files using the system calls open, read, write, lseek, close and unlink.
These are the file-management system calls of Session 1.3, operating on the open-file tables of Session 4.7.
| Call | Purpose |
|---|---|
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). |
/* 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;
}gcc fileops.c -o fileops./fileopscat demo.txt and ls -l demo.txt to check the result.strace -e trace=openat,read,write,lseek,close,unlink ./fileops to watch the system calls.$ ./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 linemycp src dst that copies any file given on the command line.mycat that prints a file using only read() and write() to fd 1.stat() to print a file's size, inode number and permissions.Prepare these for the lab evaluation.
Revealed 0A small integer index into the process's open-file table; 0, 1 and 2 are stdin, stdout and stderr.
read() is a system call; fread() is a buffered C-library function that calls read() underneath.
The size of the file (the offset of its end).
Direct (random) access.
Every write goes to the current end of the file.