Ciao a Tutti, ho fatto questo esercizio con 2 processi e 2 threads in esecuzione, pero ho un piccolo dubbio..
posto il codice e poi mi chiarirò
#include<stdio.h>
#include<unistd.h>
#include<signal.h>
#define SIZE 5
/*
*
questo programma manda in esecuzione 2 processi e ciascuno di loro richiama un thread
differente per riempire un array e azzerarlo successivamente
*
*/
int array[5];
int *pt = &array[0];
int *increment_values(int *pt)
{
int i;
for(i=0;i<SIZE;i++)
{
(*pt++) = (i*50);
printf("array[%d] = %d\n", i, array[i]);
}
pthread_exit(0);
}
int *to_0_values(int *pt)
{
int i;
for(i=0;i<SIZE;i++)
{
(*pt++) = 0;
printf("array[%d] = %d\n", i, array[i]);
}
}
void producer()
{
int i;
pthread_t tp;
pthread_attr_t attr_tp;
printf("Thread Producer increments values in Buffer\n");
pthread_attr_init(&attr_tp);
pthread_create(&tp, &attr_tp, &increment_values, &array);
pthread_join(tp, 0);
}
void consumer()
{
int i;
pthread_t tc;
pthread_attr_t attr_tc;
printf("Thread Consumer removes values from Buffer\n");
pthread_attr_init(&attr_tc);
pthread_create(&tc, &attr_tc, &to_0_values, &array);
pthread_join(tc, 0);
pthread_exit(0);
}
main()
{
int i;
pid_t p;
printf("\n**Main Process %d calling thread for function \"producer();\"\n", (int)getpid());
producer();
p = fork();
if(p==0)
{
printf("\n\n**Son Process %d calling thread for function \"consumer();\"\n", p);
consumer();
kill(p, 0);
}
printf("\nProcess & Threads successfully complets\n");
}
e questo è l'output:
Thread Producer increments values in Buffer
array[0] = 0
array[1] = 50
array[2] = 100
array[3] = 150
array[4] = 200
**Son Process 0 calling thread for function "consumer();"
Thread Consumer removes values from Buffer
array[0] = 0
array[1] = 0
array[2] = 0
array[3] = 0
array[4] = 0
Process & Threads successfully complets
Tutto sembra corretto, ma se le istruzioni di stampa per l'array io le sposto in main invece che lasciarle nelle 2 funzioni increment_values & to_0_values, quindi il main diviene cosi;
main()
{
int i;
pid_t p;
printf("\n**Main Process %d calling thread for function \"producer();\"\n", (int)getpid());
producer();
for(i=0;i<SIZE;i++)
printf("array[%d] = %d\n", i, array[i]);
p = fork();
if(p==0)
{
printf("\n\n**Son Process %d calling thread for function \"consumer();\"\n", p);
consumer();
for(i=0;i<SIZE;i++)
printf("array[%d] = %d\n", i, array[i]);
kill(p, 0);
}
for(i=0;i<SIZE;i++)
printf("array[%d] = %d\n", i, array[i]);
printf("\nProcess & Threads successfully complets\n");
}
perche l'output e' scorretto?
**Main Process 7360 calling thread for function "producer();"
Thread Producer increments values in Buffer
array[0] = 0
array[1] = 50
array[2] = 100
array[3] = 150
array[4] = 200
**Son Process 0 calling thread for function "consumer();"
Thread Consumer removes values from Buffer
array[0] = 0
array[1] = 50
array[2] = 100
array[3] = 150
array[4] = 200
Process & Threads successfully complets
Non capisco perche in teoria le 2 funzioni increment_values & to_0_values dovrebbero modificare i dati degli array.. Qualcuno mi po aiutare a chiarire questo dubbio? MAgari l'esercizio al principio della discussione e' sbagliato per qualche motivo di cui non mi sono reso conto, se qualcuno che ne sa piu di me mi puo chiarire le idee mi farebbe piacere.. Grazie mille!