How programs talk to the operating system?
The tools they use are called System Calls and APIs.
Programs cannot do everything on their own.
They need help from the operating system (OS).
π οΈ Example: A text editor wants to save a file. It can't do that alone. It asks the OS: "Please save this file for me."
System calls and APIs help the program ask for things from the OS.
A system call is a request from a program to the operating system.
It asks the OS to do something important.
Only the OS can directly talk to hardware, like the disk or memory.
read()
β read data from a filewrite()
β write data to a fileopen()
β open a fileclose()
β close a filefork()
β create a new processexec()
β run a new programπ Think of it like a phone call: Your program "calls" the OS to get something done.
API stands for Application Programming Interface.
Itβs a set of functions that make system calls easier to use.
The API is like a friendly middleman between the program and the OS.
π§βπ³ You want a pizza. Instead of calling the chef directly, you use a waiter (API). The waiter knows how to speak to the kitchen (OS).
In C, the standard library gives you API functions like fopen()
, fprintf()
, and
fclose()
.
These functions use system calls like open()
, write()
, and close()
inside.
Here's what happens when a system call is made:
fread()
.read()
.System calls switch the program from user mode to kernel mode.
In kernel mode, the OS can safely access hardware and memory.
πͺ Example: Itβs like knocking on a locked door (user mode). The owner (OS) lets you in and helps you (kernel mode).
fork()
, exec()
, exit()
open()
, read()
, write()
,
close()
ioctl()
, read()
, write()
getpid()
, alarm()
pipe()
, shmget()
System calls are hard to use.
They are different on each OS.
APIs are easier and the same across many systems.
// Using system call (Linux)
int fd = syscall(SYS_open, "file.txt", O_RDONLY);
// Using API
FILE *fp = fopen("file.txt", "r");
fopen()
to make life easier.Try writing a simple C program that opens a file, writes some text, and closes it using API functions like
fopen()
, fprintf()
, and fclose()
.
Great work! You now understand how programs get help from the OS using system calls and APIs. π
s