codice:
#include <sys/types.h>#include <unistd.h>
#include <sys/wait.h>
#include <pthread.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <fcntl.h>
#include <string.h>
#include <semaphore.h>
#define SIZE 1024
//DEFINIZIONI VARIABILI GLOBALI DEL PROGRAMMA
char *memo;
//PARAMETRI PER LE THREADS
struct thread_data{
int thread_id;
int nNodo;
};
//semafori
sem_t generale;
//###############################
void *Processor(void *thread_arg){
//VARIABILI LOCALI DELLA THREAD
int i=0,j=0,taskid,ptotal;
char *str;
//VAr file
int b,c,fd;
char temp[5],buf[SIZE],a;
str=(char*)malloc(sizeof(char)*50);
struct thread_data *mydata = (struct thread_data *) thread_arg;
taskid = mydata->thread_id;
ptotal = mydata->nNodo;
//#########################FILE###########################################
//Apro il file
char nomeFile[9];
sprintf(nomeFile,"File %d.txt",taskid+1);
if((fd=open(nomeFile,O_RDONLY,444))== -1) perror("Errore nell'Apertura del File");
//Leggo dal File
if(read(fd,buf,SIZE) == -1) perror("errore nella lettura del file");
memo[taskid]='o';
//TASK DEL NODO
while(1){
/*BLOCCO L' ACCESSO ALLA RISORSA CONDIVISA AD ALTRE THREADS*/
//#########################SCHEDULER######################################
a='\0';b=0;j=0;i=0;
//Leggo i comandi inseriti, e li eseguo
//Leggo fino alla fine del file
while(buf[i] != '\0'){
if(buf[i]>'0' && buf[i]<'9'){
while(buf[j+i]>'0' && buf[j+i]<'9'){ //finchè leggo numeri continuo a copiarli sulla stringa temporanea
temp[j]=buf[j+i];
j++;
}
temp[j]='\0'; //termino la stringa
if(b==0)
b=atoi(temp); //Se sono al primo numero
else
c=atoi(temp);
i=j+i;
j=0;
}else if(isupper(buf[i])){
a=buf[i];
i++;
}else if(buf[i] == ' ') i++;
else{
if(a=='S'){
sem_post(&generale);
sprintf(str,"Thread %d S<%d><%d>\n",taskid,b,c);
write(1,str,strlen(str));
memo[ptotal]='S';
memo[ptotal]=taskid;
memo[2+ptotal+ptotal*4]=b;
memo[2+ptotal+ptotal*4]=c;
write(1,str,strlen(str));
}
if(a=='E'){
sprintf(str,"Thread %d | Eseguo una sleep di %d Secondi..\n",taskid+1,b);
write(1,str,strlen(str));
sleep(b);
sprintf(str,"Thread %d | OK\n",taskid+1);
}
if(a=='R'){
sprintf(str,"Thread %d R\n",taskid);
write(1,str,strlen(str));
}
a='\0';
b=0;
c=0;
i++;
}
}
////////////////////////////////////////////////////////////////////////////////////////////
memo[taskid]='f';
pthread_exit(NULL);
}
}
/*@}*/
//##############################################################################################################################################
/** @name main */
void main() {
//DICHIARAZIONE VARIABILI LOCALI DEL MAIN
int fd,rc,i=0,j=0,y=0,z=0,n,nodesNumber=4,fine;
char buf[SIZE],str[10],temp[5];
//memoria condivisa
memo=(char*)malloc(2+sizeof(int)*(nodesNumber)*5);
//Inizialmete tutti liberi
for(i=1;i <=nodesNumber;i++)
memo[i]='f';
//PARAMETRI PER LE THREADS
pthread_t tid[nodesNumber];
/*definizione parametri per le threads*/
struct thread_data *mydata = (struct thread_data *) malloc (sizeof(struct thread_data)*nodesNumber);
/*definizione attributi per join di thread*/
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
/////////////////////////////////////////
sem_init(&generale,0,1);
//CREAZIONE PROCESSORI CON LE THREADS
for(i=0;i<nodesNumber;i++){
mydata[i].thread_id = i;
mydata[i].nNodo = nodesNumber;
rc = pthread_create(&tid[i],&attr,Processor,(void *) &mydata[i]);
if(rc){
perror("Error creating thread!\n");
exit(-1);
}
}
//##################################
while(1){
sem_wait(&generale);
sprintf(str,"AAAAA\n");
write(1,str,strlen(str));
sem_post(&generale);
}
//#####################################
//ASPETTO CHE TUTTE LE THREAD ABBIANO TERMINATO IL LORO LAVORO
j = nodesNumber-1;
while(j>-1){
if(memo[j] == 'f'){
sprintf(str,"%c -",memo[j]);
// write(1,str,strlen(str));
j--; }
}
//Chiusura
sleep(1);
write(1,"\n\nTermine della simulazione.\n\n",30);
sleep(1);
pthread_exit(NULL);
//RILASCIO DI RISORSE
close(fd);
pthread_attr_destroy(&attr);
//for(i=0;i<nodesNumber;i++)
// pthread_mutex_destroy(&m[i]);
free(mydata);
free(memo);
}
/*@}*/