System calls in POSIX (Portable Operating System Interface): POSIX is a standard for operating systems which was created by IEEE (Institute of Electrical and Electronics Engineers) in 1988. A system call is made to an operating system to access a system service like reading from a file.
For Example: read system call in POSIX: count = read(fd, buffer, nbytes);
Here, count specifies the number of bytes read, fd parameter is used to specify the file, buffer parameter points to the buffer and and nbytes provides number of bytes to read. For a disk error or an invalid parameter error count is set to -1. Error number is put in errno which is a global variable.
Steps in executing read system call:
1) Parameter nbytes is pushed to stack in reverse order. It is called by value.
2) Parameter &buffer is pushed to stack in reverse order. It is called by reference. Only the address of the buffer (specified by &) is passed and not the actual contents of the buffer.
3) Parameter fd is pushed to stack in reverse order. It is called by value.
4) Library procedure read is called.
5) System call number is placed in register for the operating system.
6) TRAP instruction is executed for switching from user mode to kernel mode. Execution is started at a fixed address in kernel.
7) System call number is read by kernel and is dispatched to a particular system call handler.
8) System call handler is run.
9) Control is returned to the user space library procedure.
10) Control is returned to the caller.
11) Stack is cleaned up by the user program.