Random UNIX/Linux Hacks

Looking Backward

The Renaissance was a time when someone with the wealth to fund free time, buy books and study with scholars could master much of human knowledge. Scientific works were confined to a few authors. Historical works were limited, many dating back to classical Greek and Roman times. Only in canon (church) law and Christian theology was there a literature so vast that one person would have a hard time mastering it.

When I graduated from college and in the first few years of my career computer science was in its Renaissance phase in terms of the amount of knowledge that existed. Relational database theory was in its infancy. Computer graphs was primative, as was hardware design. Image and signal processing (which some would not consider part of computer science) were primative and wavelets had not yet been discovered. Even the body of material written on algorithms was relatively small. That era is now past. Computer science has become a huge field and even someone who could spend eight or ten hours a day, five days a week, could master only a fraction of the field.

Mirroring the growth in computer science is a huge growth in software technology, especially in Java and web software. Keeping up with the latest buzzword technology (JavaScript, Java Servlets, Struts, AJAX, Java ServerFaces, Hibernate) is nearly impossible.

The UNIX/POSIX/Linux software is one of the few unchanging parts of this rapidly changing landscape. Many of the UNIX system calls that I used on the Berkeley Standard Development (BSD) UNIX 4.1 (which did not have virtual memory or TCP/IP network support) are still in use today. I find, however, that I some of these very infrequently. This web page is dedicated to those random UNIX hacks.


fork()

In the modern work of the pthreads library, fork() is a somewhat antiquated system call. For creates "heavy weight" processes, which are a copy of the complete environment and memory footprint of the parent process. The POSIX Threads Library (pthreads) allows light weight thread processes to be created with much less overhead. There are still reasons to use fork() and the related exec family of system calls. There are times when it is desirable to have the child process have all of the resources of the parent. I've included below a skeleton program that uses fork() and wait().

#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>

/**
   A simple C program that uses fork() to create a child process.  The
   parent then waits for the child to complete.
 */
int main( int arvc, char *argv[] )
{
  int status = 0;
  pid_t pid = -1;

  if ((pid = fork()) >= 0) {
    int child_status = -1;

    if (pid == 0) {
      // child process has a fork() return value of 0
      printf("Hello from the child process\n");
    }
    else { // parent process
      printf("Hello from the parent process\n");
      // wait for the child process to terminate
      while (wait( &child_status ) != pid) {
        continue;
      }
    }
  }
  else {
    char buf[128];
    snprintf(buf, sizeof( buf ), "%s: Failed to fork process", argv[0]
      );
    perror( buf );
  }
  return status;
}


back to Miscellaneous Software