CITS2002 Systems Programming  
prev
CITS2002 CITS2002 schedule  

Accessing system information using structures

Operating systems (naturally) maintain a lot of (related) information, and keep that information in structures.

So that the information about the structures (the datatypes and names of the structure's fields) can be known by both the operating system and users' programs, these structures are defined in system-wide header files - typically in /usr/include and /usr/include/sys.

For example, consider how an operating system may represent time on a computer:

#include <stdio.h>
#include <sys/time.h>

// A value accurate to the nearest microsecond but also has a range of years      
struct timeval {
    int  tv_sec;       // Seconds
    int  tv_usec;      // Microseconds
};

Note that the structure has now been given a name, and we can now define multiple variables having this named datatype (in our previous example, the structure would be described as anonymous).

We can now request information from the operating system, with the information returned to us in structures:

#include <stdio.h>
#include <sys/time.h>

    struct timeval  start_time;
    struct timeval  stop_time;

    gettimeofday( &start_time, NULL );
    printf("program started at %i.06%i\n",
                   (int)start_time.tv_sec, (int)start_time.tv_usec);

    ....
    perform_work();
    ....

    gettimeofday( &stop_time, NULL );
    printf("program stopped at %i.06%i\n",
                   (int)stop_time.tv_sec, (int)stop_time.tv_usec);

Here we are passing the structure by address, with the & operator, so that the gettimeofday() function can modify the fields of our structure.

(we're not passing a meaningful pointer as the second parameter to gettimeofday(), as we're not interested in timezone information)

 


CITS2002 Systems Programming, Lecture 5, p14, 5th August 2024.