Making Assertions About Your Code
Making Assertions About Your Code
Systems Programming
Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by
definition, not smart enough to debug it.
Brian Kernighan
A very useful feature supported by the C preprocessor permits you to make assertions about the correctness of your code.
if the assertion is correct, execution continues without any noticeable difference (a tiny, tiny, bit slower),
if the assertion fails, execution is halted, an error describing the failed assertion is printed, and the program
terminates.
Consider:
#include <stdio.h>
#include <assert.h>
....
for(int i=0 ; i <= 5 ; i = i+1)
{
assert(i != 5);
printf("i = %i\n", i);
assert(i*i < 25);
printf("i squared = %i\n", i*i);
}
i = 0, i squared = 0
i = 1, i squared = 1
i = 2, i squared = 4
i = 3, i squared = 9
i = 4, i squared = 16
try: try.c:9: main: Assertion 'i != 5' failed.
Firstly, we note that each process is uniquely identified by an integer value termed its process identifier, process-id, or pid.
A process can get its own process-id with the system call getpid(), and get its parent's process-id with getppid().
the value returned by fork() in the parent process will be the process-id of the child process;
the value returned by fork() in the child process will be 0, indicating that it is the child, because 0 is not a valid process-id.
Each successful invocation of fork() returns a new monotonically increasing process-id (the kernel 'wraps' the value back to the first unused positive value
when it reaches 100,000).
#include <stdio.h>
#include <unistd.h>
void function(void)
{
int pid; // some systems define a pid_t
switch (pid = fork()) {
case -1 :
perror("fork()"); // process creation failed
exit(1);
break;
case 0: // new child process
printf("c: value of pid=%i\n", pid);
printf("c: child's pid=%i\n", getpid());
printf("c: child's parent pid=%i\n", getppid());
break;
default: // original parent process
sleep(1);
printf("p: value of pid=%i\n", pid);
printf("p: parent's pid=%i\n", getpid());
printf("p: parent's parent pid=%i\n", getppid());
break;
}
fflush(stdout);
}
produces:
c: child's value of pid=0
c: child's pid=5642
c: child's parent pid=5641
p: parent's value of pid=5642
p: parent's pid=5641
p: parent's parent pid=3244
Of note, calling sleep(1) may help to separate the outputs, and we fflush() in each process to force its output to appear.
Importantly, both the parent and child have their own copy of their program's memory
(variables, stack, heap).
The parent naturally uses the memory that it had before it called fork(); the child receives its
own copy of the same memory. The copy is made at the time of the fork().
As execution proceeds, each process may update its own memory without affecting the other
process.
[ OK, I lied - on contemporary operating systems, the child process does not receive a full
copy of its parent's memory at the time of the fork():
the child can share any read-only memory with its parent, as neither process can
modify it.
the child's memory is only copied from the parent's memory if either the parent
modies its (original) copy, or if the child attempts to write to its copy (that it hasn't yet
received!)
this sequence is termed copy-on-write .
]
the parent waits for the child's termination, calling the blocking function
wait( &status ).
[optionally] the child process replaces details of its program (code) and
data (variables) by calling the execve() function.
the child calls exit(value), with an integer value to represent its success
or failure. By convention, zero (= EXIT_SUCCESS) indicates successful
execution, non-zero otherwise.
the child's value given to exit() is written by the operating system to the
parent's status.
Under Unix/Linux, a new program may replace the currently running program. The new program runs as the same process (it has the same pid,
confusing!), by overwriting the current process's memory (instructions and data) with the instructions and data of the new program.
The single system call execve() requests the execution of a new program as the current process:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
char *newargs[] = {
"ls",
"-l",
"-F",
NULL
};
....
execve( "/bin/ls", newargs, environ );
exit(EXIT_FAILURE);
The single system call is supported by a number of library functions (see man execl) which simplify the calling sequence.
Typically, the call to execve() (via one of its library interfaces) will be made in a child process, while the parent process continues its execution, and
eventually waits for the child to terminate.
The operating system is able to use the exit status of a program to determine if it was successful.
Consider the following program which exits with the integer status provided as a command-line argument:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int status = 0; // DEFAULT STATUS IS SUCCESS
if(argc > 1)
status = atoi(argv[1]);
printf("exiting(%i)\n", status);
exit(status);
}
Shells are typically programmed using files of commands named shellscripts or command files and these will often have conditional constructs, such as if
and while, just like C. It's thus important for our programs to work with the shells that invoke them.
We now compile our program, and invoke it with combinations of zero, and non-zero arguments: