Operating System Services
An operating system provides services to programs and users: running programs, I/O, files, communication, error handling, resource allocation, accounting and protection. System programs package these services into tools people use.
Nine Services in Two Groups
- User interface – CLI, GUI, touch screen or batch.
- Program execution – load a program into memory, run it, end it.
- I/O operations – perform input and output on behalf of programs.
- File-system manipulation – create, delete, read, write and search files and directories.
- Communication – exchange data between processes, locally or over a network.
- Error detection – detect and handle errors in hardware, devices and programs.
- Resource allocation – share CPU, memory, files and devices among processes.
- Accounting – track which users use how much of which resources.
- Protection and security – control access to resources and defend against attacks.
An operating system provides an environment in which programs run. The specific functions it offers to programs and to their users are called operating system services. They make the computer convenient to use, because a program can ask the OS to read a file instead of driving a disk controller, and efficient and safe to share, because the OS decides who gets which resource.
The services fall into two groups. The first group helps the user and the programmer. The second group exists for the efficient operation of the system itself, which matters as soon as several users or processes share one machine. Programs reach all of them through system calls.
- The grouping comes from Silberschatz; other textbooks merge or split items, but the same functions appear in every list
- A service is a function, not a program: one system call such as open() can use file-system manipulation and protection at once
Services That Help the User
Six services make the machine usable. Without them, every program would have to include its own device drivers, its own file format on disk, and its own way of talking to other programs. Program execution loads a program, runs it, and ends it normally (exit status 0) or abnormally. I/O operations run on the program's behalf, because a user program may not touch a device directly. File-system manipulation creates, deletes, reads, writes, searches and lists files and manages their permissions. Communication passes data between processes, and error detection notices faults such as a failed disk read, a divide by zero or a missing file, and responds to each.
The user interface comes in several forms. A command-line interface (CLI), such as a Unix shell, accepts typed commands; a graphical user interface (GUI) uses windows, menus and a pointer; a touch-screen interface uses gestures; and a batch interface runs commands from files with no user present. Most systems provide more than one.
- Communication has two models: shared memory and message passing (see IPC)
- Error detection may retry, report an error code, or terminate the process, depending on how serious the fault is
Services That Keep the System Efficient
Three services exist not for any one program, but so that many programs and users can share the system fairly and safely. They are invisible when only one program runs, and essential when hundreds do. Resource allocation divides CPU time, memory, storage and devices among competing processes. Accounting records which users use how much of each resource, for billing, disk quotas and usage statistics.
Protection means controlling access by processes and users to the resources the OS manages, such as files, memory and devices. Security extends that to threats from outside: it starts with each user authenticating, usually with a password, and covers defending the system and its network from attacks. A chain is only as strong as its weakest link, so both are needed.
- Resource allocation uses CPU scheduling and memory management
- See OS Protection and Security for the mechanisms
Terms, operations, and practical uses
Core vocabulary
- OS serviceA function the OS provides to programs and users, requested by a system call.
- System callA controlled trap into the kernel that asks for one service.
- System programA utility shipped with the OS, such as a shell, compiler or file manager.
- DaemonA background system program started at boot, such as a print spooler.
The nine services
- For the userUI, program execution, I/O, file systems, communication, error detection
- For the systemResource allocation, accounting, protection and security
- User interfacesCLI, GUI, touch screen, batch
- Communication modelsShared memory and message passing
Related lessons
- Introduction to Operating SystemsWhat an OS is and its main functions.
- Kernel, User Mode, and System CallsHow a system call crosses into the kernel.
- Inter-Process CommunicationThe communication service in detail.
- Protection and SecurityAccess control and authentication.
The OS services behind one cp command
import errno
import os
import shutil
import tempfile
CHUNK = 16 # bytes per read() call
calls = 0
def counted(fn, *args):
# Every os.open/read/write/close below is one system call into the kernel.
global calls
calls += 1
return fn(*args)
def copy(src, dst):
copied = 0
print(f"file-system manipulation: open {src} for reading")
fin = counted(os.open, src, os.O_RDONLY)
print(f"file-system manipulation: create {dst} for writing")
fout = counted(os.open, dst, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o644)
print(f"resource allocation: {CHUNK}-byte buffer")
while True:
data = counted(os.read, fin, CHUNK)
if not data:
print("I/O operations: read 0 bytes (end of file)")
break
counted(os.write, fout, data)
copied += len(data)
print(f"I/O operations: read {len(data)} bytes, write {len(data)} bytes")
counted(os.close, fin)
counted(os.close, fout)
print("file-system manipulation: close both files")
return copied
def try_open(path, flags, service):
try:
counted(os.close, counted(os.open, path, flags))
except OSError as e:
name = {errno.ENOENT: "no such file", errno.EACCES: "permission denied"}
print(f"{service}: open {path} -> {name[e.errno]} ({errno.errorcode[e.errno]})")
workdir = tempfile.mkdtemp()
os.chdir(workdir)
with open("notes.txt", "w") as f:
f.write("OS services let every program use hardware.\n")
with open("locked.txt", "w") as f:
f.write("read only\n")
os.chmod("locked.txt", 0o444)
calls = 0 # count only the calls made by the copy itself
print("user interface: $ cp notes.txt backup.txt")
print("program execution: cp loaded and started")
copied = copy("notes.txt", "backup.txt")
copy_calls = calls
try_open("missing.txt", os.O_RDONLY, "error detection")
try_open("locked.txt", os.O_WRONLY, "protection")
print(f"accounting: {copied} bytes copied, {copy_calls} system calls in the copy")
os.chdir(tempfile.gettempdir())
shutil.rmtree(workdir)#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fcntl.h>
#include <string>
#include <sys/stat.h>
#include <unistd.h>
using namespace std;
const int CHUNK = 16; // bytes per read() call
int calls = 0;
// Every open/read/write/close below is one system call into the kernel.
template <class F, class... A>
auto counted(F fn, A... args) {
calls++;
return fn(args...);
}
int copyFile(const char* src, const char* dst) {
int copied = 0;
char buffer[CHUNK];
printf("file-system manipulation: open %s for reading\n", src);
int in = counted([](const char* p) { return open(p, O_RDONLY); }, src);
printf("file-system manipulation: create %s for writing\n", dst);
int out = counted([](const char* p) { return open(p, O_WRONLY | O_CREAT | O_TRUNC, 0644); }, dst);
printf("resource allocation: %d-byte buffer\n", CHUNK);
while (true) {
ssize_t n = counted(read, in, buffer, CHUNK);
if (n == 0) {
printf("I/O operations: read 0 bytes (end of file)\n");
break;
}
counted(write, out, buffer, n);
copied += n;
printf("I/O operations: read %zd bytes, write %zd bytes\n", n, n);
}
counted(close, in);
counted(close, out);
printf("file-system manipulation: close both files\n");
return copied;
}
void tryOpen(const char* path, int flags, const char* service) {
int fd = counted([](const char* p, int f) { return open(p, f); }, path, flags);
if (fd >= 0) {
counted(close, fd);
return;
}
const char* what = errno == ENOENT ? "no such file (ENOENT)" : "permission denied (EACCES)";
printf("%s: open %s -> %s\n", service, path, what);
}
void writeFile(const char* path, const string& text) {
FILE* f = fopen(path, "w");
fputs(text.c_str(), f);
fclose(f);
}
int main() {
char dir[] = "/tmp/servicesXXXXXX";
mkdtemp(dir);
chdir(dir);
writeFile("notes.txt", "OS services let every program use hardware.\n");
writeFile("locked.txt", "read only\n");
chmod("locked.txt", 0444);
printf("user interface: $ cp notes.txt backup.txt\n");
printf("program execution: cp loaded and started\n");
int copied = copyFile("notes.txt", "backup.txt");
int copyCalls = calls;
tryOpen("missing.txt", O_RDONLY, "error detection");
tryOpen("locked.txt", O_WRONLY, "protection");
printf("accounting: %d bytes copied, %d system calls in the copy\n", copied, copyCalls);
for (const char* f : {"notes.txt", "backup.txt", "locked.txt"}) unlink(f);
chdir("/");
rmdir(dir);
return 0;
}import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class Main {
static final int CHUNK = 16; // bytes per read() call
static int calls = 0;
// Each open, read, write and close below is one system call into the kernel.
static int copy(Path src, Path dst) throws IOException {
int copied = 0;
byte[] buffer = new byte[CHUNK];
System.out.printf("file-system manipulation: open %s for reading%n", src.getFileName());
calls++;
try (FileInputStream in = new FileInputStream(src.toFile())) {
System.out.printf("file-system manipulation: create %s for writing%n", dst.getFileName());
calls++;
try (FileOutputStream out = new FileOutputStream(dst.toFile())) {
System.out.printf("resource allocation: %d-byte buffer%n", CHUNK);
while (true) {
calls++;
int n = in.read(buffer);
if (n == -1) {
System.out.println("I/O operations: read 0 bytes (end of file)");
break;
}
calls++;
out.write(buffer, 0, n);
copied += n;
System.out.printf("I/O operations: read %d bytes, write %d bytes%n", n, n);
}
}
calls++; // closing out
}
calls++; // closing in
System.out.println("file-system manipulation: close both files");
return copied;
}
static void tryOpen(Path path, boolean write, String service) throws IOException {
calls++;
try {
if (write) {
new FileOutputStream(path.toFile(), true).close();
} else {
new FileInputStream(path.toFile()).close();
}
calls++;
} catch (FileNotFoundException e) {
String what = Files.exists(path) ? "permission denied (EACCES)" : "no such file (ENOENT)";
System.out.printf("%s: open %s -> %s%n", service, path.getFileName(), what);
}
}
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("services");
Path notes = dir.resolve("notes.txt");
Path backup = dir.resolve("backup.txt");
Path locked = dir.resolve("locked.txt");
Files.writeString(notes, "OS services let every program use hardware.\n");
Files.writeString(locked, "read only\n");
locked.toFile().setWritable(false);
System.out.println("user interface: $ cp notes.txt backup.txt");
System.out.println("program execution: cp loaded and started");
int copied = copy(notes, backup);
int copyCalls = calls;
tryOpen(dir.resolve("missing.txt"), false, "error detection");
tryOpen(locked, true, "protection");
System.out.printf("accounting: %d bytes copied, %d system calls in the copy%n", copied, copyCalls);
for (Path p : new Path[] {notes, backup, locked}) Files.delete(p);
Files.delete(dir);
}
}Step through it
Running on $ cp notes.txt backup.txt (44 bytes, 16-byte buffer), then open missing.txt, then write to read-only locked.txt
How Programs Request Services: System Calls
A program cannot call kernel code as an ordinary function, because the kernel runs in a more privileged mode. Instead it makes a system call: a controlled trap that switches the CPU into kernel mode, runs the requested service, and returns. The tracer shows one command making 13 of them.
Programmers rarely write system calls directly. They use an API, such as the POSIX API on Linux and macOS or the Windows API, whose library functions (like fopen or printf) make the system calls for them. System calls are usually grouped into six categories that line up closely with the services, as below.
- Process control – fork(), exec(), exit(), wait(): program execution
- File management – open(), read(), write(), close(): file-system manipulation
- Device management – ioctl(), read(), write(): I/O operations
- Information maintenance – getpid(), time(), sleep(): accounting and status
- Communication – pipe(), socket(), shmget(): communication
- Protection – chmod(), chown(), umask(): protection and security
- Parameters pass in registers, a memory block, or on the stack; see system calls for the trap itself
Types of System Programs
- File management – create, delete, copy, rename, list and print files and directories (cp, mv, rm, ls, File Explorer, Finder).
- Status information – report the date, free memory and disk, users, performance and logs (date, df, top, Task Manager).
- File modification – create and change file contents; search and transform text (vim, grep, sed).
- Programming-language support – translate and debug programs (compilers, assemblers, interpreters, debuggers).
- Program loading and execution – load a program into memory ready to run (loaders, linkers, dynamic linkers).
- Communications – connect processes, users and computers (ssh, email clients, web browsers).
- Background services – long-running processes started at boot (print spooler, network services, schedulers).
System programs, also called system utilities, provide a convenient environment for developing and running programs. They sit between the user and the system calls: some are simple wrappers around a single call, others are large programs. For most users, the system programs are the operating system, because they never see the system calls underneath.
The diagram at the top shows where they sit: above the system-call interface in user space, next to application programs. The shell that ran cp in the tracer is a system program, and so is cp itself.
- Background services are called daemons on Unix and services on Windows
- A system program can be swapped for another (bash for zsh) without changing the kernel
System Programs vs Application Programs vs System Calls
Three layers are easy to confuse. System calls are the kernel's interface. System programs are general-purpose tools shipped with the OS that make the computer usable and manageable. Application programs solve a particular user's problem, such as editing a photo or browsing the web. The line between system and application programs is blurry, and different vendors draw it differently.
| Aspect | System calls | System programs | Application programs |
|---|---|---|---|
| What it is | entry points into the kernel | utilities that manage and support the system | software for users' own tasks |
| Runs in | kernel mode | user mode | user mode |
| Used by | programs, through an API | users and administrators | end users |
| Examples | open(), fork(), write() | shell, cp, compiler, file manager | browser, word processor, game |
| Needed to run the system? | yes | mostly | no |
- The same tool can be either: a text editor shipped with the OS counts as a system program, the same editor bought separately as an application
- Only system calls cross into kernel mode; both kinds of program are ordinary user processes