Read System Call



Arguments: File Descriptor(Integer) and a Buffer (a String/Integer variable) into which a word is to be read from the file

Return Value:

0 Success
-1 File Descriptor given is invalid
-2 File pointer has reached the end of file

Description : The Read operation reads one word from the position pointed by the file pointer and stores it into the buffer. After each read operation, the file pointer advances to the next word in the file.



Control flow diagram for Read system call


Algorithm:

Set the MODE_FLAG in the process table entry to 7, 
indicating that the process is in the read 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 input is to be read from terminal    /* indicated by a file descriptor value of -1 */
	Call the terminal_read()function in the Device manager  Module .
			 
/* If not terminal, read from file. */
 else 

	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 the Per Process Resource Table entry. Get the index of the Inode Table entry from the Open File Table entry. Acquire the Lock on the File by calling the acquire_inode() function in the Resource Manager module. If acquiring the inode fails, return -1. Get the Lseek position from the Open File Table entry. Get the physical address curresponding to the logical address of Memory Buffer address given as input. If the File corresponds to Root file ( indicated by Inode index as INODE_ROOT) If the lseek value is equal to the root file size(480), release_inode() return -2. Read from the word at lseek position in memory copy of root file to the translated memory address. /* Use SPL Constant ROOT_FILE */ Increment the Lseek position in the Open File Table. else If lseek position is same as the file size, release_inode() and return -2.  /* End of file reached */ Find the disk block number and the position in the block from which input is read.
Get the block index from lseek position.   /* lseek/512 gives the index of the block */ Get the disk block number corresponding to the block index from the Inode Table . Get the offset value from lseek position.   /* lseek%512 gives the position to be read from.*/
Read the data from the File Buffer by calling the buffered_read() function in the File Manager module. Increment the Lseek position in the Open File Table. Release the Lock on the File by calling the release_inode() function in the Resource Manager module. Switch back to the user stack by resoting USER SP from the process table. Set the MODE_FLAG in the process table entry of the parent process to 0. Return 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.

Questions: