Is there a difference between program, Thread and process?

I would like to know if there is difference between Thread, process and program ?

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

What I understand is that any instruction or sequence of instructions, can be called 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;
}

Output:

Stackoverflow

Could soon be considered a program, whose function is to display the word Stackoverflow in the console.

Now a slightly more complex code of a program that executes 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;
}

Output:

I created thread with Time
I created thread with Time
I created thread with Time
Hello, I am thread 1 waited 7 seconds before running.
Hello, I'm thread 2 waited 8 seconds before running.
Hello, I am thread 0 waited 8 seconds before running.

The above example is a program , but it has as task to create several threads , so these threads can be considered processes or the program whole can be considered only one process ? I can't understand the meaning of each word, and I am confused at what each represents.

Author: Maniero, 2016-05-29

3 answers

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, in fact the initial call does not leave by an operating system executable).

Program is a sequence of encoded (written) instructions to be executed by the computer. thread is a sequence of instructions being executed. Purposely I will not talk about process here.

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

Process

Every directly called executable 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 at its disposal and the available memory (which can go requested by the process). In addition, of course, access permissions to other resources that are tied to the process as a whole.

A process has a virtual memory address space just for it. It is as if it runs alone on the computer. Of course this does not occur in reality, but he behaves as if it were. But this is another matter. The same executable can run on multiple processes.

thread

A / o thread resembles the process because it has a new execution line (and everything related to it) and the operating system treats it the same as the process for processing time allocation. But in terms of memory it is the responsibility of the application to control the 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. So it is often said that it is complicated to program with threads , sharing state is difficult. threads are linked to the process, not least because the main process itself is still a thread.

Some applications have their own control of internal threads not controlled by the processor/operating system. This has some advantages (mainly avoiding OS context exchange which is something relatively expensive) and disadvantages (mostly can not use other processors). They are usually called soft, light or green threads .

Difference

So depending on how one analyzes, 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 only exists process.

Thread process comparison

 20
Author: Maniero, 2020-06-11 14:45:34

A program consists of a set of instructions that you draw up to achieve a certain goal. That is, the program consists essentially of the source code.

A thread is a component responsible for executing the instructions you have crafted in your program. The processor always executes instructions in the context of a thread.

After building a program you usually get an executable, which results from the process of compiling your program. A process, it is a component of the operating system that runs that same executable.

A process can consist of multiple threads. The creation of the threads is part of the program specification, you had to call pthread_create to create those same threads.

 4
Author: Bruno Costa, 2016-05-29 09:39:45

Program : is a static and permanent entity, composed only of a sequence of instructions. Example: MS-DOS, a running program is not a process, as MS-DOS is an S. The single user and all features are only available for one program.

Process: it is a dynamic entity, which changes its state as it progresses its execution. Thus, the process can be viewed as an abstraction that represents a running program. A process contains a single control flow and consists of program, data and context. In short: a process is a running program, added to its context.

Threads: is a process with multiple control streams.

 1
Author: renan mateus, 2017-10-16 23:37:42