Is there a difference between Program, Thread and Process?

Asked

Viewed 24,580 times

24

I wonder if there’s 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 of them if they exist, and also the concept of each one.

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;
}

Exit:

Stackoverflow

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

Now a slightly more complex code of a program that performs 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 the <0> thread with <8 time>
I created the <1> thread with time <7>
I created the <2> thread with <8 time>
Hello, I am thread 1 I waited 7 seconds before running.
Hello, I am thread 2 I waited 8 seconds before I run.
Hello, I am thread 0 I waited 8 seconds before running.

The above example is a program, but has the task to create several threads, soon threads may be considered lawsuits or the program whole can be considered only one process? I can’t understand the meaning of every word, and I’m confused about what each one represents.

4 answers

19


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 coded (written) instructions to be executed by the computer. Thread is a sequence of instructions being executed. Purposely I will not speak of process here.

In this context maybe program is being used as synonym of 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 (when 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 be requested by the process). In addition, of course, to permissions to access other resources that are linked to the process as a whole.

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

Thread

A/O thread resembles the process because it has a new execution line (and everything related to this) and the operating system treats it equally to the process for processing time allocation. 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) space heap for every process. So it is often said that it is difficult to program with threads, share state is hard. Threads are linked to the process, not least because the main process itself is thread.

Some applications have their own control of threads internal not controlled by the processor/operating system. This has some advantages (mainly avoid changing operating system context which is something relatively expensive) and disadvantages (mainly can not use other processors). They 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 a processor scheduling point of view we can consider that only exist 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 date segmnent) Proper Shared
Local memory (stack, Registers, Pcounter) Yes Generally
Memory consumption Normal Slightly smaller
Manipulators of external resources Owner Borrows from the process
Breeding time Relatively long Relatively short
Time of context exchange Relatively long Relatively short
Instances Multiple Multiple
Associating A program (executable) A process
Parallelism Limited Yes
Communication between your peers Only with an IPC mechanism Yes, within the process
Communication efficiency Not Yes
Direct communication with OS Yes Not
Exception control Own Own
Reliability and safety Yes Depends on the code
  • Very cool this table.

  • I am studying about Threads, and, I could see how it is quite complicated to work with them, as there is no sequence of operations execution, this responsibility is on the programmer manage. But from what I understood through the explanation, if the programmer does not have domain of the subject the program is subject to many bugs.

  • @Diegofarias in general is not necessary to have an order, but in cases that need not usually be the case to use threads. http://answall.com/q/1946/101. The worst bugs are caused by state sharing between threads, mainly http://answall.com/q/159342/101

  • @Mustache, from what I understand. It is more advantageous to create multi-threads when there are distinct processes to be executed and not sequential, as in the example of Fibonacci cited, however, the time it takes for the processor to make context exchanges or relays between threads, has to be minimal compared to the general processing that will be performed in the process. That’s the whole idea ?

  • @Diegofarias basically this. Fibonacci really doesn’t. Note that the bulk of what can be multithreaded can be multiprocess :) Might not mean that it should.

4

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

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

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

A process can be composed of multiple threads. Creating the threads is part of the program specification, you had to call pthread_create to create those same threads.

2

Program: 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 monouser S.O and all resources are only available for one program.

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

Threads: It is a process with multiple control flows.

0

Contributing:

Process Control Block: one PCB is a structure created by the operating system to manage the processes that are running. The PCB consists of 3 large areas: hardware context, a copy of the process processor registers, software context, process information (Owner, id, creation date, etc.) and address space, the RAM area to which the process has access.

Program: an executable file, usually written to disk, and which must be loaded into RAM memory in order to be executed by the operating system. Basically a compiled source program for the specific operating system.

Process: a running script. After loading, program becomes a process managed by the operating system. A program can create new processes, through a command (actually a system call) Fork. Each process has its own PCB separately.

Thread: a process can create several threads, that look like processes, but with 2 major differences: they are managed by the process that created them (and not by the operating system), and share the same address space, so there may be several threads who access the same variables.

When to use: Create a new process when you want it to have independent life, running some function in parallel but autonomously. Create a thread when you want to share some variable of your application, or manually manage its execution.

Examples: if your application sends an email every time a record is recorded, you can send the email at a process separately because theoretically they are independent events. If you are doing an ordering routine like the quicksort, or mergesort, you can create threads that will sort different parts of the same vector, and therefore have to share access to the vector being ordered.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.