Is there a difference between Program, Thread and Process?

18

I would like to know if there is a 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 one if it exists, and also the concept of each.

What I understand is that any statement or instruction sequences 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;
}

Output:

  

Stackoverflow

It could soon be considered a program , whose function is to display the word Stackoverflow on the console.

Now a slightly more complex code for a program that runs multiple 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 the thread < 0 > with time < 8 >   I created the thread < 1 > with time < 7 >   I created the thread < 2 > with time < 8 >   Hi, I'm thread 1 I waited 7 seconds before running.   Hi, I'm thread 2 I waited 8 seconds before running.   Hi, I'm thread 0 waited 8 seconds before running.

The above example is a program , but it has a task to create several threads , so these threads / em> or the program can all be considered just a process ? I can not understand the meaning of each word, and I am confused by what each one represents.

    
asked by anonymous 29.05.2016 / 06:23

3 answers

15

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.

Program is a sequence of coded (written) instructions to be executed by the computer. Thread is a sequence of statements being executed. Purposely I will not speak of the 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. Executable calls within a process can run explicitly in another process or in the same process (when this is 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 can be 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 only one virtual memory address space for it. It is as if he were riding alone on the computer. Of course this does not happen in reality, but it behaves as if it were. But this is another subject. The same executable can run in multiple processes.

Thread

processing. But in terms of memory it is the responsibility of the application 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 in> for every process. So it is often said that programming with threads is difficult, sharing status is difficult. Threads are linked to the process, not least because the main process itself is still a thread.

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

Difference

Then depending on how it is analyzed, thread can be considered equivalent to a process or not. From the scheduling point of view of the processor 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 one process.

    
29.05.2016 / 14:08
4

A program consists of a set of instructions that you elaborate to achieve a certain objective. In other words, the program essentially consists of the source code.

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

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

A process can be made up of multiple threads. Thread creation is part of the program's specification, you had to call pthread_create to create those threads.

    
29.05.2016 / 11:39
0

Program : It is a static and permanent entity, composed only of a sequence of instructions. Example: MS-DOS, a running program is not a process, because MS-DOS is a single-user S.O and all features are only available to a program.

Process : is a dynamic entity, which changes its state as its execution progresses. Thus, the process can be seen as an abstraction representing 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 : This is a process with multiple streams of control.

    
17.10.2017 / 01:37