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.
Very cool this table.
– Bruno Costa
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.
– Diego Farias
@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
– Maniero
@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 ?
– Diego Farias
@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.
– Maniero