Know Your Process

Program. Software. Application. Process. These are all terms that describe different aspects of a similar thing.

Software is the big category. Application is being redefined, thanks to mobile devices, although for a computer I use the term to refer to a software category, such as word processing.

A program is code designed to do something, although a single program can involve many different things happening at once. That’s where the term process comes into play.

A process is a task performed in a computer. The operating system manages all the different processes, which run at the same time in a manner I shan’t bother describing.

When you code a C program, you’re creating something that runs as a single process. Start the program and the operating system launches the process, assigning it an ID and managing its resources.

To view your program’s process ID, use the getpid() function. The function returns an integer value of the pid_t type and it requires inclusion of the unistd.h header file. Here’s sample code:

#include <stdio.h>
#include <unistd.h>

int main()
{
    pid_t process;

    process = getpid();
    printf("This program has a process ID of %d\n",process);

    return(0);
}

The getpid() function fetches the assigned process ID. The result is displayed, similar to this output:

This program has a process ID of 900

Of course, the process ID number differs from system to system, and it may change each time you run the code, but it’s unique for each running process.

While this information may seem nice and all that, it does serve a purpose: Your program need not limit itself to a single process. It’s entirely possible for one program to run multiple processes. The process ID is how you keep track of each one. In next week’s Lesson, I’ll present code that runs two different processes — essentially a single program that multitasks.

Leave a Reply