0% found this document useful (0 votes)
26 views15 pages

Introduction of System Call

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views15 pages

Introduction of System Call

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Introduction of System Call

Read Courses Jobs

In computing, a system call is a programmatic way in which a computer


program requests a service from the kernel of the operating system it is
executed on. A system call is a way for programs to interact with the
operating system. A computer program makes a system call when it makes
a request to the operating system’s kernel. System call provides the services
of the operating system to the user programs via Application Program
90%Interface(API).
Refund @Courses ItAptitude
providesEngineering Mathematics
an interface Discrete
between Mathematics
a process and Operating System DBM
an operating
system to allow user-level processes to request services of the operating
system. System calls are the only entry points into the kernel system. All
programs needing resources must use system calls.

A user program can interact with the operating system using a system call.
A number of services are requested by the program, and the OS responds by
launching a number of systems calls to fulfill the request. A system call can
be written in high-level languages like C or Pascal or in assembly language.
If a high-level language is used, the operating system may directly invoke
system calls, which are predefined functions.

A system call is a mechanism used by programs to request services from the


operating system (OS). In simpler terms, it is a way for a program to interact
with the underlying system, such as accessing hardware resources or
performing privileged operations.

A system call is initiated by the program executing a specific instruction,


which triggers a switch to kernel mode, allowing the program to request a
service from the OS. The OS then handles the request, performs the
necessary operations, and returns the result back to the program.

System calls are essential for the proper functioning of an operating system,
as they provide a standardized way for programs to access system
resources. Without system calls, each program would need to implement its
own methods for accessing hardware and system services, leading to
inconsistent and error-prone behavior.

Services Provided by System Calls


Process creation and management
Main memory management
File Access, Directory, and File system management
Device handling(I/O)
Protection
Networking, etc.
Process control: end, abort, create, terminate, allocate, and free
memory.
File management: create, open, close, delete, read files,s, etc.
Device management
Information maintenance
Communication

Features of System Calls


Interface: System calls provide a well-defined interface between user
programs and the operating system. Programs make requests by calling
specific functions, and the operating system responds by executing the
requested service and returning a result.
Protection: System calls are used to access privileged operations that are
not available to normal user programs. The operating system uses this
privilege to protect the system from malicious or unauthorized access.
Kernel Mode: When a system call is made, the program is temporarily
switched from user mode to kernel mode. In kernel mode, the program
has access to all system resources, including hardware, memory, and
other processes.
Context Switching: A system call requires a context switch, which
involves saving the state of the current process and switching to the
kernel mode to execute the requested service. This can introduce
overhead, which can impact system performance.
Error Handling: System calls can return error codes to indicate problems
with the requested service. Programs must check for these errors and
handle them appropriately.
Synchronization: System calls can be used to synchronize access to
shared resources, such as files or network connections. The operating
system provides synchronization mechanisms, such as locks or
semaphores, to ensure that multiple programs can access these resources
safely.

System Calls Advantages


Access to hardware resources: System calls allow programs to access
hardware resources such as disk drives, printers, and network devices.
Memory management: System calls provide a way for programs to
allocate and deallocate memory, as well as access memory-mapped
hardware devices.
Process management: System calls allow programs to create and
terminate processes, as well as manage inter-process communication.
Security: System calls provide a way for programs to access privileged
resources, such as the ability to modify system settings or perform
operations that require administrative permissions.
Standardization: System calls provide a standardized interface for
programs to interact with the operating system, ensuring consistency and
compatibility across different hardware platforms and operating system
versions.

How does System Call Work?


Here is the detailed explanation step by step how system call work:

User need special resources : Sometimes programs need to do some


special things which can’t be done without the permission of OS like
reading from a file, writing to a file , getting any information from the
hardware or requesting a space in memory.

Program makes a system call request : There are special predefined


instruction to make a request to the operating system. These instruction
are nothing but just a “system call”. The program uses these system calls
in its code when needed.

Operating system sees the system call : When the OS sees the system
call then it recongnises that the program need help at this time so it
temporarily stop the program execution and give all the control to special
part of itself called ‘Kernel’ . Now ‘Kernel’ solve the need of program.

Operating system performs the operations :Now the operating system


perform the operation which is requested by program . Example : reading
content from a file etc.

Operating system give control back to the program : After performing


the special operation, OS give control back to the program for further
execution of program .

Examples of a System Call in Windows and Unix


System calls for Windows and Unix come in many different forms. These are
listed in the table below as follows:

Process Windows Quiz

CreateProcess() Fork()

Process Control ExitProcess() Exit()

WaitForSingleObject() Wait()

Open()
CreateFile()
Read()
File manipulation ReadFile()
Write()
WriteFile()
Close()

SetConsoleMode() Ioctl()

Device Management ReadConsole() Read()

WriteConsole() Write()

GetCurrentProcessID() Getpid()
Information
SetTimer() Alarm()
Maintenance
Sleep() Sleep()
Process Windows Quiz

CreatePipe() Pipe()

Communication CreateFileMapping() Shmget()

MapViewOfFile() Mmap()

SetFileSecurity()
Chmod()
InitializeSecurityDescrip
Protection tor() Umask()

SetSecurityDescriptorgr Chown()
oup()

open(): Accessing a file on a file system is possible with the open() system
call. It gives the file resources it needs and a handle the process can use. A
file can be opened by multiple processes simultaneously or just one process.
Everything is based on the structure and file system.

read(): Data from a file on the file system is retrieved using it. In general, it
accepts three arguments:

1. A description of a file.
2. A buffer for read data storage.
3. How many bytes should be read from the file
Before reading, the file to be read could be identified by its file descriptor
and opened using the open() function.

wait(): In some systems, a process might need to hold off until another
process has finished running before continuing. When a parent process
creates a child process, the execution of the parent process is halted until
the child process is complete. The parent process is stopped using the wait()
system call. The parent process regains control once the child process has
finished running.

write(): Data from a user buffer is written using it to a device like a file. A
program can produce data in one way by using this system call. generally,
there are three arguments:
1. A description of a file.
2. A reference to the buffer where data is stored.
3. The amount of data that will be written from the buffer in bytes.

fork(): The fork() system call is used by processes to create copies of


themselves. It is one of the methods used the most frequently in operating
systems to create processes. When a parent process creates a child process,
the parent process’s execution is suspended until the child process is
finished. The parent process regains control once the child process has
finished running.

exit(): A system call called exit() is used to terminate a program. In


environments with multiple threads, this call indicates that the thread
execution is finished. After using the exit() system function, the operating
system recovers the resources used by the process.

Methods to pass parameters to OS


If a system call occur, we have to pass parameter to the Kernal part of the
Operating system.

For example look at the given open() system call:

//function call example

#include <fcntl.h>

int open(const char *pathname, int flags, mode_t mode);

Here pathname, flags and mode_t are the parameters.

So it is to be noted that :

We can’t pass the parameters directly like in an ordinary function call.


In Kernal mode there is a different way to perform a function call.
So we can’t run it in the normal address space that the process had already
created and hence we cant place the parameters in the top of the stack
because it is not available to the Kernal of the operating system for
processing. so we have to adopt any other methods to pass the parameters
to the Kernal of the OS.

We can done it through,

1. Passing parameters in registers


2. Address of the block is passed as a parameter in a register.
3. Parameters are pushed into a stack.

Let us discuss about each points in detail:

1. Passing parameters in registers.

It is the simplest method among the three


Here we directly pass the parameters to registers.
But it will it is limited when, number of parameters are greater than the
number of registers.
Here is the C program code:

// Passing parameters in registers.

#include <fcntl.h>
#include <stdio.h>

int main()
{
const char* pathname = "example.txt";
int flags = O_RDONLY;
mode_t mode = 0644;

int fd = open(pathname, flags, mode);


// in function call open(), we passed the parameters pathanme,flags,mode to t

if (fd == -1) {
perror("Error opening file");
return 1;
}

// File operations here...


close(fd);
return 0;
}

2.Address of the block is passed as parameters

It can be applied when the number of parameters are greater than the
number of registers.
Parameters are stored in blocks or table.
The address of the block is passed to a register as a parameter.
Most commonly used in Linux and Solaris.
Here is the C program code:

//Address of the block is passed as parameters

#include <stdio.h>
#include <fcntl.h>

int main() {
const char *pathname = "example.txt";
int flags = O_RDONLY;
mode_t mode = 0644;

int params[3];
// Block of data(parameters) in array
params[0] = (int)pathname;
params[1] = flags;
params[2] = mode;

int fd = syscall(SYS_open, params);


// system call

if (fd == -1) {
perror("Error opening file");
return 1;
}

// File operations here...

close(fd);
return 0;
}
3.Parameters are pushed in a stack

In this method parameters can be pushed in using the program and


popped out using the operating system
So the Kernal can easily access the data by retrieving information from
the top of the stack.
Here is the C program code

//parameters are pushed into the stack

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

int main() {
const char *pathname = "example.txt";
int flags = O_RDONLY;
mode_t mode = 0644;

int fd;
asm volatile(
"mov %1, %%rdi\n"
"mov %2, %%rsi\n"
"mov %3, %%rdx\n"
"mov $2, %%rax\n"
"syscall"
: "=a" (fd)
: "r" (pathname), "r" (flags), "r" (mode)
: "%rdi", "%rsi", "%rdx"
);

if (fd == -1) {
perror("Error opening file");
return 1;
}

// File operations here...

close(fd);
return 0;
}
Frequently Asked Question

Q.1: How does a system call work?

Answer:

When a program executes a system call, it transitions from user mode


to kernel mode, which is a higher privileged mode. The transition is
typically initiated by invoking a specific function or interrupting
instruction provided by the programming language or the operating
system.

Once in kernel mode, the system call is handled by the operating


system. The kernel performs the requested operation on behalf of the
program and returns the result. Afterward, control is returned to the
user-level program, which continues its execution.

Q.2: Why are system calls necessary?

Answer:

System calls are necessary for several reasons:

Access to privileged operations: Many operations, such as managing


hardware devices or modifying system configurations, require higher
privileges that are only accessible through system calls.

Resource management: System calls provide a standardized interface


for allocating and managing system resources like memory, files, and
devices, ensuring fair and controlled access by different processes.

Abstraction: System calls abstract the underlying complexities of the


operating system, allowing application developers to interact with the
system in a higher-level, platform-independent manner.

Security and protection: System calls enforce access control and


security policies, preventing unauthorized access to sensitive resources
and protecting the integrity of the system.

Unlock the Power of Placement Preparation!


Feeling lost in OS, DBMS, CN, SQL, and DSA chaos? Our Complete
Interview Preparation Course is the ultimate guide to conquer placements.
Trusted by over 100,000+ geeks, this course is your roadmap to interview
triumph.
Ready to dive in? Explore our Free Demo Content and join our Complete
Interview Preparation course.

Get paid for your published articles and stand a chance to win tablet, smartwatch
and exclusive GfG goodies! Submit your entries in Dev Scripter 2024 today.

Participate in Three 90 Challenge! Enroll in any GeeksforGeeks course and get 90%
refund by completing 90% course. Explore offer now.

Last Updated : 25 Sep, 2023 250

Previous Next

Operating System Services System Programs in Operating System

Share your thoughts in the comments Add Your Comment

Similar Reads
Difference between system call and Xv6 Operating System -adding a new
library call system call

Fork System Call in Operating System Implementation of sleep (system call)


in OS

Linux system call in Detail Difference between system() and


execl() call
What is System Call Interposition? Code for Fork System Call in OS

Remote Procedure Call (RPC) in Operating System - Difference Between


Operating System Distributed System and Parallel System

S Samit Ma…

Article Tags : Operating Systems

Additional Information

A-143, 9th Floor, Sovereign Corporate


Tower, Sector-136, Noida, Uttar Pradesh -
201305

Company Explore
About Us Job-A-Thon Hiring Challenge
Legal Hack-A-Thon
Careers GfG Weekly Contest
In Media Offline Classes (Delhi/NCR)
Contact Us DSA in JAVA/C++
Advertise with us Master System Design
GFG Corporate Solution Master CP
Placement Training Program GeeksforGeeks Videos
Apply for Mentor Geeks Community

Languages DSA
Python Data Structures
Java Algorithms
C++ DSA for Beginners
PHP Basic DSA Problems
GoLang DSA Roadmap
SQL Top 100 DSA Interview Problems
R Language DSA Roadmap by Sandeep Jain
Android Tutorial All Cheat Sheets
Tutorials Archive

Data Science & ML HTML & CSS


Data Science With Python HTML
Data Science For Beginner CSS
Machine Learning Tutorial Web Templates
ML Maths CSS Frameworks
Data Visualisation Tutorial Bootstrap
Pandas Tutorial Tailwind CSS
NumPy Tutorial SASS
NLP Tutorial LESS
Deep Learning Tutorial Web Design

Python Computer Science


Python Programming Examples GATE CS Notes
Django Tutorial Operating Systems
Python Projects Computer Network
Python Tkinter Database Management System
Web Scraping Software Engineering
OpenCV Python Tutorial Digital Logic Design
Python Interview Question Engineering Maths

DevOps Competitive Programming


Git Top DS or Algo for CP
AWS Top 50 Tree
Docker Top 50 Graph
Kubernetes Top 50 Array
Azure Top 50 String
GCP Top 50 DP
DevOps Roadmap Top 15 Websites for CP

System Design JavaScript


High Level Design JavaScript Examples
Low Level Design TypeScript
UML Diagrams ReactJS
Interview Guide NextJS
Design Patterns AngularJS
OOAD NodeJS
System Design Bootcamp Lodash
Interview Questions Web Browser

NCERT Solutions School Subjects


Class 12 Mathematics
Class 11 Physics
Class 10 Chemistry
Class 9 Biology
Class 8 Social Science
Complete Study Material English Grammar

Commerce UPSC Study Material


Accountancy Polity Notes
Business Studies Geography Notes
Economics History Notes
Management Science and Technology Notes
HR Management Economy Notes
Finance Ethics Notes
Income Tax Previous Year Papers

SSC/ BANKING Colleges


SSC CGL Syllabus Indian Colleges Admission & Campus Experiences
SBI PO Syllabus List of Central Universities - In India
SBI Clerk Syllabus Colleges in Delhi University
IBPS PO Syllabus IIT Colleges
IBPS Clerk Syllabus NIT Colleges
SSC CGL Practice Papers IIIT Colleges
Companies Preparation Corner
META Owned Companies Company-Wise Recruitment Process
Alphabhet Owned Companies Resume Templates
TATA Group Owned Companies Aptitude Preparation
Reliance Owned Companies Puzzles
Fintech Companies Company-Wise Preparation
EdTech Companies

Exams More Tutorials


JEE Mains Software Development
JEE Advanced Software Testing
GATE CS Product Management
NEET SAP
UGC NET SEO - Search Engine Optimization
Linux
Excel

Free Online Tools Write & Earn


Typing Test Write an Article
Image Editor Improve an Article
Code Formatters Pick Topics to Write
Code Converters Share your Experiences
Currency Converter Internships
Random Number Generator
Random Password Generator

@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved

You might also like