SlideShare a Scribd company logo
1 of 17
© 2010-19 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
System Calls
2© 2010-19 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
What to Expect?
W's of System Calls
System Call vs Library Function
System Call Tracing
Hands-On
3© 2010-19 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
W's of System Calls
User programs vs Kernel programs
Runs in different spaces
Runs with different privileges
User space not allowed access to Kernel space
But they need the Kernel services
OS provides service points
For User programs
To request services from the Kernel
In Linux, these are called System Calls
4© 2010-19 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
System Calls in Linux
About 300 in count
Listing: /usr/include/asm/unistd.h
Provide layer between
Kernel Space (typically hardware)
User Space (typically user process)
Serve three purposes
Provide an Abstracted h/w interface for user space
Ensures System security and stability
Makes Process Management easier
5© 2010-19 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Working of a Linux System Call
Implemented as an ordinary function in the Linux Kernel
Executes like others in the Kernel Space
However, the call to that function isn't ordinary
When a user program makes a system call
Arguments are packaged up and handed to the kernel
A special procedure is required to transfer control to the kernel
Kernel takes over execution of the program until the call completes
Kernel transfers control back to the program with return value
Special procedure is typically achieved using “trap”
6© 2010-19 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
System Call Execution Flow
…
xyz()
...
xyz() {
…
SYSCALL
…
}
system_call:
…
sys_xyz()
SYSEXIT
sys_xyz() {
…
...
}
System Call
invocation in
application
program
Wrapper
routine in libc
standard
library
System Call
handler
System Call
service routine
User Mode Kernel Mode
7© 2010-19 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Linux System Call Wrappers
Every System Call has standard steps
GNU C library (glibc) abstracts them
By wrapping with functions of same name
For easy invocation
Examples
I/O functions: open, read, ...
We rarely invoke direct system calls
But rather these system call (wrapper) functions
Any Exception?
Custom defined system call – using syscall(sno, ...)
8© 2010-19 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Contrast with a Library Function
A library function is an ordinary function
It resides in a library external to the program
But in the User Space only
Moreover, the call to it is also ordinary
Arguments placed in processor registers or the stack
Execution transferred to the start of the function
Typically resides in a loaded shared library
In the User Space only
Examples
fopen, printf, getopt, mkstemp (all from glibc)
9© 2010-19 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Return Values
Library functions often return pointers
Example: FILE * fp = fopen("harry","r");
NULL indicates failure
System calls usually return an integer
Example: int res = open(“harry”, O_RDONLY);
Return value
>= 0 indicates success
< 0, typically -1 indicates failure, and error is set in errno
Note the counter intuitive return of System Calls
Opposite way round
Cannot use as Boolean
10© 2010-19 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
More Information
Manual Sections
2 System calls e.g. _exit, read, write
3 Library calls e.g. exit, printf
7 Miscellaneous e.g. ascii, fifo, pthreads
9 POSIX Programmer Manual
Info pages are also available
11© 2010-19 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Tracing System Calls
Command: strace <program> [args]
Traces the execution of <program>
And Lists
System Calls made by <program>
Signals received by <program>
Controlled by various options
An interesting one is “-e”
Example
strace cat /dev/null
12© 2010-19 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Pros & Cons
Pros
System calls provide direct & hence more control over the
kernel services
Library functions abstract the nitty-gritty of architecture or
OS specific details of the system calls
Library functions can provide wrappers over repeated set
of system calls
Cons
Library functions may have overheads
System calls at times may expose the underlying system
dependency
13© 2010-19 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Let's try some Examples
System Call Invocation
System calls vs Library functions
File Operations
Observe the various system calls invoked
Use strace
14© 2010-19 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
System Call: access
Helps to determine whether the calling process
has access permission for a particular file
int access(const char *pathname, int mode);
pathname – Path to the file
mode – Accessibility checks. F_OK or a mask
consisting of bitwise or of R_OK, W_OK and X_OK
15© 2010-19 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
System Call: fcntl
Is used for performing the various advanced
operations on an open file descriptor
It allows to place a read or write lock on the file
More than one process can hold the read lock,
while only one process can hold the write lock
int fcntl(int fd, int cmd, ... /* arg */ )
fd – File descriptor
cmd – Operation to perform
… – Arguments
16© 2010-19 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
What all have we learnt?
W's of System Calls
Working of a System Call & syscall()
System Call Wrapper Functions
System Call vs Library Function
Pros & Cons
System Call Tracing
Hands-On
17© 2010-19 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Any Queries?

More Related Content

What's hot (20)

Embedded Software Design
Embedded Software DesignEmbedded Software Design
Embedded Software Design
 
Processes
ProcessesProcesses
Processes
 
Linux Memory Management
Linux Memory ManagementLinux Memory Management
Linux Memory Management
 
Kernel Debugging & Profiling
Kernel Debugging & ProfilingKernel Debugging & Profiling
Kernel Debugging & Profiling
 
BeagleBone Black Bootloaders
BeagleBone Black BootloadersBeagleBone Black Bootloaders
BeagleBone Black Bootloaders
 
File System Modules
File System ModulesFile System Modules
File System Modules
 
System calls
System callsSystem calls
System calls
 
Part 04 Creating a System Call in Linux
Part 04 Creating a System Call in LinuxPart 04 Creating a System Call in Linux
Part 04 Creating a System Call in Linux
 
Block Drivers
Block DriversBlock Drivers
Block Drivers
 
Linux User Space Debugging & Profiling
Linux User Space Debugging & ProfilingLinux User Space Debugging & Profiling
Linux User Space Debugging & Profiling
 
Linux Network Management
Linux Network ManagementLinux Network Management
Linux Network Management
 
Introduction to Linux
Introduction to LinuxIntroduction to Linux
Introduction to Linux
 
System call
System callSystem call
System call
 
Introduction to System Calls
Introduction to System CallsIntroduction to System Calls
Introduction to System Calls
 
Kernel Debugging & Profiling
Kernel Debugging & ProfilingKernel Debugging & Profiling
Kernel Debugging & Profiling
 
Introduction to Linux Drivers
Introduction to Linux DriversIntroduction to Linux Drivers
Introduction to Linux Drivers
 
Embedded Storage Management
Embedded Storage ManagementEmbedded Storage Management
Embedded Storage Management
 
Mobile Hacking using Linux Drivers
Mobile Hacking using Linux DriversMobile Hacking using Linux Drivers
Mobile Hacking using Linux Drivers
 
Studying a decade of Linux system calls
Studying a decade of Linux system callsStudying a decade of Linux system calls
Studying a decade of Linux system calls
 
Introduction to Embedded Systems
Introduction to Embedded SystemsIntroduction to Embedded Systems
Introduction to Embedded Systems
 

Similar to System Calls

W5 system call, DD, OS structure.ppt
W5 system call, DD, OS structure.pptW5 system call, DD, OS structure.ppt
W5 system call, DD, OS structure.pptiqrayounus5
 
Operating system structures
Operating system structuresOperating system structures
Operating system structuresPrathamKotian3
 
Operating System- Structures of Operating System
Operating System- Structures of Operating SystemOperating System- Structures of Operating System
Operating System- Structures of Operating SystemJimmyWilson26
 
Operation system structure
Operation system structureOperation system structure
Operation system structurebrysan30
 
OPERATING SYSTEM SERVICES, OPERATING SYSTEM STRUCTURES
OPERATING SYSTEM SERVICES, OPERATING SYSTEM STRUCTURESOPERATING SYSTEM SERVICES, OPERATING SYSTEM STRUCTURES
OPERATING SYSTEM SERVICES, OPERATING SYSTEM STRUCTURESpriyasoundar
 
OS - Ch2
OS - Ch2OS - Ch2
OS - Ch2sphs
 
Chapter 2 - Operating System Structures
Chapter 2 - Operating System StructuresChapter 2 - Operating System Structures
Chapter 2 - Operating System StructuresWayne Jones Jnr
 
Structure of operating system
Structure of operating systemStructure of operating system
Structure of operating systemRafi Dar
 
Chapter 2 Operating System Structures.ppt
Chapter 2 Operating System Structures.pptChapter 2 Operating System Structures.ppt
Chapter 2 Operating System Structures.pptErenJeager20
 
Chapter 2 Operating System Structures.ppt
Chapter 2 Operating System Structures.pptChapter 2 Operating System Structures.ppt
Chapter 2 Operating System Structures.pptalo154283
 

Similar to System Calls (20)

W5 system call, DD, OS structure.ppt
W5 system call, DD, OS structure.pptW5 system call, DD, OS structure.ppt
W5 system call, DD, OS structure.ppt
 
Operating system structures
Operating system structuresOperating system structures
Operating system structures
 
Operating System- Structures of Operating System
Operating System- Structures of Operating SystemOperating System- Structures of Operating System
Operating System- Structures of Operating System
 
Operation system structure
Operation system structureOperation system structure
Operation system structure
 
Ch2
Ch2Ch2
Ch2
 
Ch2
Ch2Ch2
Ch2
 
CH02.pdf
CH02.pdfCH02.pdf
CH02.pdf
 
OPERATING SYSTEM SERVICES, OPERATING SYSTEM STRUCTURES
OPERATING SYSTEM SERVICES, OPERATING SYSTEM STRUCTURESOPERATING SYSTEM SERVICES, OPERATING SYSTEM STRUCTURES
OPERATING SYSTEM SERVICES, OPERATING SYSTEM STRUCTURES
 
MODULE 2.ppt
MODULE 2.pptMODULE 2.ppt
MODULE 2.ppt
 
OS-ch02-part-1-2024.ppt
OS-ch02-part-1-2024.pptOS-ch02-part-1-2024.ppt
OS-ch02-part-1-2024.ppt
 
OS - Ch2
OS - Ch2OS - Ch2
OS - Ch2
 
Chapter 2 - Operating System Structures
Chapter 2 - Operating System StructuresChapter 2 - Operating System Structures
Chapter 2 - Operating System Structures
 
Ch2
Ch2Ch2
Ch2
 
Structure of operating system
Structure of operating systemStructure of operating system
Structure of operating system
 
os
osos
os
 
Chapter 2 Operating System Structures.ppt
Chapter 2 Operating System Structures.pptChapter 2 Operating System Structures.ppt
Chapter 2 Operating System Structures.ppt
 
Chapter 2 Operating System Structures.ppt
Chapter 2 Operating System Structures.pptChapter 2 Operating System Structures.ppt
Chapter 2 Operating System Structures.ppt
 
ch2.ppt
ch2.pptch2.ppt
ch2.ppt
 
ch2.ppt
ch2.pptch2.ppt
ch2.ppt
 
ch2.ppt
ch2.pptch2.ppt
ch2.ppt
 

More from Anil Kumar Pugalia (19)

File System Modules
File System ModulesFile System Modules
File System Modules
 
Embedded C
Embedded CEmbedded C
Embedded C
 
Playing with R L C Circuits
Playing with R L C CircuitsPlaying with R L C Circuits
Playing with R L C Circuits
 
Audio Drivers
Audio DriversAudio Drivers
Audio Drivers
 
Video Drivers
Video DriversVideo Drivers
Video Drivers
 
Shell Scripting
Shell ScriptingShell Scripting
Shell Scripting
 
References
ReferencesReferences
References
 
Functional Programming with LISP
Functional Programming with LISPFunctional Programming with LISP
Functional Programming with LISP
 
Power of vi
Power of viPower of vi
Power of vi
 
gcc and friends
gcc and friendsgcc and friends
gcc and friends
 
"make" system
"make" system"make" system
"make" system
 
Hardware Design for Software Hackers
Hardware Design for Software HackersHardware Design for Software Hackers
Hardware Design for Software Hackers
 
RPM Building
RPM BuildingRPM Building
RPM Building
 
Timers
TimersTimers
Timers
 
Threads
ThreadsThreads
Threads
 
Synchronization
SynchronizationSynchronization
Synchronization
 
Processes
ProcessesProcesses
Processes
 
Signals
SignalsSignals
Signals
 
Linux File System
Linux File SystemLinux File System
Linux File System
 

Recently uploaded

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 

Recently uploaded (20)

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 

System Calls

  • 1. © 2010-19 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. System Calls
  • 2. 2© 2010-19 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. What to Expect? W's of System Calls System Call vs Library Function System Call Tracing Hands-On
  • 3. 3© 2010-19 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. W's of System Calls User programs vs Kernel programs Runs in different spaces Runs with different privileges User space not allowed access to Kernel space But they need the Kernel services OS provides service points For User programs To request services from the Kernel In Linux, these are called System Calls
  • 4. 4© 2010-19 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. System Calls in Linux About 300 in count Listing: /usr/include/asm/unistd.h Provide layer between Kernel Space (typically hardware) User Space (typically user process) Serve three purposes Provide an Abstracted h/w interface for user space Ensures System security and stability Makes Process Management easier
  • 5. 5© 2010-19 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Working of a Linux System Call Implemented as an ordinary function in the Linux Kernel Executes like others in the Kernel Space However, the call to that function isn't ordinary When a user program makes a system call Arguments are packaged up and handed to the kernel A special procedure is required to transfer control to the kernel Kernel takes over execution of the program until the call completes Kernel transfers control back to the program with return value Special procedure is typically achieved using “trap”
  • 6. 6© 2010-19 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. System Call Execution Flow … xyz() ... xyz() { … SYSCALL … } system_call: … sys_xyz() SYSEXIT sys_xyz() { … ... } System Call invocation in application program Wrapper routine in libc standard library System Call handler System Call service routine User Mode Kernel Mode
  • 7. 7© 2010-19 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Linux System Call Wrappers Every System Call has standard steps GNU C library (glibc) abstracts them By wrapping with functions of same name For easy invocation Examples I/O functions: open, read, ... We rarely invoke direct system calls But rather these system call (wrapper) functions Any Exception? Custom defined system call – using syscall(sno, ...)
  • 8. 8© 2010-19 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Contrast with a Library Function A library function is an ordinary function It resides in a library external to the program But in the User Space only Moreover, the call to it is also ordinary Arguments placed in processor registers or the stack Execution transferred to the start of the function Typically resides in a loaded shared library In the User Space only Examples fopen, printf, getopt, mkstemp (all from glibc)
  • 9. 9© 2010-19 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Return Values Library functions often return pointers Example: FILE * fp = fopen("harry","r"); NULL indicates failure System calls usually return an integer Example: int res = open(“harry”, O_RDONLY); Return value >= 0 indicates success < 0, typically -1 indicates failure, and error is set in errno Note the counter intuitive return of System Calls Opposite way round Cannot use as Boolean
  • 10. 10© 2010-19 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. More Information Manual Sections 2 System calls e.g. _exit, read, write 3 Library calls e.g. exit, printf 7 Miscellaneous e.g. ascii, fifo, pthreads 9 POSIX Programmer Manual Info pages are also available
  • 11. 11© 2010-19 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Tracing System Calls Command: strace <program> [args] Traces the execution of <program> And Lists System Calls made by <program> Signals received by <program> Controlled by various options An interesting one is “-e” Example strace cat /dev/null
  • 12. 12© 2010-19 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Pros & Cons Pros System calls provide direct & hence more control over the kernel services Library functions abstract the nitty-gritty of architecture or OS specific details of the system calls Library functions can provide wrappers over repeated set of system calls Cons Library functions may have overheads System calls at times may expose the underlying system dependency
  • 13. 13© 2010-19 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Let's try some Examples System Call Invocation System calls vs Library functions File Operations Observe the various system calls invoked Use strace
  • 14. 14© 2010-19 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. System Call: access Helps to determine whether the calling process has access permission for a particular file int access(const char *pathname, int mode); pathname – Path to the file mode – Accessibility checks. F_OK or a mask consisting of bitwise or of R_OK, W_OK and X_OK
  • 15. 15© 2010-19 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. System Call: fcntl Is used for performing the various advanced operations on an open file descriptor It allows to place a read or write lock on the file More than one process can hold the read lock, while only one process can hold the write lock int fcntl(int fd, int cmd, ... /* arg */ ) fd – File descriptor cmd – Operation to perform … – Arguments
  • 16. 16© 2010-19 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. What all have we learnt? W's of System Calls Working of a System Call & syscall() System Call Wrapper Functions System Call vs Library Function Pros & Cons System Call Tracing Hands-On
  • 17. 17© 2010-19 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Any Queries?