System Calls in Operating System

Lists of Questions answers And Study notes

  • (1) What is a system call? How it is handled by an OS? OR Write a short note on system calls.


Question -1 What is a system call? How it is handled by an OS? OR  Write a short note on system calls.

  • The interface between the operating system and the user programs is defined by the set of system calls that the operating system provides.
  • The system calls available in the interface vary from operating system to operating system.
  • 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 or system call instruction to transfer control to the operating system.
  • The operating system then figures out what the calling process wants by inspecting the parameters.
  • Then it carries out the system call and returns control to the instruction following the system call.


Following steps describe how a system call is handled by an operating system.

system calls steps

  • To understand how OS handles system calls, let us take an example of read system call.
  • Read system call 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 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-offile is encountered while reading.
  • If the system call cannot be carried out, either due to an invalid parameter or a disk error, count is set to -1, and the error number is put in a global variable, errno.
  • Programs should always check the results of a system call to see if an error occurred.
  • System calls are performed in a series of steps.
  • To make this concept clearer, let us examine the read call discussed above.
  • In preparation for calling the read library procedure, which actually makes the read system call, the calling program first pushes the parameters onto the stack, as shown in steps 1-3 in Fig.
  • 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 kernel code that starts 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 the system call handler 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)


Some of the major POSIX system calls.

Process management

Call Description
pid = fork( ) Create a child process identical to the parent
pid = waitpid(pid, &statloc, options) Wait for a child to terminate
s = execve(name, argv, environp) Replace a process’ core image
exit(status) Terminate process execution and return status


File management

Call Description
fd = open(file, how, …) Open a file for reading, writing, or both
s = close(fd) Close an open file
n = read(fd, buffer, nbytes) Read data from a file into a buffer
n = write(fd, buffer, nbytes) Write data from a buffer into a file
position = lseek(fd, offset, whence) Move the file pointer
s = stat(name, &buf) Get a file’s status information


Director and file system management

Call Description
s = mkdir(name,mode) Create a new directory
s = rmdir(name) Remove an empty directory
s = link(name1, name2) Create a new entry, name2, pointing to name1
s = unlink(name) Remove a directory entry
s = mount(special, name, flag) Mount a file system
s = umount(special) Unmount a file system



Call Description
s = chdir(dir name) Change the working directory
s = chmod(name,mode) Change a file’s protection bits
s = kill(pid, signal) Send a signal to a process
seconds = time(&seconds) Get the elapsed time since Jan. 1, 1970