ICS 2305 SYSTEMS PROGRAMMING (45 CONTACT HOURS)
Lecturer: Ms. Merceline Ochieng
INTRODUCTION AND DEFINITIONS
What is Systems Programming?
Systems programming refers to the activity of writing system software. It's a form
of programming that is used to create software that provides services to other
software and manages hardware resources.
Key Characteristics
a. Close interaction with the hardware
b. Requires detailed understanding of the operating system
c. Often involves lower-level programming languages (e.g., C, C++)
d. Focuses on efficiency and performance
Application vs. System Programming
Application Programming
Purpose: Develops software for end-users
Focus: User interface, business logic, data processing
Language Level: Often uses high-level languages (e.g., Java, Python, JavaScript)
Interaction: Typically interacts with the OS through APIs
System Programming
Purpose: Develops software that supports other software
Focus: Hardware management, OS services, system utilities
Language Level: Often uses lower-level languages (e.g., C, Assembly)
Interaction: Directly interacts with hardware and low-level OS components
Key Differences
Abstraction Level:
Application programming relies on abstractions provided by the OS and libraries.
Developers work with high-level concepts like "windows" or "database
connections."
System programming often deals directly with hardware and low-level OS
structures. Developers might work with concepts like "memory pages" or "I/O
ports."
Performance Requirements:
While all software benefits from being efficient, system software's performance
directly impacts the entire system. A slow file system or inefficient memory
manager will degrade the performance of all applications on the system.
Application software can often trade some performance for developer productivity
or additional features.
Hardware Knowledge:
Application developers generally don't need to know much about the underlying
hardware. The OS abstracts most hardware details.
System programmers often need detailed knowledge of CPU architectures,
memory hierarchies, bus protocols, and specific hardware components.
User Interaction:
Application software is designed for direct user interaction, with a focus on user
experience and interface design.
System software often runs in the background with no direct user interface. When
user interaction is required (e.g., for configuration), it's often through command-
line interfaces or system utilities.
System Software And Operating Systems
System Software
Definition: Software designed to provide a platform for other software
Types
Operating Systems: The core software that manages hardware resources and
provides services to applications. Examples include Windows, macOS, and Linux.
Device Drivers: Software that allows the operating system to interact with specific
hardware devices. For instance, a printer driver allows the OS to send print jobs to
a particular model of printer.
Firmware: Software that's embedded in hardware devices. It's often stored in read-
only memory (ROM) or flash memory. Examples include the BIOS in a PC or the
software in a smart thermostat.
Utilities: Programs that help manage, maintain, and optimize the computer system.
Examples include disk defragmenters, system monitors, and backup tools.
Compilers and Assemblers: Tools that translate human-readable source code into
machine code that can be executed by the computer. GCC (GNU Compiler
Collection) is a well-known example of a compiler suite.
Operating Systems
Definition: A collection of software that manages computer hardware and provides
services for computer programs
Core Functions of an OS
a. Process Management: Creating, scheduling, and terminating processes
b. Memory Management: Allocating and deallocating memory to processes
c. File System Management: Organizing and maintaining file structures
d. Device Management: Controlling input/output devices
e. Security and Protection: Ensuring system integrity and user authentication
Types of Operating Systems
Single-User, Single-Task:
Designed to manage computer resources for one user running one program at a
time.
Simple in design but limited in functionality.
Example: MS-DOS was a single-user, single-task OS. When running a program,
the user couldn't perform any other tasks until that program finished or was
terminated.
Single-User, Multi-Task:
Allows one user to run multiple programs simultaneously.
Uses time-sharing to allocate CPU time to different tasks.
Example: Modern desktop operating systems like Windows and macOS are single-
user, multi-task. You can browse the web, listen to music, and work on a document
simultaneously.
Multi-User:
Allows multiple users to use the computer's resources simultaneously.
Provides protection between users' processes and data.
Example: Unix and Linux systems are multi-user. Multiple users can log in (locally
or remotely) and run processes simultaneously. The OS ensures that users can't
interfere with each other's files or processes without permission.
Real-Time OS:
Designed for applications where timing is crucial.
Guarantees that certain operations will be completed within specific time
constraints.
Often used in embedded systems, industrial control, and scientific instruments.
Example: VxWorks is used in NASA's Mars rovers. It ensures that critical
operations (like adjusting the rover's position) happen within precise time limits,
regardless of other system activities.
Distributed OS:
Manages a group of independent computers, making them appear as a single
coherent system to the user.
Handles communication between machines and distributes processing and storage
across the network.
Example: While not widely used in their pure form, concepts from distributed
operating systems are applied in cloud computing environments. Google's internal
Borg system, which manages cluster resources across thousands of machines,
incorporates many distributed OS concepts
Device Drivers
A device driver is a specialized program that acts as an interface between the
operating system and a specific hardware device.
Purpose
a. Translate general input/output instructions from the OS to device-specific
commands
b. Handle device-specific operations and interrupts
c. Provide a uniform interface for the OS to interact with various devices
Character Device Drivers:
Handle devices that provide or accept data as a stream of characters. Often don't
need buffering and are accessed sequentially.
Example: A keyboard driver processes each keystroke as it occurs. When you press
a key, the driver receives an interrupt, identifies which key was pressed, and sends
this information to the OS.
Block Device Drivers:
Manage devices that handle data in fixed-size blocks.
Often involve buffering and can be accessed randomly (non-sequentially).
Example: A hard drive driver manages read and write operations to the disk. When
the OS requests to read a file, the driver determines which physical blocks on the
disk contain the file data, issues commands to read these blocks, and assembles the
data into a format the OS can use.
Network Device Drivers:
Handle communication between the OS and network interface hardware.
Manage sending and receiving network packets.
Example: An Ethernet card driver manages the process of sending and receiving
Ethernet frames. When the OS wants to send data over the network, the driver
packages it into Ethernet frames and controls the hardware to transmit these frames
Key Concepts in Device Driver Development
Interrupt Handling:
Interrupts are signals sent by hardware devices to the CPU to indicate that they
need attention.
Drivers must implement interrupt handlers to respond to these signals quickly and
efficiently.
Example: When a USB device is plugged in, it generates an interrupt. The USB
driver's interrupt handler responds by initializing the device and notifying the OS
that a new device is available.
DMA (Direct Memory Access):
A feature that allows certain hardware subsystems to access main system memory
independently of the CPU.
Drivers often set up DMA transfers to move data efficiently between the device
and system memory.
Example: A high-speed network card might use DMA to transfer received packets
directly into system memory without CPU intervention, improving performance.
I/O Control (IOCTL) commands:
A mechanism for sending device-specific commands from user space to the driver.
Allows for configuration and control of devices beyond simple read/write
operations.
Example: A video card driver might provide IOCTL commands to change
resolution, adjust color settings, or enable hardware acceleration features.
Memory mapping:
A technique where a device's memory or registers are mapped into the system's
address space.
Allows for direct access to device resources, often improving performance.
Example: A graphics driver might map the video card's frame buffer into system
memory, allowing applications to write directly to the display without going
through the OS for each pixel change.
Power management:
Modern drivers need to support various power states to conserve energy.
This involves handling sleep/wake events and potentially adjusting device
performance based on system power state.
Example: A Wi-Fi driver might implement power-saving features that turn off the
radio when not in use, and quickly wake it when data needs to be sent or received.
5. Operating System Calls
An OS call (or system call) is the programmatic way in which a computer program
requests a service from the kernel of the operating system.
Purpose:
Controlled Access: By channeling all requests through system calls, the OS can
maintain control over how and when applications access hardware and other
system resources. This prevents applications from directly manipulating hardware,
which could lead to system instability or security breaches.
Abstraction: System calls provide a level of abstraction, hiding the complexities
of hardware interaction from application developers. This allows developers to
work with high-level concepts rather than dealing with hardware-specific details.
Security: System calls allow the OS to implement security policies, checking
permissions before allowing access to resources. This helps prevent unauthorized
access to sensitive system components or other users' data.
Resource Management: Through system calls, the OS can manage and allocate
system resources efficiently among multiple processes.
Common Types of System Calls
Process Control:
These system calls manage the creation, execution, and termination of processes.
Example: The fork() system call creates a new process by duplicating the calling
process. The new process (child) is an exact copy of the parent process, except for
a few specific values. This is often used in Unix-like systems to create new
processes.
File Management:
These calls handle file operations such as opening, reading, writing, and closing
files.
Example: The open(), read(), write(), and close() system calls are used to interact
with files
Device Management:
These calls are used to interact with devices. The ioctl() call is particularly
important as it allows for device-specific operations.
Information Maintenance: These calls handle getting and setting system
information. E.g. getpid(), alarm(), sleep()
Communication: These calls manage inter-process communication (IPC) and
networking.
Example: Using pipe() to create a communication channel between a parent and
child process.
System Call Interface
Most programming languages provide wrappers around system calls
In C, system calls are often accessed through the Standard C Library (libc)
System Call Mechanism
The process of making a system call involves several steps and requires
cooperation between the hardware and the operating system:
a. Making the Call: The program prepares the system call arguments and
triggers the call, usually through a special instruction (like syscall on x86-
64).
b. Mode Switch: The CPU switches from user mode to kernel mode. This
change in privilege level is crucial for system security, as it allows the kernel
to perform operations that would be unsafe if done by user programs.
c. Handler Invocation: The kernel's system call handler is called. It examines
the system call number and arguments to determine what action to take.
d. Performing the Operation: The kernel executes the requested operation.
This might involve interacting with hardware, managing memory, or
manipulating kernel data structures.
e. Returning to User Mode: Once the operation is complete, the CPU
switches back to user mode, lowering the privilege level.
f. Resuming the Program: Control is returned to the program, often with a
return value indicating the success or failure of the system call.
Examples of System Calls in Different OS
Unix/Linux System Calls:
Unix-like systems have a rich set of system calls that form the basis of their
powerful and flexible design:
a. open(): Opens a file or device. Returns a file descriptor.
b. read(): Reads data from a file descriptor into a buffer.
c. write(): Writes data from a buffer to a file descriptor.
d. close(): Closes a file descriptor.
e. fork(): Creates a new process by duplicating the calling process.
f. exec(): Replaces the current process image with a new process image.
Windows System Calls:
Windows uses a different set of system calls, often referred to as the Windows API
or Win32 API:
a. CreateProcess(): Creates a new process and its primary thread.
b. ReadFile(): Reads data from a file, starting at the position indicated by the
file pointer.
c. WriteFile(): Writes data to a file, starting at the position indicated by the file
pointer.
d. CloseHandle(): Closes an open object handle.