Programmazione Concorrente ed execv
Ciao a tutti,
sto sviluppando un progetto per l' università, tuttavia sono incappato in una serie di problemini che non riesco assolutamente a risolvere.
In pratica:
il progetto si propone di simulare una società con due tipi di individui( i quali, durante la simulazione dovranno accoppiarsi etc...)e un processo gestore.
Il mio problema sta nella execv() in quanto dopo la prima fase di generazione dei processi ogni volta che la invoco mi da come errore Bad Argument;
Se qualcuno, buttando un occhio al codice che allego qui sotto, potesse dirmi perchè ciò accada glie ne sarei estremamente grato,
GESTORE
PROCESSI A
PROCESSI B
Vi ringrazio anticipatamente per la risposta,
saluti!!
sto sviluppando un progetto per l' università, tuttavia sono incappato in una serie di problemini che non riesco assolutamente a risolvere.
In pratica:
il progetto si propone di simulare una società con due tipi di individui( i quali, durante la simulazione dovranno accoppiarsi etc...)e un processo gestore.
Il mio problema sta nella execv() in quanto dopo la prima fase di generazione dei processi ogni volta che la invoco mi da come errore Bad Argument;
Se qualcuno, buttando un occhio al codice che allego qui sotto, potesse dirmi perchè ciò accada glie ne sarei estremamente grato,
GESTORE
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/wait.h> #include <sys/ipc.h> #include <sys/shm.h> #include <sys/sem.h> #include <sys/msg.h> #include <sys/types.h> #include <time.h> #include <errno.h> #include <stdbool.h> #include <string.h> #define MAX_SIZE 600 #define PRINT 1 #define TEST_ERR if(errno){\ printf("%d\n",__LINE__);\ printf("%s\n",strerror(errno));\ exit(-1);\ } bool flag_b_d =false; int vissuti = 0; int vissuti_a = 0; int vissuti_b = 0; struct data{ char * name; unsigned long gen; char type; pid_t pid; }; struct msg{ long mytype; int text; }; char * ch_to_cp(char a);//TESTED char * ul_to_cp(unsigned long l);//TESTED char * i_to_cp(int i); unsigned long rand_range(int low, int high,int seed); unsigned long MCD(unsigned long a, unsigned long b); char rand_type(); void pulizia(int people,pid_t pid[],int a[], int b[], int c[], int d[],int e,int f,int g,int h); void print_data(int a, int b, int c, char * d, char e, unsigned long f,char * g,char h,unsigned long i); void hand(int signum); pid_t genesi(char t, unsigned long g,pid_t fork_pid,char * args[],char * max_gen_name,unsigned long max_gen_gen,char max_gen_type); int main(int argc , char * argv[]){ int status = 0; //HANDLER ALARM struct sigaction s_al; s_al.sa_handler = &hand; sigaction(SIGALRM,&s_al,NULL); char tmp_char_o = 'L'; char tmp_char_t = 'L'; char * tmp_str_o = malloc(sizeof(char)+1); char * tmp_str_t = malloc(sizeof(char)+1); int index_one = 0; int index_two = 0; unsigned long tmp_gen_o = 0; unsigned long tmp_gen_t = 0; int rcv_check = -1; char max_gen_type = 'Z'; char * max_gen_name = "A"; unsigned long max_gen_gen = 0; char max_name_type = 'Z'; char * max_name_name = "A"; unsigned long max_name_gen = 0; int rcv_test = 0; int death = 0; pid_t fork_pid,pid_due,pid_accop; int init_people = 0; unsigned long genes = 0; int sim_time = 0; int birth_death = 0; int n_alarms = 0; printf("Inserire init_people\n"); scanf("%d",&init_people); printf("Inserire genes\n"); scanf("%ld",&genes); printf("Inserire sim_time\n"); scanf("%d",&sim_time); printf("Inserire birth_death\n"); scanf("%d",&birth_death); int alrm_count = 0; n_alarms = sim_time/birth_death; int secondi_per_clock = clock()/CLOCKS_PER_SEC; int b_d_ck = 0; vissuti = init_people; struct msg rcv; rcv.mytype = 1; struct dati * data[init_people]; struct sembuf my_buf; my_buf.sem_num = 0; my_buf.sem_flg = 0; int msg_id[init_people]; int shm_id[init_people]; int sem_shm[init_people]; int sem_msg[init_people]; int sem_parent; int msg_parent; int sem_A; int sem_B; for(int i = 0; i < init_people; i++){ shm_id[i] = shmget(IPC_PRIVATE,sizeof(struct data),IPC_CREAT|IPC_EXCL|0666); TEST_ERR; data[i] = shmat(shm_id[i],NULL,SHM_RND); TEST_ERR; msg_id[i] = msgget(IPC_PRIVATE,IPC_CREAT|IPC_EXCL|0666); TEST_ERR; sem_shm[i] = semget(IPC_PRIVATE,3,IPC_CREAT|IPC_EXCL|0666);//ricordare di incrementarli TEST_ERR; sem_msg[i] = semget(IPC_PRIVATE,2,IPC_CREAT|IPC_EXCL|0666); TEST_ERR; } sem_parent = semget(IPC_PRIVATE,2,IPC_CREAT|IPC_EXCL|0666); TEST_ERR; msg_parent = msgget(IPC_PRIVATE,IPC_CREAT|IPC_EXCL|0666); TEST_ERR; sem_A = semget(IPC_PRIVATE,init_people,IPC_CREAT|IPC_EXCL|0666); TEST_ERR; sem_B = semget(IPC_PRIVATE,2,IPC_CREAT|IPC_EXCL|0666); TEST_ERR; for(int i = 0; i < init_people; i++){ semctl(sem_shm[i],0,SETVAL,1); TEST_ERR; semctl(sem_msg[i],0,SETVAL,1); TEST_ERR; } semctl(sem_parent,0,SETVAL,0); TEST_ERR; semctl(sem_A,0,SETVAL,0); TEST_ERR; semctl(sem_B,0,SETVAL,0); TEST_ERR; char t[init_people]; char n[init_people]; unsigned long g[init_people]; pid_t all_pid[init_people]; int num_proc[init_people]; int length = 12+init_people*4; char * args[init_people][length]; int flag[init_people]; for(int i = 0; i < init_people; i++){ flag[i] = 0; srand(clock()); t[i] = rand_type(); if(t[i] == 'A'){ vissuti_a++; args[i][0] = "ProcA"; } else{ vissuti_b++; args[i][0] = "ProcB"; } n[i] = (char)rand_range(65,90,clock()); g[i] = rand_range(2,2+genes,clock()); num_proc[i] = i; args[i][1] = ch_to_cp((t[i]));//TIPO args[i][2] = ch_to_cp(n[i]);//NOME args[i][3] = ul_to_cp(g[i]);//GENOMA args[i][4] = i_to_cp(init_people); args[i][5] = i_to_cp(i);//NUMERO DEL PROCESSO args[i][length - 6] = i_to_cp(flag[i]); args[i][length - 5] = i_to_cp(sem_B); args[i][length - 4] = i_to_cp(sem_A); args[i][length - 3] = i_to_cp(msg_parent); args[i][length - 2] = i_to_cp(sem_parent); args[i][length - 1] = NULL; for(int j = 0; j < init_people; j++){ args[i][6+4*j] = i_to_cp(shm_id[j]); args[i][7+4*j] = i_to_cp(msg_id[j]); args[i][8+4*j] = i_to_cp(sem_shm[j]); args[i][9+4*j] = i_to_cp(sem_msg[j]); } } for(int i = 0; i < init_people; i++){ all_pid[i] = genesi(t[i],g[i],fork_pid,args[i],max_gen_name,max_gen_gen,max_gen_type); } for(int i = 0; i < init_people; i++){ printf("PID = %d\n",all_pid[i]); } //SEGNALA AGLI A DI PUBBLICARE semctl(sem_A,0,SETVAL,vissuti_a); TEST_ERR; //ASPETTA CHE ABBIANO SCRITTO my_buf.sem_op = 0; semop(sem_A,&my_buf,1); TEST_ERR; //DA IL SEGNALE AI B semctl(sem_B,0,SETVAL,vissuti_b); TEST_ERR; printf("______________________________INIZIO SIMULAZIONE______________________________\n"); alarm(birth_death); while(alrm_count < n_alarms){ if(flag_b_d == false){ rcv.mytype = 1; rcv_check = -1; rcv_check = msgrcv(msg_parent,&rcv,MAX_SIZE,0,0);//ASPETTA DI ESSERE CONTATTATO DAI FIGLI if(rcv_check >= 0){//E' STATO CONTATTATO DA UN PROCESSO rcv_check = -1; index_one = rcv.text; msgrcv(msg_parent,&rcv,MAX_SIZE,0,0); index_two = rcv.text; wait(&status); wait(&status); tmp_char_o = (char)rand_range(65,90,clock()); tmp_char_t = (char)rand_range(65,90,clock()+5); tmp_str_o = ch_to_cp(tmp_char_o); tmp_str_t = ch_to_cp(tmp_char_t); args[index_one][2] = strcat(args[index_one][2],tmp_str_o); args[index_two][2] = strcat(args[index_two][2],tmp_str_t); tmp_gen_o = atol(args[index_one][3]); tmp_gen_t = atol(args[index_two][3]); args[index_one][3] = ul_to_cp(rand_range(MCD(tmp_gen_o,tmp_gen_t),MCD(tmp_gen_o,tmp_gen_t)+2,clock())); args[index_two][3] = ul_to_cp(rand_range(MCD(tmp_gen_o,tmp_gen_t),MCD(tmp_gen_o,tmp_gen_t)+2,clock())); tmp_char_o = (char)rand_range(65,66,clock()); tmp_char_t = (char)rand_range(65,66,clock()+5); args[index_one][1] = ch_to_cp(tmp_char_o); args[index_two][1] = ch_to_cp(tmp_char_t); if(tmp_char_o == 'A'){ args[index_one][0] = "ProcA"; vissuti_a++; my_buf.sem_op = 1; semop(sem_A,&my_buf,1); if(errno == EINTR){ my_buf.sem_op = 1; semop(sem_B,&my_buf,1); } } else{ args[index_one][0] = "ProcB"; vissuti_b++; my_buf.sem_op = 1; semop(sem_B,&my_buf,1); if(errno == EINTR){ my_buf.sem_op = 1; semop(sem_B,&my_buf,1); } } switch(pid_accop = fork()){ case -1: fprintf(stderr,"(RIPRODUZIONE)ERRORE IN FORK\n"); exit(-1); break; case 0: if(tmp_char_o == 'A'){ execv("./ProcA",args[index_one]); printf("(RIPRODUZIONE)ERRORE IN EXECV\n"); TEST_ERR; } else{ execv("./ProcB",args[index_one]); printf("(RIPRODUZIONE)ERRORE IN EXECV\n"); TEST_ERR; } break; default: if(tmp_gen_o > max_gen_gen){ max_gen_gen = tmp_gen_o; max_gen_name = malloc(sizeof(args[index_one][2])); max_gen_name = args[index_one][2]; max_gen_type = *args[index_one][1]; } if(strlen(args[index_one][2]) > strlen(max_name_name)){ max_name_gen = tmp_gen_o; max_name_name = malloc(sizeof(args[index_one][2])); max_name_name = args[index_one][2]; max_name_type = tmp_char_o; } all_pid[index_one] = pid_accop; break; } if(PRINT == 1){ printf("FIGLIO 1:\nNOME : %s\nTIPO : %c\nGENOMA: %ld\nPID: %d\n",args[index_one][2],tmp_char_o,tmp_gen_o,all_pid[index_one]); } if(tmp_char_t == 'A'){ args[index_two][0] = "ProcA"; vissuti_a++; my_buf.sem_op = 1; semop(sem_A,&my_buf,1); if(errno == EINTR){ my_buf.sem_op = 1; semop(sem_B,&my_buf,1); } } else{ args[index_two][0] = "ProcB"; vissuti_b++; my_buf.sem_op = 1; semop(sem_B,&my_buf,1); if(errno == EINTR){ my_buf.sem_op = 1; semop(sem_B,&my_buf,1); } } switch(pid_accop = fork()){ case -1: fprintf(stderr,"(RIPRODUZIONE)ERRORE IN FORK\n"); exit(-1); break; case 0: if(tmp_char_t == 'A'){ execv("./ProcA",args[index_two]); printf("(RIPRODUZIONE)ERRORE IN EXECV\n"); TEST_ERR; } else{ execv("./ProcB",args[index_two]); printf("(RIPRODUZIONE)ERRORE IN EXECV\n"); TEST_ERR; } break; default: if(tmp_gen_t > max_gen_gen){ max_gen_gen = tmp_gen_t; max_gen_name = malloc(sizeof(args[index_two][2])); max_gen_name = args[index_two][2]; max_gen_type = tmp_char_t; } if(strlen(args[index_two][2]) > strlen(max_name_name)){ max_name_gen = tmp_gen_t; max_name_name = malloc(sizeof(args[index_two][2])); max_name_name = args[index_two][2]; max_name_type = tmp_char_t; } all_pid[index_two] = pid_accop; break; } if(PRINT == 1){ printf("FIGLIO 2:\nNOME : %s\nTIPO : %c\nGENOMA: %ld\nPID: %d\n",args[index_two][2],tmp_char_t,tmp_gen_t,all_pid[index_two]); } } } else{//BIRTH_DEATH flag_b_d = false; alrm_count++; death = rand_range(0,init_people,clock()); printf("AGGIORNAMENTO:\n"); printf("Il processo con PID = %d è stato terminato\n",all_pid[death]); if(all_pid[death] != 0){ kill(all_pid[death],SIGTERM); } t[death] = rand_type(); vissuti++; if(t[death] == 'A'){ args[death][0] = "ProcA"; vissuti_a++; my_buf.sem_op = 1; semop(sem_A,&my_buf,1); if(errno == EINTR){ my_buf.sem_op = 1; semop(sem_B,&my_buf,1); } } else{ args[death][0] = "ProcB"; vissuti_b++; my_buf.sem_op = 1; semop(sem_B,&my_buf,1); if(errno == EINTR){ my_buf.sem_op = 1; semop(sem_B,&my_buf,1); } } n[death] = (char)rand_range(65,90,clock()); g[death] = rand_range(2,2+genes,clock()); args[death][1] = ch_to_cp(t[death]); args[death][2] = ch_to_cp(n[death]); args[death][3] = ul_to_cp(g[death]); all_pid[death] = genesi(t[death],g[death],pid_due,args[death],max_gen_name,max_gen_gen,max_gen_type); alarm(birth_death); } } pulizia(init_people,all_pid,sem_shm,sem_msg,shm_id,msg_id,sem_parent,sem_A,sem_B,msg_parent); print_data(vissuti,vissuti_a,vissuti_b,max_gen_name,max_gen_type,max_gen_gen,max_name_name,max_name_type,max_name_gen); } char * ch_to_cp(char a){ char * ret = malloc(sizeof(char)); *ret = a; return ret; } char * ul_to_cp(unsigned long l){ char * ret = malloc(sizeof(unsigned long)); sprintf(ret,"%ld",l); return ret; } char * i_to_cp(int i){ char * ret = malloc(sizeof(int)); sprintf(ret,"%d",i); return ret; } unsigned long rand_range(int low, int high,int seed) { srand(seed + time(NULL)); return (low+rand()%(high-low+1)); } unsigned long MCD(unsigned long a, unsigned long b) { int r; if(b == 0){ return a; } while(b != 0) { r = a % b; a = b; b = r; } return a; } char rand_type(){ if(clock() %2 == 0){ return 'A'; } else{ return 'B'; } } void pulizia(int people,pid_t pid[],int a[], int b[], int c[], int d[],int e,int f,int g,int h){ for(int i = 0; i < people; i++){ kill(pid[i],SIGKILL); semctl(a[i],IPC_RMID,0); semctl(b[i],IPC_RMID,0); shmctl(c[i],IPC_RMID,0); msgctl(d[i],IPC_RMID,0); } semctl(e,IPC_RMID,0); semctl(f,IPC_RMID,0); semctl(g,IPC_RMID,0); msgctl(h,IPC_RMID,0); } void print_data(int a, int b, int c, char * d, char e, unsigned long f,char * g,char h,unsigned long i){ printf("________________FINE SIMULAZIONE____________________________________\n"); printf("N° PROCESSI VISSUTI = %d\nN° PROCESSI A VISSUTI = %d\nN° PROCESSI B VISSUTI = %d\n",a,b,c); printf("DATI DEL PROCESSO CON GENOMA PIU' LUNGO:\n"); printf("NOME : %s\nGENOMA : %ld\nTIPO : %c\n",d,f,e); printf("DATI DEL PROCESSO CON NOME PIU' LUNGO\n"); printf("NOME : %s\nGENOMA : %ld\nTIPO : %c\n",g,i,h); printf("____________________________________________________________________\n"); } void hand(int signum){ flag_b_d = true; } pid_t genesi(char t, unsigned long g,pid_t fork_pid,char * args[],char * max_gen_name,unsigned long max_gen_gen,char max_gen_type){ switch(fork_pid = fork()){ case -1: fprintf(stderr,"Errore in fork\n"); exit(-1); break; case 0: if(t == 'A'){//tipo == A execv("./ProcA",args); printf("(GENESI)ERRORE NELLA EXECV DEL FIGLIO CON PID %d\n",getpid()); exit(EXIT_FAILURE); } else{ //tipo == B; execv("./ProcB",args); printf("(GENESI)ERRORE NELLA EXECV DEL FIGLIO CON PID %d\n",getpid()); exit(EXIT_FAILURE); } break; default: if(g > max_gen_gen){ max_gen_gen = g; max_gen_name = malloc(sizeof(args[1])); max_gen_name = args[1]; max_gen_type = t; } return fork_pid; break; } }
PROCESSI A
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/wait.h> #include <sys/ipc.h> #include <sys/shm.h> #include <sys/sem.h> #include <sys/msg.h> #include <sys/types.h> #include <time.h> #include <errno.h> #include <stdbool.h> #include <string.h> #define MAX_SIZE 600 #define TEST_ERR if(errno){\ printf("PROC A %d\n",__LINE__);\ printf("%s\n",strerror(errno));\ exit(-1);\ } struct data{ char * name; unsigned long gen; char type; pid_t pid; }; struct msg{ long mytype; int text; }; union semun{ int val; struct semid_ds *buf; unsigned short *array; struct seminfo *__buf; }; unsigned long MCD(unsigned long n, unsigned long m); int main(int argc , char * argv[]){ //TMP int tmp_n_proc = 0; unsigned long tmp_gen = 0; pid_t tmp_pid; //STRUTTURE struct msg my_msg; struct data * my_data = malloc(sizeof(struct data)); struct data * partner_data; struct sembuf my_buf; union semun my_union; //DATI char tipo = 'A'; char * nome = argv[2]; unsigned long genoma = atol(argv[3]); int init_people = atoi(argv[4]); int n_proc = atoi(argv[5]); int length = 12+init_people*4;//LUNGHEZZA ARRAY; int flag = atoi(argv[length -6]); //TARGET unsigned long target = 0; //ALTRI DATI int shm_id[init_people]; int msg_id[init_people]; int sem_shm[init_people]; int sem_msg[init_people]; int sem_B; int sem_A; int msg_parent; int sem_parent; for(int i = 0; i < init_people; i++){ shm_id[i] = atoi(argv[6+4*i]); msg_id[i] = atoi(argv[7+4*i]); sem_shm[i] = atoi(argv[8+4*i]); sem_msg[i] = atoi(argv[9+4*i]); } sem_B = atoi(argv[length-5]); sem_A = atoi(argv[length-4]); msg_parent = atoi(argv[length-3]); sem_parent = atoi(argv[length-2]); //SCRIVE I DATI E... my_data = shmat(shm_id[n_proc],NULL,SHM_RND); my_data->gen = genoma; my_data->name = nome; my_data->type = tipo; shmdt(my_data); //...LO COMUNICA AL PARENT my_buf.sem_op = -1; my_buf.sem_flg = 0; my_buf.sem_num = 0; semop(sem_A,&my_buf,1); my_buf.sem_flg = 0; while(1){ //SI METTE IN ATTESA msgrcv(msg_id[n_proc],&my_msg,MAX_SIZE,0,0); TEST_ERR; //CONTROLLA I DATI DELL' ALTRO tmp_n_proc = my_msg.text; my_buf.sem_op = -1; semop(sem_shm[tmp_n_proc],&my_buf,1); TEST_ERR; partner_data = shmat(shm_id[tmp_n_proc],NULL,SHM_RND); tmp_gen = partner_data->gen; tmp_pid = partner_data->pid; shmdt(partner_data); if(MCD(tmp_gen,genoma) >= target || tmp_gen % genoma == 0){ flag = 1; my_msg.mytype = 1; my_msg.text = 1; msgsnd(msg_id[tmp_n_proc],&my_msg,MAX_SIZE,0); TEST_ERR; my_buf.sem_op = 1; my_buf.sem_num = 0; my_buf.sem_flg = 0; semop(sem_shm[tmp_n_proc],&my_buf,1); //CONTATTA IL GESTORE my_buf.sem_op = -1; semop(sem_parent,&my_buf,1); TEST_ERR; my_msg.text = n_proc; TEST_ERR; msgsnd(msg_parent,&my_msg,MAX_SIZE,0); TEST_ERR; //ATTENZIONE : IL SEMAFORO VIENE RIPRISTINATO DA B flag = 0; exit(0); } //RIFIUTA my_msg.mytype = 1; my_msg.text = 0; msgsnd(msg_id[tmp_n_proc],&my_msg,MAX_SIZE,0); my_buf.sem_op = 1; semop(sem_shm[tmp_n_proc],&my_buf,1); target = target - 2; } } unsigned long MCD(unsigned long a,unsigned long b) // prototipo della funzione Euclide // { int r; if(b == 0){ return a; } while(b != 0) { r = a % b; a = b; b = r; } return a; }
PROCESSI B
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/wait.h> #include <sys/ipc.h> #include <sys/shm.h> #include <sys/sem.h> #include <sys/msg.h> #include <sys/types.h> #include <time.h> #include <errno.h> #include <stdbool.h> #include <string.h> #define MAX_SIZE 600 #define TEST_ERR if(errno){\ printf("PROC B %d\n",__LINE__);\ printf("%s\n",strerror(errno));\ exit(-1);\ } struct data{ char * name; unsigned long gen; char type; pid_t pid; }; struct msg{ long mytype; long text; }; union semun{ int val; struct semid_ds *buf; unsigned short *array; struct seminfo *__buf; }; bool flag = false; unsigned long MCD(unsigned long n, unsigned long m); unsigned long rand_range(int low, int high,int seed); int main(int argc , char * argv[]){ //TMP int i = -1; char tmp_type = 'Z'; int tmp_n_proc = 0; unsigned long tmp_gen = 0; pid_t tmp_pid; //STRUTTURE struct msg my_msg; struct data * my_data; struct data * partner_data; struct sembuf my_buf; union semun my_union; //DATI char tipo = 'B'; char * nome = argv[2]; unsigned long genoma = atol(argv[3]); int init_people = atoi(argv[4]); int n_proc = atoi(argv[5]); int length = 12+init_people*4;//LUNGHEZZA ARRAY; int flag = atoi(argv[length -6]); //TARGET unsigned long target = 0; //ALTRI DATI int prova = 0; int shm_id[init_people]; int msg_id[init_people]; int sem_shm[init_people]; int sem_msg[init_people]; int sem_B; int sem_A; int msg_parent; int sem_parent; for(int i = 0; i < init_people; i++){ shm_id[i] = atoi(argv[6+4*i]); msg_id[i] = atoi(argv[7+4*i]); sem_shm[i] = atoi(argv[8+4*i]); sem_msg[i] = atoi(argv[9+4*i]); } sem_B = atoi(argv[length-5]); sem_A = atoi(argv[length-4]); msg_parent = atoi(argv[length-3]); sem_parent = atoi(argv[length-2]); //PUBBLICA I SUOI DATI my_buf.sem_op = -1; my_buf.sem_flg = 0; my_buf.sem_num = 0; semop(sem_shm[n_proc],&my_buf,1); TEST_ERR; my_data = shmat(shm_id[n_proc],NULL,SHM_RND); my_data->name = nome; my_data->gen = genoma; my_data->pid = getpid(); shmdt(my_data); my_buf.sem_op = 1; semop(sem_shm[n_proc],&my_buf,1); TEST_ERR; //ASPETTA SEGNALE my_buf.sem_op = -1; my_buf.sem_flg = 0; my_buf.sem_num = 0; semop(sem_B,&my_buf,1); TEST_ERR; while(1){ prova = (int)rand_range(0,init_people,clock()); my_buf.sem_flg = 0; my_buf.sem_num = 0; my_buf.sem_op = -1; semop(sem_shm[prova],&my_buf,1); partner_data = shmat(shm_id[prova],NULL,SHM_RND); tmp_pid = partner_data->pid; if(partner_data->type == 'A' && MCD(partner_data->gen,genoma) >= target){ shmdt(partner_data); my_msg.mytype = 1; my_msg.text = n_proc; TEST_ERR; my_buf.sem_op = -1; semop(sem_msg[prova],&my_buf,1); msgsnd(msg_id[prova],&my_msg,MAX_SIZE,0); TEST_ERR; my_buf.sem_op = 1; semop(sem_shm[prova],&my_buf,1); TEST_ERR; my_buf.sem_op = 1; semop(sem_msg[prova],&my_buf,1); TEST_ERR; //ATTENDE RISPOSTA msgrcv(msg_id[n_proc],&my_msg,MAX_SIZE,0,0); TEST_ERR; if(my_msg.text == 1){ flag = 1; my_msg.mytype = 1; my_msg.text = n_proc; TEST_ERR; msgsnd(msg_parent,&my_msg,MAX_SIZE,0); TEST_ERR; my_buf.sem_op = 1; my_buf.sem_num = 0; semop(sem_parent,&my_buf,1);//RIPRISTINA IL SEMAFORO TEST_ERR; flag = 0; exit(0); } } target = target - 2; } } unsigned long MCD(unsigned long a,unsigned long b) { int r; if(b == 0){ return a; } while(b != 0) { r = a % b; a = b; b = r; } return a; } unsigned long rand_range(int low, int high,int seed) { srand(seed + time(NULL)); return (low+rand()%(high-low+1)); }
Vi ringrazio anticipatamente per la risposta,
saluti!!
Risposte
CHIEDO SCUSA!! NON BAD ARGUMENT BENSI' BAD ADDRESS