Chapter 2 · Midterm syllabus · Lectures 4–6
Operating-System Services
The services an OS provides, CLI, GUI and touch interfaces, system calls and APIs, the six types of system calls, system programs, design goals, monolithic, layered, microkernel, modular and hybrid structures, booting and debugging.
← → move between slides · F full screen · M slide list
Before we start
Why this matters
A program cannot touch the disk, the screen or another process directly. It has to ask.
System calls are how it asks, and they are the boundary between your code and the kernel.
This chapter shows what the OS offers, how a request crosses into the kernel, and how different OSes organise the code behind that boundary.
Chapter 2 · Lectures 4–6
Outline
Select a topic to jump to the slide that explains it.
Operating system services
Operating system services
Operating systems provide an environment for execution of programs and services to programs and users.
One set of operating-system services provides functions that are helpful to the user:
- User interface – almost all operating systems have a user interface (UI)
- Varies between command-line (CLI), graphics user interface (GUI), touch-screen, batch
- Program execution – the system must be able to load a program into memory and to run that program, end execution, either normally or abnormally (indicating error)
- I/O operations – a running program may require I/O, which may involve a file or an I/O device
- File-system manipulation – the file system is of particular interest. Programs need to read and write files and directories, create and delete them, search them, list file information, permission management.
Operating system services
Operating system services (cont.)
One set of operating-system services provides functions that are helpful to the user (cont.):
- Communications – processes may exchange information, on the same computer or between computers over a network
- Communications may be via shared memory or through message passing (packets moved by the OS)
- Error detection – OS needs to be constantly aware of possible errors
- May occur in the CPU and memory hardware, in I/O devices, in user program
- For each type of error, OS should take the appropriate action to ensure correct and consistent computing
- Debugging facilities can greatly enhance the user’s and programmer’s abilities to efficiently use the system
Operating system services
Operating system services (cont.)
Another set of OS functions exists for ensuring the efficient operation of the system itself via resource sharing:
- Resource allocation – when multiple users or multiple jobs are running concurrently, resources must be allocated to each of them
- Many types of resources – CPU cycles, main memory, file storage, I/O devices
- Logging – to keep track of which users use how much and what kinds of computer resources
- Protection and security – the owners of information stored in a multiuser or networked computer system may want to control use of that information, concurrent processes should not interfere with each other
- Protection involves ensuring that all access to system resources is controlled
- Security of the system from outsiders requires user authentication, extends to defending external I/O devices from invalid access attempts
Operating system services
A view of operating system services
User and operating-system interface
Command line interpreter
- CLI allows direct command entry
- Sometimes implemented in kernel, sometimes by systems program
- Sometimes multiple flavors implemented – shells
- Primarily fetches a command from user and executes it
- Sometimes commands built-in, sometimes just names of programs
- If the latter, adding new features doesn’t require shell modification
$ ls notes.txt fork.c a.out $ cp notes.txt backup.txt $ echo $SHELL /bin/bash
cp is not built into the shell: it is a program in /bin, so new commands can be added without changing the shell.
User and operating-system interface
User operating system interface – GUI
- User-friendly desktop metaphor interface
- Usually mouse, keyboard, and monitor
- Icons represent files, programs, actions, etc.
- Various mouse buttons over objects in the interface cause various actions (provide information, options, execute function, open directory (known as a folder))
- Invented at Xerox PARC
- Many systems now include both CLI and GUI interfaces
- Microsoft Windows is GUI with CLI “command” shell
- Apple Mac OS X is “Aqua” GUI interface with UNIX kernel underneath and shells available
- Unix and Linux have CLI with optional GUI interfaces (CDE, KDE, GNOME)
User and operating-system interface
Touchscreen interfaces
- Touchscreen devices require new interfaces
- Mouse not possible or not desired
- Actions and selection based on gestures
- Virtual keyboard for text entry
- Voice commands
System calls
System calls
- Programming interface to the services provided by the OS
- Typically written in a high-level language (C or C++)
- Mostly accessed by programs via a high-level Application Programming Interface (API) rather than direct system call use
- Three most common APIs are Win32 API for Windows, POSIX API for POSIX-based systems (including virtually all versions of UNIX, Linux, and Mac OS X), and Java API for the Java virtual machine (JVM)
- Note that the system-call names used throughout this text are generic
Interactive
Example of system calls
System call sequence to copy the contents of one file to another file:
Interactive
Example of system calls (cont.)
Code and output
The copy program in C
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int main(void)
{
char buf[64];
ssize_t n, total = 0;
int in = open("notes.txt", O_RDONLY); /* system call */
int out = open("copy.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644); /* system call */
if (in < 0 || out < 0) { perror("open"); return 1; }
while ((n = read(in, buf, sizeof buf)) > 0) { /* system call */
Code and output
The copy program in C (cont.)
write(out, buf, n); /* system call */
total += n;
}
close(in); /* system call */
close(out); /* system call */
printf("copied %zd bytes\n", total); /* write() inside */
return 0;
}$ ./copy # copies notes.txt to copy.txt copied 48 bytes
Run strace ./copy on Linux to see each call listed with its arguments and return value.
System calls
API – system call – OS relationship
System calls
API – system call – OS relationship (cont.)
- The program calls
open()in the API (a library function). - The library puts the system-call number in a register and traps into the kernel.
- The system-call interface looks up entry i in its table and runs the kernel implementation of
open(). - The return status and any values go back to the program.
Animated
The journey of a system call
Types of system calls
Types of system calls
Process control
- create process, terminate process
- end, abort
- load, execute
- get process attributes, set process attributes
- wait for time
- wait event, signal event
- allocate and free memory
- Dump memory if error
- Debugger for determining bugs, single step execution
- Locks for managing access to shared data between processes
File management
- create file, delete file
- open, close file
- read, write, reposition
- get and set file attributes
Device management
- request device, release device
- read, write, reposition
- get device attributes, set device attributes
- logically attach or detach devices
Types of system calls
Types of system calls (cont.)
Information maintenance
- get time or date, set time or date
- get system data, set system data
- get and set process, file, or device attributes
Protection
- Control access to resources
- Get and set permissions
- Allow and deny user access
Communications
- create, delete communication connection
- send, receive messages if message passing model to host name or process name
- From client to server
- Shared-memory model create and gain access to memory regions
- transfer status information
- attach and detach remote devices
Types of system calls
Examples of Windows and Unix system calls
| Type | Windows | Unix |
|---|---|---|
| Process control | CreateProcess() ExitProcess() WaitForSingleObject() | fork() exit() wait() |
| File management | CreateFile() ReadFile() WriteFile() CloseHandle() | open() read() write() close() |
| Device management | SetConsoleMode() ReadConsole() WriteConsole() | ioctl() read() write() |
| Information maintenance | GetCurrentProcessID() SetTimer() Sleep() | getpid() alarm() sleep() |
| Communications | CreatePipe() CreateFileMapping() MapViewOfFile() | pipe() shm_open() mmap() |
| Protection | SetFileSecurity() InitializeSecurityDescriptor() SetSecurityDescriptorGroup() | chmod() umask() chown() |
Interactive
Classify the calls
Types of system calls
Standard C library example
C program invoking the printf() library call, which calls the write() system call.
#include <stdio.h>
int main()
{
printf("Greetings");
return 0;
}
Types of system calls
Standard C library example (cont.)
Code and output
printf( ) and write( ): run it
#include <stdio.h>
#include <unistd.h>
int main(void)
{
printf("Greetings from printf\n"); /* C library: formats, then calls write() */
write(1, "Greetings from write\n", 21); /* system call: fd 1 is the screen */
return 0;
}$ gcc greet.c -o greet -pthread && ./greet Greetings from printf Greetings from write
Both lines reach the screen through the write() system call. printf() first formats the text and keeps it in a buffer in user space; when the output is a file instead of the screen, that buffer is flushed only at exit, so the order can change.
System services
System services
System programs provide a convenient environment for program development and execution. They can be divided into:
- File manipulation
- Status information sometimes stored in a file
- Programming language support
- Program loading and execution
- Communications
- Background services
- Application programs
System services
System services (cont.)
- Provide a convenient environment for program development and execution
- Some of them are simply user interfaces to system calls; others are considerably more complex
- File management – create, delete, copy, rename, print, dump, list, and generally manipulate files and directories
- Status information
- Some ask the system for info – date, time, amount of available memory, disk space, number of users
- Others provide detailed performance, logging, and debugging information
- Typically, these programs format and print the output to the terminal or other output devices
- Some systems implement a registry – used to store and retrieve configuration information
System services
System services (cont.)
- File modification
- Text editors to create and modify files
- Special commands to search contents of files or perform transformations of the text
- Programming-language support – compilers, assemblers, debuggers and interpreters sometimes provided
- Program loading and execution – absolute loaders, relocatable loaders, linkage editors, and overlay-loaders, debugging systems for higher-level and machine language
- Communications – provide the mechanism for creating virtual connections among processes, users, and computer systems
- Allow users to send messages to one another’s screens, browse web pages, send electronic-mail messages, log in remotely, transfer files from one machine to another
System services
System services (cont.)
Background services
- Launch at boot time: some for system startup, then terminate; some from system boot to shutdown
- Provide facilities like disk checking, process scheduling, error logging, printing
- Run in user context not kernel context
- Known as services, subsystems, daemons
Application programs
- Don’t pertain to system
- Run by users
- Not typically considered part of OS
- Launched by command line, mouse click, finger poke
Why applications are OS specific
Why applications are operating system specific
- Apps compiled on one operating system usually not executable on other operating systems
- Each operating system provides its own unique system calls
- Own file formats, etc.
- Apps can be multi-operating system
- Written in interpreted language like Python, Ruby, and interpreter available on multiple operating systems
- App written in language that includes a VM containing the running app (like Java)
- Use standard language (like C), compile separately on each operating system to run on each
- Application Binary Interface (ABI) is architecture equivalent of API, defines how different components of binary code can interface for a given operating system on a given architecture, CPU, etc.
Design and implementation
Design and implementation
- Design and implementation of OS is not “solvable”, but some approaches have proven successful
- Internal structure of different operating systems can vary widely
- Start the design by defining goals and specifications
- Affected by choice of hardware, type of system
- User goals and system goals
- User goals – operating system should be convenient to use, easy to learn, reliable, safe, and fast
- System goals – operating system should be easy to design, implement, and maintain, as well as flexible, reliable, error-free, and efficient
- Specifying and designing an OS is highly creative task of software engineering
Design and implementation
Implementation
- Much variation
- Early OSes in assembly language
- Then system programming languages like Algol, PL/1
- Now C, C++
- Actually usually a mix of languages
- Lowest levels in assembly
- Main body in C
- Systems programs in C, C++, scripting languages like PERL, Python, shell scripts
- More high-level language easier to port to other hardware
- But slower
- Emulation can allow an OS to run on non-native hardware
Operating system structure
Operating system structure
- General-purpose OS is very large program
- Various ways to structure ones
- Simple structure – MS-DOS
- More complex – UNIX
- Layered – an abstraction
- Microkernel – Mach
Monolithic
everything in one kernelLayered
each layer uses only lower layersMicrokernel
minimal kernel, services in user spaceModules / hybrid
loadable pieces, mixed modelsOperating system structure
Monolithic structure – original UNIX
- UNIX – limited by hardware functionality, the original UNIX operating system had limited structuring.
- The UNIX OS consists of two separable parts
- Systems programs
- The kernel
- Consists of everything below the system-call interface and above the physical hardware
- Provides the file system, CPU scheduling, memory management, and other operating-system functions; a large number of functions for one level
Operating system structure
Monolithic structure – original UNIX (cont.)
Operating system structure
Linux system structure
Operating system structure
Linux system structure (cont.)
- The whole kernel runs in one address space (monolithic): fast, because services call each other directly.
- Loadable kernel modules (for example device drivers) can be added and removed while the system runs.
Operating system structure
Layered approach
- The operating system is divided into a number of layers (levels), each built on top of lower layers. The bottom layer (layer 0) is the hardware; the highest (layer N) is the user interface.
- With modularity, layers are selected such that each uses functions (operations) and services of only lower-level layers
Operating system structure
Layered approach (cont.)
Operating system structure
Microkernels
- Moves as much from the kernel into user space
- Mach is an example of microkernel
- Mac OS X kernel (Darwin) partly based on Mach
- Communication takes place between user modules using message passing
- Benefits:
- Easier to extend a microkernel
- Easier to port the operating system to new architectures
- More reliable (less code is running in kernel mode)
- More secure
- Detriments:
- Performance overhead of user space to kernel space communication
Operating system structure
Microkernels (cont.)
Operating system structure
Modules
- Many modern operating systems implement loadable kernel modules (LKMs)
- Uses object-oriented approach
- Each core component is separate
- Each talks to the others over known interfaces
- Each is loadable as needed within the kernel
- Overall, similar to layers but with more flexible
- Linux, Solaris, etc.
Operating system structure
Hybrid systems
- Most modern operating systems are not one pure model
- Hybrid combines multiple approaches to address performance, security, usability needs
- Linux and Solaris kernels in kernel address space, so monolithic, plus modular for dynamic loading of functionality
- Windows mostly monolithic, plus microkernel for different subsystem personalities
- Apple Mac OS X hybrid, layered, Aqua UI plus Cocoa programming environment
- Below is kernel consisting of Mach microkernel and BSD Unix parts, plus I/O kit and dynamically loadable modules (called kernel extensions)
Operating system structure
Hybrid systems (cont.)
Operating system structure
iOS
- Apple mobile OS for iPhone, iPad
- Structured on Mac OS X, added functionality
- Does not run OS X applications natively
- Also runs on different CPU architecture (ARM vs. Intel)
- Cocoa Touch Objective-C API for developing apps
- Media services layer for graphics, audio, video
- Core services provides cloud computing, databases
- Core operating system, based on Mac OS X kernel
Operating system structure
iOS (cont.)
Operating system structure
Android
- Developed by Open Handset Alliance (mostly Google)
- Open source
- Similar stack to iOS
- Based on Linux kernel but modified
- Provides process, memory, device-driver management
- Adds power management
- Runtime environment includes core set of libraries and Dalvik virtual machine
- Apps developed in Java plus Android API
- Java class files compiled to Java bytecode then translated to executable that runs in Dalvik VM
- Libraries include frameworks for web browser (webkit), database (SQLite), multimedia, smaller libc
Operating system structure
Android (cont.)
Building and booting an operating system
Building and booting an operating system
- Operating systems generally designed to run on a class of systems with variety of peripherals
- Commonly, operating system already installed on purchased computer
- But can build and install some other operating systems
- If generating an operating system from scratch
- Write the operating system source code
- Configure the operating system for the system on which it will run
- Compile the operating system
- Install the operating system
- Boot the computer and its new operating system
Building and booting an operating system
Building and booting Linux
- Download Linux source code (kernel.org)
- Configure kernel via
make menuconfig - Compile the kernel using
make- Produces
vmlinuz, the kernel image - Compile kernel modules via
make modules - Install kernel modules into vmlinuz via
make modules_install - Install new kernel on the system via
make install
- Produces
$ make menuconfig $ make $ make modules $ sudo make modules_install $ sudo make install
Building and booting an operating system
System boot
- When power initialized on system, execution starts at a fixed memory location
- Operating system must be made available to hardware so hardware can start it
- Small piece of code – bootstrap loader, BIOS, stored in ROM or EEPROM locates the kernel, loads it into memory, and starts it
- Sometimes two-step process where boot block at fixed location loaded by ROM code, which loads bootstrap loader from disk
- Modern systems replace BIOS with Unified Extensible Firmware Interface (UEFI)
- Common bootstrap loader, GRUB, allows selection of kernel from multiple disks, versions, kernel options
- Kernel loads and system is then running
- Boot loaders frequently allow various boot states, such as single user mode
Animated
System boot, animated
Operating system debugging
Operating-system debugging
- Debugging is finding and fixing errors, or bugs
- Also performance tuning
- OS generate log files containing error information
- Failure of an application can generate core dump file capturing memory of the process
- Operating system failure can generate crash dump file containing kernel memory
- Beyond crashes, performance tuning can optimize system performance
- Sometimes using trace listings of activities, recorded for analysis
- Profiling is periodic sampling of instruction pointer to look for statistical trends
Operating system debugging
Performance tuning
- Improve performance by removing bottlenecks
- OS must provide means of computing and displaying measures of system behavior
- For example,
topprogram or Windows Task Manager
Tasks: 212 total, 1 running, 211 sleeping %Cpu(s): 12.5 us, 3.1 sy, 0.0 ni, 84.0 id MiB Mem : 7841.2 total, 2210.5 free PID USER %CPU %MEM COMMAND 1432 ayan 35.2 4.1 firefox 877 root 6.0 0.8 Xorg 2210 ayan 1.3 0.5 bash
Operating system debugging
Tracing
- Collects data for a specific event, such as steps involved in a system call invocation
- Tools include
strace– trace system calls invoked by a processgdb– source-level debuggerperf– collection of Linux performance toolstcpdump– collects network packets
openat(AT_FDCWD, "notes.txt", O_RDONLY) = 3 read(3, "OS notes\n", 131072) = 9 write(1, "OS notes\n", 9) = 9 read(3, "", 131072) = 0 close(3) = 0
Worked example 1 of 2
Worked example 1
Show the solution
Prompt + read input name (2), prompt + read output name (2), open input (1), create output (1), then 3 × (read + write) = 6, the read that fails at end of file (1), close output (1), completion message (1), terminate (1).
Total: 16 system calls to copy three blocks. Step through it in the copy-file tool.
Worked example 2 of 2
Worked example 2
Show the solution
Each message goes user → kernel → file-system server and back, so a read needs several mode switches and copies; a monolithic kernel calls the file-system function directly inside the kernel.
A designer accepts the overhead for the benefits: easier to extend and port, more reliable (a crashing server does not crash the kernel) and more secure, because less code runs in kernel mode.
Practice
Practice (1 of 2)
Try each one on paper first, then check your answer.
Show the answer
Show the answer
Show the answer
Practice
Practice (2 of 2)
Try each one on paper first, then check your answer.
Show the answer
Show the answer
Show the answer
Check yourself
Quiz
Resources
Further learning
Free material to go deeper. Links open in a new tab.
Summary · End of Chapter 2
Summary
- The OS offers services for users (UI, program execution, I/O, files, communication, error detection) and for the system (resource allocation, logging, protection).
- Programs reach the services through system calls, usually via an API such as POSIX or Win32; the system-call interface dispatches by number.
- System calls fall into six types: process control, file management, device management, information maintenance, communications, protection.
- System programs define most users’ view of the OS; apps are OS specific because system calls and ABIs differ.
- Structures: monolithic (UNIX, Linux), layered, microkernel (Mach), modules, and hybrids (Windows, macOS, iOS, Android).
- A bootstrap loader (BIOS/UEFI, GRUB) loads the kernel; logs, core dumps, top, strace, gdb and perf help debugging and tuning.