System Call
Dr. Sarvesh Tanwar
System Call
• A system call is a request to the operating system for a service.
• A system call is a mechanism that provides the interface between a
process and the operating system. It is a programmatic method in
which a computer program requests a service from the kernel of the
OS.
• System call offers the services of the operating system to the user
programs via API (Application Programming Interface). System calls
are the only entry points for the kernel system.
Example of System Call
• For example if we need to write a program code to read data from
one file, copy that data into another file. The first information that the
program requires is the name of the two files, the input and output
files.
In an interactive system, this type of program execution requires some
system calls by OS.
• First call is to write a prompting message on the screen
• Second, to read from the keyboard, the characters which define the
two files.
3
How System Call Works?
4
System call open()
• Whenever data in a file accessed, we have to first open the respective file. To
open the file open() system call is used
• Syntax of open system call
• fd=open(file path ,flags, modes)
• Where…
• -File path -> It is string which has path of the file that we want to open. The
path may be relative or absolute and must be with the file name.
• -Flags -> This specifies the way by which the file should be opened , i.e.
reading or writing or for both
• -modes -> This specifies the “file permission” to set, if the file is being created
for the first time
Return value of system call open()
• The system call in turn returns an integer value. - If successful the
returned integer is called as “file descriptor” which can be further
used for file I/O like calls, such as read, write, lseek, etc.
• On failure the returned integer is -1,indicating that this system call is
failed to open the specified file
Steps of open system call
• Kernel first converts the “file path string” into respective files inode by
using namei() algorithm
• When it found the inode (this is incore copy of inode) it checks the
permission field in in-core structure with those of given 3rd
parameter (if any)
• If the file dose not exist or if the permission are invalid for accessing
the file, kernel return error
• If everything up till now is “ok” ,then kernel makes an entry in the
kernels file table i.e. global. While making this entry kernel does four
things
Some System Calls For Process
Management
Why do you need System Calls
in OS?
• Reading and writing from files demand system calls.
• If a file system wants to create or delete files, system calls are
required.
• System calls are used for the creation and management of new
processes.
• Network connections need system calls for sending and receiving
packets.
• Access to hardware devices like scanner, printer, need a system call.
11
Types of System calls
• Process Control
• File Management
• Device Management
• Information Maintenance
• Communications
12
13
Process Control
This system calls perform the task of process creation, process
termination, etc.
• End and Abort
• Load and Execute
• Create Process and Terminate Process
• Wait and Signed Event
• Allocate and free memory
14
File Management
File management system calls handle file manipulation jobs like
creating a file, reading, and writing, etc.
• Functions:
• Create a file
• Delete file
• Open and close file
• Read, write, and reposition
• Get and set file attributes
15
Device Management
Device management does the job of device manipulation like reading
from device buffers, writing into device buffers, etc.
• Functions
• Request and release device
• Logically attach/ detach devices
• Get and Set device attributes
16
Information Management
• It handles information and its transfer between the OS and the user
program.
• Get or set time and date
• Get process and device attributes
17
Communication
• These types of system calls are specially used for interprocess
communications.
• Create, delete communications connections
• Send, receive message
• Help OS to transfer status information
• Attach or detach remote devices
18
Some Important System Call
wait()
• In some systems, a process needs to wait for another process to complete its
execution. This type of situation occurs when a parent process creates a child
process, and the execution of the parent process remains suspended until its child
process executes.
• The suspension of the parent process automatically occurs with a wait() system call.
When the child process ends execution, the control moves back to the parent
process.
fork()
• Processes use this system call to create processes that are a copy of themselves.
With the help of this system Call parent process creates a child process, and the
execution of the parent process will be suspended till the child process executes.
19
exec()
• This system call runs when an executable file in the context of an
already running process that replaces the older executable file.
However, the original process identifier remains as a new process is
not built, but stack, data, head, data, etc. are replaced by the new
process.
kill():
• The kill() system call is used by OS to send a termination signal to a
process that urges the process to exit. However, a kill system call does
not necessarily mean killing the process and can have various
meanings.
20
exit():
• The exit() system call is used to terminate program execution.
Specially in the multi-threaded environment, this call defines that the
thread execution is complete. The OS reclaims resources that were
used by the process after the use of exit() system call.
21
Summary
Categories Windows Unix
CreateProcess() ExitProcess()
Process control WaitForSingleObject() fork() exit() wait()
SetConsoleMode() ReadConsole()
Device manipulation WriteConsole() loctl() read() write()
CreateFile() ReadFile() WriteFile()
File manipulation Open() Read() write() close!)
CloseHandle()
Information
maintanence GetCurrentProcessID() SetTimer() Sleep() getpid() alarm() sleep()
CreatePipe() CreateFileMapping()
Communication MapViewOfFile() Pipe() shm_open() mmap()
Protection SetFileSecurity() InitlializeSecurityDescriptor() Chmod() Umask() Chown()
SetSecurityDescriptorGroup ()
22
Some System Calls For File
Management
Some System Calls For Directory
Management
Some System Calls For
Miscellaneous Tasks