Systems Programming and C Fundamentals
Introduction to Systems Programming
Definitions and Core Concepts
System: A collection of various components working together.
Programming: The activity of designing and implementing programs.
Systems Programming: The activity of designing and implementing system programs.
System Programs: Programs required for the effective execution of general user programs on a computer system. They provide the necessary environment and tools for applications to function.
Utilities and Tools in Systems Programming
System Utilities: Small programs or tools designed to perform specific tasks that help manage, maintain, or support system functionality.
Disk Cleanup (Windows): Used to free disk space by removing unnecessary files.
Task Manager (Windows): Used to monitor and manage running processes and evaluate system performance.
Terminal (Unix/Linux): Also known as cmd.exe in Windows environments; a command-line interface for interacting directly with the operating system.
Security Software
Antivirus Programs: Protect the system from malware and viruses. Named examples include Norton and McAfee.
Firewall Software: Monitors and controls incoming and outgoing network traffic to ensure security (e.g., Windows Firewall).
System Configuration Tools
Control Panel (Windows): Provides a centralized interface for accessing system settings and configurations.
System Preferences (macOS): Allows users to configure system-specific settings on Apple computers.
Software Categories and Comparisons
Overview of Categories
Application Software: Programs used to solve a specific problem using the computer as an instrument. They provide services directly to the user. Examples include word processors, web browsers, media players, and spreadsheets.
System Software: Supports the operation of the computer and facilitates the interface between hardware and application software. Examples include compilers, interpreters, assemblers, linkers, loaders, debuggers, and Operating Systems (OS).
Comparative Analysis: System Software vs. Application Software
Functionality: System software serves as a platform for application software; application software is used by users for specific tasks.
Installation/Usage: System software is typically part of the OS installation; application software is installed based on user requirements.
Operating Environment: System software works in the background and can run independently; application software cannot run without the system software presence.
Interactivity: User interaction is high with application software; system software generally works behind the scenes.
Complexity and Modification:
System Software: Difficult to make changes; debugging is difficult; error correction (fixing badly written programs) is not easy but is reliable. Makes machines do work.
Application Software: User-friendly and modifiable; debugging is not difficult; error correction/fault-fixing is easy and reliable. Makes system programs do work.
Level of Language: System software typically uses low or middle-level languages; application software uses high-level languages.
The Operating System (OS)
Definition: A special program that resides between the computer hardware and application software.
Complexity of Operating Systems:
OS are exceptionally complex systems described as large, parallel, and expensive.
Case Study: Windows NT/XP: Development spanned years and involved of people. Despite this, bugs still occur.
Comparison to Other Complex Systems: Complexity is equated to the Internet, air traffic control, governments, weather, and human relationships.
Managing Complexity: Achieved through abstractions and layering. The goal is to build systems trusted with sensitive data and critical roles.
Logical OS Structure (Hierarchical Layers):
Applications: (e.g., Quake, SQL Server).
System Utilities and Shells.
OS Interface.
Operating System Services: Naming, windowing and graphics, networking, virtual memory, access control, and generic I/O.
Logical OS Components: File system, process management, device drivers, and memory management.
Physical Machine Interface: Interrupts, Cache, Physical Memory, Translation Lookaside Buffer (TLB), and Hardware Devices.
Linux Operating System
Core Characteristics: Linux is a multiuser, multitasking operating system with a Unix-like feel. It is a free and open-source OS.
Distributions: Consist of the Linux kernel, basic software/utilities, and a software package manager. Examples include:
Debian (and its derivative, Ubuntu).
Fedora.
Gentoo.
Kali.
Linux OS Architecture:
Kernel: The central part of the OS interfacing directly with hardware.
Shell: User interface for command execution.
Applications: User programs interacting via system calls.
System Calls and Library Calls
The System Call Flow:
Initiation: A user command or application is initiated.
API Interaction: The API receives the request.
Translation: The API translates the request into system calls.
Execution: The system executes necessary calls to access resources.
Access: Resources or services are accessed as a result.
GNU C Library (glibc):
Provides basic application services and wrappers for system calls.
Supports threading and complies with POSIX standards.
Linux-specific application services are facilitated through the glibc version on Linux.
System Call Properties:
Generally available as assembly-language instructions.
Higher-level languages like C allow system calls to be made directly, replacing the need for assembly in systems programming.
Parameter Passing Methods:
Registers: Parameters are passed directly in CPU registers for fast access.
Memory Table: A table in memory holds parameters; its address is passed in a register.
Stack Method: Parameters are pushed onto the stack by the program and popped by the OS to ensure specific order.
System Call Operation and Modes
CPU Usage and Modes: The CPU spends time in two distinct modes:
User Mode: Standard application execution.
Kernel Mode (System Mode): Privileged execution where system calls are implemented.
Prohibited Instructions: Certain instructions are prohibited in user mode, such as modifying page tables.
Comparison: Function Call vs. System Call:
Function Call: Caller and callee are in the same process; same user; same "domain of trust."
System Call: OS is trusted while the user is not; transitions from user mode to kernel mode; OS has super-privileges. Measures must be taken to prevent abuse.
Overhead: System calls are "expensive" because of context switches.
Hardware saves its state.
OS code takes control and updates privileges.
OS examines call parameters and performs the function.
OS saves its state and returns results/control to the caller.
Categories and Implementation of System Calls
System Call Categories:
Process Control: End, abort, load, execute, create/terminate process, allocate/free memory.
File Manipulation: Create, delete, open, close, read, write file.
Device Manipulation: Request device, release device.
Information Maintenance: Get time, set date.
Communications: Send/receive messages.
System Calls vs. Library Calls Mapping:
openvs.fopenclosevs.fclosereadvs.fread,getchar,scanf,fscanf,getc,fgetc,gets,fgetswritevs.fwrite,putchar,printf,fprintf,putc,fputc,puts,fputslseekvs.fseek
System Call Usage Logic: When a system call is made, the system checks if an error occurred. If yes, it returns , stores an error number in
errno, and potentially callsperror()to display a message. If no, it continues execution.Specific Function Examples:
Create File:
int creat(char* filename, mode); e.g.,creat("newfile", 0666);(Header:<sys/file.h>)Open File:
int open(char* filename, int access, int mode); e.g.,open("filename", O_RDONLY | O_TRUNC, 0);(Header:<sys/file.h>)Read Bytes:
size_t read(int fd, char *buffer, size_t bytes); e.g.,read(0, buf, 100);(Header:<sys/types.h>)Write Bytes:
size_t write(int fd, char *buffer, size_t bytes); e.g.,write(1, buf, 100);(Header:<sys/types.h>)Close File:
int close(int fd); (Header:<unistd.h>)Seeking Position:
long lseek(int fd, long offset, int origin); origins include (start), (current), (end).Directory/Permission ops:
mkdir,rmdir,chmod,opendir,readdir,stat,getcwd.
Rationale for Studying Systems Programming
Security Concerns: Understanding and mitigating digital threats.
Building Complex Systems: Learning how to manage future project complexities.
Engineering Issues: Addressing web performance and system design.
Personal Computers: Understanding factors influencing PC performance and choices.
Business Issues: Informing decision-making on technology investments.
Hardware Functionality: Managing differences in CPUs (Pentium, PowerPC, ARM, MIPS), memory sizes, and device types (NICs, wireless, mice, etc.).
Review of C Programming Language
Structure of a C Program:
#include <stdio.h>: Pre-processor directive for standard I/O.int main(): Function definition required for all programs.printf("Hello World!\n");: Literal C-string containing characters including the NULL character.return 0;: Return type call for main function functionality.
Similarities between C and C++:
Built-in primary data types: Character, Integer, Float, Double, Void.
Secondary data types: Array, Pointer, Structure, Union, Enum.
Same compiler preprocessor directives:
#include,#define,#if,#ifndef,#endif.Same built-in control structures:
if,for,while,switch.Same built-in operators (Unary, Multiplicative, Relational, Logical, Assignment, etc.).
Major Differences: C vs. C++:
Paradigm: C is procedural (step-driven); C++ is multi-paradigm/Object Oriented (data-driven).
Security: C++ has data hiding/security via OOP features; C data is not secured.
Approach: C uses top-down; C++ uses bottom-up (elements first, then linked).
Abstraction Level: C is low-level; C++ is middle-level (combines low and high-level features).
Input/Output: C uses
scanf()andprintf(); C++ usescin >>andcout <<.Features: C++ supports function overloading (polymorphism), Namespace (to avoid name collisions), and reference variables; C does not.
Bitwise Operators and Memory Manipulation
Bitwise Structure:
Smallest type is bits (
char).Individual bits cannot be accessed directly via address; bitwise operators are required for manipulation.
Provides high speed and efficiency for real-time applications.
Operators:
&: Bitwise AND (anything with results in the same value; anything with results in ).|: Bitwise OR (anything with results in ).^: Bitwise XOR.~: 1's Complement.<<: Shift left.>>: Shift right.Can be suffixed with
=(e.g.,x &= y;).
Bitwise Logic Examples (Assume [0011 1100], [0000 1101]):
x & y:x | y:x ^ y:~x:x << 2:x >> 1:
Memory Manipulation Functions:
void *memcpy (void *dest, const void *src, size_t n);: Copies bytes fromsrctodest.void *memmove(void *dest, const void *src, size_t n);: Moves bytes fromsrctodest.void *memset (void *s, int c, size_t n);: Sets bytes ofsto bytec.void *memchr (const void *s, int c, size_t n);: Searches first bytes ofsfor characterc.int memcmp (const void *s1, const void *s2, size_t n);: Case-sensitive comparison of the first bytes ofs1ands2.
Questions & Discussion
System vs. Application Interaction: How do System Software and Application Software work together to help us use a computer effectively? (System software provides the platform and hardware interface while applications solve user problems).
OS Experience: Can you compare two different operating systems—like Windows and macOS or Android and iOS—and discuss how they might offer different experiences for users? (Discussing user interface, hardware integration, and ecosystem).
Programming Logic:
Should the programmer write a single program that performs many independent activities?
Does every program have to be altered for every piece of hardware?
Does a faulty program crash everything?
Does every program have access to all of the hardware?