Write a short note on system call or What is system call? or Explain system call.

Definition- A set instructions which provides an interface between Operating system and user programs is called system call.



-> Any single-CPU computer can execute only one instruction at a time. If a process is running a user program in user mode and needs a system service, such as reading data from a file, it has to execute a trap instruction to transfer control to the operating system.




Mechanism




-> To make the system-call mechanism clearer, let us take a quick look at the read system call.

-> It has three parameters: the first one specifying the file, the second one pointing to the buffer, and the third one giving the number of bytes to read.
-> Like nearly all system calls, it is invoked from C programs by calling a library procedure with the same name as the system call:read. A call from a C program might look like this:
count = read( fd, buffer, nbytes);


-> The system call (and the library procedure) return the number of bytes actually read in count. This value is normally the same as nbytes, but may be smaller, if, for example, end-of-file is encountered while reading.
-> If the system call cannot be carried out owing to an invalid parameter or a disk error, count is set to −1, and the error number is put in a global variable, errno.
-> The first and third parameters are called by value, but the second parameter is passed by reference, meaning that the address of the buffer (indicated by &) is passed, not the contents of the buffer.
-> Then comes the actual call to the library procedure (step 4). This instruction is the normal procedure-call instruction used to call all procedures.
-> The library procedure, possibly written in assembly language, typically puts the system-call number in a place where the operating system expects it, such as a register (step 5).
-> Then it executes a TRAP instruction to switch from user mode to kernel mode and start execution at a fixed address within the kernel (step 6).
-> The TRAP instruction is actually fairly similar to the procedure call instruction in the sense that the instruction following it is taken from a distant location and the return address is saved on the stack for use later.
-> The TRAP instruction also differs from the procedure-call instruction in two fundamental ways. First, as a side effect, it switches into kernel mode.The procedure call instruction does not change the mode. Second, rather than giving a relative or absolute address where the procedure is located, the TRAP instruction cannot jump to an arbitrary address.
-> The kernel code that starts following the TRAP examines the system-call number and then dispatches to the correct system-call handler, usually via a table of pointers to system-call handlers indexed on system-call number (step 7).
-> At that point the system-call handler runs (step 8).
-> Once it has completed its work, control may be returned to the user-space library procedure at the instruction following the TRAP instruction (step 9).
-> This procedure then returns to the user program in the usual way procedure calls return (step 10).
-> To finish the job, the user program has to clean up the stack, as it does after any procedure call (step 11).
-> Assuming the stack grows downward, as it often does, the compiled code increments the stack pointer exactly enough to remove the parameters pushed before the call to read.

source: MODERN OPERATING SYSTEMS FOURTH EDITION
ANDREW S. TANENBAUM,HERBERT BOS

Post a Comment

GTU done 2018 |