Skip to content

Close System Call

Arguments: File Descriptor (Integer)

Return Value:

0 Success
-1 File Descriptor given is invalid

Description

The Close system call closes an open file. The file descriptor ceases to be valid once the close system call is invoked.

Control flow diagram for *Close* system call

Algorithm


Set the MODE_FLAG in the process table entry to 3, 
indicating that the process is in the close system call.

//Switch to Kernel Stack - See Kernel Stack Management during System Calls. 
Save the value of SP to the USER SP field in the Process Table entry of the process.
Set the value of SP to the beginning of User Area Page.

If file descriptor is invalid, return -1.    /* File descriptor value should be within the range 0 to 7 (both included). */

Locate the Per-Process Resource Table of the current process.
    Find the PID of the current process from the System Status Table.
    Find the User Area page number from the Process Table entry.
    The Per-Process Resource Table is located at the  RESOURCE_TABLE_OFFSET from the base of the  User Area Page

If the Resource identifier field of the Per Process Resource Table entry is invalid or does not indicate a FILE, return -1.   

/* No file is open with this file descriptor. */

Get the index of the Open File Table entry from Per-Process Resource Table entry.

Call the close() function in the File Manager module with the Open File Table index as arguement.

Invalidate the Per-Process Resource Table entry.

Set the MODE_FLAG in the process table entry to 0.
Switch back to the user stack.

Return from system call with 0.    /* success */

Note

At each point of return from the system call, remember to reset the MODE FLAG and switch back to the user stack.

Question

Why did we not check if the file is locked?

Back to top