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.