CITS2002 Systems Programming  
CITS2002 CITS2002 schedule  

What have we learnt about C?

C is:

  • a good general purpose programming language (providing the necessary features to define and store data, perform calculations, and provide control-flow to sequence our programs), and

  • an excellent systems programming language (providing well-defined and well-documented interfaces to system-level APIs for system-calls and support functions).

C is a procedural programming language, not an object-oriented language like Java, Objective-C, or C#.

C programs can be described as "good" programs, if they are:

  • well designed,
  • clearly written and (hence) easy to read,
  • well documented,
  • use high level programming practices,
  • written with portability in mind, and
  • are well tested.

Of course, the above properties are independent of C, and are offered by many high level languages.

  • C has programming features provided by most procedural programming languages - strongly typed variables, constants, standard (or base) datatypes, enumerated types, user-defined types, aggregate structures, standard control flow, recursion, and program modularization.

  • C does not offer OO's classes or objects, nested functions, subrange types, run-time error detection, container data-structures (lists, dictionaries, tuples, sets), and only recently added a Boolean datatype.

  • C does, however, have separate compilation, conditional compilation (through its pre-processor), bitwise operators, pointer arithmetic, and language independent input and output.

CITS2002 Systems Programming, Lecture 23, p1, 17th October 2023.


Why do we teach C?

  • small, imperative, procedural language

  • available on very wide-variety of hardware architectures, from IoT devices to supercomputers

  • well defined by (formal) accessible standards

  • three contemporary compiler suites, each available on three contemporary platforms

  • well supported by 3rd-party cross-platform open-source and proprietary libraries

  • used to implement most operating-systems and other programming languages and libraries

  • provides software bridges to, and connecting, other programming languages

CITS2002 Systems Programming, Lecture 23, p2, 17th October 2023.


Features of C common to most programming languages

  • base data types - integers, characters, floating-point

  • conditions (though evaluated as integers, 0 is false, anything other is true)

  • expressions with type promotion (integers and floating-point)

  • control flow - if/else, switch; for, while, do...while loops

  • aggregate data-structures; one- and multi-dimensional arrays, structures

  • functions, accepting parameters, return value and pointer parameters

CITS2002 Systems Programming, Lecture 23, p3, 17th October 2023.


Less common C features supporting systems programming

  • systems programming demands speed and consistency
    (compilers can generate very efficient code, and support system-specific function calling conventions)

  • close mapping to underlying processor instruction set
    (pre- and post- increment operators, standard string- and memory-based function map to single instructions, pointer dereferencing, pointer arithmetic)

  • representation/access to hardware characteristics, fixed-sized data objects
    (well-defined integers of different lengths - 8, 16, 32, and 64 bits)

  • permits direct access to process's own memory through pointers

CITS2002 Systems Programming, Lecture 23, p4, 17th October 2023.


What's (still) missing from the C language?

During this unit, you will have naturally felt that the C language is missing some essential features for writing general programs, even general systems programs.

For example, we have seen that C does not provide features for graphics, networking, cryptography, or multimedia. Does this mean that C cannot be used to implement massive online multi-player games?

At one level, you probably appreciate that C doesn't have all these features (to learn). Instead, it is hoped that you have developed an appreciation that the "missing" features are provided by operating systems, other programs, 3rd-party libraries, and even bindings between C and other programming languages.

C permits, enables, and encourages additional 3rd-party libraries (both open-source and commercial) to provide these facilities. The reason for these "omissions" is that C rigorously defines what it does provide, and rigorously defines how C must interact with external libraries.

We have previously listed some well-respected 3rd-party libraries, frequently employed in large C programs:

Function domain 3rd-party libraries
operating system services
(files, directories, processes, inter-process communication)
OS-specific libraries, e.g. glibc, Win32, Carbon
web-based programming libcgi, libxml, libcurl
data structures and algorithms the generic data structures library (GDSL)
GUI and graphics development OpenGL, GTK, Qt, UIKit, Win32, Tcl/Tk
image processing (GIFs, JPGs, etc) gd
networking Berkeley sockets, AT&T's TLI
security, cryptography openssl, libmp
scientific computing NAG, Blas3, GNU scientific library (gsl)
concurrency, parallel and GPU programming pthreads, OpenMPI, openLinda, CUDA, OpenCL

From this unit, it is hoped that you are now confident to investigate the use of these external libraries, be able to search for and read their documentation, and know how to incorporate their header files, functions, and libraries into your own projects.

CITS2002 Systems Programming, Lecture 23, p5, 17th October 2023.


The role and responsibilities of an OS

  • manage and arbitrate access to hardware resources - CPU, memory, devices

  • manage and arbitrate access to software abstractions - processes, file-systems, users

  • support efficient context switching between processes, possibly supporting different per-process and per-user priorities

  • impose limits and accounting of resource usage, per-process and per-user (e.g. varying scheduling timequantum, maximum execution time, maximum number of processes, maximum amount of memory, maximum open files)

  • support constrained and exclusive access to resources (e.g. read-only, read-write access)

  • permit sharing of resources when requested (shared access to memory, use of dynamically loaded libraries)

  • permit requests to obtain, increase, or relinquish resource ownership

CITS2002 Systems Programming, Lecture 23, p6, 17th October 2023.


The interface between an OS and programming languages

  • interface is well-defined and documented using C headers files and online documentation

  • well-defined, orthogonal set of system-calls

  • system-calls receive a small number of fixed-sized parameters (integers and pointers that can be passed in CPU registers)

  • system-calls return results through returned integer or reference parameters

  • fixed-sized identifiers and parameters handled by the kernel

  • variable-sized objects handled by user-level libraries

  • collections - processes, memory, files, directories, users, networking

  • consistent use of file-descriptors for all 'types' of files and devices

CITS2002 Systems Programming, Lecture 23, p7, 17th October 2023.


C programs as good OS citizens

  • consistently indicate their success or failure with their exit status

  • consistenly accept command-line arguments
    (options, then filenames or data)

  • may receive more persistent configuration via environment variables
    (conveniently managed by interactive shells)

  • consistent representation of system-specific errors setting errno and reporting of error messages using perror()

  • consistent default data streams, stdin, stdout, stderr which may be redirected using shells

  • many utility programs perform as filters independently of how their input and output is obtained
    (command-line, keyboard & screen, redirected files, or pipes)

CITS2002 Systems Programming, Lecture 23, p8, 17th October 2023.