Is there a difference between Program, Thread and Process?

Question:

I would like to know if there is a difference between Thread , Process and Program ?

These three words are widely used in the Information Technology area, so it would be interesting to know the difference between each one of them, if any, and also the concept of each one.

What I understand is that any instruction or sequence of instructions can be called a program , see this code:

#include <stdio.h>

int main(void)
{
        char str[13] = "Stackoverflow";
        int i;

        for (i = 0; i < 13; i++)
                printf("%c", str[i]);

        return 0;
}

Exit:

Stackoverflow

So it could be considered a program whose function is to display the word Stackoverflow on the console.

Now for a little more complex code of a program that runs several threads , see:

#ifdef __unix__
# include <unistd.h>
#elif defined _WIN32
# include <windows.h>
#define sleep(x) Sleep(1000 * x)
#endif

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

struct valor
{
        int tempo;
        int id;
};

void *espera(void *tmp)
{
        struct valor *v = (struct valor *) tmp;
        sleep(v->tempo);
        printf("Ola, eu sou a thread %d esperei %d segundos antes de executar.\n", v->id, v->tempo);
}

int main(void)
{
        pthread_t linhas[10];
        int execute, i;
        struct valor *v;
        srand(time(NULL));

        for (i = 0; i < 3; i++)
        {
                v = (struct valor *) malloc(sizeof(struct valor *));
                v->tempo = (rand() % 10) + 2;
                v->id = i;

                printf("Criei a thread <%d> com tempo <%d>\n", i, v->tempo);

                execute = pthread_create(&linhas[i], NULL, espera, (void *)v);
        }

        pthread_exit(NULL);

        return 0;
}

Exit:

I created thread <0> with time <8>
I created thread <1> with time <7>
I created thread <2> with time <8>
Hello, I'm thread 1 waited 7 seconds before executing.
Hello, I'm thread 2 I waited 8 seconds before executing.
Hello, I'm thread 0 I waited 8 seconds before executing.

The example above is a program , but its task is to create several threads , so these threads can be considered processes or can the entire program be considered just one process ? I can't understand the meaning of each word, and I'm confused as to what each one represents.

Answer:

Program

The program is what is already defined in the question. In general, a program generates an executable that can be called directly or through other executables (well, actually the initial call doesn't leave an operating system executable).

A program is a sequence of coded (written) instructions to be executed by the computer. Thread is a sequence of instructions being executed. I will purposely not talk about process here.

In this context perhaps program is being used as a synonym for executable.

Process

Every executable called directly will run in a process. Executables called within a process can run explicitly in another process or in the same process (where possible), usually using a DLL.

The process is controlled by the operating system, both the processor time it has available and the available memory (which the process may request). In addition, of course, to access permissions to other resources that are linked to the process as a whole.

A process has a virtual memory address space all to itself. It's like it runs alone on the computer. Of course that doesn't happen in reality, but he behaves as if it were. But this is another subject. The same executable can run in several processes.

Thread

The A/O thread resembles the process because it has a new thread (and everything related to it) and the operating system treats it the same as the process for allocating processing time. But in terms of memory, it is the application's responsibility to control shared access throughout the process.

It is common for applications to have a stack for each thread , but only one (I've seen cases of having more than one) heap space for every process. That's why it's often said that programming with threads is complicated, sharing state is difficult. Threads are linked to the process, as the main process itself is still a thread .

Some applications have their own internal thread control not controlled by the processor/operating system. This has some advantages (mainly avoiding OS context switching which is relatively expensive) and disadvantages (mostly not being able to use other processors). These are often called soft , light or green threads .

Difference

So depending on how you analyze it, thread can be considered equivalent to a process or not. From the processor scheduling point of view we can consider that there are only threads . Simply put, from the point of view of memory management and access to external resources, there is only the process.

Feature Process Thread
Execution Own line Own line
Global memory (heap and data segment) own shared
Local memory (stack, registers, PCounter) Yea Usually
memory consumption Normal slightly smaller
External resource handlers Owner borrow from the process
creation time relatively long Relatively short
Context switch time relatively long Relatively short
Instances multiples multiples
Association A program (executable) a process
Parallelism Limited Yea
communication between your peers Only with an IPC mechanism Yes, within the process
communication efficiency No Yea
Direct communication with the OS Yea No
Exception control Own Own
Reliability and security Yea depends on the code
Scroll to Top