As an example, consider the open system call in UNIX:
open(char *path, int flags, int mode)
This system call opens a specified file and returns a descriptor for that file. path is the address of a string of characters representing a path name which identifies the file to be opened. flags define how the file is to be opened. It can be set to any of several values, e.g. O_RDONLY (for reading only), O_WRONLY (for writing only), O_RDWR (for reading and writing), O_CREAT (create file if it does not exist). mode is only used with the O_CREAT flag and specifies the mode with which the file is to be created.
Some common system calls in UNIX are fork(), read(), write(), execve(), wait() and exit().
Why do we need them?
Increased sharing of system resources in modern operating systems has given
rise to various problems. A bug in a program can adversely affect all the
other programs running on the system. An erroneous program can modify the
program or data of another program or even modify the operating system program
itself. For the proper operation of any system, it is necessary to protect
the operating system and other programs from any malfunctioning program. An
approach used most often is having two (or more) modes of execution -
user mode and privileged mode .
At system boot time, the hardware starts in privileged mode and loads the operating system. User processes are started in user mode. Whenever a trap or interrupt occurs, the hardware switches from user mode to privileged mode. The operating system is protected from erroneous programs by designating some of the machine instructions as privileged instructions, those that can be executed only in privileged mode. I/O instructions and instructions to modify the memory-management registers or the timer are privileged instructions. The halt instruction is also privileged.
Since user programs cannot perform I/O but need to, they have to ask the operating system to do it for them. System calls are the way the user programs request the operating system to perform some instructions for them.
Implementation
details
A system call usually takes the form of a trap to a specific location in the interrupt vector. This trap can be executed by a generic trap instruction or by a syscall instruction. A system call is treated as a software interrupt by the hardware. Control passes through an interrupt vector to a service routine in the operating system. The kernel examines the interrupting instruction to determine the type of system call, verifies the correctness of the parameters and executes the request, returning control to the instruction following the system call. The parameters to the system call may be passed in registers, on the stack, or in memory with pointers to memory locations passed in registers.
Upon a program error, the operating system may abnormally terminate the program. When abnormally terminating a program, an error message is displayed and a memory dump of the program is saved in a core file. The user can then examine this memory dump and hopefully fix the problem.
Nachos System Call | UNIX System Call | Nachos Functionality |
---|---|---|
Halt() | None | Stop Nachos. |
Exit(int Status) | exit() | Quit the user program making the call. |
Exec(char *name) | execv(),execl(),execvp() etc. | Run the executable identified by "name". |
Join(SpaceId id) | wait(), waitpid(), wait3() etc. | Wait for "id". Return only when "id" is done. | Create(char *name) | creat() | Create a nachos file with name "name". |
Open(char *name) | open() | Open the nachos file with name "name". |
Write(char *buffer, int size, OpenFileId id) | write(), writev() | Write "size" bytes from "buffer" to the open file. |
Read(char *buffer, int size, OpenFilesId id) | read(), readv() | Read "size" bytes from open file into "buffer". |
Close(OpenFileId id) | close() | Close the file. |
Fork(void (*func)()) | fork() | Fork a thread to run a procedure "func". |
Yield(void (*func)()) | None | Yield the CPU to another runnable thread. |
Source code in code/userprog
Lab
Quiz
Last modified on Thursday, 29-May-97 17:14:41 EDT