0% found this document useful (0 votes)
5 views4 pages

ui3

The document provides detailed explanations of various system calls in the UNIX Operating System, including the open, read, and write system calls, as well as data structures like the inode table and file table. It outlines the steps involved in these operations and how they manage file access and permissions. Additionally, it discusses the mount and unmount system calls for managing file systems within the directory tree.
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)
5 views4 pages

ui3

The document provides detailed explanations of various system calls in the UNIX Operating System, including the open, read, and write system calls, as well as data structures like the inode table and file table. It outlines the steps involved in these operations and how they manage file access and permissions. Additionally, it discusses the mount and unmount system calls for managing file systems within the directory tree.
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/ 4

ui3.

md 2025-04-07

Here are the answers to your questions based on the "Design of the UNIX Operating System" (Maurice J.
Bach):

1. Explain open system call

The open system call establishes a connection between a process and a file.
Steps:

1. The kernel parses the pathname using namei to find the file's inode.
2. Checks permissions (read/write access based on flags like O_RDONLY, O_WRONLY, O_RDWR).
3. Allocates a file table entry (kernel-wide) and a user file descriptor (process-specific).
4. Initializes the file table entry with:
File offset = 0 (unless O_APPEND is set).
Access mode (read/write).
Pointer to the inode.
5. Returns a file descriptor (integer) to the process for future operations (e.g., read, write).

Flags:

O_CREAT: Create file if it doesn’t exist.


O_TRUNC: Truncate file to length 0.
O_APPEND: Set offset to end-of-file before writes.

2. Data Structures for Managing Files in UNIX

Three key data structures manage opened files:

1. Inode Table:

Contains in-core copies of disk inodes (file metadata: size, permissions, disk blocks).
Shared across processes opening the same file.

2. File Table:

Global kernel structure with entries for each open instance of a file.
Stores:
File offset (current read/write position).
Access mode (read/write).
Pointer to the inode.
Reference count (number of file descriptors pointing to it).

3. User File Descriptor Table:

Per-process array indexed by file descriptor (e.g., fd1, fd2).


Each entry points to a file table entry.

3. Status of Data Structures After System Calls

1/4
ui3.md 2025-04-07

Process A:

fd1 = open("a.txt", O_RDONLY):

Inode table: Loads inode for a.txt (reference count = 1).


File table: New entry with offset=0, mode=read-only, pointer to a.txt inode.
User descriptor table: fd1 points to this file table entry.

fd2 = open("stack.c", O_RDWR):

New inode for stack.c (refcount=1).


New file table entry (offset=0, mode=read-write).
fd2 points to this entry.

fd3 = open("a.txt", O_WRONLY):

Reuses a.txt inode (refcount=2).


New file table entry (offset=0, mode=write-only).
fd3 points to this entry.

Process B:

fd1 = open("a.txt", O_RDONLY):

Reuses a.txt inode (refcount=3).


New file table entry (offset=0, mode=read-only).
fd1 in Process B points to this entry.

fd2 = open("queue.c", O_RDWR):

New inode for queue.c (refcount=1).


New file table entry (offset=0, mode=read-write).
fd2 points to this entry.

Summary:

a.txt inode: refcount=3 (shared by 3 file table entries across both processes).
Each open creates a new file table entry, even for the same file.

4. mount and unmount System Calls

mount:

Attaches a new file system (e.g., a disk partition) to the directory tree.
Kernel actions:
1. Checks if the mount point is a directory.
2. Verifies the device is a valid file system.
3. Updates the mount table with device and inode of mount point.
4. Sets flag in mount point inode to indicate it’s a mount point.

unmount:
2/4
ui3.md 2025-04-07

Detaches a file system.


Checks that no files on the file system are in use.
Flushes pending writes and updates the superblock.

5. read System Call

Steps:

1. Uses fd to find the file table entry (contains offset and inode pointer).
2. Checks if the file is opened for reading.
3. Uses bmap to convert logical offset (124 in your example) to disk block number.
4. Reads data from disk into a buffer cache block.
5. Copies data from buffer to user space (buf).
6. Updates the file offset in the file table entry.

Returns: Number of bytes read (or -1 on error).

6. write System Call

Steps:

1. Uses fd to locate the file table entry and inode.


2. Checks write permissions.
3. Converts offset to disk block(s) via bmap.
4. Allocates new disk blocks if needed (alloc).
5. Copies data from user space (buf) to kernel buffer.
6. Marks buffer as "dirty" (delayed write).
7. Updates file size and modification time in inode.

Returns: Number of bytes written (or -1 on error).

7. Disk Blocks Read for read(fd, buf, 2000)

Given:

Block size = 1KB (1024 bytes).


Current offset = 124.
Requested bytes = 2000.

Calculation:

1. First block:
Bytes remaining in block 0: 1024 - 124 = 900.
Read 900 bytes (covers offset 124–1023).
2. Second block:
Remaining bytes: 2000 - 900 = 1100.
Read next full block (1024 bytes, covers 1024–2047).
3. Third block:

3/4
ui3.md 2025-04-07

Remaining bytes: 1100 - 1024 = 76.


Read 76 bytes from block starting at offset 2048.

Total blocks read: 3 (partial first block + full second block + partial third block).
Bytes per block:

Block 1: 900 bytes (from offset 124).


Block 2: 1024 bytes.
Block 3: 76 bytes.

Let me know if you need further clarification!

4/4

You might also like