erased_signal_handlers 161 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819
  1. /*void sig_IDAG_FIND_IDAGS_handler(int signo, siginfo_t *info, void *context)
  2. {
  3. int fd_r=-1, sender_id, num_of_idags, i, one_idag;
  4. char *fifo_name;
  5. region cur_reg;
  6. inter_list *tmp_inter_list;
  7. //printf("I came through FIND_IDAGS!! idag_id = %d, node_id = %d\n",idag_id,node_id);
  8. signals_disable();
  9. sender_id = get_id_from_pid(info->si_pid);
  10. fifo_name = get_pipe_name(node_id);
  11. cur_time = time(NULL);
  12. cur_t = localtime(&cur_time);
  13. fprintf(log_file, "[%d:%d:%d]: I entered sig_IDAG_FIND_IDAGS_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  14. fflush(log_file);
  15. if (core_inter_head[sender_id] == NULL || core_inter_head[sender_id]->type == REP_IDAG_FIND_IDAGS){ //I am the idag
  16. sem_getvalue(&node_sem[node_id],&i);
  17. fprintf(log_file, "In 1 Trying to acquire semaphore. Sem value = %d\n",i);
  18. fflush(log_file);
  19. kill(info->si_pid, SIG_ACK);
  20. sem_wait(&node_sem[node_id]);
  21. sem_getvalue(&node_sem[node_id],&i);
  22. fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
  23. fflush(log_file);
  24. my_stats.msg_count++;
  25. my_stats.distance += distance(node_id,sender_id);
  26. //printf("I am inside with sender_id %d %s\n",sender_id,fifo_name);
  27. //printf("open3 idag_id=%d node_id=%d\n",idag_id,node_id);
  28. fd_r = open(fifo_name, O_RDONLY);
  29. if (fd_r == -1) {
  30. perror("opening reading pipe");
  31. signals_enable();
  32. return;
  33. }
  34. read(fd_r, &cur_reg, sizeof(region));
  35. //printf("asdI am node with id %d idag_id %d and I am to investigate region C=%d r=%d\n",node_id,idag_id,cur_reg.C,cur_reg.r);
  36. if (core_inter_head[sender_id] == NULL){
  37. core_inter_head[sender_id] = (inter_list *) malloc(sizeof(inter_list));
  38. core_inter_tail[sender_id] = core_inter_head[sender_id];
  39. } else {
  40. core_inter_tail[sender_id]->next = (inter_list *) malloc(sizeof(inter_list));
  41. core_inter_tail[sender_id] = core_inter_tail[sender_id]->next;
  42. }
  43. core_inter_tail[sender_id]->type = REP_IDAG_FIND_IDAGS;
  44. core_inter_tail[sender_id]->data.idags_in_reg = (int *) malloc((num_idags_x*num_idags_y+1)*sizeof(int));
  45. core_inter_tail[sender_id]->next = NULL;
  46. get_reg_idags(cur_reg, core_inter_tail[sender_id]->data.idags_in_reg);
  47. if (core_inter_head[sender_id]->next == NULL) {
  48. kill(info->si_pid, SIG_IDAG_FIND_IDAGS);
  49. my_stats.msg_count++;
  50. my_stats.distance += distance(node_id,sender_id);
  51. }
  52. //sem_post(&node_sem[node_id]);
  53. sem_getvalue(&node_sem_out[sender_id],&i);
  54. fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
  55. fflush(log_file);
  56. if (sem_post(&node_sem_out[sender_id]) == -1){
  57. printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
  58. perror("sem_post");
  59. }
  60. sem_getvalue(&node_sem_out[sender_id],&i);
  61. fprintf(log_file, "Sem value = %d\n",i);
  62. fflush(log_file);
  63. } else if (core_inter_head[sender_id]->type == IDAG_FIND_IDAGS && state != IDLE_INIT_MAN && state != WORKING_NODE_IDLE_INIT) {
  64. //IDLE_INIT_MAN_SELFOPT_PENDING && state != IDLE_INIT_MAN_WORK_PENDING) {
  65. printf("I am %d and i think i cought a stray init idag_find_idags reply from %d. My current state is %d\n",node_id,sender_id,state);
  66. fprintf(log_file,"I am %d and i think i cought a stray init idag_find_idags reply from %d. My current state is %d\n",node_id,sender_id,state);
  67. fflush(log_file);
  68. kill(info->si_pid, SIG_REJECT);
  69. my_stats.msg_count++;
  70. my_stats.distance += distance(node_id,sender_id);
  71. tmp_inter_list = core_inter_head[sender_id];
  72. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  73. free(tmp_inter_list);
  74. if(core_inter_head[sender_id] == NULL) {
  75. if (core_inter_tail[sender_id] == NULL){
  76. printf("Malaka gamietai to core_inter_tail\n");
  77. core_inter_tail[sender_id] = NULL;
  78. }
  79. } else send_next_signal(core_inter_head[sender_id], sender_id);
  80. } else if (core_inter_head[sender_id]->type == SELFOPT_IDAG_FIND_IDAGS && state != IDLE_AGENT_WAITING_OFF) {
  81. printf("I am %d and i think i cought a stray selfopt idag_find_idags reply from %d. My current state is %d\n",node_id,sender_id,state);
  82. fprintf(log_file,"I am %d and i think i cought a stray selfopt idag_find_idags reply from %d. My current state is %d\n",node_id,sender_id,state);
  83. fflush(log_file);
  84. kill(info->si_pid, SIG_REJECT);
  85. my_stats.msg_count++;
  86. my_stats.distance += distance(node_id,sender_id);
  87. tmp_inter_list = core_inter_head[sender_id];
  88. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  89. free(tmp_inter_list);
  90. if(core_inter_head[sender_id] == NULL) {
  91. if (core_inter_tail[sender_id] == NULL){
  92. printf("Malaka gamietai to core_inter_tail\n");
  93. core_inter_tail[sender_id] = NULL;
  94. }
  95. } else send_next_signal(core_inter_head[sender_id], sender_id);
  96. } else if (core_inter_head[sender_id]->type == IDAG_FIND_IDAGS || core_inter_head[sender_id]->type == SELFOPT_IDAG_FIND_IDAGS){ //I am the requesting common node
  97. //printf("qweI am inside i am node with node id %d idag_id %d and sender_id %d %s\n",node_id,idag_id,sender_id,fifo_name);
  98. sem_getvalue(&node_sem[node_id],&i);
  99. fprintf(log_file, "In 2 Trying to acquire semaphore. Sem value = %d\n",i);
  100. fflush(log_file);
  101. kill(info->si_pid, SIG_ACK);
  102. sem_wait(&node_sem[node_id]);
  103. sem_getvalue(&node_sem[node_id],&i);
  104. fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
  105. fflush(log_file);
  106. my_stats.msg_count++;
  107. my_stats.distance += distance(node_id,sender_id);
  108. //printf("open4 idag_id=%d node_id=%d\n",idag_id,node_id);
  109. fd_r = open(fifo_name, O_RDONLY);
  110. if (fd_r == -1) {
  111. perror("opening reading pipe");
  112. signals_enable();
  113. return;
  114. }
  115. read(fd_r, &num_of_idags, sizeof(int));
  116. fprintf(log_file,"Number of agents in region %d\n",num_of_idags);
  117. fflush(log_file);
  118. //if (core_inter_head[sender_id]->type == FAR_INIT_IDAG_FIND_IDAGS) far_req_max_man_count = num_of_idags;
  119. //else
  120. if (core_inter_head[sender_id]->type == IDAG_FIND_IDAGS) {
  121. init_idags_areas_replies++;
  122. init_DDS_idags += num_of_idags;
  123. } else if (core_inter_head[sender_id]->type == SELFOPT_IDAG_FIND_IDAGS) selfopt_DDS_idags += num_of_idags;
  124. for (i=0; i<num_of_idags; i++){
  125. read(fd_r, &one_idag, sizeof(int));
  126. fprintf(log_file,"In the region I have idag with id %d\n",one_idag);
  127. fflush(log_file);
  128. if (core_inter_head[one_idag] == NULL){
  129. core_inter_head[one_idag] = (inter_list *) malloc(sizeof(inter_list));
  130. core_inter_tail[one_idag] = core_inter_head[one_idag];
  131. } else {
  132. for (tmp_inter_list = core_inter_head[one_idag]; tmp_inter_list != NULL; tmp_inter_list = tmp_inter_list->next)
  133. if (tmp_inter_list->type == FAR_REQ_MAN) break;
  134. if (tmp_inter_list != NULL) {
  135. fprintf(log_file,"I dismissed node %d in rep_idag_find_idags\n",one_idag);
  136. fflush(log_file);
  137. init_DDS_idags--;
  138. continue;
  139. }
  140. core_inter_tail[one_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  141. core_inter_tail[one_idag] = core_inter_tail[one_idag]->next;
  142. }
  143. if (core_inter_head[sender_id]->type == IDAG_FIND_IDAGS) core_inter_tail[one_idag]->type = IDAG_REQ_DDS_PENDING;
  144. else if (core_inter_head[sender_id]->type == FAR_REQ_IDAG_FIND_IDAGS) core_inter_tail[one_idag]->type = FAR_REQ_IDAG_REQ_DDS_PENDING;
  145. else core_inter_tail[one_idag]->type = SELFOPT_IDAG_REQ_DDS_PENDING;
  146. core_inter_tail[one_idag]->data.reg.C = core_inter_head[sender_id]->data.reg.C;
  147. core_inter_tail[one_idag]->data.reg.r = core_inter_head[sender_id]->data.reg.r;
  148. core_inter_tail[one_idag]->next = NULL;
  149. if (core_inter_head[one_idag]->next == NULL) {
  150. kill(pid_num[one_idag], SIG_REQ_DDS); //newly created
  151. my_stats.msg_count++;
  152. my_stats.distance += distance(node_id,one_idag);
  153. } else {
  154. fprintf(log_file,"I did not sent req_dds to %d with interaction = %d inter 2=%d\n",one_idag,core_inter_head[one_idag]->type,core_inter_head[one_idag]->next->type);
  155. fflush(log_file);
  156. }
  157. }
  158. //sem_post(&node_sem[node_id]);
  159. sem_getvalue(&node_sem_out[sender_id],&i);
  160. fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
  161. fflush(log_file);
  162. if (sem_post(&node_sem_out[sender_id]) == -1){
  163. printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
  164. perror("sem_post");
  165. }
  166. sem_getvalue(&node_sem_out[sender_id],&i);
  167. fprintf(log_file, "Sem value = %d\n",i);
  168. fflush(log_file);
  169. tmp_inter_list = core_inter_head[sender_id];
  170. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  171. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  172. else send_next_signal(core_inter_head[sender_id], sender_id);
  173. free(tmp_inter_list);
  174. } else if (core_inter_head[sender_id]->type == IDAG_FIND_IDAGS_PENDING || core_inter_head[sender_id]->type == SELFOPT_IDAG_FIND_IDAGS_PENDING){
  175. printf("I am %d and deadlock was prevented in sig_find_idags\n",node_id);
  176. tmp_inter_list = core_inter_head[sender_id];
  177. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  178. free(tmp_inter_list);
  179. if(core_inter_head[sender_id] == NULL) {
  180. if (core_inter_tail[sender_id] == NULL){
  181. printf("Malaka gamietai to core_inter_tail\n");
  182. core_inter_tail[sender_id] = NULL;
  183. }
  184. } else send_next_signal(core_inter_head[sender_id], sender_id);
  185. } else {
  186. printf("I have to reject sig_IDAG_FIND_IDAGS_handler sender_id=%d node_id=%d interaction=%d\n",sender_id,node_id,core_inter_head[sender_id]->type);
  187. fprintf(log_file,"I have to reject sig_IDAG_FIND_IDAGS_handler sender_id=%d interaction=%d\n",sender_id,core_inter_head[sender_id]->type);
  188. fflush(log_file);
  189. kill(info->si_pid, SIG_REJECT);
  190. my_stats.msg_count++;
  191. my_stats.distance += distance(node_id,sender_id);
  192. }
  193. if (fd_r != -1) close(fd_r);
  194. free(fifo_name);
  195. cur_time = time(NULL);
  196. cur_t = localtime(&cur_time);
  197. fprintf(log_file, "[%d:%d:%d]: I ended sig_IDAG_FIND_IDAGS_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  198. fflush(log_file);
  199. signals_enable();
  200. }*/
  201. /*void sig_REQ_DDS_handler(int signo, siginfo_t *info, void *context)
  202. {
  203. int fd_r=-1, sender_id, num_of_agents, i, agent_id, num_of_cores,j;
  204. char *fifo_name;
  205. region cur_reg;
  206. core_list *tmp_cores_list;
  207. inter_list *tmp_inter_list;
  208. DDS_list *tmp_DDS;
  209. target_list *tmp_target_list;
  210. signals_disable();
  211. sender_id = get_id_from_pid(info->si_pid);
  212. fifo_name = get_pipe_name(node_id);
  213. cur_time = time(NULL);
  214. cur_t = localtime(&cur_time);
  215. fprintf(log_file, "[%d:%d:%d]: I entered sig_REQ_DDS_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  216. fflush(log_file);
  217. if (core_inter_head[sender_id] == NULL){ //I am the idag
  218. //printf("I am %d and %d requested DDS\n",idag_id,sender_id);
  219. sem_getvalue(&node_sem[node_id],&i);
  220. fprintf(log_file, "In 3 Trying to acquire semaphore. Sem value = %d\n",i);
  221. fflush(log_file);
  222. kill(info->si_pid, SIG_ACK);
  223. sem_wait(&node_sem[node_id]);
  224. sem_getvalue(&node_sem[node_id],&i);
  225. fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
  226. fflush(log_file);
  227. my_stats.msg_count++;
  228. my_stats.distance += distance(node_id,sender_id);
  229. //printf("open5 idag_id=%d node_id=%d\n",idag_id,node_id);
  230. fd_r = open(fifo_name, O_RDONLY);
  231. if (fd_r == -1) {
  232. perror("opening reading pipe");
  233. signals_enable();
  234. return;
  235. }
  236. read(fd_r, &cur_reg, sizeof(region));
  237. //sem_post(&node_sem[sender_id]);
  238. //while (cur_reg.C < 0) {
  239. // printf("I am %d and i got shit region (%d,%d) in req_dds from %d\n",node_id,cur_reg.C,cur_reg.r,sender_id);
  240. // read(fd_r, &cur_reg, sizeof(region));
  241. //}
  242. fprintf(log_file,"I am to investigate region C=%d r=%d for %d\n",cur_reg.C,cur_reg.r,sender_id);
  243. fflush(log_file);
  244. if (core_inter_head[sender_id] == NULL){
  245. core_inter_head[sender_id] = (inter_list *) malloc(sizeof(inter_list));
  246. core_inter_tail[sender_id] = core_inter_head[sender_id];
  247. } else {
  248. printf("Ton hpiame\n");
  249. core_inter_tail[sender_id]->next = (inter_list *) malloc(sizeof(inter_list));
  250. core_inter_tail[sender_id] = core_inter_tail[sender_id]->next;
  251. }
  252. core_inter_tail[sender_id]->type = REP_IDAG_REQ_DDS;
  253. core_inter_tail[sender_id]->next = NULL;
  254. if (cur_reg.C > -1) {//gia na zhtaei o node 0 dds
  255. core_inter_tail[sender_id]->data.agents_in_reg = (int *) malloc((2*DDS_count+1)*sizeof(int));
  256. core_inter_tail[sender_id]->data.agents_in_reg[0] = 0;
  257. for (i=1; i<=2*DDS_count; i+=2){
  258. core_inter_tail[sender_id]->data.agents_in_reg[i] = -1;
  259. core_inter_tail[sender_id]->data.agents_in_reg[i+1] = 0;
  260. }
  261. tmp_cores_list = my_cores;
  262. while (tmp_cores_list != NULL){
  263. if (distance(tmp_cores_list->core_id, cur_reg.C) <= cur_reg.r){
  264. if (tmp_cores_list->offered_to == -1) agent_id=node_id;
  265. else {
  266. agent_id=tmp_cores_list->offered_to;
  267. for (tmp_DDS = DDS->next; tmp_DDS!=NULL; tmp_DDS=tmp_DDS->next) if (tmp_DDS->agent_id == agent_id) break;
  268. if (tmp_DDS == NULL) agent_id = node_id;
  269. }
  270. for (i=1; i<=2*DDS_count; i+=2)
  271. if (core_inter_tail[sender_id]->data.agents_in_reg[i] == agent_id) break;
  272. else if (core_inter_tail[sender_id]->data.agents_in_reg[i] == -1){
  273. core_inter_tail[sender_id]->data.agents_in_reg[0]++;
  274. core_inter_tail[sender_id]->data.agents_in_reg[i] = agent_id;
  275. break;
  276. }
  277. core_inter_tail[sender_id]->data.agents_in_reg[i+1]++;
  278. }
  279. tmp_cores_list = tmp_cores_list->next;
  280. }
  281. } else core_inter_tail[sender_id]->data.agents_in_reg = NULL;
  282. kill(info->si_pid, SIG_REQ_DDS);
  283. my_stats.msg_count++;
  284. my_stats.distance += distance(node_id,sender_id);
  285. //sem_post(&node_sem[node_id]);
  286. sem_getvalue(&node_sem_out[sender_id],&i);
  287. fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
  288. fflush(log_file);
  289. if (sem_post(&node_sem_out[sender_id]) == -1){
  290. printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
  291. perror("sem_post");
  292. }
  293. sem_getvalue(&node_sem_out[sender_id],&i);
  294. fprintf(log_file, "Sem value = %d\n",i);
  295. fflush(log_file);
  296. } else if (core_inter_head[sender_id]->type == IDAG_REQ_DDS && state != IDLE_INIT_MAN && state != WORKING_NODE_IDLE_INIT) {
  297. //IDLE_INIT_MAN_SELFOPT_PENDING && state != IDLE_INIT_MAN_WORK_PENDING) {
  298. printf("I am %d and i think i cought a stray init IDAG_REQ_DDS reply from %d. My current state is %d\n",node_id,sender_id,state);
  299. fprintf(log_file,"I am %d and i think i cought a stray init IDAG_REQ_DDS reply from %d. My current state is %d\n",node_id,sender_id,state);
  300. fflush(log_file);
  301. kill(info->si_pid, SIG_REJECT);
  302. my_stats.msg_count++;
  303. my_stats.distance += distance(node_id,sender_id);
  304. tmp_inter_list = core_inter_head[sender_id];
  305. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  306. free(tmp_inter_list);
  307. if(core_inter_head[sender_id] == NULL) {
  308. if (core_inter_tail[sender_id] == NULL){
  309. printf("Malaka gamietai to core_inter_tail\n");
  310. core_inter_tail[sender_id] = NULL;
  311. }
  312. } else send_next_signal(core_inter_head[sender_id], sender_id);
  313. } else if (core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS && state != IDLE_AGENT_WAITING_OFF) {
  314. printf("I am %d and i think i cought a stray selfopt SELFOPT_IDAG_REQ_DDS reply from %d. My current state is %d\n",node_id,sender_id,state);
  315. fprintf(log_file,"I am %d and i think i cought a stray selfopt SELFOPT_IDAG_REQ_DDS reply from %d. My current state is %d\n",node_id,sender_id,state);
  316. fflush(log_file);
  317. kill(info->si_pid, SIG_REJECT);
  318. my_stats.msg_count++;
  319. my_stats.distance += distance(node_id,sender_id);
  320. tmp_inter_list = core_inter_head[sender_id];
  321. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  322. free(tmp_inter_list);
  323. if(core_inter_head[sender_id] == NULL) {
  324. if (core_inter_tail[sender_id] == NULL){
  325. printf("Malaka gamietai to core_inter_tail\n");
  326. core_inter_tail[sender_id] = NULL;
  327. }
  328. } else send_next_signal(core_inter_head[sender_id], sender_id);
  329. } else if (core_inter_head[sender_id]->type == IDAG_REQ_DDS || core_inter_head[sender_id]->type == FAR_REQ_IDAG_REQ_DDS || core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS){ //I am the requesting common node
  330. //printf("vbnI am inside i am node with node id %d and sender_id %d\n",node_id,sender_id);
  331. sem_getvalue(&node_sem[node_id],&i);
  332. fprintf(log_file, "In 4 Trying to acquire semaphore. Sem value = %d\n",i);
  333. fflush(log_file);
  334. kill(info->si_pid, SIG_ACK);
  335. sem_wait(&node_sem[node_id]);
  336. sem_getvalue(&node_sem[node_id],&i);
  337. fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
  338. fflush(log_file);
  339. my_stats.msg_count++;
  340. my_stats.distance += distance(node_id,sender_id);
  341. //printf("open5 idag_id=%d node_id=%d\n",idag_id,node_id);
  342. fd_r = open(fifo_name, O_RDONLY);
  343. if (fd_r == -1) {
  344. perror("opening reading pipe");
  345. signals_enable();
  346. return;
  347. }
  348. if (core_inter_head[sender_id]->type == IDAG_REQ_DDS) {
  349. init_DDS_replies++;
  350. fprintf(log_file,"init_DDS_idags = %d, init_DDS_replies = %d init_idags_areas_replies=%d\n",init_DDS_idags,init_DDS_replies,init_idags_areas_replies);
  351. fflush(log_file);
  352. } else if (core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS) {
  353. selfopt_DDS_replies++;
  354. fprintf(log_file,"selfopt_DDS_idags = %d, selfopt_DDS_replies = %d\n",selfopt_DDS_idags,selfopt_DDS_replies);
  355. fflush(log_file);
  356. }
  357. read(fd_r, &num_of_agents, sizeof(int));
  358. while (num_of_agents > X_max*Y_max) {
  359. printf("I am %d and in req_dds i got shit num_of_agents %d from %d\n",node_id,num_of_agents,sender_id);
  360. read(fd_r, &num_of_agents, sizeof(int));
  361. }
  362. //if (core_inter_head[sender_id]->type == FAR_REQ_IDAG_REQ_DDS) far_man_agent_count += num_of_agents;
  363. //printf("Number of agents in region = %d\n",num_of_agents);
  364. //fprintf(log_file, "Number of agents in region = %d of %d init_DDS_replies = %d init_DDS_idags = %d\n",num_of_agents,sender_id,init_DDS_replies,init_DDS_idags);
  365. fprintf(log_file, "Number of agents in region = %d of %d reg = (%d,%d)\n",num_of_agents,sender_id,core_inter_head[sender_id]->data.reg.C,core_inter_head[sender_id]->data.reg.r);
  366. fflush(log_file);
  367. for (i=0; i<num_of_agents; i++) {
  368. read(fd_r, &agent_id, sizeof(int));
  369. read(fd_r, &num_of_cores, sizeof(int));
  370. //printf("zxc I am %d and In the DDS of %d there is an agent with id %d and %d cores\n",node_id,sender_id,agent_id,num_of_cores);
  371. fprintf(log_file, "there is an agent with id %d and %d cores\n",agent_id,num_of_cores);
  372. fflush(log_file);
  373. if (agent_id == node_id) continue;//((node_id == -1 && agent_id == idag_id) || node_id == agent_id)
  374. if (core_inter_head[sender_id]->type == IDAG_REQ_DDS){
  375. tmp_target_list = init_targets_head;
  376. while (tmp_target_list != NULL && tmp_target_list->target != agent_id)
  377. //if (tmp_target_list->target != agent_id)
  378. tmp_target_list = tmp_target_list->next;
  379. if (tmp_target_list == NULL) {
  380. if (init_targets_head == NULL) {
  381. init_targets_head = (target_list *) malloc(sizeof(target_list));
  382. init_targets_tail = init_targets_head;;
  383. } else {
  384. init_targets_tail->next = (target_list *) malloc(sizeof(target_list));
  385. init_targets_tail = init_targets_tail->next;
  386. }
  387. //fprintf(log_file, "in here it begins a\n");
  388. //fflush(log_file);
  389. init_targets_tail->next = NULL;
  390. init_targets_tail->target = agent_id;
  391. init_targets_tail->num_of_regions = 1;
  392. //init_targets_tail->region_arr = (region *) malloc(INIT_AREAS_NUM * sizeof(region));
  393. init_targets_tail->region_arr[0] = core_inter_head[sender_id]->data.reg;
  394. //fprintf(log_file, "in here it begins b\n");
  395. //fflush(log_file);
  396. } else {
  397. fprintf(log_file, "in here num_of_regions = %d\n",tmp_target_list->num_of_regions);
  398. fflush(log_file);
  399. for (j=0; j<tmp_target_list->num_of_regions; j++)
  400. if (tmp_target_list->region_arr[j].C == core_inter_head[sender_id]->data.reg.C && tmp_target_list->region_arr[j].r == core_inter_head[sender_id]->data.reg.r){
  401. fprintf(log_file, "fucking area allready exists\n");
  402. fflush(log_file);
  403. break;
  404. }
  405. if (j == tmp_target_list->num_of_regions) {
  406. tmp_target_list->region_arr[tmp_target_list->num_of_regions++] = core_inter_head[sender_id]->data.reg;
  407. //fprintf(log_file, "new area added\n");
  408. //fflush(log_file);
  409. }
  410. }
  411. } else if (core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS){
  412. tmp_target_list = selfopt_targets_head;
  413. while (tmp_target_list != NULL && tmp_target_list->target != agent_id)
  414. tmp_target_list = tmp_target_list->next;
  415. if (tmp_target_list == NULL) {
  416. if (selfopt_targets_head == NULL) {
  417. selfopt_targets_head = (target_list *) malloc(sizeof(target_list));
  418. selfopt_targets_tail = selfopt_targets_head;;
  419. } else {
  420. selfopt_targets_tail->next = (target_list *) malloc(sizeof(target_list));
  421. selfopt_targets_tail = selfopt_targets_tail->next;
  422. }
  423. selfopt_targets_tail->next = NULL;
  424. selfopt_targets_tail->target = agent_id;
  425. selfopt_targets_tail->num_of_regions = 1;
  426. selfopt_targets_tail->region_arr[0] = core_inter_head[sender_id]->data.reg;
  427. } else {
  428. for (j=0; j<tmp_target_list->num_of_regions; j++)
  429. if (tmp_target_list->region_arr[j].C == core_inter_head[sender_id]->data.reg.C && tmp_target_list->region_arr[j].r == core_inter_head[sender_id]->data.reg.r){
  430. break;
  431. }
  432. if (j == tmp_target_list->num_of_regions)
  433. tmp_target_list->region_arr[tmp_target_list->num_of_regions++] = core_inter_head[sender_id]->data.reg;
  434. } //tmp_target_list->region_arr[tmp_target_list->num_of_regions++] = core_inter_head[sender_id]->data.reg;
  435. } else if (core_inter_head[sender_id]->type == FAR_REQ_IDAG_REQ_DDS){
  436. if (core_inter_head[agent_id] == NULL){
  437. core_inter_head[agent_id] = (inter_list *) malloc(sizeof(inter_list));
  438. core_inter_tail[agent_id] = core_inter_head[agent_id];
  439. } else {
  440. core_inter_tail[agent_id]->next = (inter_list *) malloc(sizeof(inter_list));
  441. core_inter_tail[agent_id] = core_inter_tail[agent_id]->next;
  442. }
  443. //if (core_inter_head[sender_id]->type == IDAG_REQ_DDS) core_inter_tail[agent_id]->type = AGENT_REQ_CORES_PENDING;
  444. //else if (core_inter_head[sender_id]->type == FAR_REQ_IDAG_REQ_DDS) core_inter_tail[agent_id]->type = FAR_REQ_CORES;
  445. //else core_inter_tail[agent_id]->type = SELFOPT_REQ_CORES_PENDING;
  446. core_inter_tail[agent_id]->type = FAR_REQ_CORES_PENDING;
  447. core_inter_tail[agent_id]->data.reg = core_inter_head[sender_id]->data.reg;
  448. core_inter_tail[agent_id]->next = NULL;
  449. if (core_inter_head[agent_id]->next == NULL) {
  450. kill(pid_num[agent_id], SIG_REQ_CORES); //newly created, not an idag
  451. my_stats.msg_count++;
  452. my_stats.distance += distance(node_id,agent_id);
  453. }
  454. //else printf("It is fucked here interaction= %d\n",core_inter_head[agent_id]->type);
  455. }
  456. }
  457. if (core_inter_head[sender_id]->type == IDAG_REQ_DDS && init_DDS_replies == init_DDS_idags && init_idags_areas_replies == init_areas_num) {
  458. for (tmp_target_list = init_targets_head; tmp_target_list != NULL; tmp_target_list = tmp_target_list->next){
  459. agent_id = tmp_target_list->target;
  460. for (tmp_inter_list = core_inter_head[agent_id]; tmp_inter_list != NULL; tmp_inter_list=tmp_inter_list->next)
  461. if (tmp_inter_list->type == FAR_INIT_REQ || tmp_inter_list->type == FAR_REQ_MAN) break;
  462. if (tmp_inter_list == NULL) {
  463. if (core_inter_head[agent_id] == NULL){
  464. core_inter_head[agent_id] = (inter_list *) malloc(sizeof(inter_list));
  465. core_inter_tail[agent_id] = core_inter_head[agent_id];
  466. } else {
  467. core_inter_tail[agent_id]->next = (inter_list *) malloc(sizeof(inter_list));
  468. core_inter_tail[agent_id] = core_inter_tail[agent_id]->next;
  469. }
  470. core_inter_tail[agent_id]->type = AGENT_REQ_CORES_PENDING;
  471. core_inter_tail[agent_id]->data.reg_arr.num_of_regions = tmp_target_list->num_of_regions;
  472. core_inter_tail[agent_id]->data.reg_arr.region_arr = (region *) malloc(tmp_target_list->num_of_regions * sizeof(region));
  473. if (tmp_target_list->target == node_id) printf("Why is this here node_id=%d\n",node_id);
  474. fprintf(log_file,"Init req target = %d, num_of_regions = %d. Αreas:",tmp_target_list->target,tmp_target_list->num_of_regions);
  475. for (i=0; i<tmp_target_list->num_of_regions; i++) {
  476. core_inter_tail[agent_id]->data.reg_arr.region_arr[i] = tmp_target_list->region_arr[i];
  477. fprintf(log_file," (%d,%d),",tmp_target_list->region_arr[i].C,tmp_target_list->region_arr[i].r);
  478. }
  479. fprintf(log_file,"\n");
  480. fflush(log_file);
  481. core_inter_tail[agent_id]->next = NULL;
  482. //kill(pid_num[agent_id], SIG_REQ_CORES);
  483. if (core_inter_head[agent_id]->next == NULL) {
  484. kill(pid_num[agent_id], SIG_REQ_CORES);
  485. my_stats.msg_count++;
  486. my_stats.distance += distance(node_id,agent_id);
  487. } else if (agent_id == sender_id && core_inter_head[agent_id]->next->type == AGENT_REQ_CORES_PENDING) { //den exei fugei apo to free alla einai wra na stalei
  488. kill(pid_num[agent_id], SIG_REQ_CORES);
  489. my_stats.msg_count++;
  490. my_stats.distance += distance(node_id,agent_id);
  491. } else printf("This init fucker is not NULL interaction=%d interaction2=%d\n",core_inter_head[agent_id]->type,core_inter_head[agent_id]->next->type);
  492. } else
  493. printf("I am %d and i did not send local requests to %d because he is my far manager\n",node_id,agent_id);
  494. }
  495. its.it_value.tv_nsec = INIT_NODE_INTERVAL * MS;//750000000;// * MS;
  496. if (timer_settime(timerid, 0, &its, NULL) == -1) printf("timer_settime error4\n");
  497. } else if (core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS && selfopt_DDS_replies == selfopt_DDS_idags) {
  498. for (tmp_target_list = selfopt_targets_head; tmp_target_list != NULL; tmp_target_list = tmp_target_list->next){
  499. agent_id = tmp_target_list->target;
  500. if (core_inter_head[agent_id] == NULL){
  501. core_inter_head[agent_id] = (inter_list *) malloc(sizeof(inter_list));
  502. core_inter_tail[agent_id] = core_inter_head[agent_id];
  503. } else {
  504. core_inter_tail[agent_id]->next = (inter_list *) malloc(sizeof(inter_list));
  505. core_inter_tail[agent_id] = core_inter_tail[agent_id]->next;
  506. }
  507. core_inter_tail[agent_id]->type = SELFOPT_REQ_CORES_PENDING;
  508. core_inter_tail[agent_id]->data.reg_arr.num_of_regions = tmp_target_list->num_of_regions;
  509. core_inter_tail[agent_id]->data.reg_arr.region_arr = (region *) malloc(tmp_target_list->num_of_regions * sizeof(region));
  510. if (tmp_target_list->target == node_id) printf("selfopt Why is this here node_id=%d\n",node_id);
  511. fprintf(log_file,"Selfopt req target = %d, num_of_regions = %d. Αreas:",tmp_target_list->target,tmp_target_list->num_of_regions);
  512. for (i=0; i<tmp_target_list->num_of_regions; i++) {
  513. core_inter_tail[agent_id]->data.reg_arr.region_arr[i] = tmp_target_list->region_arr[i];
  514. fprintf(log_file," (%d,%d),",tmp_target_list->region_arr[i].C,tmp_target_list->region_arr[i].r);
  515. }
  516. fprintf(log_file,"\n");
  517. fflush(log_file);
  518. core_inter_tail[agent_id]->next = NULL;
  519. if (core_inter_head[agent_id]->next == NULL) {
  520. kill(pid_num[agent_id], SIG_REQ_CORES);
  521. my_stats.msg_count++;
  522. my_stats.distance += distance(node_id,agent_id);
  523. } else if (agent_id == sender_id && core_inter_head[agent_id]->next->type == SELFOPT_REQ_CORES_PENDING) {
  524. kill(pid_num[agent_id], SIG_REQ_CORES);
  525. my_stats.msg_count++;
  526. my_stats.distance += distance(node_id,agent_id);
  527. } else printf("This selfopt fucker is not NULL interaction=%d interaction2=%d\n",core_inter_head[agent_id]->type,core_inter_head[agent_id]->next->type);
  528. }
  529. its.it_value.tv_nsec = 500 * MS;// 750000000;
  530. selfopt_time_rem = its.it_value.tv_nsec;
  531. if (timer_settime(timerid, 0, &its, NULL) == -1) printf("timer_settime error7\n");
  532. if (state != IDLE_AGENT_WAITING_OFF) {
  533. printf("I am %d and about to set my alarm for selfopt check and my state before that was %d\n",node_id,state);
  534. state = IDLE_AGENT_WAITING_OFF;
  535. }
  536. }
  537. //printf("I come here\n");
  538. //sem_post(&node_sem[node_id]);
  539. sem_getvalue(&node_sem_out[sender_id],&i);
  540. fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
  541. fflush(log_file);
  542. if (sem_post(&node_sem_out[sender_id]) == -1){
  543. printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
  544. perror("sem_post");
  545. }
  546. sem_getvalue(&node_sem_out[sender_id],&i);
  547. fprintf(log_file, "Sem value = %d\n",i);
  548. fflush(log_file);
  549. tmp_inter_list = core_inter_head[sender_id];
  550. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  551. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  552. else if (core_inter_head[sender_id]->type != AGENT_REQ_CORES_PENDING && core_inter_head[sender_id]->type != SELFOPT_REQ_CORES_PENDING)//far_req_max_man != sender_id &&
  553. send_next_signal(core_inter_head[sender_id], sender_id);
  554. free(tmp_inter_list);
  555. //printf("I come here as well\n");
  556. } else if (core_inter_head[sender_id]->type == DEBUG_IDAG_REQ_DDS){ //I am the requesting common node
  557. //printf("I am inside i am node with node id %d and sender_id %d %s\n",node_id,sender_id,fifo_name);
  558. sem_getvalue(&node_sem[node_id],&i);
  559. fprintf(log_file, "In DEBUG_IDAG_REQ_DDS Trying to acquire semaphore. Sem value = %d\n",i);
  560. fflush(log_file);
  561. kill(info->si_pid, SIG_ACK);
  562. sem_wait(&node_sem[node_id]);
  563. sem_getvalue(&node_sem[node_id],&i);
  564. fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
  565. fflush(log_file);
  566. //kill(info->si_pid, SIG_ACK);
  567. my_stats.msg_count++;
  568. my_stats.distance += distance(node_id,sender_id);
  569. //printf("open6 idag_id=%d node_id=%d\n",idag_id,node_id);
  570. fd_r = open(fifo_name, O_RDONLY);
  571. if (fd_r == -1) {
  572. perror("opening reading pipe");
  573. signals_enable();
  574. return;
  575. }
  576. read(fd_r, &num_of_agents, sizeof(int));
  577. printf("\n");
  578. printf("Number of agents in region = %d\n",num_of_agents);
  579. for (i=0; i<num_of_agents; i++){
  580. read(fd_r, &agent_id, sizeof(int));
  581. read(fd_r, &num_of_cores, sizeof(int));
  582. printf("Agent no %d is %d with %d cores\n",i,agent_id,num_of_cores);
  583. }
  584. sem_getvalue(&node_sem_out[sender_id],&i);
  585. fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
  586. fflush(log_file);
  587. if (sem_post(&node_sem_out[sender_id]) == -1){
  588. printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
  589. perror("sem_post");
  590. }
  591. sem_getvalue(&node_sem_out[sender_id],&i);
  592. fprintf(log_file, "Sem value = %d\n",i);
  593. fflush(log_file);
  594. tmp_inter_list = core_inter_head[sender_id];
  595. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  596. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  597. else send_next_signal(core_inter_head[sender_id], sender_id);
  598. free(tmp_inter_list);
  599. } else if (core_inter_head[sender_id]->type == IDAG_REQ_DDS_PENDING || core_inter_head[sender_id]->type == FAR_REQ_IDAG_REQ_DDS_PENDING
  600. || core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS_PENDING){
  601. printf("I am %d and deadlock was prevented in sig_req_dds\n",node_id);
  602. tmp_inter_list = core_inter_head[sender_id];
  603. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  604. free(tmp_inter_list);
  605. if(core_inter_head[sender_id] == NULL) {
  606. if (core_inter_tail[sender_id] == NULL){
  607. printf("Malaka gamietai to core_inter_tail\n");
  608. core_inter_tail[sender_id] = NULL;
  609. }
  610. } else send_next_signal(core_inter_head[sender_id], sender_id);
  611. } else {
  612. //printf("I am %d and We were fucked in sig_REQ_DDS_handler from %d interaction is = %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  613. printf("I am %d and i have to reject req_dds from %d. Our interaction is %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  614. fprintf(log_file,"I am %d and i have to reject req_dds from %d. Our interaction is %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  615. fflush(log_file);
  616. kill(info->si_pid, SIG_REJECT);
  617. my_stats.msg_count++;
  618. my_stats.distance += distance(node_id,sender_id);
  619. }
  620. if (fd_r != -1) close(fd_r);
  621. free(fifo_name);
  622. cur_time = time(NULL);
  623. cur_t = localtime(&cur_time);
  624. fprintf(log_file, "[%d:%d:%d]: I ended sig_REQ_DDS_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  625. fflush(log_file);
  626. signals_enable();
  627. }*/
  628. void sig_REQ_CORES_handler(int signo, siginfo_t *info, void *context)
  629. {
  630. int fd_r=-1, sender_id, i, tmp_int, num_of_offers, j, non_zero_offers=0, cores_util;//num_of_idags, i, one_idag;
  631. char *fifo_name;
  632. float req_gain;
  633. region cur_reg;
  634. app req_app;
  635. offer one_offer;
  636. offer_list *tmp_offer_list, *tmp_offer_prev = NULL, *tmp_head, *chosen_node;
  637. inter_list *tmp_inter_list;
  638. offer_array off_arr;
  639. DDS_list *tmp_DDS;
  640. signals_disable();
  641. sender_id = get_id_from_pid(info->si_pid);
  642. fifo_name = get_pipe_name(node_id);
  643. cur_time = time(NULL);
  644. cur_t = localtime(&cur_time);
  645. fprintf(log_file, "[%d:%d:%d]: I entered sig_REQ_CORES_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  646. fflush(log_file);
  647. if (core_inter_head[sender_id] == NULL && my_cores != NULL){ //I am the agent && (my_idag == -1 || (my_idag != -1 && my_app.num_of_cores != -1))
  648. sem_getvalue(&node_sem[node_id],&i);
  649. fprintf(log_file, "In 5 Trying to acquire semaphore. Sem value = %d\n",i);
  650. fflush(log_file);
  651. kill(info->si_pid, SIG_ACK);
  652. sem_wait(&node_sem[node_id]);
  653. sem_getvalue(&node_sem[node_id],&i);
  654. fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
  655. fflush(log_file);
  656. my_stats.msg_count++;
  657. my_stats.distance += distance(node_id,sender_id);
  658. //printf("I am inside with sender_id %d %s\n",sender_id,fifo_name);
  659. //printf("open7 idag_id=%d node_id=%d\n",idag_id,node_id);
  660. fd_r = open(fifo_name, O_RDONLY);
  661. if (fd_r == -1) {
  662. perror("opening reading pipe");
  663. signals_enable();
  664. return;
  665. }
  666. read(fd_r, &req_app, sizeof(app));
  667. //printf("I am node with id %d idag id %d with sender id %d and I am to investigate region C=%d r=%d and app %f\n",node_id,idag_id,sender_id,cur_reg.C,cur_reg.r,req_app.A);
  668. if (core_inter_head[sender_id] == NULL){
  669. core_inter_head[sender_id] = (inter_list *) malloc(sizeof(inter_list));
  670. core_inter_tail[sender_id] = core_inter_head[sender_id];
  671. } else {
  672. core_inter_tail[sender_id]->next = (inter_list *) malloc(sizeof(inter_list));
  673. core_inter_tail[sender_id] = core_inter_tail[sender_id]->next;
  674. }
  675. core_inter_tail[sender_id]->type = REP_AGENT_REQ_CORES;
  676. read(fd_r, &off_arr.num_of_offers, sizeof(int));
  677. while (off_arr.num_of_offers > OLD_INIT_AREAS_NUM) {
  678. printf("I am %d kai fagame skoupidia apo ton %d kai einai %d\n",node_id,sender_id,off_arr.num_of_offers);
  679. read(fd_r, &off_arr.num_of_offers, sizeof(int));
  680. }
  681. off_arr.offer_arr = (offer *) malloc(off_arr.num_of_offers * sizeof(offer));
  682. core_inter_tail[sender_id]->next = NULL;
  683. for (i=0; i<off_arr.num_of_offers; i++) {
  684. read(fd_r, &cur_reg, sizeof(region));
  685. if (my_idag == -1) {
  686. off_arr.offer_arr[i].offered_cores = (int *) malloc(my_cores_count*sizeof(int));
  687. tmp_int = offer_cores(my_cores, req_app, cur_reg, off_arr.offer_arr[i].offered_cores, sender_id);
  688. off_arr.offer_arr[i].num_of_cores = tmp_int;
  689. my_stats.comp_effort++;
  690. if (tmp_int > 0) non_zero_offers++;
  691. cores_util = 0;
  692. for (tmp_DDS = DDS->next; tmp_DDS != NULL; tmp_DDS = tmp_DDS->next)
  693. cores_util += tmp_DDS->num_of_cores;
  694. if (cores_util == 0) off_arr.offer_arr[i].spd_loss = -2.0;
  695. else off_arr.offer_arr[i].spd_loss = (float) (-1 * cores_util) / (my_cores_count-1);//0.0;
  696. } else if (my_cores != NULL && my_cores_count>2) {
  697. off_arr.offer_arr[i].offered_cores = (int *) malloc(my_cores_count*sizeof(int));
  698. tmp_int = offer_cores(my_cores, req_app, cur_reg, off_arr.offer_arr[i].offered_cores, sender_id);
  699. off_arr.offer_arr[i].num_of_cores = tmp_int;
  700. my_stats.comp_effort++;
  701. //printf("I am %d in i = %d and i offer %d cores\n",node_id,i,tmp_int);
  702. off_arr.offer_arr[i].spd_loss = Speedup(my_app, my_cores_count) - Speedup(my_app, my_cores_count-tmp_int);
  703. req_gain = Speedup(req_app,req_app.num_of_cores+tmp_int) - Speedup(req_app,req_app.num_of_cores);
  704. if (tmp_int > 0) {
  705. non_zero_offers++;
  706. fprintf(log_file,"I offered %d cores with spd_loss = %0.2f and %0.2f gain for the req_app\n",tmp_int,off_arr.offer_arr[i].spd_loss,req_gain);
  707. fflush(log_file);
  708. fprintf(app_log_file,"I offered %d cores with spd_loss = %0.2f and %0.2f gain for the req_app\n"
  709. ,tmp_int,off_arr.offer_arr[i].spd_loss,req_gain);
  710. fflush(app_log_file);
  711. }
  712. } else {
  713. off_arr.offer_arr[i].num_of_cores = 0;
  714. off_arr.offer_arr[i].spd_loss = 0.0;
  715. }
  716. fprintf(log_file, "I offered %d %d cores: ",sender_id,off_arr.offer_arr[i].num_of_cores);
  717. for (j=0; j<off_arr.offer_arr[i].num_of_cores; j++)
  718. fprintf(log_file, "%d, ",off_arr.offer_arr[i].offered_cores[j]);
  719. fprintf(log_file, "\n");
  720. fflush(log_file);
  721. }
  722. fprintf(log_file,"non_zero_offers = %d\n",non_zero_offers);
  723. core_inter_tail[sender_id]->data.off_arr.num_of_offers = non_zero_offers;
  724. if (non_zero_offers > 0) core_inter_tail[sender_id]->data.off_arr.offer_arr = (offer *) malloc(non_zero_offers * sizeof(offer));
  725. else core_inter_tail[sender_id]->data.off_arr.offer_arr = NULL;
  726. j = 0;
  727. for (i=0; i<off_arr.num_of_offers; i++)
  728. if (off_arr.offer_arr[i].num_of_cores > 0) {
  729. core_inter_tail[sender_id]->data.off_arr.offer_arr[j].num_of_cores = off_arr.offer_arr[i].num_of_cores;
  730. core_inter_tail[sender_id]->data.off_arr.offer_arr[j].spd_loss = off_arr.offer_arr[i].spd_loss;
  731. core_inter_tail[sender_id]->data.off_arr.offer_arr[j].offered_cores = (int *) malloc(off_arr.offer_arr[i].num_of_cores * sizeof(int));
  732. for (tmp_int=0; tmp_int<off_arr.offer_arr[i].num_of_cores; tmp_int++)
  733. core_inter_tail[sender_id]->data.off_arr.offer_arr[j].offered_cores[tmp_int] = off_arr.offer_arr[i].offered_cores[tmp_int];
  734. j++;
  735. }
  736. if (core_inter_head[sender_id]->next == NULL) {
  737. kill(info->si_pid, SIG_REQ_CORES);
  738. my_stats.msg_count++;
  739. my_stats.distance += distance(node_id,sender_id);
  740. } else printf("Apparently not null interaction=%d\n",core_inter_head[sender_id]->type);
  741. //sem_post(&node_sem[node_id]);
  742. sem_getvalue(&node_sem_out[sender_id],&i);
  743. fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
  744. fflush(log_file);
  745. if (sem_post(&node_sem_out[sender_id]) == -1){
  746. printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
  747. perror("sem_post");
  748. }
  749. sem_getvalue(&node_sem_out[sender_id],&i);
  750. fprintf(log_file, "Sem value = %d\n",i);
  751. fflush(log_file);
  752. } else if (core_inter_head[sender_id] == NULL) {
  753. printf("I am %d and i have to reject req_cores from %d with null interaction\n",node_id,sender_id);
  754. fprintf(log_file,"i have to reject req_cores from %d. with null interaction\n",sender_id);
  755. fflush(log_file);
  756. kill(info->si_pid, SIG_REJECT);
  757. my_stats.msg_count++;
  758. my_stats.distance += distance(node_id,sender_id);
  759. } else if (core_inter_head[sender_id]->type == AGENT_REQ_CORES && state != IDLE_INIT_MAN && state != WORKING_NODE_IDLE_INIT) {
  760. //IDLE_INIT_MAN_SELFOPT_PENDING && state != IDLE_INIT_MAN_WORK_PENDING) {
  761. printf("I am %d and i think i cought a stray init AGENT_REQ_CORES reply from %d. My current state is %d\n",node_id,sender_id,state);
  762. fprintf(log_file,"I am %d and i think i cought a stray init AGENT_REQ_CORES reply from %d. My current state is %d\n",node_id,sender_id,state);
  763. fflush(log_file);
  764. kill(info->si_pid, SIG_REJECT);
  765. my_stats.msg_count++;
  766. my_stats.distance += distance(node_id,sender_id);
  767. tmp_inter_list = core_inter_head[sender_id];
  768. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  769. free(tmp_inter_list);
  770. if(core_inter_head[sender_id] == NULL) {
  771. if (core_inter_tail[sender_id] == NULL){
  772. printf("Malaka gamietai to core_inter_tail\n");
  773. core_inter_tail[sender_id] = NULL;
  774. }
  775. } else send_next_signal(core_inter_head[sender_id], sender_id);
  776. } else if (core_inter_head[sender_id]->type == SELFOPT_REQ_CORES && state != IDLE_AGENT_WAITING_OFF) {
  777. printf("I am %d and i think i cought a stray selfopt SELFOPT_REQ_CORES reply from %d. My current state is %d\n",node_id,sender_id,state);
  778. fprintf(log_file,"I am %d and i think i cought a stray selfopt SELFOPT_REQ_CORES reply from %d. My current state is %d\n",node_id,sender_id,state);
  779. fflush(log_file);
  780. kill(info->si_pid, SIG_REJECT);
  781. my_stats.msg_count++;
  782. my_stats.distance += distance(node_id,sender_id);
  783. tmp_inter_list = core_inter_head[sender_id];
  784. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  785. free(tmp_inter_list);
  786. if(core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  787. else send_next_signal(core_inter_head[sender_id], sender_id);
  788. } else if (core_inter_head[sender_id]->type == FAR_REQ_CORES && (state == IDLE_IDAG || (node_id == 0 && time_for_farman == -1))) {
  789. printf("I am %d and i think i cought a stray far_REQ_CORES reply from %d. My current state is %d\n",node_id,sender_id,state);
  790. fprintf(log_file,"I am %d and i think i cought a stray far_REQ_CORES reply from %d. My current state is %d\n",node_id,sender_id,state);
  791. fflush(log_file);
  792. kill(info->si_pid, SIG_REJECT);
  793. my_stats.msg_count++;
  794. my_stats.distance += distance(node_id,sender_id);
  795. tmp_inter_list = core_inter_head[sender_id];
  796. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  797. free(tmp_inter_list);
  798. if(core_inter_head[sender_id] == NULL) {
  799. if (core_inter_tail[sender_id] == NULL){
  800. printf("Malaka gamietai to core_inter_tail\n");
  801. core_inter_tail[sender_id] = NULL;
  802. }
  803. } else send_next_signal(core_inter_head[sender_id], sender_id);
  804. } else if (core_inter_head[sender_id]->type == AGENT_REQ_CORES || core_inter_head[sender_id]->type == FAR_REQ_CORES || core_inter_head[sender_id]->type == SELFOPT_REQ_CORES){
  805. //I am the requesting common node
  806. sem_getvalue(&node_sem[node_id],&i);
  807. fprintf(log_file, "In 6 Trying to acquire semaphore. Sem value = %d\n",i);
  808. fflush(log_file);
  809. kill(info->si_pid, SIG_ACK);
  810. sem_wait(&node_sem[node_id]);
  811. sem_getvalue(&node_sem[node_id],&i);
  812. fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
  813. fflush(log_file);
  814. my_stats.msg_count++;
  815. my_stats.distance += distance(node_id,sender_id);
  816. //printf("open8 idag_id=%d node_id=%d\n",idag_id,node_id);
  817. fd_r = open(fifo_name, O_RDONLY);
  818. if (fd_r == -1) {
  819. perror("opening reading pipe");
  820. signals_enable();
  821. return;
  822. } else {
  823. fprintf(log_file, "I went through open type = %d\n",core_inter_head[sender_id]->type);
  824. fflush(log_file);
  825. }
  826. if (core_inter_head[sender_id]->type == AGENT_REQ_CORES || core_inter_head[sender_id]->type == SELFOPT_REQ_CORES) {//den erxontai ta far edw
  827. read(fd_r, &num_of_offers, sizeof(int));
  828. fprintf(log_file, "num_of_offers = %d\n",num_of_offers);
  829. fflush(log_file);
  830. if (num_of_offers > 0) {
  831. core_inter_head[sender_id]->data.offer_acc_array = (int *) malloc((num_of_offers+1)*sizeof(int));
  832. core_inter_head[sender_id]->data.offer_acc_array[0] = num_of_offers;
  833. //core_inter_head[sender_id]->data.offer_accepted = -1;
  834. //one_offer.offered_cores = NULL;
  835. for (j=1; j<=num_of_offers; j++){
  836. read(fd_r, &one_offer.num_of_cores, sizeof(int));
  837. read(fd_r, &one_offer.spd_loss, sizeof(float));
  838. if (core_inter_head[sender_id]->type == AGENT_REQ_CORES && init_man_offers == NULL) {
  839. init_man_offers = (offer_list *) malloc(sizeof(offer_list));
  840. chosen_node = init_man_offers;
  841. init_man_offers->next = NULL;
  842. } else if (core_inter_head[sender_id]->type == SELFOPT_REQ_CORES && selfopt_man_offers == NULL){
  843. selfopt_man_offers = (offer_list *) malloc(sizeof(offer_list));
  844. chosen_node = selfopt_man_offers;
  845. selfopt_man_offers->next = NULL;
  846. } else {
  847. tmp_offer_prev = NULL;
  848. if (core_inter_head[sender_id]->type == AGENT_REQ_CORES) {
  849. tmp_offer_list = init_man_offers;
  850. if (one_offer.spd_loss < 0.0) {
  851. while (tmp_offer_list != NULL && tmp_offer_list->off.num_of_cores >= one_offer.num_of_cores && tmp_offer_list->off.spd_loss < 0.0){
  852. if (tmp_offer_list->off.num_of_cores > one_offer.num_of_cores) {
  853. tmp_offer_prev = tmp_offer_list;
  854. tmp_offer_list = tmp_offer_list->next;
  855. } else if (tmp_offer_list->off.spd_loss < one_offer.spd_loss) {
  856. tmp_offer_prev = tmp_offer_list;
  857. tmp_offer_list = tmp_offer_list->next;
  858. } else break;
  859. }
  860. /*while (tmp_offer_list != NULL && tmp_offer_list->off.spd_loss < one_offer.spd_loss && tmp_offer_list->off.spd_loss < 0.0){
  861. tmp_offer_prev = tmp_offer_list;
  862. tmp_offer_list = tmp_offer_list->next;
  863. }*/
  864. } else {
  865. while (tmp_offer_list != NULL && (tmp_offer_list->off.spd_loss < 0.0 || tmp_offer_list->off.num_of_cores >= one_offer.num_of_cores)){
  866. tmp_offer_prev = tmp_offer_list;
  867. tmp_offer_list = tmp_offer_list->next;
  868. }
  869. }
  870. } else {
  871. tmp_offer_list = selfopt_man_offers;
  872. while (tmp_offer_list != NULL && tmp_offer_list->off.num_of_cores >= one_offer.num_of_cores){
  873. tmp_offer_prev = tmp_offer_list;
  874. tmp_offer_list = tmp_offer_list->next;
  875. }
  876. }
  877. if (tmp_offer_list == NULL) { //prepei na mpei teleutaio
  878. tmp_offer_prev->next = (offer_list *) malloc(sizeof(offer_list));
  879. tmp_offer_list = tmp_offer_prev->next;
  880. tmp_offer_list->next = NULL;
  881. chosen_node = tmp_offer_list;
  882. } else if (tmp_offer_prev == NULL) { //prepei na mpei prwto
  883. if (core_inter_head[sender_id]->type == AGENT_REQ_CORES) {
  884. init_man_offers = (offer_list *) malloc(sizeof(offer_list));
  885. tmp_head = init_man_offers;
  886. } else {
  887. selfopt_man_offers = (offer_list *) malloc(sizeof(offer_list));
  888. tmp_head = selfopt_man_offers;
  889. }
  890. chosen_node = tmp_head;
  891. tmp_head->next = tmp_offer_list;
  892. } else {
  893. tmp_offer_prev->next = (offer_list *) malloc(sizeof(offer_list));
  894. tmp_offer_prev = tmp_offer_prev->next;
  895. chosen_node = tmp_offer_prev;
  896. tmp_offer_prev->next = tmp_offer_list;
  897. }
  898. }
  899. chosen_node->off.num_of_cores = one_offer.num_of_cores;
  900. chosen_node->off.spd_loss = one_offer.spd_loss;
  901. chosen_node->off.offered_cores = (int *) malloc(one_offer.num_of_cores*sizeof(int));
  902. for (i=0; i<one_offer.num_of_cores; i++)
  903. read(fd_r, &chosen_node->off.offered_cores[i], sizeof(int));
  904. /*printf("asdasNode %d is offering %d cores: ",sender_id,chosen_node->off.num_of_cores);
  905. for (i=0; i<chosen_node->off.num_of_cores; i++)
  906. printf(" %d,",chosen_node->off.offered_cores[i]);
  907. printf("\n");*/
  908. chosen_node->sender = sender_id;
  909. core_inter_head[sender_id]->data.offer_acc_array[j] = -1;
  910. chosen_node->answer = &core_inter_head[sender_id]->data.offer_acc_array[j];
  911. cur_time = time(NULL);
  912. cur_t = localtime(&cur_time);
  913. fprintf(log_file, "[%d:%d:%d]: One node successfully added in list type=%d sender_id=%d\n",
  914. cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,core_inter_head[sender_id]->type,sender_id);
  915. fflush(log_file);
  916. }
  917. core_inter_head[sender_id]->type = REP_AGENT_OFFER_PENDING;
  918. } else { //no answer is required
  919. tmp_inter_list = core_inter_head[sender_id];
  920. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  921. free(tmp_inter_list);
  922. if(core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  923. else send_next_signal(core_inter_head[sender_id], sender_id);
  924. }
  925. } else {
  926. read(fd_r, &num_of_offers, sizeof(int));
  927. fprintf(log_file, "num_of_offers is %d\n",num_of_offers);
  928. fflush(log_file);
  929. if (num_of_offers > 0) {
  930. core_inter_head[sender_id]->data.offer_acc_array = (int *) malloc((num_of_offers+1)*sizeof(int));
  931. core_inter_head[sender_id]->data.offer_acc_array[0] = num_of_offers;
  932. core_inter_head[sender_id]->type = REP_AGENT_OFFER_PENDING;
  933. read(fd_r, &one_offer.num_of_cores, sizeof(int));
  934. read(fd_r, &one_offer.spd_loss, sizeof(float));
  935. one_offer.offered_cores = (int *) malloc(one_offer.num_of_cores*sizeof(int));
  936. for (i=0; i<one_offer.num_of_cores; i++)
  937. read(fd_r, &one_offer.offered_cores[i], sizeof(int));
  938. fprintf(log_file, "%d is offering %d cores with speedup loss %f\n",sender_id,one_offer.num_of_cores,one_offer.spd_loss);
  939. fflush(log_file);
  940. //core_inter_head[sender_id]->data.offer_accepted = -1;
  941. core_inter_head[sender_id]->data.offer_acc_array[1] = -1;
  942. fprintf(log_file, "a\n");
  943. fflush(log_file);
  944. if (far_man_offers == NULL){
  945. //fprintf(log_file, "b1\n");
  946. //fflush(log_file);
  947. far_man_offers = (offer_list *) malloc(sizeof(offer_list));
  948. far_man_offers_tail = far_man_offers;
  949. } else {
  950. //fprintf(log_file, "b2\n");
  951. //fflush(log_file);
  952. far_man_offers_tail->next = (offer_list *) malloc(sizeof(offer_list));
  953. far_man_offers_tail = far_man_offers_tail->next;
  954. }
  955. //fprintf(log_file, "c1\n");
  956. //fflush(log_file);
  957. far_man_offers_tail->off = one_offer;
  958. far_man_offers_tail->sender = sender_id;
  959. //fprintf(log_file, "c2\n");
  960. //fflush(log_file);
  961. far_man_offers_tail->answer = &core_inter_head[sender_id]->data.offer_acc_array[1];//offer_accepted;
  962. //fprintf(log_file, "c3\n");
  963. //fflush(log_file);
  964. far_man_offers_tail->next = NULL;
  965. far_list_count++;
  966. //fprintf(log_file, "c4\n");
  967. //fflush(log_file);
  968. cur_time = time(NULL);
  969. cur_t = localtime(&cur_time);
  970. fprintf(log_file, "[%d:%d:%d]: One node successfully added in far_man_offers list %d\n",
  971. cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,core_inter_head[sender_id]->data.offer_acc_array[0]);
  972. fflush(log_file);
  973. } else { //no answer is required
  974. tmp_inter_list = core_inter_head[sender_id];
  975. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  976. free(tmp_inter_list);
  977. if(core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  978. else send_next_signal(core_inter_head[sender_id], sender_id);
  979. }
  980. }
  981. sem_getvalue(&node_sem_out[sender_id],&i);
  982. fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
  983. fflush(log_file);
  984. if (sem_post(&node_sem_out[sender_id]) == -1){
  985. printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
  986. perror("sem_post");
  987. }
  988. sem_getvalue(&node_sem_out[sender_id],&i);
  989. fprintf(log_file, "Sem value = %d\n",i);
  990. fflush(log_file);
  991. //sem_post(&node_sem[node_id]);
  992. ////sem_post(&node_sem[sender_id]);
  993. /*tmp_inter_list = core_inter_head[sender_id]->next;
  994. free(core_inter_head[sender_id]);
  995. core_inter_head[sender_id] = (inter_list *) malloc(sizeof(inter_list));
  996. core_inter_head[sender_id]->next = tmp_inter_list;
  997. core_inter_head[sender_id]->type = REP_AGENT_OFFER_SENT;
  998. core_inter_head[sender_id]->data.offer_accepted = -1;*/
  999. /*if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  1000. else send_next_signal(core_inter_head[sender_id], sender_id);
  1001. free(tmp_inter_list);*/
  1002. } else if (core_inter_head[sender_id]->type == AGENT_REQ_CORES_PENDING || core_inter_head[sender_id]->type == SELFOPT_REQ_CORES_PENDING
  1003. || core_inter_head[sender_id]->type == FAR_REQ_CORES_PENDING){
  1004. printf("I am %d and deadlock was prevented in req_cores by %d\n",node_id,sender_id);
  1005. tmp_inter_list = core_inter_head[sender_id];
  1006. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  1007. free(tmp_inter_list);
  1008. if(core_inter_head[sender_id] == NULL) {
  1009. if (core_inter_tail[sender_id] == NULL){
  1010. printf("Malaka gamietai to core_inter_tail\n");
  1011. core_inter_tail[sender_id] = NULL;
  1012. }
  1013. } else send_next_signal(core_inter_head[sender_id], sender_id);
  1014. } else {
  1015. //printf("I am %d and We were fucked in sig_REQ_DDS_handler from %d interaction is = %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  1016. printf("I am %d and i have to reject req_cores from %d. Our interaction is %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  1017. fprintf(log_file,"I am %d and i have to reject req_cores from %d. Our interaction is %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  1018. fflush(log_file);
  1019. kill(info->si_pid, SIG_REJECT);
  1020. my_stats.msg_count++;
  1021. my_stats.distance += distance(node_id,sender_id);
  1022. }
  1023. //printf("We were fucked in sig_REQ_CORES_handler node_id = %d sender_id = %d interaction=%d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  1024. if (fd_r != -1) close(fd_r);
  1025. free(fifo_name);
  1026. cur_time = time(NULL);
  1027. cur_t = localtime(&cur_time);
  1028. fprintf(log_file, "[%d:%d:%d]: I ended sig_REQ_CORES_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  1029. fflush(log_file);
  1030. signals_enable();
  1031. }
  1032. void sig_REP_OFFERS_handler(int signo, siginfo_t *info, void *context)
  1033. {
  1034. int fd_r, sender_id, offer_ans, i, one_core, j, old_cores_cnt;//num_of_idags, i, one_idag;
  1035. core_list *tmp_cores, *tmp_cores_prev, *tmp_cores_list;
  1036. inter_list *tmp_inter_list, *tmp_inter_prev;
  1037. offer_list *tmp_offer_list;
  1038. char *fifo_name;
  1039. signals_disable();
  1040. sender_id = get_id_from_pid(info->si_pid);
  1041. fifo_name = get_pipe_name(node_id);
  1042. cur_time = time(NULL);
  1043. cur_t = localtime(&cur_time);
  1044. fprintf(log_file, "[%d:%d:%d]: I entered sig_REP_OFFERS_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  1045. fflush(log_file);
  1046. if (core_inter_head[sender_id] == NULL)
  1047. printf("fail!\n");
  1048. else if (core_inter_head[sender_id]->type == AGENT_OFFER_SENT) {
  1049. sem_getvalue(&node_sem[node_id],&i);
  1050. fprintf(log_file, "In 7 Trying to acquire semaphore. Sem value = %d\n",i);
  1051. fflush(log_file);
  1052. kill(info->si_pid, SIG_ACK);
  1053. sem_wait(&node_sem[node_id]);
  1054. sem_getvalue(&node_sem[node_id],&i);
  1055. fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
  1056. fflush(log_file);
  1057. my_stats.msg_count++;
  1058. my_stats.distance += distance(node_id,sender_id);
  1059. //printf("open9 idag_id=%d node_id=%d fifo_name=%s\n",idag_id,node_id,fifo_name);
  1060. fd_r = open(fifo_name, O_RDONLY);
  1061. if (fd_r == -1) {
  1062. perror("opening reading pipe");
  1063. signals_enable();
  1064. return;
  1065. }
  1066. fprintf(log_file, "Num of offers is = %d\n",core_inter_head[sender_id]->data.off_arr.num_of_offers);
  1067. fflush(log_file);
  1068. //even if i am in a far req offer, my answer will be the first
  1069. for (j=0; j<core_inter_head[sender_id]->data.off_arr.num_of_offers; j++) {
  1070. read(fd_r, &offer_ans, sizeof(int));
  1071. fprintf(log_file, "offer_ans = %d\n",offer_ans);
  1072. fflush(log_file);
  1073. //printf("I am node %d and my offer answer is %d interaction=%d\n",node_id,offer_ans,core_inter_head[sender_id]->type);
  1074. //printf("I am %d and i am getting bizarre answer = %d from %d with num of offers = %d\n",node_id,offer_ans,sender_id,core_inter_head[sender_id]->data.off_arr.num_of_offers);
  1075. while (offer_ans != 0 && offer_ans != 1){
  1076. printf("I am %d and i am getting bizarre answer = %d from %d\n",node_id,offer_ans,sender_id);
  1077. read(fd_r, &offer_ans, sizeof(int));
  1078. }
  1079. if (offer_ans == 0 && my_cores != NULL) { // && my_cores != NULL
  1080. for (i=0; i<core_inter_head[sender_id]->data.off_arr.offer_arr[j].num_of_cores; i++)
  1081. for (tmp_cores = my_cores->next; tmp_cores != NULL; tmp_cores = tmp_cores->next)
  1082. if (tmp_cores->core_id == core_inter_head[sender_id]->data.off_arr.offer_arr[j].offered_cores[i]) {
  1083. fprintf(log_file,"core id = %d offered_to = %d\n",tmp_cores->core_id,tmp_cores->offered_to);
  1084. fflush(log_file);
  1085. if (tmp_cores->offered_to == sender_id) tmp_cores->offered_to = -1;
  1086. break;
  1087. }
  1088. if (app_state == APP_TERMINATED) {//app_terminated
  1089. for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next)
  1090. if (tmp_cores_list->offered_to != -1) break;
  1091. //if (tmp_cores_list != NULL && state ) state = AGENT_ZOMBIE;
  1092. //else state = AGENT_ENDING;
  1093. if (tmp_cores_list == NULL && state == AGENT_ZOMBIE) state = AGENT_ENDING;
  1094. }
  1095. /*tmp_cores = my_cores;
  1096. while (tmp_cores != NULL){
  1097. if (tmp_cores->offered_to == sender_id) tmp_cores->offered_to = -1;
  1098. tmp_cores = tmp_cores->next;
  1099. }*/
  1100. } else if (offer_ans == 1) {//&& (!app_terminated || my_idag == -1) my_cores != NULL inform my idag if I have. If i am an idag i just reduce my DDS core num. I keep my core.
  1101. if (my_idag == -1){ //I am an idag
  1102. //if (node_id == 0) printf("qwer asdf %d\n",DDS->num_of_cores);
  1103. //if (DDS->agent_id != node_id) printf("Fuck i am not first in my DDS list!!\n");
  1104. DDS->num_of_cores -= core_inter_head[sender_id]->data.off_arr.offer_arr[j].num_of_cores;
  1105. //if (node_id == 0) printf("kariolares asdf %d\n",core_inter_head[sender_id]->data.off_arr.offer_arr[j].num_of_cores);
  1106. //printf("I did REM\n");
  1107. } else {//I am common node
  1108. //printf("I start REM here!!!!!\n");
  1109. /*first i must get the remaining time from my cores, before i change my core list*/
  1110. if (app_state != APP_TERMINATED) { //!app_terminated
  1111. old_Speedup = my_Speedup;
  1112. old_cores_cnt = my_cores_count;
  1113. my_cores_count -= core_inter_head[sender_id]->data.off_arr.offer_arr[j].num_of_cores;
  1114. my_app.num_of_cores = my_cores_count;
  1115. my_Speedup = Speedup(my_app, my_cores_count);
  1116. fprintf(log_file,"Initialising removal new_cores_count = %d app_state = %d\n",my_cores_count,app_state);
  1117. fflush(log_file);
  1118. fprintf(app_log_file,"Initialising removal new_cores_count = %d app_state = %d\n",my_cores_count,app_state);
  1119. fflush(app_log_file);
  1120. if (app_state == RUNNING) {
  1121. chk_rem_num = old_cores_cnt-1;
  1122. chk_rem_count = 0;
  1123. sum_rem_time = 0;
  1124. app_state = RESIZING;
  1125. for(tmp_cores_list=my_cores->next; tmp_cores_list!=NULL; tmp_cores_list=tmp_cores_list->next) {//first is myself
  1126. one_core = tmp_cores_list->core_id;
  1127. if (core_inter_head[one_core] == NULL){
  1128. core_inter_head[one_core] = (inter_list *) malloc(sizeof(inter_list));
  1129. core_inter_tail[one_core] = core_inter_head[one_core];
  1130. } else {
  1131. core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
  1132. core_inter_tail[one_core] = core_inter_tail[one_core]->next;
  1133. }
  1134. core_inter_tail[one_core]->type = APPOINT_WORK_NODE_PENDING;//CHK_REM_TIME;
  1135. core_inter_tail[one_core]->next = NULL;
  1136. if (core_inter_head[one_core]->next == NULL) {
  1137. kill(pid_num[one_core], SIG_CHECK_REM_TIME);
  1138. my_stats.msg_count++;
  1139. my_stats.distance += distance(node_id,one_core);
  1140. } else printf("I am %d and I am doing smth else with my working node %d in send SIG_CHECK_REM_TIME in rep offers type = %d\n",
  1141. node_id,one_core,core_inter_head[one_core]->type);
  1142. }
  1143. } //else {
  1144. } else {
  1145. my_cores_count -= core_inter_head[sender_id]->data.off_arr.offer_arr[j].num_of_cores;
  1146. my_app.num_of_cores = my_cores_count;
  1147. fprintf(log_file,"Initialising removal app finished new_cores_count = %d\n",my_cores_count);
  1148. fflush(log_file);
  1149. fprintf(app_log_file,"Initialising removal app finished new_cores_count = %d\n",my_cores_count);
  1150. fflush(app_log_file);
  1151. }
  1152. if (core_inter_head[my_idag] == NULL){
  1153. core_inter_head[my_idag] = (inter_list *) malloc(sizeof(inter_list));
  1154. core_inter_tail[my_idag] = core_inter_head[my_idag];
  1155. } else {
  1156. core_inter_tail[my_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  1157. core_inter_tail[my_idag] = core_inter_tail[my_idag]->next;
  1158. }
  1159. core_inter_tail[my_idag]->type = IDAG_REM_CORES_DDS;
  1160. core_inter_tail[my_idag]->data.app_cores = (int *)malloc((core_inter_head[sender_id]->data.off_arr.offer_arr[j].num_of_cores+1)*sizeof(int));
  1161. core_inter_tail[my_idag]->data.app_cores[0] = core_inter_head[sender_id]->data.off_arr.offer_arr[j].num_of_cores;
  1162. for(i=1; i<=core_inter_head[sender_id]->data.off_arr.offer_arr[j].num_of_cores; i++) {
  1163. one_core = core_inter_head[sender_id]->data.off_arr.offer_arr[j].offered_cores[i-1];
  1164. if (app_state != APP_TERMINATED) {
  1165. tmp_inter_prev = core_inter_head[one_core];
  1166. tmp_inter_list = core_inter_head[one_core]->next;
  1167. } else {
  1168. tmp_inter_prev = NULL;
  1169. tmp_inter_list = core_inter_head[one_core];
  1170. }
  1171. //for (tmp_inter_list = core_inter_head[one_core]; tmp_inter_list != NULL; tmp_inter_list=tmp_inter_list->next)
  1172. while (tmp_inter_list != NULL)
  1173. if (tmp_inter_list->type == INIT_WORK_NODE || tmp_inter_list->type == APPOINT_WORK_NODE || tmp_inter_list->type == INIT_WORK_NODE_PENDING
  1174. || tmp_inter_list->type == APPOINT_WORK_NODE_PENDING) {
  1175. //|| (tmp_inter_list->type == APPOINT_WORK_NODE_PENDING && tmp_inter_prev != NULL)) {
  1176. fprintf(log_file, "Removing in rem offers one node of %d with inter = %d\n",one_core,tmp_inter_list->type);
  1177. fflush(log_file);
  1178. if (tmp_inter_prev == NULL) {
  1179. core_inter_head[one_core] = core_inter_head[one_core]->next;
  1180. free(tmp_inter_list);
  1181. tmp_inter_list = core_inter_head[one_core];
  1182. } else {
  1183. tmp_inter_prev->next = tmp_inter_list->next;
  1184. if (tmp_inter_prev->next == NULL) core_inter_tail[one_core] = tmp_inter_prev;
  1185. free(tmp_inter_list);
  1186. tmp_inter_list = tmp_inter_prev->next;
  1187. }
  1188. } else {
  1189. tmp_inter_prev = tmp_inter_list;
  1190. tmp_inter_list = tmp_inter_list->next;
  1191. }
  1192. if (app_state != APP_TERMINATED) {
  1193. if (core_inter_head[one_core] == NULL) {
  1194. fprintf(log_file,"No interaction with %d. Theoritically impossible\n",one_core);
  1195. fflush(log_file);
  1196. } else if (core_inter_head[one_core]->type == INIT_WORK_NODE_PENDING) {
  1197. fprintf(log_file,"I offered my new core %d. I will clear the interaction\n",one_core);
  1198. fflush(log_file);
  1199. tmp_inter_list = core_inter_head[one_core];
  1200. core_inter_head[one_core] = core_inter_head[one_core]->next;
  1201. if (core_inter_head[one_core] == NULL) core_inter_tail[one_core] = NULL;
  1202. else send_next_signal(core_inter_head[one_core], one_core);
  1203. free(tmp_inter_list);
  1204. } else if (core_inter_head[one_core]->type == INIT_WORK_NODE || core_inter_head[one_core]->type == APPOINT_WORK_NODE) {
  1205. fprintf(log_file,"Invalidating %d. Interaction is %d\n",one_core,core_inter_head[one_core]->type);
  1206. fflush(log_file);
  1207. core_inter_head[one_core]->data.work_time = -1;
  1208. core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
  1209. core_inter_tail[one_core] = core_inter_tail[one_core]->next;
  1210. core_inter_tail[one_core]->type = REMOVED_NODE_REM_TIME;//APPOINT_WORK_NODE_PENDING;//CHK_REM_TIME;
  1211. core_inter_tail[one_core]->next = NULL;
  1212. } else if (core_inter_head[one_core]->type == APPOINT_WORK_NODE_PENDING) {
  1213. fprintf(log_file,"Everything ok %d.\n",one_core);
  1214. fflush(log_file);
  1215. core_inter_head[one_core]->type = REMOVED_NODE_REM_TIME;
  1216. } else {
  1217. fprintf(log_file,"Another interaction with %d. Interaction is %d\n",one_core,core_inter_head[one_core]->type);
  1218. fflush(log_file);
  1219. core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
  1220. core_inter_tail[one_core] = core_inter_tail[one_core]->next;
  1221. core_inter_tail[one_core]->type = REMOVED_NODE_REM_TIME;//APPOINT_WORK_NODE_PENDING;//CHK_REM_TIME;
  1222. core_inter_tail[one_core]->next = NULL;
  1223. }
  1224. }
  1225. tmp_cores = my_cores;
  1226. tmp_cores_prev = NULL;
  1227. while (tmp_cores != NULL && tmp_cores->core_id != core_inter_head[sender_id]->data.off_arr.offer_arr[j].offered_cores[i-1]){
  1228. tmp_cores_prev = tmp_cores;
  1229. tmp_cores = tmp_cores->next;
  1230. }
  1231. if (tmp_cores == NULL) printf("strangely offered core is not in my_cores list\n");
  1232. else {
  1233. if (tmp_cores_prev == NULL) {
  1234. printf("i fucking offered my agent core!!!\n");
  1235. my_cores = my_cores->next;
  1236. } else if (tmp_cores == my_cores_tail){
  1237. my_cores_tail = tmp_cores_prev;
  1238. my_cores_tail->next = NULL;
  1239. } else tmp_cores_prev->next = tmp_cores->next;
  1240. free(tmp_cores);
  1241. }
  1242. core_inter_tail[my_idag]->data.app_cores[i] = core_inter_head[sender_id]->data.off_arr.offer_arr[j].offered_cores[i-1];
  1243. }
  1244. core_inter_tail[my_idag]->next = NULL;
  1245. if (core_inter_head[my_idag]->next == NULL) {
  1246. kill(pid_num[my_idag], SIG_REM_CORES_DDS);
  1247. my_stats.msg_count++;
  1248. my_stats.distance += distance(node_id,my_idag);
  1249. } else printf("I did not send rem signal!\n");
  1250. //printf("At least i am coming here!!!!!\n");
  1251. cur_time = time(NULL);
  1252. cur_t = localtime(&cur_time);
  1253. fprintf(app_log_file, "[%d:%d:%d]: Removal ok. my_cores_count = %d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,my_cores_count);
  1254. fprintf(app_log_file, "my cores are:");
  1255. for (tmp_cores_list=my_cores; tmp_cores_list!=NULL; tmp_cores_list=tmp_cores_list->next) {
  1256. //fprintf(log_file, " %d", tmp_cores_list->core_id);
  1257. fprintf(app_log_file, " %d", tmp_cores_list->core_id);
  1258. //printf(" %d",tmp_cores_list->core_id);
  1259. }
  1260. //printf("\n");
  1261. //fprintf(log_file, "\n");
  1262. //fflush(log_file);
  1263. fprintf(app_log_file, "\n");
  1264. fflush(app_log_file);
  1265. if (app_state == APP_TERMINATED) {//app_terminated
  1266. for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next)
  1267. if (tmp_cores_list->offered_to != -1) break;
  1268. if (tmp_cores_list == NULL && state == AGENT_ZOMBIE) state = AGENT_ENDING;
  1269. }
  1270. }
  1271. } else printf("I am %d and my Answer from %d different than 0 or 1 and is %d!!\n",node_id,sender_id,offer_ans);
  1272. }
  1273. sem_getvalue(&node_sem_out[sender_id],&i);
  1274. fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
  1275. fflush(log_file);
  1276. if (sem_post(&node_sem_out[sender_id]) == -1){
  1277. printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
  1278. perror("sem_post");
  1279. }
  1280. sem_getvalue(&node_sem_out[sender_id],&i);
  1281. fprintf(log_file, "Sem value = %d\n",i);
  1282. fflush(log_file);
  1283. //sem_post(&node_sem[node_id]);
  1284. //sem_post(&node_sem[sender_id]);
  1285. } else if (core_inter_head[sender_id]->type == FAR_REQ_OFFER_SENT) {
  1286. sem_getvalue(&node_sem[node_id],&i);
  1287. fprintf(log_file, "In 8 Trying to acquire semaphore. Sem value = %d\n",i);
  1288. fflush(log_file);
  1289. kill(info->si_pid, SIG_ACK);
  1290. sem_wait(&node_sem[node_id]);
  1291. sem_getvalue(&node_sem[node_id],&i);
  1292. fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
  1293. fflush(log_file);
  1294. my_stats.msg_count++;
  1295. my_stats.distance += distance(node_id,sender_id);
  1296. //printf("open9 idag_id=%d node_id=%d fifo_name=%s\n",idag_id,node_id,fifo_name);
  1297. fd_r = open(fifo_name, O_RDONLY);
  1298. if (fd_r == -1) {
  1299. perror("opening reading pipe");
  1300. signals_enable();
  1301. return;
  1302. } else {
  1303. fprintf(log_file, "I went through open\n");
  1304. fflush(log_file);
  1305. }
  1306. if (far_man_offers->sender == node_id) {
  1307. read(fd_r, &offer_ans, sizeof(int));
  1308. printf("I am node %d and my offer answer is %d interaction=%d\n",node_id,offer_ans,core_inter_head[sender_id]->type);
  1309. if (offer_ans == 0) {
  1310. tmp_cores = my_cores;
  1311. while (tmp_cores != NULL){
  1312. if (tmp_cores->offered_to == sender_id) tmp_cores->offered_to = -1;
  1313. tmp_cores = tmp_cores->next;
  1314. }
  1315. } else if (offer_ans == 1) {//inform my idag if I have. If i am an idag i just reduce my DDS core num. I keep my core.
  1316. if (DDS->agent_id != node_id) printf("Fuck i am not first in my DDS list!!\n");
  1317. DDS->num_of_cores -= core_inter_head[sender_id]->data.my_offer.num_of_cores;
  1318. } else printf("I am %d Answer different than 0 or 1 in far from %d!!\n",node_id,sender_id);
  1319. //if (core_inter_head[sender_id]->type == FAR_REQ_OFFER_SENT) {//FAR_REQ_OFFER
  1320. /*printf("poutanares\n");
  1321. if (far_man_offers == NULL) printf("kai poytanes kai karioles\n");
  1322. else printf("gamw ths papias\n");*/
  1323. tmp_offer_list = far_man_offers;
  1324. far_man_offers = far_man_offers->next;
  1325. free(tmp_offer_list);
  1326. }
  1327. while (far_man_offers != NULL){
  1328. //printf("kai edw ftamw\n");
  1329. read(fd_r, &offer_ans, sizeof(int));
  1330. printf("I am node %d and far offer answer for node %d is %d\n",node_id,far_man_offers->sender,offer_ans);
  1331. if (offer_ans == 0 || offer_ans == 1){
  1332. *far_man_offers->answer = offer_ans;
  1333. if (core_inter_head[far_man_offers->sender]->type == REP_AGENT_OFFER_PENDING){
  1334. kill(pid_num[far_man_offers->sender],SIG_REP_OFFERS);
  1335. core_inter_head[far_man_offers->sender]->type = REP_AGENT_OFFER_SENT;
  1336. my_stats.msg_count++;
  1337. my_stats.distance += distance(node_id,far_man_offers->sender);
  1338. } else printf("Apparently lists are poutana ola\n");
  1339. } else printf("Far Answer different than 0 or 1!!\n");
  1340. tmp_offer_list = far_man_offers;
  1341. far_man_offers = far_man_offers->next;
  1342. free(tmp_offer_list);
  1343. }
  1344. sem_getvalue(&node_sem_out[sender_id],&i);
  1345. fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
  1346. fflush(log_file);
  1347. if (sem_post(&node_sem_out[sender_id]) == -1){
  1348. printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
  1349. perror("sem_post");
  1350. }
  1351. sem_getvalue(&node_sem_out[sender_id],&i);
  1352. fprintf(log_file, "Sem value = %d\n",i);
  1353. fflush(log_file);
  1354. //sem_post(&node_sem[node_id]);
  1355. //sem_post(&node_sem[sender_id]);
  1356. far_req_app.A=-1.0;
  1357. far_req_app.var=-1.0;
  1358. far_req_app.num_of_cores=-1;
  1359. far_req_or_sender = -1;
  1360. far_reg.C = -1;
  1361. far_reg.r = -1;
  1362. } else printf("I am %d and fail 2\n",node_id);
  1363. tmp_inter_list = core_inter_head[sender_id];
  1364. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  1365. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  1366. else send_next_signal(core_inter_head[sender_id], sender_id);
  1367. free(tmp_inter_list);
  1368. close(fd_r);
  1369. free(fifo_name);
  1370. cur_time = time(NULL);
  1371. cur_t = localtime(&cur_time);
  1372. fprintf(log_file, "[%d:%d:%d]: I ended sig_REP_OFFERS_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  1373. fflush(log_file);
  1374. signals_enable();
  1375. }
  1376. void sig_INIT_AGENT_handler(int signo, siginfo_t *info, void *context)
  1377. {
  1378. int fd_r, i, tmp, sender_id;
  1379. char *fifo_name;
  1380. core_list *tmp_core;
  1381. signals_disable();
  1382. sender_id = get_id_from_pid(info->si_pid);
  1383. fifo_name = get_pipe_name(node_id);
  1384. cur_time = time(NULL);
  1385. cur_t = localtime(&cur_time);
  1386. fprintf(log_file, "[%d:%d:%d]: I entered sig_INIT_AGENT_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  1387. fflush(log_file);
  1388. sem_getvalue(&node_sem[node_id],&i);
  1389. fprintf(log_file, "In 9 Trying to acquire semaphore. Sem value = %d\n",i);
  1390. fflush(log_file);
  1391. kill(info->si_pid, SIG_ACK);
  1392. sem_wait(&node_sem[node_id]);
  1393. sem_getvalue(&node_sem[node_id],&i);
  1394. fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
  1395. fflush(log_file);
  1396. my_stats.msg_count++;
  1397. my_stats.distance += distance(node_id,get_id_from_pid(info->si_pid));
  1398. //printf("open10 idag_id=%d node_id=%d\n",idag_id,node_id);
  1399. fd_r = open(fifo_name, O_RDONLY);
  1400. if (fd_r == -1) {
  1401. perror("opening reading pipe");
  1402. signals_enable();
  1403. return;
  1404. } else {
  1405. fprintf(log_file, "I went through open\n");
  1406. fflush(log_file);
  1407. }
  1408. if (my_app.num_of_cores != -1) { //Very very very important!!!
  1409. printf ("I am already managing an app!!! Fail!!!!\n");
  1410. while (my_cores != NULL){
  1411. tmp_core = my_cores;
  1412. my_cores = my_cores->next;
  1413. free(tmp_core);
  1414. }
  1415. my_cores_tail = NULL;
  1416. my_cores_count = 0;
  1417. }
  1418. read(fd_r, &my_app, sizeof(app));
  1419. my_cores_count = my_app.num_of_cores;//+1;
  1420. if (my_cores == NULL) {
  1421. my_cores = (core_list *) malloc(sizeof(core_list));
  1422. my_cores_tail = my_cores;
  1423. } else {
  1424. printf("My cores still not fucking null!!\n");
  1425. my_cores_tail->next = (core_list *) malloc(sizeof(core_list));
  1426. my_cores_tail = my_cores_tail->next;
  1427. }
  1428. my_cores_tail->core_id = node_id;
  1429. my_cores_tail->offered_to = -1;
  1430. my_cores_tail->next = NULL;
  1431. //I want myself to be first in my_cores list
  1432. for (i=0; i<my_app.num_of_cores; i++){
  1433. read(fd_r, &tmp, sizeof(int));
  1434. if (tmp != node_id){
  1435. my_cores_tail->next = (core_list *) malloc(sizeof(core_list));
  1436. my_cores_tail = my_cores_tail->next;
  1437. //read(fd_r, &my_cores_tail->core_id, sizeof(int));
  1438. my_cores_tail->core_id = tmp;
  1439. my_cores_tail->offered_to = -1;
  1440. my_cores_tail->next = NULL;
  1441. }
  1442. }
  1443. read(fd_r, &my_app_times[0], sizeof(my_time_stamp));
  1444. read(fd_r, &my_app_times[1], sizeof(my_time_stamp));
  1445. ////sem_post(&node_sem[sender_id]);
  1446. printf("I am new agent with id %d and app with A=%.2f, var=%.2f and %d my_cores_count = %d cores: \n"
  1447. ,node_id,my_app.A,my_app.var,my_app.num_of_cores,my_cores_count);
  1448. my_Speedup = Speedup(my_app, my_app.num_of_cores);
  1449. if (core_inter_head[my_idag] == NULL){
  1450. core_inter_head[my_idag] = (inter_list *) malloc(sizeof(inter_list));
  1451. core_inter_tail[my_idag] = core_inter_head[my_idag];
  1452. } else {
  1453. core_inter_tail[my_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  1454. core_inter_tail[my_idag] = core_inter_tail[my_idag]->next;
  1455. }
  1456. core_inter_tail[my_idag]->type = IDAG_ADD_CORES_DDS;
  1457. //core_inter_tail[my_idag]->data.my_offer = core_inter_head[sender_id]->data.my_offer;
  1458. core_inter_tail[my_idag]->data.app_cores = (int *)malloc((my_cores_count+1)*sizeof(int));
  1459. core_inter_tail[my_idag]->data.app_cores[0] = my_cores_count;
  1460. //for(i=1; i<=my_cores_count; i++)
  1461. // core_inter_tail[my_idag]->data.app_cores[i] = init_man_offers->off.offered_cores[i-1];
  1462. tmp_core = my_cores;
  1463. i=1;
  1464. while (tmp_core != NULL){
  1465. core_inter_tail[my_idag]->data.app_cores[i] = tmp_core->core_id;
  1466. tmp_core = tmp_core->next;
  1467. i++;
  1468. }
  1469. core_inter_tail[my_idag]->next = NULL;
  1470. if (core_inter_head[my_idag]->next == NULL) {
  1471. kill(pid_num[my_idag], SIG_ADD_CORES_DDS);
  1472. my_stats.msg_count++;
  1473. my_stats.distance += distance(node_id,my_idag);
  1474. } else printf("I am %d and i didn't call add!! with interaction %d\n",node_id,core_inter_head[my_idag]->type);
  1475. if (my_agent != -1) {
  1476. printf("I am %d and i do this agent switch\n",node_id);
  1477. //if (state != IDLE_CORE) {
  1478. if (state == WORKING_NODE || state == WORKING_NODE_IDLE_INIT) {
  1479. if (core_inter_head[my_agent] == NULL){
  1480. core_inter_head[my_agent] = (inter_list *) malloc(sizeof(inter_list));
  1481. core_inter_tail[my_agent] = core_inter_head[my_agent];
  1482. } else {
  1483. core_inter_tail[my_agent]->next = (inter_list *) malloc(sizeof(inter_list));
  1484. core_inter_tail[my_agent] = core_inter_tail[my_agent]->next;
  1485. }
  1486. core_inter_tail[my_agent]->type = REP_CHK_REM_TIME;
  1487. core_inter_tail[my_agent]->data.work_time = upper_work_bound - time_worked;
  1488. core_inter_tail[my_agent]->next = NULL;
  1489. }
  1490. upper_work_bound = 0;
  1491. my_agent = -1;
  1492. }
  1493. //sem_post(&node_sem[node_id]);
  1494. sem_getvalue(&node_sem_out[sender_id],&i);
  1495. fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
  1496. fflush(log_file);
  1497. if (sem_post(&node_sem_out[sender_id]) == -1){
  1498. printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
  1499. perror("sem_post");
  1500. }
  1501. sem_getvalue(&node_sem_out[sender_id],&i);
  1502. fprintf(log_file, "Sem value = %d\n",i);
  1503. fflush(log_file);
  1504. printf("Init ok!! my_cores_count = %d app_cores=%d app_id=%d\n",my_cores_count,my_app.num_of_cores,my_app.id);
  1505. cur_time = time(NULL);
  1506. cur_t = localtime(&cur_time);
  1507. fprintf(log_file, "[%d:%d:%d]: Init ok!! my_cores_count = %d app_id=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,my_cores_count,my_app.id);
  1508. fprintf(log_file, "my cores are:");
  1509. for (tmp_core=my_cores; tmp_core!=NULL; tmp_core=tmp_core->next) fprintf(log_file, " %d", tmp_core->core_id);
  1510. fprintf(log_file, "\n");
  1511. printf("I am %d and about to do an selfopt agent with my state before change = %d\n",node_id,state);
  1512. if (state == IDLE_INIT_MAN || state == INIT_MANAGER || state == INIT_MANAGER_SEND_OFFERS || state == INIT_MAN_CHK_OFFERS || state == WORKING_NODE_IDLE_INIT) {
  1513. fprintf(log_file,"I have pending init\n");
  1514. //state = AGENT_INIT_STATE_INIT_INTERRUPTED;
  1515. pending_state = state;
  1516. state = AGENT_INIT_STATE;
  1517. } else state = AGENT_INIT_STATE;
  1518. close(fd_r);
  1519. free(fifo_name);
  1520. cur_time = time(NULL);
  1521. cur_t = localtime(&cur_time);
  1522. fprintf(log_file, "[%d:%d:%d]: I ended sig_INIT_AGENT_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  1523. fflush(log_file);
  1524. signals_enable();
  1525. }
  1526. //an mou erthei to shma apo allon idag prepei apla na valw, an mou erthei apo common node tote prepei na upologisw
  1527. void sig_ADD_CORES_DDS_handler(int signo, siginfo_t *info, void *context)
  1528. {
  1529. int fd_r, i, is_sender_idag, sender_id, j, new_agent_id;
  1530. int *nodes_to_process, nodes_cnt=0, processed_cnt=0;
  1531. int *tmp_list, tmp_cnt, tmp_idag;
  1532. char *fifo_name;
  1533. core_list *tmp_cores;//, *tmp_cores_list;
  1534. DDS_list *tmp_DDS;
  1535. signals_disable();
  1536. sender_id = get_id_from_pid(info->si_pid);
  1537. fifo_name = get_pipe_name(node_id);
  1538. cur_time = time(NULL);
  1539. cur_t = localtime(&cur_time);
  1540. fprintf(log_file, "[%d:%d:%d]: I entered sig_ADD_CORES_DDS_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  1541. fflush(log_file);
  1542. sem_getvalue(&node_sem[node_id],&i);
  1543. fprintf(log_file, "In 10 Trying to acquire semaphore. Sem value = %d\n",i);
  1544. fflush(log_file);
  1545. kill(info->si_pid, SIG_ACK);
  1546. sem_wait(&node_sem[node_id]);
  1547. sem_getvalue(&node_sem[node_id],&i);
  1548. fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
  1549. fflush(log_file);
  1550. my_stats.msg_count++;
  1551. my_stats.distance += distance(node_id,sender_id);
  1552. //printf("open11 idag_id=%d node_id=%d\n",idag_id,node_id);
  1553. fd_r = open(fifo_name, O_RDONLY);
  1554. if (fd_r == -1) {
  1555. perror("opening reading pipe");
  1556. signals_enable();
  1557. return;
  1558. } else {
  1559. fprintf(log_file, "I went through open\n");
  1560. fflush(log_file);
  1561. }
  1562. is_sender_idag = 0;
  1563. for (i=0; i<num_idags; i++)
  1564. if (idag_id_arr[i] == sender_id){
  1565. is_sender_idag = 1;
  1566. break;
  1567. }
  1568. //printf("I am in add with is_sender_idag = %d\n",is_sender_idag);
  1569. if (is_sender_idag == 0){
  1570. read(fd_r, &nodes_cnt, sizeof(int));
  1571. while (nodes_cnt <=0){
  1572. printf("i am %d and i read %d in nodes_cnt from %d\n",node_id,nodes_cnt,sender_id);
  1573. read(fd_r, &nodes_cnt, sizeof(int));
  1574. }
  1575. nodes_to_process = (int *) malloc(nodes_cnt * sizeof(int));
  1576. for (i=0; i<nodes_cnt; i++)
  1577. read(fd_r, &nodes_to_process[i], sizeof(int));
  1578. ////sem_post(&node_sem[sender_id]);
  1579. tmp_list = (int *) malloc(nodes_cnt * sizeof(int));
  1580. printf("I am %d in add cores with sender %d and nodes_cnt = %d\n",node_id,sender_id,nodes_cnt);
  1581. while (processed_cnt < nodes_cnt){
  1582. tmp_cnt = 0;
  1583. tmp_idag = -1;
  1584. for (i=0; i<nodes_cnt; i++){
  1585. if (processed_cnt == nodes_cnt) break;
  1586. if (nodes_to_process[i] == -1) continue;
  1587. else {
  1588. if (tmp_idag == -1) tmp_idag = idag_mask[nodes_to_process[i]];
  1589. if (idag_mask[nodes_to_process[i]] != tmp_idag) continue;
  1590. else {
  1591. tmp_list[tmp_cnt++] = nodes_to_process[i];
  1592. nodes_to_process[i] = -1;
  1593. processed_cnt++;
  1594. }
  1595. }
  1596. }
  1597. if (tmp_idag != node_id){
  1598. if (core_inter_head[tmp_idag] == NULL){
  1599. core_inter_head[tmp_idag] = (inter_list *) malloc(sizeof(inter_list));
  1600. core_inter_tail[tmp_idag] = core_inter_head[tmp_idag];
  1601. } else {
  1602. printf("Adding to DDS not in high priority! Shouldn't I high prioritize? tmp = %d idag = %d\n",tmp_idag,node_id);
  1603. core_inter_tail[tmp_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  1604. core_inter_tail[tmp_idag] = core_inter_tail[tmp_idag]->next;
  1605. }
  1606. printf("In ADD tmp_idag = %d tmp_cnt = %d sender_id = %d node_id = %d\n",tmp_idag,tmp_cnt,sender_id,node_id);
  1607. core_inter_tail[tmp_idag]->type = IDAG_ADD_CORES_DDS;
  1608. core_inter_tail[tmp_idag]->data.app_cores = (int *)malloc((tmp_cnt+2)*sizeof(int));
  1609. core_inter_tail[tmp_idag]->data.app_cores[0] = tmp_cnt;//+1;
  1610. core_inter_tail[tmp_idag]->data.app_cores[tmp_cnt+1] = sender_id;
  1611. for (j=1; j<= tmp_cnt; j++)
  1612. core_inter_tail[tmp_idag]->data.app_cores[j] = tmp_list[j-1];
  1613. core_inter_tail[tmp_idag]->next = NULL;
  1614. if (core_inter_head[tmp_idag]->next == NULL) {
  1615. kill(pid_num[tmp_idag], SIG_ADD_CORES_DDS);
  1616. my_stats.msg_count++;
  1617. my_stats.distance += distance(node_id,tmp_idag);
  1618. }
  1619. } else {
  1620. printf("In ADD same tmp_idag = %d tmp_cnt = %d nodes_cnt = %d sender_id = %d node_id = %d\n",tmp_idag,tmp_cnt,nodes_cnt,sender_id,node_id);
  1621. new_agent_id = sender_id;
  1622. for (i=0; i<tmp_cnt; i++){//nodes_cnt
  1623. fprintf(log_file,"I am importing node %d\n",tmp_list[i]);
  1624. fflush(log_file);
  1625. tmp_cores = my_cores;
  1626. while (tmp_cores->core_id != tmp_list[i])//nodes_to_process[i]
  1627. tmp_cores = tmp_cores->next;
  1628. if (tmp_cores->offered_to == -1) {
  1629. fprintf(log_file,"Node %d was offered to nobody\n",tmp_list[i]);
  1630. fflush(log_file);
  1631. DDS->num_of_cores--;
  1632. tmp_cores->offered_to = new_agent_id;
  1633. } else if (tmp_cores->offered_to != new_agent_id) {
  1634. fprintf(log_file,"Node %d is offered to %d\n",tmp_list[i],tmp_cores->offered_to);
  1635. fflush(log_file);
  1636. tmp_cores->offered_to = new_agent_id;
  1637. }
  1638. }
  1639. tmp_DDS = DDS;
  1640. while (tmp_DDS != NULL && tmp_DDS->agent_id != new_agent_id)
  1641. tmp_DDS = tmp_DDS->next;
  1642. if (tmp_DDS != NULL)
  1643. tmp_DDS->num_of_cores += nodes_cnt;
  1644. else {
  1645. //printf("I am in here DDSing nodes_cnt=%d\n",nodes_cnt);
  1646. DDS_tail->next = (DDS_list *) malloc(sizeof(DDS_list));
  1647. DDS_tail = DDS_tail->next;
  1648. DDS_tail->agent_id = new_agent_id;
  1649. DDS_tail->num_of_cores = nodes_cnt;
  1650. DDS_tail->next = NULL;
  1651. DDS_count++;
  1652. }
  1653. }
  1654. }
  1655. } else {
  1656. read(fd_r, &nodes_cnt, sizeof(int));
  1657. nodes_to_process = (int *) malloc(nodes_cnt * sizeof(int));
  1658. for (i=0; i<nodes_cnt; i++)
  1659. read(fd_r, &nodes_to_process[i], sizeof(int));
  1660. read(fd_r, &new_agent_id, sizeof(int));
  1661. ////sem_post(&node_sem[sender_id]);
  1662. //printf("new agent id = %d\n",new_agent_id);
  1663. for (i=0; i<nodes_cnt; i++){
  1664. //printf("Node %d is %d\n",i,nodes_to_process[i]);
  1665. fprintf(log_file,"I am importing node %d\n",nodes_to_process[i]);
  1666. fflush(log_file);
  1667. tmp_cores = my_cores;
  1668. while (tmp_cores->core_id != nodes_to_process[i])
  1669. tmp_cores = tmp_cores->next;
  1670. if (tmp_cores->offered_to == -1) {
  1671. fprintf(log_file,"Node %d was offered to nobody\n",nodes_to_process[i]);
  1672. fflush(log_file);
  1673. DDS->num_of_cores--;
  1674. tmp_cores->offered_to = new_agent_id;
  1675. } else if (tmp_cores->offered_to != new_agent_id) {
  1676. fprintf(log_file,"Node %d is offered to %d\n",nodes_to_process[i],tmp_cores->offered_to);
  1677. fflush(log_file);
  1678. tmp_cores->offered_to = new_agent_id;
  1679. }
  1680. //tmp_cores->offered_to = new_agent_id;
  1681. }
  1682. tmp_DDS = DDS;
  1683. while (tmp_DDS != NULL && tmp_DDS->agent_id != new_agent_id)
  1684. tmp_DDS = tmp_DDS->next;
  1685. if (tmp_DDS != NULL)
  1686. tmp_DDS->num_of_cores += nodes_cnt;
  1687. else {
  1688. if (DDS_count == 1 && DDS != DDS_tail) printf("I am %d kai ta hpiame asxhma!\n",node_id);
  1689. DDS_tail->next = (DDS_list *) malloc(sizeof(DDS_list));
  1690. DDS_tail = DDS_tail->next;
  1691. DDS_tail->agent_id = new_agent_id;
  1692. DDS_tail->num_of_cores = nodes_cnt;
  1693. DDS_tail->next = NULL;
  1694. DDS_count++;
  1695. //printf("I did this with node_id = %d, new_agent_id = %d\n",node_id,new_agent_id);
  1696. }
  1697. }
  1698. //sem_post(&node_sem[node_id]);
  1699. sem_getvalue(&node_sem_out[sender_id],&i);
  1700. fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
  1701. fflush(log_file);
  1702. if (sem_post(&node_sem_out[sender_id]) == -1){
  1703. printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
  1704. perror("sem_post");
  1705. }
  1706. sem_getvalue(&node_sem_out[sender_id],&i);
  1707. fprintf(log_file, "Sem value = %d\n",i);
  1708. fflush(log_file);
  1709. //for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next)
  1710. // if (tmp_cores_list->offered_to != -1) my_stats.cores_utilized++;
  1711. my_stats.times_accessed++;
  1712. printf("I am %d Adding ended well with sender_id=%d!\n",node_id,sender_id);
  1713. printf("Number of agents in region = %d\n",DDS_count);
  1714. printf("Agent no 0 is %d with %d cores\n",DDS->agent_id,DDS->num_of_cores);
  1715. i=1;
  1716. for (tmp_DDS = DDS->next; tmp_DDS != NULL; tmp_DDS = tmp_DDS->next){
  1717. printf("Agent no %d is %d with %d cores\n",i,tmp_DDS->agent_id,tmp_DDS->num_of_cores);
  1718. my_stats.cores_utilized += tmp_DDS->num_of_cores;
  1719. i++;
  1720. }
  1721. close(fd_r);
  1722. free(fifo_name);
  1723. cur_time = time(NULL);
  1724. cur_t = localtime(&cur_time);
  1725. fprintf(log_file, "[%d:%d:%d]: I ended sig_ADD_CORES_DDS_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  1726. fflush(log_file);
  1727. signals_enable();
  1728. }
  1729. void sig_REM_CORES_DDS_handler(int signo, siginfo_t *info, void *context)
  1730. {
  1731. int fd_r, i, is_sender_idag, sender_id, j, new_agent_id;
  1732. int *nodes_to_process, nodes_cnt=0, processed_cnt=0;
  1733. int *tmp_list, tmp_cnt, tmp_idag;
  1734. char *fifo_name;
  1735. DDS_list *tmp_DDS,*tmp_DDS_prev;
  1736. signals_disable();
  1737. sender_id = get_id_from_pid(info->si_pid);
  1738. fifo_name = get_pipe_name(node_id);
  1739. cur_time = time(NULL);
  1740. cur_t = localtime(&cur_time);
  1741. fprintf(log_file, "[%d:%d:%d]: I entered sig_REM_CORES_DDS_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  1742. fflush(log_file);
  1743. sem_getvalue(&node_sem[node_id],&i);
  1744. fprintf(log_file, "In 11 Trying to acquire semaphore. Sem value = %d\n",i);
  1745. fflush(log_file);
  1746. kill(info->si_pid, SIG_ACK);
  1747. sem_wait(&node_sem[node_id]);
  1748. sem_getvalue(&node_sem[node_id],&i);
  1749. fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
  1750. fflush(log_file);
  1751. my_stats.msg_count++;
  1752. my_stats.distance += distance(node_id,sender_id);
  1753. //printf("open11 idag_id=%d node_id=%d\n",idag_id,node_id);
  1754. fd_r = open(fifo_name, O_RDONLY);
  1755. if (fd_r == -1) {
  1756. perror("opening reading pipe");
  1757. signals_enable();
  1758. return;
  1759. } else {
  1760. fprintf(log_file, "I went through open\n");
  1761. fflush(log_file);
  1762. }
  1763. is_sender_idag = 0;
  1764. for (i=0; i<num_idags; i++)
  1765. if (idag_id_arr[i] == sender_id){
  1766. is_sender_idag = 1;
  1767. break;
  1768. }
  1769. //printf("I am in rem with is_sender_idag = %d\n",is_sender_idag);
  1770. if (is_sender_idag == 0){
  1771. read(fd_r, &nodes_cnt, sizeof(int));
  1772. nodes_to_process = (int *) malloc(nodes_cnt * sizeof(int));
  1773. for (i=0; i<nodes_cnt; i++)
  1774. read(fd_r, &nodes_to_process[i], sizeof(int));
  1775. ////sem_post(&node_sem[sender_id]);
  1776. tmp_list = (int *) malloc(nodes_cnt * sizeof(int));
  1777. while (processed_cnt < nodes_cnt){
  1778. tmp_cnt = 0;
  1779. tmp_idag = -1;
  1780. for (i=0; i<nodes_cnt; i++){
  1781. if (processed_cnt == nodes_cnt) break;
  1782. if (nodes_to_process[i] == -1) continue;
  1783. else {
  1784. if (tmp_idag == -1) tmp_idag = idag_mask[nodes_to_process[i]];
  1785. if (idag_mask[nodes_to_process[i]] != tmp_idag) continue;
  1786. else {
  1787. tmp_list[tmp_cnt++] = nodes_to_process[i];
  1788. nodes_to_process[i] = -1;
  1789. processed_cnt++;
  1790. }
  1791. }
  1792. }
  1793. if (tmp_idag != node_id){
  1794. if (core_inter_head[tmp_idag] == NULL){
  1795. core_inter_head[tmp_idag] = (inter_list *) malloc(sizeof(inter_list));
  1796. core_inter_tail[tmp_idag] = core_inter_head[tmp_idag];
  1797. } else {
  1798. printf("Removing from DDS not in high priority! Shouldn't I high prioritize? tmp = %d idag = %d\n",tmp_idag,node_id);
  1799. core_inter_tail[tmp_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  1800. core_inter_tail[tmp_idag] = core_inter_tail[tmp_idag]->next;
  1801. }
  1802. //printf("In REM tmp_idag = %d tmp_cnt = %d sender_id = %d idag_id = %d\n",tmp_idag,tmp_cnt,sender_id,idag_id);
  1803. core_inter_tail[tmp_idag]->type = IDAG_REM_CORES_DDS;
  1804. core_inter_tail[tmp_idag]->data.app_cores = (int *)malloc((tmp_cnt+2)*sizeof(int));
  1805. core_inter_tail[tmp_idag]->data.app_cores[0] = tmp_cnt;//+1;
  1806. core_inter_tail[tmp_idag]->data.app_cores[tmp_cnt+1] = sender_id;
  1807. for (j=1; j<= tmp_cnt; j++)
  1808. core_inter_tail[tmp_idag]->data.app_cores[j] = tmp_list[j-1];
  1809. core_inter_tail[tmp_idag]->next = NULL;
  1810. if (core_inter_head[tmp_idag]->next == NULL) {
  1811. kill(pid_num[tmp_idag], SIG_REM_CORES_DDS);
  1812. my_stats.msg_count++;
  1813. my_stats.distance += distance(node_id,tmp_idag);
  1814. }
  1815. } else {
  1816. printf("In REM node_id = %d same tmp_idag = %d tmp_cnt = %d nodes_cnt = %d sender_id = %d \n",node_id,tmp_idag,tmp_cnt,nodes_cnt,sender_id);
  1817. new_agent_id = sender_id;
  1818. tmp_DDS = DDS;
  1819. tmp_DDS_prev = NULL;
  1820. while (tmp_DDS != NULL && tmp_DDS->agent_id != new_agent_id){
  1821. tmp_DDS_prev = tmp_DDS;
  1822. tmp_DDS = tmp_DDS->next;
  1823. }
  1824. if (tmp_DDS == NULL) printf("Agent does not exist in my DDS\n");
  1825. else if (tmp_DDS == DDS) printf("I am removing from myself in REM?\n");
  1826. else {
  1827. tmp_DDS->num_of_cores -= nodes_cnt;
  1828. if (tmp_DDS->num_of_cores == 0){
  1829. DDS_count--;
  1830. if (tmp_DDS == DDS_tail){
  1831. DDS_tail = tmp_DDS_prev;
  1832. DDS_tail->next = NULL;
  1833. } else tmp_DDS_prev->next = tmp_DDS->next;
  1834. free(tmp_DDS);
  1835. }
  1836. }
  1837. }
  1838. }
  1839. } else { //den afairw tous purhnes apo thn core list giati mporei na exoun hdh ginei add
  1840. read(fd_r, &nodes_cnt, sizeof(int));
  1841. nodes_to_process = (int *) malloc(nodes_cnt * sizeof(int));
  1842. for (i=0; i<nodes_cnt; i++)
  1843. read(fd_r, &nodes_to_process[i], sizeof(int));
  1844. read(fd_r, &new_agent_id, sizeof(int));
  1845. ////sem_post(&node_sem[sender_id]);
  1846. //printf("I am in the second rem with new agent id = %d and nodes_cnt = %d\n",new_agent_id,nodes_cnt);
  1847. tmp_DDS = DDS;
  1848. tmp_DDS_prev = NULL;
  1849. while (tmp_DDS != NULL && tmp_DDS->agent_id != new_agent_id){
  1850. tmp_DDS_prev = tmp_DDS;
  1851. tmp_DDS = tmp_DDS->next;
  1852. }
  1853. if (tmp_DDS == NULL) printf("Agent does not exist in my DDS\n");
  1854. else if (tmp_DDS == DDS) printf("I am removing from myself in REM?\n");
  1855. else {
  1856. tmp_DDS->num_of_cores -= nodes_cnt;
  1857. if (tmp_DDS->num_of_cores == 0){
  1858. DDS_count--;
  1859. if (tmp_DDS == DDS_tail){
  1860. DDS_tail = tmp_DDS_prev;
  1861. DDS_tail->next = NULL;
  1862. } else tmp_DDS_prev->next = tmp_DDS->next;
  1863. free(tmp_DDS);
  1864. }
  1865. }
  1866. }
  1867. //sem_post(&node_sem[node_id]);
  1868. sem_getvalue(&node_sem_out[sender_id],&i);
  1869. fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
  1870. fflush(log_file);
  1871. if (sem_post(&node_sem_out[sender_id]) == -1){
  1872. printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
  1873. perror("sem_post");
  1874. }
  1875. sem_getvalue(&node_sem_out[sender_id],&i);
  1876. fprintf(log_file, "Sem value = %d\n",i);
  1877. fflush(log_file);
  1878. printf("I am %d Removing ended well! with sender_id=%d!\n",node_id,sender_id);
  1879. printf("Number of agents in region = %d\n",DDS_count);
  1880. i=0;
  1881. for (tmp_DDS = DDS; tmp_DDS != NULL; tmp_DDS = tmp_DDS->next){
  1882. printf("Agent no %d is %d with %d cores\n",i,tmp_DDS->agent_id,tmp_DDS->num_of_cores);
  1883. i++;
  1884. }
  1885. close(fd_r);
  1886. free(fifo_name);
  1887. cur_time = time(NULL);
  1888. cur_t = localtime(&cur_time);
  1889. fprintf(log_file, "[%d:%d:%d]: I ended sig_REM_CORES_DDS_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  1890. fflush(log_file);
  1891. signals_enable();
  1892. }
  1893. void sig_FAR_REQ_handler(int signo, siginfo_t *info, void *context)
  1894. {
  1895. int fd_r=-1, sender_id, i, one_idag, *idags_in_reg, agent_id, num_of_offers, j, far_req_man, one_far_sender, cores_util;//num_of_idags,
  1896. char *fifo_name;
  1897. region cur_reg;
  1898. offer one_offer;
  1899. inter_list *tmp_inter_list, *tmp_inter_prev;
  1900. DDS_list *tmp_DDS;
  1901. offer_list *tmp_offer_list, *tmp_offer_prev;
  1902. app one_app;
  1903. signals_disable();
  1904. sender_id = get_id_from_pid(info->si_pid);
  1905. fifo_name = get_pipe_name(node_id);
  1906. cur_time = time(NULL);
  1907. cur_t = localtime(&cur_time);
  1908. fprintf(log_file, "[%d:%d:%d]: I entered sig_FAR_REQ_handler with sender=%d and signo=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,signo,state);
  1909. fflush(log_file);
  1910. //I am the node that is far.
  1911. //I keep that as NULL because this must be the way, or else why is he sending me smth if we are allready communicating?
  1912. if (core_inter_head[sender_id] == NULL && signo == SIG_FAR_REQ && my_idag != -1) {
  1913. printf("I am %d and i think i cought a stray far_manager reply from %d. My current state is %d\n",node_id,sender_id,state);
  1914. fprintf(log_file,"I am %d and i think i cought a stray far_manager reply from %d. My current state is %d\n",node_id,sender_id,state);
  1915. fflush(log_file);
  1916. kill(info->si_pid, SIG_REJECT);
  1917. my_stats.msg_count++;
  1918. my_stats.distance += distance(node_id,sender_id);
  1919. } else if (core_inter_head[sender_id] == NULL && (signo == SIG_INIT_FAR_REQ || (signo == SIG_FAR_REQ && my_idag == -1))) {
  1920. sem_getvalue(&node_sem[node_id],&i);
  1921. fprintf(log_file, "In 12 Trying to acquire semaphore. Sem value = %d\n",i);
  1922. fflush(log_file);
  1923. kill(info->si_pid, SIG_ACK);
  1924. sem_wait(&node_sem[node_id]);
  1925. sem_getvalue(&node_sem[node_id],&i);
  1926. fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
  1927. fflush(log_file);
  1928. my_stats.msg_count++;
  1929. my_stats.distance += distance(node_id,sender_id);
  1930. //printf("I am inside with sender_id %d %s\n",sender_id,fifo_name);
  1931. //printf("open12 idag_id=%d node_id=%d\n",idag_id,node_id);
  1932. fd_r = open(fifo_name, O_RDONLY);
  1933. if (fd_r == -1) {
  1934. perror("opening reading pipe");
  1935. signals_enable();
  1936. return;
  1937. }
  1938. if (signo == SIG_INIT_FAR_REQ) {
  1939. //far_req_or_sender = sender_id;
  1940. one_far_sender = sender_id;
  1941. //read(fd_r, &far_req_app, sizeof(app));
  1942. read(fd_r, &one_app, sizeof(app));
  1943. read(fd_r, &cur_reg, sizeof(region));
  1944. if (core_inter_head[one_far_sender] == NULL){
  1945. core_inter_head[one_far_sender] = (inter_list *) malloc(sizeof(inter_list));
  1946. core_inter_tail[one_far_sender] = core_inter_head[one_far_sender];
  1947. } else {
  1948. core_inter_tail[one_far_sender]->next = (inter_list *) malloc(sizeof(inter_list));
  1949. core_inter_tail[one_far_sender] = core_inter_tail[one_far_sender]->next;
  1950. }
  1951. core_inter_tail[one_far_sender]->type = REP_FAR_INIT_REQ;
  1952. if (my_idag != -1) {
  1953. far_req_man = my_idag;
  1954. if (core_inter_head[far_req_man] == NULL){
  1955. core_inter_head[far_req_man] = (inter_list *) malloc(sizeof(inter_list));
  1956. core_inter_tail[far_req_man] = core_inter_head[far_req_man];
  1957. } else {
  1958. core_inter_tail[far_req_man]->next = (inter_list *) malloc(sizeof(inter_list));
  1959. core_inter_tail[far_req_man] = core_inter_tail[far_req_man]->next;
  1960. }
  1961. //far_req_or_sender = one_far_sender;
  1962. //far_req_app = one_app;
  1963. core_inter_tail[far_req_man]->type = FAR_REQ_MAN_APPOINT_PENDING;
  1964. core_inter_tail[far_req_man]->data.far_req.orig_sender = one_far_sender;
  1965. core_inter_tail[far_req_man]->data.far_req.far_app = one_app;
  1966. core_inter_tail[far_req_man]->data.far_req.reg = cur_reg;
  1967. core_inter_tail[far_req_man]->next = NULL;
  1968. //core_inter_tail[far_req_or_sender]->data.far_req_man = my_idag;
  1969. } else {
  1970. far_req_man = node_id;
  1971. if (far_req_or_sender == -1) {
  1972. far_req_or_sender = one_far_sender;
  1973. far_req_app = one_app;
  1974. }
  1975. }
  1976. //core_inter_tail[one_far_sender]->data.far_req_man = node_id;
  1977. core_inter_tail[one_far_sender]->data.far_req_man = far_req_man;
  1978. core_inter_tail[one_far_sender]->next = NULL;
  1979. if (core_inter_head[one_far_sender]->next == NULL) {
  1980. kill(pid_num[one_far_sender], SIG_INIT_FAR_REQ);
  1981. my_stats.msg_count++;
  1982. my_stats.distance += distance(node_id,one_far_sender);
  1983. } else printf("I am %d and did not send %d one_far_sender init_far_req in here. interaction = %d\n",node_id,one_far_sender,core_inter_head[one_far_sender]->type);
  1984. } else { //SIG_FAR_REQ
  1985. //read(fd_r, &one_far_sender, sizeof(int));
  1986. //read(fd_r, &far_req_app, sizeof(app));
  1987. read(fd_r, &one_far_sender, sizeof(int));
  1988. read(fd_r, &one_app, sizeof(app));
  1989. read(fd_r, &cur_reg, sizeof(region));
  1990. while (cur_reg.C > X_max * Y_max){
  1991. printf("I am %d kai mas taizei malakies o %d\n",node_id,sender_id);
  1992. read(fd_r, &cur_reg, sizeof(region));
  1993. }
  1994. if (far_req_or_sender == -1) {
  1995. far_req_or_sender = one_far_sender;
  1996. far_req_app = one_app;
  1997. far_reg = cur_reg;
  1998. //printf("I am %d and i come here far_req_or_sender = %d\n",node_id,far_req_or_sender);
  1999. } //else printf("I am %d and karioles far_req_or_sender = %d one_far_sender = %d\n",node_id,far_req_or_sender,one_far_sender);
  2000. fprintf(log_file,"one_far_sender=%d far_req_or_sender = %d\n",one_far_sender,far_req_or_sender);
  2001. fflush(log_file);
  2002. }
  2003. if (signo == SIG_FAR_REQ || (signo == SIG_INIT_FAR_REQ && far_req_man == node_id)) {
  2004. if (far_req_or_sender == one_far_sender && far_reg.C == cur_reg.C && far_reg.r == cur_reg.r) {// far_man_offers == NULL
  2005. printf("I am node with id %d I am to far check-manage region C=%d r=%d for %d\n",node_id,cur_reg.C,cur_reg.r,far_req_or_sender);
  2006. fprintf(log_file,"I am node with id %d I am to far check-manage region C=%d r=%d for %d\n",node_id,cur_reg.C,cur_reg.r,far_req_or_sender);
  2007. fflush(log_file);
  2008. //my offer
  2009. if (far_man_offers == NULL){
  2010. far_list_count = 1;
  2011. far_man_offers = (offer_list *) malloc(sizeof(offer_list));
  2012. far_man_offers_tail = far_man_offers;
  2013. } else printf ("I am %d Far man offers list not NULL far_req_or_sender = %d one_far_sender = %d\n",node_id,far_req_or_sender,one_far_sender);
  2014. far_man_offers_tail->sender = node_id;
  2015. far_man_offers_tail->off.offered_cores = (int *) malloc(my_cores_count*sizeof(int));
  2016. far_man_offers_tail->off.num_of_cores = offer_cores(my_cores, far_req_app, cur_reg, far_man_offers_tail->off.offered_cores, far_req_or_sender);
  2017. my_stats.comp_effort++;
  2018. //if (my_idag == -1)
  2019. //far_man_offers_tail->off.spd_loss = 0.0;
  2020. cores_util = 0;
  2021. for (tmp_DDS = DDS->next; tmp_DDS != NULL; tmp_DDS = tmp_DDS->next)
  2022. cores_util += tmp_DDS->num_of_cores;
  2023. if (cores_util == 0) far_man_offers_tail->off.spd_loss = -2.0;
  2024. else far_man_offers_tail->off.spd_loss = (float) (-1 * cores_util) / (my_cores_count-1);
  2025. //else far_man_offers_tail->off.spd_loss = Speedup(my_app, my_cores_count) - Speedup(my_app, my_cores_count-far_man_offers_tail->off.num_of_cores);
  2026. far_man_offers_tail->sender = node_id;
  2027. far_man_offers_tail->next = NULL;
  2028. //far_list_count++;
  2029. cur_time = time(NULL);
  2030. cur_t = localtime(&cur_time);
  2031. fprintf(log_file, "[%d:%d:%d]: I added my offer in far_man_offers list num_of_cores=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,far_man_offers_tail->off.num_of_cores);
  2032. fflush(log_file);
  2033. if (far_man_offers_tail->off.num_of_cores == 0) {
  2034. free(far_man_offers_tail->off.offered_cores);
  2035. free(far_man_offers);
  2036. far_man_offers = NULL;
  2037. far_man_offers_tail = NULL;
  2038. far_list_count = 0;
  2039. }
  2040. idags_in_reg = (int *) malloc((num_idags+1)*sizeof(int));
  2041. get_reg_idags(cur_reg, idags_in_reg);
  2042. for (i=0; i<num_idags; i++)
  2043. if (idags_in_reg[i] && idag_id_arr[i] != node_id){
  2044. one_idag = idag_id_arr[i];
  2045. if (core_inter_head[one_idag] == NULL){
  2046. core_inter_head[one_idag] = (inter_list *) malloc(sizeof(inter_list));
  2047. core_inter_tail[one_idag] = core_inter_head[one_idag];
  2048. } else {
  2049. core_inter_tail[one_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  2050. core_inter_tail[one_idag] = core_inter_tail[one_idag]->next;
  2051. }
  2052. //if (signo == SIG_FAR_REQ)
  2053. //else core_inter_tail[one_idag]->type = FAR_INIT_IDAG_REQ_DDS;
  2054. core_inter_tail[one_idag]->type = FAR_REQ_IDAG_REQ_DDS_PENDING;
  2055. core_inter_tail[one_idag]->data.reg = cur_reg;
  2056. core_inter_tail[one_idag]->next = NULL;
  2057. if (core_inter_head[one_idag]->next == NULL) {
  2058. kill(pid_num[one_idag], SIG_REQ_DDS); //newly created
  2059. my_stats.msg_count++;
  2060. my_stats.distance += distance(node_id,one_idag);
  2061. } else printf("I am %d and i want far dds from %d but interaction is %d\n",node_id,one_idag,core_inter_head[one_idag]->type);
  2062. }
  2063. //printf("uiou 0 qw %d num_of_idags = %d idag_id=%d\n",idags_in_reg[idag_id],idags_in_reg[num_idags],idag_id);
  2064. //for (i=0; i<num_idags; i++)
  2065. //printf("idag i=%d node_num = %d idags_in_reg = %d\n",i,idag_id_arr[i],idags_in_reg[i]);
  2066. //asking offers from other agents in my DDS
  2067. tmp_DDS = DDS->next;
  2068. if (tmp_DDS != NULL) {
  2069. while (tmp_DDS != NULL){
  2070. agent_id = tmp_DDS->agent_id;
  2071. if (core_inter_head[agent_id] == NULL){
  2072. core_inter_head[agent_id] = (inter_list *) malloc(sizeof(inter_list));
  2073. core_inter_tail[agent_id] = core_inter_head[agent_id];
  2074. core_inter_tail[agent_id]->type = FAR_REQ_CORES_PENDING;
  2075. core_inter_tail[agent_id]->data.reg = cur_reg;//core_inter_head[sender_id]->data.reg;
  2076. core_inter_tail[agent_id]->next = NULL;
  2077. //if (core_inter_head[agent_id]->next == NULL) {
  2078. kill(pid_num[agent_id], SIG_REQ_CORES);
  2079. my_stats.msg_count++;
  2080. my_stats.distance += distance(node_id,agent_id);
  2081. //} else printf("!!!!! I am %d and in sending local-far offer to %d i have interaction %d\n",node_id,agent_id,core_inter_head[agent_id]->type);
  2082. } else {
  2083. printf("I did not send I am %d and in sending local-far offer to %d i have interaction %d\n",node_id,agent_id,core_inter_head[agent_id]->type);
  2084. //core_inter_tail[agent_id]->next = (inter_list *) malloc(sizeof(inter_list));
  2085. //core_inter_tail[agent_id] = core_inter_tail[agent_id]->next;
  2086. }
  2087. tmp_DDS = tmp_DDS->next;
  2088. }
  2089. if (state != IDLE_CHK_APP_FILE) {
  2090. its.it_value.tv_nsec = 250 * MS;
  2091. if (timer_settime(timerid, 0, &its, NULL) == -1) printf("timer_settime error2\n");
  2092. state = IDLE_FAR_MAN;
  2093. } else time_for_farman = 25;
  2094. } else if (idags_in_reg[num_idags] == 1) {//i am the only idag in region with no agents
  2095. if (core_inter_head[far_req_or_sender] == NULL){
  2096. core_inter_head[far_req_or_sender] = (inter_list *) malloc(sizeof(inter_list));
  2097. core_inter_tail[far_req_or_sender] = core_inter_head[far_req_or_sender];
  2098. } else {
  2099. core_inter_tail[far_req_or_sender]->next = (inter_list *) malloc(sizeof(inter_list));
  2100. core_inter_tail[far_req_or_sender] = core_inter_tail[far_req_or_sender]->next;
  2101. }
  2102. core_inter_tail[far_req_or_sender]->type = FAR_REQ_OFFER;
  2103. if (far_man_offers != NULL) core_inter_tail[far_req_or_sender]->data.my_offer = far_man_offers->off;
  2104. else core_inter_tail[far_req_or_sender]->data.my_offer.num_of_cores = 0;
  2105. core_inter_tail[far_req_or_sender]->next = NULL;
  2106. if (core_inter_head[far_req_or_sender]->next == NULL) {
  2107. kill(pid_num[far_req_or_sender],SIG_FAR_REQ);
  2108. my_stats.msg_count++;
  2109. my_stats.distance += distance(node_id,far_req_or_sender);
  2110. } else printf("I am %d and i am doing smth else with far_req_or_sender %d interaction %d\n",node_id,far_req_or_sender,core_inter_head[far_req_or_sender]->type);
  2111. } else {
  2112. if (state != IDLE_CHK_APP_FILE) {
  2113. its.it_value.tv_nsec = 250 * MS;
  2114. if (timer_settime(timerid, 0, &its, NULL) == -1) printf("timer_settime error3\n");
  2115. state = IDLE_FAR_MAN;
  2116. } else time_for_farman = 25;
  2117. }
  2118. } else if (far_req_or_sender != one_far_sender) {
  2119. fprintf(log_file,"I am node with id %d and I am allready far managing %d so i am going to send %d only my offer\n",node_id,far_req_or_sender,one_far_sender);
  2120. fflush(log_file);
  2121. if (core_inter_head[one_far_sender] == NULL){
  2122. core_inter_head[one_far_sender] = (inter_list *) malloc(sizeof(inter_list));
  2123. core_inter_tail[one_far_sender] = core_inter_head[one_far_sender];
  2124. core_inter_tail[one_far_sender]->type = REP_AGENT_REQ_CORES;
  2125. core_inter_tail[one_far_sender]->data.off_arr.offer_arr = (offer *) malloc(sizeof(offer));
  2126. core_inter_tail[one_far_sender]->next = NULL;
  2127. core_inter_tail[one_far_sender]->data.off_arr.offer_arr[0].offered_cores = (int *) malloc(my_cores_count*sizeof(int));
  2128. j = offer_cores(my_cores, one_app, cur_reg, core_inter_tail[one_far_sender]->data.off_arr.offer_arr[0].offered_cores, one_far_sender);
  2129. /*if (j > 0) {
  2130. core_inter_tail[one_far_sender]->data.off_arr.offer_arr[0].num_of_cores = j;
  2131. core_inter_tail[one_far_sender]->data.off_arr.num_of_offers = 1;
  2132. } else {
  2133. core_inter_tail[one_far_sender]->data.off_arr.num_of_offers = 0;
  2134. free(core_inter_tail[one_far_sender]->data.off_arr.offer_arr);
  2135. core_inter_tail[one_far_sender]->data.off_arr.offer_arr = NULL;
  2136. }*/
  2137. my_stats.comp_effort++;
  2138. core_inter_tail[one_far_sender]->data.off_arr.offer_arr[0].num_of_cores = j;
  2139. //gia na einai sumvato me to far_list_count=0 se mhdenikh prosfora
  2140. if (j == 0) core_inter_tail[one_far_sender]->data.off_arr.num_of_offers = 0;
  2141. else core_inter_tail[one_far_sender]->data.off_arr.num_of_offers = 1;
  2142. //core_inter_tail[one_far_sender]->data.off_arr.offer_arr[0].spd_loss = 0.0;
  2143. cores_util = 0;
  2144. for (tmp_DDS = DDS->next; tmp_DDS != NULL; tmp_DDS = tmp_DDS->next)
  2145. cores_util += tmp_DDS->num_of_cores;
  2146. if (cores_util == 0) core_inter_tail[one_far_sender]->data.off_arr.offer_arr[0].spd_loss = -2.0;
  2147. else core_inter_tail[one_far_sender]->data.off_arr.offer_arr[0].spd_loss = (float) (-1 * cores_util) / (my_cores_count-1);
  2148. i=0;
  2149. fprintf(log_file, "I offered in far only %d %d cores: ",one_far_sender,core_inter_tail[one_far_sender]->data.off_arr.offer_arr[i].num_of_cores);
  2150. for (j=0; j<core_inter_tail[one_far_sender]->data.off_arr.offer_arr[i].num_of_cores; j++)
  2151. fprintf(log_file, "%d, ",core_inter_tail[one_far_sender]->data.off_arr.offer_arr[i].offered_cores[j]);
  2152. fprintf(log_file, "\n");
  2153. fflush(log_file);
  2154. //get_reg_idags(cur_reg, core_inter_tail[one_far_sender]->data.idags_in_reg);
  2155. //if (core_inter_head[one_far_sender]->next == NULL) {
  2156. kill(pid_num[one_far_sender], SIG_FAR_REQ);
  2157. my_stats.msg_count++;
  2158. my_stats.distance += distance(node_id,one_far_sender);
  2159. //} else printf("i am %d and Apparently not null interaction in far only=%d\n",node_id,core_inter_head[one_far_sender]->type);
  2160. } else {
  2161. fprintf(log_file,"den to evala I am %d with one_far_sender %d in only my offer and interaction is %d\n",node_id,one_far_sender,core_inter_head[one_far_sender]->type);
  2162. fflush(log_file);
  2163. //core_inter_tail[one_far_sender]->next = (inter_list *) malloc(sizeof(inter_list));
  2164. //core_inter_tail[one_far_sender] = core_inter_tail[one_far_sender]->next;
  2165. }
  2166. //far_man_agent_count = DDS_count;
  2167. //printf("uioy 1 far_list_count = %d far_man_agent_count=%d\n",far_list_count,far_man_agent_count);
  2168. } else printf("I am node with id %d and I am allready far managing %d so i am going to abort re managing him\n",node_id,far_req_or_sender);
  2169. } else printf("I am node with id %d and I am to far check region C=%d r=%d with sender %d\n",node_id,cur_reg.C,cur_reg.r,sender_id);
  2170. //sem_post(&node_sem[node_id]);
  2171. sem_getvalue(&node_sem_out[sender_id],&i);
  2172. fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
  2173. fflush(log_file);
  2174. if (sem_post(&node_sem_out[sender_id]) == -1){
  2175. printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
  2176. perror("sem_post");
  2177. }
  2178. sem_getvalue(&node_sem_out[sender_id],&i);
  2179. fprintf(log_file, "Sem value = %d\n",i);
  2180. fflush(log_file);
  2181. } else if (core_inter_head[sender_id]->type == FAR_INIT_REQ && signo == SIG_INIT_FAR_REQ) {//in original manager change who the far manager is
  2182. sem_getvalue(&node_sem[node_id],&i);
  2183. fprintf(log_file, "In 13 Trying to acquire semaphore. Sem value = %d\n",i);
  2184. fflush(log_file);
  2185. kill(info->si_pid, SIG_ACK);
  2186. sem_wait(&node_sem[node_id]);
  2187. sem_getvalue(&node_sem[node_id],&i);
  2188. fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
  2189. fflush(log_file);
  2190. my_stats.msg_count++;
  2191. my_stats.distance += distance(node_id,sender_id);
  2192. //printf("I am inside with sender_id %d %s\n",sender_id,fifo_name);
  2193. //printf("open13 idag_id=%d node_id=%d\n",idag_id,node_id);
  2194. fd_r = open(fifo_name, O_RDONLY);
  2195. if (fd_r == -1) {
  2196. perror("opening reading pipe");
  2197. signals_enable();
  2198. return;
  2199. }
  2200. read(fd_r, &agent_id, sizeof(int));
  2201. ////sem_post(&node_sem[sender_id]);
  2202. for (tmp_inter_list = core_inter_head[agent_id]; tmp_inter_list != NULL; tmp_inter_list=tmp_inter_list->next)
  2203. if (tmp_inter_list->type == FAR_REQ_MAN) break;
  2204. if (tmp_inter_list == NULL) {
  2205. if (core_inter_head[agent_id] == NULL){
  2206. core_inter_head[agent_id] = (inter_list *) malloc(sizeof(inter_list));
  2207. core_inter_tail[agent_id] = core_inter_head[agent_id];
  2208. } else {
  2209. core_inter_tail[agent_id]->next = (inter_list *) malloc(sizeof(inter_list));
  2210. core_inter_tail[agent_id] = core_inter_tail[agent_id]->next;
  2211. }
  2212. core_inter_tail[agent_id]->type = FAR_REQ_MAN;
  2213. core_inter_tail[agent_id]->data.far_req_man = sender_id;
  2214. core_inter_tail[agent_id]->next = NULL;
  2215. printf("I am node with id %d and my far request manager is %d\n",node_id,agent_id);
  2216. fprintf(log_file,"I am node with id %d and my far request manager is %d\n",node_id,agent_id);
  2217. fflush(log_file);
  2218. } else printf("I am node with id %d my far request manager is %d and i am aborting him being appointed again by sender=%d\n",node_id,agent_id,sender_id);
  2219. /*if (agent_id == sender_id) {
  2220. tmp_inter_prev = NULL;
  2221. tmp_inter_list = core_inter_head[agent_id];//core_inter_head[agent_id]->next;
  2222. } else {
  2223. tmp_inter_prev = core_inter_head[agent_id];
  2224. tmp_inter_list = core_inter_head[agent_id]->next;
  2225. }*/
  2226. tmp_inter_prev = core_inter_head[agent_id];
  2227. tmp_inter_list = core_inter_head[agent_id]->next;
  2228. j = 0;
  2229. while (tmp_inter_list != NULL) {
  2230. if (tmp_inter_list->type == FAR_INIT_REQ || tmp_inter_list->type == IDAG_REQ_DDS_PENDING) {
  2231. if (tmp_inter_list->type == IDAG_REQ_DDS_PENDING){
  2232. init_DDS_replies++;
  2233. if (init_DDS_idags == init_DDS_replies && init_idags_areas_replies == init_areas_num) j=1; //send_init_reqs(sender_id);
  2234. }
  2235. fprintf(log_file, "I dismissed type = %d of sender = %d\n",tmp_inter_list->type,agent_id);
  2236. fflush(log_file);
  2237. if (tmp_inter_prev == NULL) {
  2238. core_inter_head[agent_id] = core_inter_head[agent_id]->next;
  2239. free(tmp_inter_list);
  2240. tmp_inter_list = core_inter_head[agent_id];
  2241. } else {
  2242. tmp_inter_prev->next = tmp_inter_list->next;
  2243. if (tmp_inter_prev->next == NULL) core_inter_tail[agent_id] = tmp_inter_prev;
  2244. free(tmp_inter_list);
  2245. tmp_inter_list = tmp_inter_prev->next;
  2246. }
  2247. }
  2248. else tmp_inter_list = tmp_inter_list->next;
  2249. }
  2250. if (j) send_init_reqs(sender_id);
  2251. //sem_post(&node_sem[node_id]);
  2252. sem_getvalue(&node_sem_out[sender_id],&i);
  2253. fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
  2254. fflush(log_file);
  2255. if (sem_post(&node_sem_out[sender_id]) == -1){
  2256. printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
  2257. perror("sem_post");
  2258. }
  2259. sem_getvalue(&node_sem_out[sender_id],&i);
  2260. fprintf(log_file, "Sem value = %d\n",i);
  2261. fflush(log_file);
  2262. tmp_inter_list = core_inter_head[sender_id];
  2263. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  2264. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  2265. else send_next_signal(core_inter_head[sender_id], sender_id);
  2266. free(tmp_inter_list);
  2267. } else if (core_inter_head[sender_id]->type == FAR_REQ_MAN && signo == SIG_FAR_REQ
  2268. && state != IDLE_INIT_MAN && state != IDLE_INIT_MAN_SELFOPT_PENDING && state != IDLE_INIT_MAN_WORK_PENDING) {
  2269. printf("I am %d and i think i cought a stray init far_REQ_CORES reply from %d. My current state is %d\n",node_id,sender_id,state);
  2270. fprintf(log_file,"I am %d and i think i cought a stray init far_REQ_CORES reply from %d. My current state is %d\n",node_id,sender_id,state);
  2271. fflush(log_file);
  2272. kill(info->si_pid, SIG_REJECT);
  2273. my_stats.msg_count++;
  2274. my_stats.distance += distance(node_id,sender_id);
  2275. tmp_inter_list = core_inter_head[sender_id];
  2276. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  2277. free(tmp_inter_list);
  2278. if(core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  2279. else send_next_signal(core_inter_head[sender_id], sender_id);
  2280. } else if (core_inter_head[sender_id]->type == FAR_REQ_MAN && signo == SIG_FAR_REQ) {//in original manager get far offers
  2281. sem_getvalue(&node_sem[node_id],&i);
  2282. fprintf(log_file, "In 14 Trying to acquire semaphore. Sem value = %d\n",i);
  2283. fflush(log_file);
  2284. kill(info->si_pid, SIG_ACK);
  2285. sem_wait(&node_sem[node_id]);
  2286. sem_getvalue(&node_sem[node_id],&i);
  2287. fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
  2288. fflush(log_file);
  2289. my_stats.msg_count++;
  2290. my_stats.distance += distance(node_id,sender_id);
  2291. //printf("I am inside with sender_id %d %s\n",sender_id,fifo_name);
  2292. //printf("open14 idag_id=%d node_id=%d\n",idag_id,node_id);
  2293. fd_r = open(fifo_name, O_RDONLY);
  2294. if (fd_r == -1) {
  2295. perror("opening reading pipe");
  2296. signals_enable();
  2297. return;
  2298. }
  2299. read(fd_r, &num_of_offers, sizeof(int));
  2300. fprintf(log_file,"num of offers = %d\n",num_of_offers);
  2301. fflush(log_file);
  2302. if (num_of_offers > 0) {
  2303. core_inter_head[sender_id]->type = REP_FAR_REQ_OFFER_PENDING;
  2304. core_inter_head[sender_id]->data.offer_acc_array = (int *) malloc((num_of_offers+1)*sizeof(int));
  2305. core_inter_head[sender_id]->data.offer_acc_array[0] = num_of_offers;
  2306. //printf("qweNode %d num_of_offers %d \n",sender_id,num_of_offers);
  2307. for (i=1; i<=num_of_offers; i++){
  2308. read(fd_r, &one_offer.num_of_cores, sizeof(int));
  2309. read(fd_r, &one_offer.spd_loss, sizeof(float));
  2310. one_offer.offered_cores = (int *) malloc(one_offer.num_of_cores*sizeof(int));
  2311. for (j=0; j<one_offer.num_of_cores; j++)
  2312. read(fd_r, &one_offer.offered_cores[j], sizeof(int));
  2313. //printf("qweNode %d is offering %d cores with speedup loss %f\n",sender_id,one_offer.num_of_cores,one_offer.spd_loss);
  2314. core_inter_head[sender_id]->data.offer_acc_array[i] = -1;
  2315. if (init_man_offers == NULL) {
  2316. init_man_offers = (offer_list *) malloc(sizeof(offer_list));
  2317. init_man_offers->off = one_offer;
  2318. init_man_offers->sender = sender_id;
  2319. init_man_offers->answer = &core_inter_head[sender_id]->data.offer_acc_array[i];
  2320. init_man_offers->next = NULL;
  2321. } else {
  2322. tmp_offer_prev = NULL;
  2323. tmp_offer_list = init_man_offers;
  2324. while (tmp_offer_list != NULL && tmp_offer_list->off.num_of_cores >= one_offer.num_of_cores){
  2325. tmp_offer_prev = tmp_offer_list;
  2326. tmp_offer_list = tmp_offer_list->next;
  2327. }
  2328. if (tmp_offer_list == NULL) { //prepei na mpei teleutaio
  2329. tmp_offer_prev->next = (offer_list *) malloc(sizeof(offer_list));
  2330. tmp_offer_list = tmp_offer_prev->next;
  2331. tmp_offer_list->next = NULL;
  2332. tmp_offer_list->off = one_offer;
  2333. tmp_offer_list->sender = sender_id;
  2334. tmp_offer_list->answer = &core_inter_head[sender_id]->data.offer_acc_array[i];
  2335. } else if (tmp_offer_prev == NULL) { //prepei na mpei prwto
  2336. init_man_offers = (offer_list *) malloc(sizeof(offer_list));
  2337. init_man_offers->off = one_offer;
  2338. init_man_offers->sender = sender_id;
  2339. init_man_offers->answer = &core_inter_head[sender_id]->data.offer_acc_array[i];
  2340. init_man_offers->next = tmp_offer_list;
  2341. } else {
  2342. tmp_offer_prev->next = (offer_list *) malloc(sizeof(offer_list));
  2343. tmp_offer_prev = tmp_offer_prev->next;
  2344. tmp_offer_prev->off = one_offer;
  2345. tmp_offer_prev->sender = sender_id;
  2346. tmp_offer_prev->answer = &core_inter_head[sender_id]->data.offer_acc_array[i];
  2347. tmp_offer_prev->next = tmp_offer_list;
  2348. }
  2349. }
  2350. cur_time = time(NULL);
  2351. cur_t = localtime(&cur_time);
  2352. fprintf(log_file, "[%d:%d:%d]: One node added in init_man_offers in sig_FAR_REQ\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec);
  2353. fflush(log_file);
  2354. }
  2355. } else {
  2356. tmp_inter_list = core_inter_head[sender_id];
  2357. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  2358. free(tmp_inter_list);
  2359. if(core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  2360. else send_next_signal(core_inter_head[sender_id], sender_id);
  2361. }
  2362. sem_getvalue(&node_sem_out[sender_id],&i);
  2363. fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
  2364. fflush(log_file);
  2365. if (sem_post(&node_sem_out[sender_id]) == -1){
  2366. printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
  2367. perror("sem_post");
  2368. }
  2369. sem_getvalue(&node_sem_out[sender_id],&i);
  2370. fprintf(log_file, "Sem value = %d\n",i);
  2371. fflush(log_file);
  2372. //sem_post(&node_sem[node_id]);
  2373. ////sem_post(&node_sem[sender_id]);
  2374. } else {
  2375. //printf("I am %d and We were fucked in sig_REQ_DDS_handler from %d interaction is = %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  2376. printf("I am %d and i have to reject sig_far_req from %d. Our interaction is %d sig=%d\n",node_id,sender_id,core_inter_head[sender_id]->type,signo);
  2377. fprintf(log_file,"I am %d and i have to reject sig_far_req from %d. Our interaction is %d sig=%d\n",node_id,sender_id,core_inter_head[sender_id]->type,signo);
  2378. fflush(log_file);
  2379. kill(info->si_pid, SIG_REJECT);
  2380. my_stats.msg_count++;
  2381. my_stats.distance += distance(node_id,sender_id);
  2382. }
  2383. //printf("IIIII I am %d in sig_far_req with sender %d and interaction = %d sig=%d\n",node_id,sender_id,core_inter_head[sender_id]->type,signo);
  2384. //sem_getvalue(&node_sem[node_id],&i);
  2385. //fprintf(log_file, "Sem value = %d\n",i);
  2386. //fflush(log_file);
  2387. if (fd_r != -1) close(fd_r);
  2388. free(fifo_name);
  2389. cur_time = time(NULL);
  2390. cur_t = localtime(&cur_time);
  2391. fprintf(log_file, "[%d:%d:%d]: I ended sig_FAR_REQ_handler with sender=%d and signo=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,signo,state);
  2392. fflush(log_file);
  2393. signals_enable();
  2394. }
  2395. void sig_APPOINT_WORK_handler(int signo, siginfo_t *info, void *context)
  2396. {
  2397. int fd_r, sender_id, i, valid=0;
  2398. char *fifo_name;
  2399. // inter_list *tmp_inter_list;
  2400. signals_disable();
  2401. sender_id = get_id_from_pid(info->si_pid);
  2402. fifo_name = get_pipe_name(node_id);
  2403. cur_time = time(NULL);
  2404. cur_t = localtime(&cur_time);
  2405. fprintf(log_file, "[%d:%d:%d]: I entered sig_APPOINT_WORK_handler with sender=%d and state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  2406. fflush(log_file);
  2407. if (core_inter_head[sender_id] != NULL) {
  2408. //else {
  2409. printf("I am %d and i in am doing smth else with my agent %d in sig_APPOINT_WORK_handler interaction is %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  2410. /*tmp_inter_list = core_inter_head[sender_id];
  2411. while (tmp_inter_list != NULL) {
  2412. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  2413. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  2414. fprintf(log_file, "I dismissed type = %d of sender = %d\n",tmp_inter_list->type,sender_id);
  2415. fflush(log_file);
  2416. free(tmp_inter_list);
  2417. tmp_inter_list = core_inter_head[sender_id];
  2418. }*/
  2419. }
  2420. sem_getvalue(&node_sem[node_id],&i);
  2421. fprintf(log_file, "In 15 Trying to acquire semaphore. Sem value = %d\n",i);
  2422. fflush(log_file);
  2423. kill(info->si_pid, SIG_ACK);
  2424. sem_wait(&node_sem[node_id]);
  2425. sem_getvalue(&node_sem[node_id],&i);
  2426. fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
  2427. fflush(log_file);
  2428. my_stats.msg_count++;
  2429. my_stats.distance += distance(node_id,sender_id);
  2430. fd_r = open(fifo_name, O_RDONLY);
  2431. if (fd_r == -1) {
  2432. perror("opening reading pipe");
  2433. signals_enable();
  2434. return;
  2435. } else {
  2436. fprintf(log_file, "I went through open\n");
  2437. fflush(log_file);
  2438. }
  2439. read(fd_r, &valid, sizeof(int));
  2440. fprintf(log_file, "Validity of message = %d\n",valid);
  2441. fflush(log_file);
  2442. if (valid == 1) {
  2443. if (my_agent == -1) read(fd_r, &my_agent, sizeof(int));// || my_agent != sender_id
  2444. else if (my_agent != sender_id) {
  2445. printf("I am %d and i do in appoint work switch of agent with upper_work_bound = %ld\n",node_id,upper_work_bound);
  2446. fprintf(log_file,"I do in appoint work switch of agent with upper_work_bound = %ld\n",upper_work_bound);
  2447. fflush(log_file);
  2448. //if (upper_work_bound > 0) {//case where my former agent has not collected my remaining work time
  2449. if (state == WORKING_NODE || state == WORKING_NODE_IDLE_INIT) {
  2450. if (core_inter_head[my_agent] == NULL){
  2451. core_inter_head[my_agent] = (inter_list *) malloc(sizeof(inter_list));
  2452. core_inter_tail[my_agent] = core_inter_head[my_agent];
  2453. } else {
  2454. core_inter_tail[my_agent]->next = (inter_list *) malloc(sizeof(inter_list));
  2455. core_inter_tail[my_agent] = core_inter_tail[my_agent]->next;
  2456. }
  2457. core_inter_tail[my_agent]->type = REP_CHK_REM_TIME;
  2458. core_inter_tail[my_agent]->data.work_time = upper_work_bound - time_worked;
  2459. core_inter_tail[my_agent]->next = NULL;
  2460. }
  2461. read(fd_r, &my_agent, sizeof(int));
  2462. }
  2463. //else if (state != WORKING_NODE) printf("I have been initialied but i am not working\n");
  2464. read(fd_r, &upper_work_bound, sizeof(long int));
  2465. time_worked=0;
  2466. ////sem_post(&node_sem[sender_id]);
  2467. printf("I am node %d with agent %d I am going to start working for %ld\n",node_id,my_agent,upper_work_bound);
  2468. if (upper_work_bound > 0) {
  2469. if (state == IDLE_INIT_MAN) state = WORKING_NODE_IDLE_INIT;
  2470. else if (state == INIT_MANAGER || state == INIT_MAN_CHK_OFFERS || state == INIT_MANAGER_SEND_OFFERS) {
  2471. //state = IDLE_INIT_MAN_WORK_PENDING;
  2472. pending_state = WORKING_NODE;
  2473. printf("I am node %d and sou milaw gia mallia\n",node_id);
  2474. } else if (state != WORKING_NODE_IDLE_INIT) state = WORKING_NODE;
  2475. } else {
  2476. kill(info->si_pid, SIG_FINISH);
  2477. my_stats.msg_count++;
  2478. my_stats.distance += distance(node_id,sender_id);
  2479. }
  2480. }
  2481. //sem_post(&node_sem[node_id]);
  2482. sem_getvalue(&node_sem_out[sender_id],&i);
  2483. fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
  2484. fflush(log_file);
  2485. if (sem_post(&node_sem_out[sender_id]) == -1){
  2486. printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
  2487. perror("sem_post");
  2488. }
  2489. sem_getvalue(&node_sem_out[sender_id],&i);
  2490. fprintf(log_file, "Sem value = %d\n",i);
  2491. fflush(log_file);
  2492. //} else printf("I am %d and i in am doing smth else with my agent %d in sig_APPOINT_WORK_handler interaction is %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  2493. close(fd_r);
  2494. free(fifo_name);
  2495. cur_time = time(NULL);
  2496. cur_t = localtime(&cur_time);
  2497. fprintf(log_file, "[%d:%d:%d]: I ended sig_APPOINT_WORK_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  2498. fflush(log_file);
  2499. signals_enable();
  2500. }
  2501. void sig_CHECK_REM_TIME_handler(int signo, siginfo_t *info, void *context)
  2502. {
  2503. int fd_r=-1, sender_id, tmp_rem_time, i;
  2504. char *fifo_name=NULL;
  2505. int time_per_node, time_left, time_to_work, one_core;
  2506. core_list *tmp_cores_list;
  2507. inter_list *tmp_inter_list;
  2508. float rem_workld;
  2509. //inter_list tmp_inter_list;
  2510. signals_disable();
  2511. sender_id = get_id_from_pid(info->si_pid);
  2512. fifo_name = get_pipe_name(node_id);
  2513. cur_time = time(NULL);
  2514. cur_t = localtime(&cur_time);
  2515. fprintf(log_file, "[%d:%d:%d]: I entered sig_CHECK_REM_TIME_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  2516. fflush(log_file);
  2517. if (core_inter_head[sender_id] == NULL && sender_id == my_agent){ //edw tha mpei otan exw allaksei agent
  2518. fprintf(log_file, "I sig_CHECK_REM_TIME_handler a\n");
  2519. fflush(log_file);
  2520. if (core_inter_head[sender_id] == NULL){
  2521. core_inter_head[sender_id] = (inter_list *) malloc(sizeof(inter_list));
  2522. core_inter_tail[sender_id] = core_inter_head[sender_id];
  2523. } else {
  2524. core_inter_tail[sender_id]->next = (inter_list *) malloc(sizeof(inter_list));
  2525. core_inter_tail[sender_id] = core_inter_tail[sender_id]->next;
  2526. }
  2527. //fprintf(log_file, "I sig_CHECK_REM_TIME_handler a1\n");
  2528. //fflush(log_file);
  2529. core_inter_tail[sender_id]->type = REP_CHK_REM_TIME;
  2530. core_inter_tail[sender_id]->data.work_time = upper_work_bound - time_worked;
  2531. if (core_inter_tail[sender_id]->data.work_time < 0) core_inter_tail[sender_id]->data.work_time = 0;
  2532. core_inter_tail[sender_id]->next = NULL;
  2533. //fprintf(log_file, "I sig_CHECK_REM_TIME_handler a2\n");
  2534. //fflush(log_file);
  2535. upper_work_bound = 0;
  2536. if (state == WORKING_NODE) state = IDLE_CORE;
  2537. kill(info->si_pid, SIG_CHECK_REM_TIME);
  2538. //fprintf(log_file, "I sig_CHECK_REM_TIME_handler a3\n");
  2539. //fflush(log_file);
  2540. my_stats.msg_count++;
  2541. my_stats.distance += distance(node_id,sender_id);
  2542. //fprintf(log_file, "I sig_CHECK_REM_TIME_handler a4\n");
  2543. //fflush(log_file);
  2544. } else if (core_inter_head[sender_id] == NULL && sender_id != my_agent) {
  2545. //I hope that this is where i end up when my former agent asks me for my remaining work but my original upper_work_bound was 0
  2546. fprintf(log_file, "I sig_CHECK_REM_TIME_handler c\n");
  2547. fflush(log_file);
  2548. if (core_inter_head[sender_id] == NULL){
  2549. core_inter_head[sender_id] = (inter_list *) malloc(sizeof(inter_list));
  2550. core_inter_tail[sender_id] = core_inter_head[sender_id];
  2551. } else {
  2552. core_inter_tail[sender_id]->next = (inter_list *) malloc(sizeof(inter_list));
  2553. core_inter_tail[sender_id] = core_inter_tail[sender_id]->next;
  2554. }
  2555. core_inter_tail[sender_id]->type = REP_CHK_REM_TIME;
  2556. core_inter_tail[sender_id]->data.work_time = 0;
  2557. core_inter_tail[sender_id]->next = NULL;
  2558. kill(info->si_pid, SIG_CHECK_REM_TIME);
  2559. my_stats.msg_count++;
  2560. my_stats.distance += distance(node_id,sender_id);
  2561. } else if (core_inter_head[sender_id]->type == REP_CHK_REM_TIME) {
  2562. fprintf(log_file, "I sig_CHECK_REM_TIME_handler b\n");
  2563. fflush(log_file);
  2564. kill(info->si_pid, SIG_CHECK_REM_TIME);
  2565. my_stats.msg_count++;
  2566. my_stats.distance += distance(node_id,sender_id);
  2567. } else if (core_inter_head[sender_id]->type == APPOINT_WORK_NODE_PENDING || core_inter_head[sender_id]->type == REMOVED_NODE_REM_TIME) {//CHK_REM_TIME
  2568. //printf("I an in here node_id=%d\n",node_id);
  2569. sem_getvalue(&node_sem[node_id],&i);
  2570. fprintf(log_file, "In 16 Trying to acquire semaphore. Sem value = %d\n",i);
  2571. fflush(log_file);
  2572. kill(info->si_pid, SIG_ACK);
  2573. sem_wait(&node_sem[node_id]);
  2574. sem_getvalue(&node_sem[node_id],&i);
  2575. fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
  2576. fflush(log_file);
  2577. my_stats.msg_count++;
  2578. my_stats.distance += distance(node_id,sender_id);
  2579. fd_r = open(fifo_name, O_RDONLY);
  2580. //printf("I come through here\n");
  2581. if (fd_r == -1) {
  2582. perror("opening reading pipe");
  2583. signals_enable();
  2584. return;
  2585. }
  2586. if (app_state != RESIZING) {
  2587. printf("I am %d in check rem and app_state = %d app_id = %d\n",node_id,app_state,my_app.id);
  2588. fprintf(log_file,"I am in check rem and app_state = %d app_id = %d\n",app_state,my_app.id);
  2589. fflush(log_file);
  2590. }
  2591. read(fd_r, &tmp_rem_time, sizeof(int));
  2592. fprintf(app_log_file,"tmp_rem_time = %d sender_id = %d\n",tmp_rem_time,sender_id);
  2593. fflush(app_log_file);
  2594. ////sem_post(&node_sem[sender_id]);
  2595. my_stats.app_turnaround -= tmp_rem_time;
  2596. sum_rem_time += tmp_rem_time;
  2597. if (++chk_rem_count == chk_rem_num){
  2598. nodes_ended_cnt = 0;
  2599. rem_workld = sum_rem_time * old_Speedup;
  2600. time_to_work = (int) roundf(rem_workld / my_Speedup);
  2601. if (my_cores_count > 1) {
  2602. time_per_node = time_to_work / (my_cores_count-1);
  2603. time_left = time_to_work % (my_cores_count-1);
  2604. printf("i am %d in check_rem. sum_rem_time =%d rem_workld = %0.2f time_to_work =%d time_per_node = %d time_left = %d, my_cores_count = %d\n",
  2605. node_id,sum_rem_time,rem_workld, time_to_work,time_per_node,time_left,my_cores_count);
  2606. fprintf(app_log_file,"i am in check_rem. sum_rem_time =%d rem_workld = %0.2f time_to_work =%d time_per_node = %d time_left = %d, my_cores_count = %d\n",
  2607. sum_rem_time,rem_workld, time_to_work,time_per_node,time_left,my_cores_count);
  2608. fflush(app_log_file);
  2609. if (rem_workld > 0.0) {
  2610. if (time_to_work == 0) time_left++;
  2611. tmp_cores_list = my_cores->next;
  2612. while (tmp_cores_list != NULL) {
  2613. one_core = tmp_cores_list->core_id;
  2614. if (core_inter_head[one_core]->type != APPOINT_WORK_NODE_PENDING && core_inter_head[one_core]->type != INIT_WORK_NODE_PENDING) {
  2615. printf("Wrong fucking interaction core = %d interaction = %d\n",one_core,core_inter_head[one_core]->type);
  2616. tmp_inter_list = core_inter_head[one_core];
  2617. while (tmp_inter_list != NULL) {
  2618. if (tmp_inter_list->type == APPOINT_WORK_NODE_PENDING || tmp_inter_list->type == INIT_WORK_NODE_PENDING) break;
  2619. tmp_inter_list = tmp_inter_list->next;
  2620. }
  2621. if (tmp_inter_list == NULL) printf("I am %d and mas ksekwliase o %d\n",node_id,one_core);
  2622. } else tmp_inter_list = core_inter_head[one_core];
  2623. if (tmp_inter_list->type == APPOINT_WORK_NODE_PENDING) tmp_inter_list->type = APPOINT_WORK_NODE;
  2624. else tmp_inter_list->type = INIT_WORK_NODE;
  2625. tmp_inter_list->data.work_time = time_per_node;
  2626. if (time_left > 0) {
  2627. tmp_inter_list->data.work_time++;
  2628. time_left--;
  2629. }
  2630. if (tmp_inter_list->data.work_time > 0) {
  2631. my_stats.app_turnaround += tmp_inter_list->data.work_time;
  2632. //printf("sending core with id=%d\n",one_core);
  2633. if (core_inter_head[one_core]->type == APPOINT_WORK_NODE || core_inter_head[one_core]->type == INIT_WORK_NODE) {//||next == NULL
  2634. kill(pid_num[one_core], SIG_APPOINT_WORK);
  2635. my_stats.msg_count++;
  2636. my_stats.distance += distance(node_id,one_core);
  2637. } else printf("I am %d doing smth else with my working node %d in re appointing inter1 = %d inter2 = %d\n",node_id,one_core,core_inter_head[one_core]->type
  2638. ,core_inter_head[one_core]->next->type);
  2639. } else nodes_ended_cnt++;
  2640. /*core_inter_head[one_core]->data.work_time = time_per_node;
  2641. if (time_left > 0) {
  2642. core_inter_head[one_core]->data.work_time++;
  2643. time_left--;
  2644. }
  2645. if (core_inter_head[one_core]->data.work_time > 0) {
  2646. my_stats.app_turnaround += core_inter_head[one_core]->data.work_time;
  2647. //printf("sending core with id=%d\n",one_core);
  2648. if (core_inter_head[one_core]->next == NULL) {
  2649. kill(pid_num[one_core], SIG_APPOINT_WORK);
  2650. my_stats.msg_count++;
  2651. my_stats.distance += distance(node_id,one_core);
  2652. } else printf("I am %d doing smth else with my working node %d in re appointing inter1 = %d inter2 = %d\n",node_id,one_core,core_inter_head[one_core]->type
  2653. ,core_inter_head[one_core]->next->type);
  2654. } else nodes_ended_cnt++;*/
  2655. tmp_cores_list = tmp_cores_list->next;
  2656. }
  2657. app_state = RUNNING;
  2658. } else {
  2659. //app_terminated = 1;
  2660. app_state = APP_TERMINATED;
  2661. //state = AGENT_ENDING;
  2662. }
  2663. } else {
  2664. printf("I am %d and i am so stupid that i offered my only working core\n",node_id);
  2665. fprintf(app_log_file,"I have only one core state = %d and pending_state = %d\n",state,pending_state);
  2666. fflush(app_log_file);
  2667. if (selfopt_time_rem == -1 && state == IDLE_AGENT) state = AGENT_REWIND_FILE;
  2668. else if (pending_state == IDLE_AGENT) pending_state = AGENT_REWIND_FILE;
  2669. }
  2670. /*if (app_terminated) {
  2671. for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next)
  2672. if (core_inter_head[tmp_cores_list->core_id]->type == APPOINT_WORK_NODE_PENDING || core_inter_head[tmp_cores_list->core_id]->type == INIT_WORK_NODE_PENDING){
  2673. tmp_inter_list = core_inter_head[tmp_cores_list->core_id];
  2674. core_inter_head[tmp_cores_list->core_id] = core_inter_head[tmp_cores_list->core_id]->next;
  2675. free(tmp_inter_list);
  2676. printf("I am %d and i pre freed node %d\n",node_id,tmp_cores_list->core_id);
  2677. } else printf("I am %d and i tried to pre free but my interaction with my working node %d is %d\n",
  2678. node_id,tmp_cores_list->core_id,core_inter_head[tmp_cores_list->core_id]->type);
  2679. } else {*/
  2680. if (app_state != APP_TERMINATED) {//!app_terminated
  2681. if (pending_state == INIT_MANAGER) {
  2682. state = INIT_MANAGER;
  2683. pending_state = IDLE_AGENT;
  2684. } else {
  2685. fprintf(app_log_file, "I enter here with app_state = %d.\n",app_state);
  2686. fflush(app_log_file);
  2687. if (selfopt_interval > 0) {
  2688. if (my_cores_count == max_cores_count) {
  2689. fprintf(app_log_file, "I have maximum cores count. I don't initiate selfopt process.\n");
  2690. fflush(app_log_file);
  2691. selfopt_time_rem = -1;
  2692. } else if (time_per_node <= (selfopt_interval / 2) && my_cores_count > 1) {
  2693. fprintf(app_log_file, "I have little working time left time_per_node=%d selfopt_interval=%d.\n",time_per_node,selfopt_interval);
  2694. fflush(app_log_file);
  2695. selfopt_time_rem = -1;
  2696. } else {
  2697. its.it_value.tv_nsec = selfopt_interval * MS;
  2698. selfopt_time_rem = selfopt_interval;
  2699. if (timer_settime(timerid, 0, &its, NULL) == -1) {
  2700. perror("timer_settime error8");
  2701. printf("I am %d timer_settime error8 selfopt_interval = %d\n",node_id,selfopt_interval);
  2702. }
  2703. }
  2704. } else selfopt_time_rem = -1;
  2705. }
  2706. }
  2707. }
  2708. if (core_inter_head[sender_id]->type == REMOVED_NODE_REM_TIME) {
  2709. fprintf(log_file,"I am with sender_id = %d and a\n",sender_id);
  2710. fflush(log_file);
  2711. tmp_inter_list = core_inter_head[sender_id];
  2712. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  2713. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  2714. else send_next_signal(core_inter_head[sender_id], sender_id);
  2715. free(tmp_inter_list);
  2716. fprintf(log_file,"I am with sender_id = %d and b\n",sender_id);
  2717. fflush(log_file);
  2718. }
  2719. //printf("I am node %d my agent is %d and I am going to start working\n",node_id,my_agent);
  2720. //sem_post(&node_sem[node_id]);
  2721. sem_getvalue(&node_sem_out[sender_id],&i);
  2722. fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
  2723. fflush(log_file);
  2724. if (sem_post(&node_sem_out[sender_id]) == -1){
  2725. printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
  2726. perror("sem_post");
  2727. }
  2728. sem_getvalue(&node_sem_out[sender_id],&i);
  2729. fprintf(log_file, "Sem value = %d\n",i);
  2730. fflush(log_file);
  2731. } else if (sender_id == my_agent){
  2732. printf("I am %d and i am doing smth else with my agent %d in sig_CHECK_REM_TIME_handler interaction=%d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  2733. fprintf(log_file,"I am doing smth else with my agent %d in sig_CHECK_REM_TIME_handler interaction=%d\n",sender_id,core_inter_head[sender_id]->type);
  2734. fflush(log_file);
  2735. core_inter_tail[sender_id]->next = (inter_list *) malloc(sizeof(inter_list));
  2736. core_inter_tail[sender_id] = core_inter_tail[sender_id]->next;
  2737. core_inter_tail[sender_id]->type = REP_CHK_REM_TIME;
  2738. core_inter_tail[sender_id]->data.work_time = upper_work_bound - time_worked;
  2739. if (core_inter_tail[sender_id]->data.work_time < 0) core_inter_tail[sender_id]->data.work_time = 0;
  2740. core_inter_tail[sender_id]->next = NULL;
  2741. //fprintf(log_file, "I sig_CHECK_REM_TIME_handler a2\n");
  2742. //fflush(log_file);
  2743. upper_work_bound = 0;
  2744. if (state == WORKING_NODE) state = IDLE_CORE;
  2745. } else {
  2746. printf("I am %d and i am doing smth else with random node %d in sig_CHECK_REM_TIME_handler interaction=%d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  2747. fprintf(log_file,"I am doing smth else with random node %d in sig_CHECK_REM_TIME_handler interaction=%d\n",sender_id,core_inter_head[sender_id]->type);
  2748. fflush(log_file);
  2749. kill(info->si_pid, SIG_REJECT);
  2750. my_stats.msg_count++;
  2751. my_stats.distance += distance(node_id,sender_id);
  2752. }
  2753. //fprintf(log_file, "I sig_CHECK_REM_TIME_handler a5\n");
  2754. //fflush(log_file);
  2755. if (fd_r != -1) close(fd_r);
  2756. //fprintf(log_file, "I sig_CHECK_REM_TIME_handler a6\n");
  2757. //fflush(log_file);
  2758. free(fifo_name);
  2759. //fprintf(log_file, "I sig_CHECK_REM_TIME_handler a7\n");
  2760. //fflush(log_file);
  2761. cur_time = time(NULL);
  2762. cur_t = localtime(&cur_time);
  2763. fprintf(log_file, "[%d:%d:%d]: I ended sig_CHECK_REM_TIME_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  2764. fflush(log_file);
  2765. signals_enable();
  2766. }
  2767. void sig_FINISH_handler(int signo, siginfo_t *info, void *context)
  2768. {
  2769. int fd_r, i, is_sender_idag, sender_id, new_agent_id;
  2770. int *nodes_to_process, nodes_cnt=0, processed_cnt=0;
  2771. int *tmp_list, tmp_cnt, tmp_idag;
  2772. char *fifo_name;
  2773. core_list *tmp_cores_list;//tmp_cores,
  2774. DDS_list *tmp_DDS,*prev_DDS;
  2775. inter_list *tmp_inter_list;
  2776. sender_id = get_id_from_pid(info->si_pid);
  2777. fifo_name = get_pipe_name(node_id);
  2778. cur_time = time(NULL);
  2779. cur_t = localtime(&cur_time);
  2780. fprintf(log_file, "[%d:%d:%d]: I entered sig_FINISH_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  2781. fflush(log_file);
  2782. //if i am an idag, sig_finish should be proceed instantly, so i am dropping.
  2783. //On the other hand if an common node doesn't set its agent to -1 its not a big deal. (Hopefully)
  2784. if (core_inter_head[sender_id] != NULL && my_idag == -1) {
  2785. //else {
  2786. printf("I am %d in sig finish with sender %d and i don't know what to do with interaction %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  2787. if (my_idag == -1) {
  2788. is_sender_idag = 0;
  2789. for (i=0; i<num_idags; i++)
  2790. if (idag_id_arr[i] == sender_id){
  2791. is_sender_idag = 1;
  2792. break;
  2793. }
  2794. if (!is_sender_idag) {
  2795. tmp_inter_list = core_inter_head[sender_id];
  2796. while (tmp_inter_list != NULL) {
  2797. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  2798. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  2799. fprintf(log_file, "I dismissed type = %d of sender = %d\n",tmp_inter_list->type,sender_id);
  2800. fflush(log_file);
  2801. free(tmp_inter_list);
  2802. tmp_inter_list = core_inter_head[sender_id];
  2803. }
  2804. }
  2805. }
  2806. }
  2807. if (my_idag != -1){
  2808. if (my_agent != -1 && my_agent == sender_id) my_agent = -1;//working node
  2809. if (++nodes_ended_cnt == (my_cores_count - 1)) app_state = APP_TERMINATED;//app_terminated = 1;
  2810. } else {
  2811. sem_getvalue(&node_sem[node_id],&i);
  2812. fprintf(log_file, "In 17 Trying to acquire semaphore. Sem value = %d\n",i);
  2813. fflush(log_file);
  2814. kill(info->si_pid, SIG_ACK);
  2815. sem_wait(&node_sem[node_id]);
  2816. sem_getvalue(&node_sem[node_id],&i);
  2817. fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
  2818. fflush(log_file);
  2819. my_stats.msg_count++;
  2820. my_stats.distance += distance(node_id,sender_id);
  2821. fd_r = open(fifo_name, O_RDONLY);
  2822. if (fd_r == -1) {
  2823. perror("opening reading pipe");
  2824. signals_enable();
  2825. return;
  2826. }
  2827. is_sender_idag = 0;
  2828. for (i=0; i<num_idags; i++)
  2829. if (idag_id_arr[i] == sender_id){
  2830. is_sender_idag = 1;
  2831. break;
  2832. }
  2833. printf("I am %d in sig finish with is_sender_idag = %d\n",node_id,is_sender_idag);
  2834. if (is_sender_idag == 0){
  2835. read(fd_r, &nodes_cnt, sizeof(int));
  2836. nodes_to_process = (int *) malloc(nodes_cnt * sizeof(int));
  2837. for (i=0; i<nodes_cnt; i++)
  2838. read(fd_r, &nodes_to_process[i], sizeof(int));
  2839. ////sem_post(&node_sem[sender_id]);
  2840. tmp_list = (int *) malloc(nodes_cnt * sizeof(int));
  2841. printf("I am node_id=%d Cores to be processed: ",node_id);
  2842. for (i=0; i<nodes_cnt; i++)
  2843. printf(" %d",nodes_to_process[i]);
  2844. printf("\n");
  2845. printf("I am node_id=%d nodes_cnt = %d\n",node_id,nodes_cnt);
  2846. while (processed_cnt < nodes_cnt){
  2847. tmp_cnt = 0;
  2848. tmp_idag = -1;
  2849. for (i=0; i<nodes_cnt; i++){
  2850. if (processed_cnt == nodes_cnt) break;
  2851. if (nodes_to_process[i] == -1) continue;
  2852. else {
  2853. if (tmp_idag == -1) tmp_idag = idag_mask[nodes_to_process[i]];
  2854. if (idag_mask[nodes_to_process[i]] != tmp_idag) continue;
  2855. else {
  2856. tmp_list[tmp_cnt++] = nodes_to_process[i];
  2857. nodes_to_process[i] = -1;
  2858. processed_cnt++;
  2859. }
  2860. }
  2861. }
  2862. if (tmp_idag != node_id){
  2863. if (core_inter_head[tmp_idag] == NULL){
  2864. core_inter_head[tmp_idag] = (inter_list *) malloc(sizeof(inter_list));
  2865. core_inter_tail[tmp_idag] = core_inter_head[tmp_idag];
  2866. } else {
  2867. printf("finish agent DDS not in high priority! Shouldn't I high prioritize? tmp = %d idag = %d\n",tmp_idag,node_id);
  2868. core_inter_tail[tmp_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  2869. core_inter_tail[tmp_idag] = core_inter_tail[tmp_idag]->next;
  2870. }
  2871. printf("node_id = %d In finish tmp_idag = %d tmp_cnt = %d sender_id = %d is_sender_idag=%d tmp_list[0]=%d\n",node_id,tmp_idag,tmp_cnt,sender_id,is_sender_idag,tmp_list[0]);
  2872. core_inter_tail[tmp_idag]->type = REMOVE_APP;
  2873. core_inter_tail[tmp_idag]->data.agent_ended = sender_id;
  2874. /*core_inter_tail[tmp_idag]->data.app_cores = (int *)malloc((tmp_cnt+2)*sizeof(int));
  2875. core_inter_tail[tmp_idag]->data.app_cores[0] = tmp_cnt;//+1;
  2876. core_inter_tail[tmp_idag]->data.app_cores[tmp_cnt+1] = sender_id;
  2877. for (j=1; j<= tmp_cnt; j++)
  2878. core_inter_tail[tmp_idag]->data.app_cores[j] = tmp_list[j-1];
  2879. */
  2880. core_inter_tail[tmp_idag]->next = NULL;
  2881. //if (tmp_idag != 0) why???
  2882. if (core_inter_head[tmp_idag]->next == NULL) {
  2883. kill(pid_num[tmp_idag], SIG_FINISH);
  2884. my_stats.msg_count++;
  2885. my_stats.distance += distance(node_id,tmp_idag);
  2886. }
  2887. } else {
  2888. printf("In FINISH same tmp_idag = %d tmp_cnt = %d nodes_cnt = %d sender_id = %d node_id = %d\n",tmp_idag,tmp_cnt,nodes_cnt,sender_id,node_id);
  2889. new_agent_id = sender_id;
  2890. /*for (i=0; i<tmp_cnt; i++){
  2891. tmp_cores = my_cores;
  2892. while (tmp_cores->core_id != tmp_list[i])//nodes_to_process[i]
  2893. tmp_cores = tmp_cores->next;
  2894. if (tmp_cores->offered_to == new_agent_id) tmp_cores->offered_to = -1;
  2895. }
  2896. DDS->num_of_cores += tmp_cnt;*/
  2897. for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next)
  2898. if (tmp_cores_list->offered_to == new_agent_id) {
  2899. tmp_cores_list->offered_to = -1;
  2900. DDS->num_of_cores++;
  2901. }
  2902. prev_DDS = DDS;
  2903. tmp_DDS = DDS->next;
  2904. while (tmp_DDS != NULL && tmp_DDS->agent_id != new_agent_id){
  2905. prev_DDS = tmp_DDS;
  2906. tmp_DDS = tmp_DDS->next;
  2907. }
  2908. if (tmp_DDS != NULL){
  2909. DDS_count--;
  2910. prev_DDS->next = tmp_DDS->next;
  2911. if (tmp_DDS->next == NULL) DDS_tail = prev_DDS;
  2912. free(tmp_DDS);
  2913. } else printf("Fuck agent that finished is not in my DDS\n");
  2914. printf("My removal of agent complete node_id = %d sender_id=%d\n",node_id,sender_id);
  2915. printf("Number of agents in region = %d\n",DDS_count);
  2916. i=0;
  2917. for (tmp_DDS = DDS; tmp_DDS != NULL; tmp_DDS = tmp_DDS->next){
  2918. printf("Agent no %d is %d with %d cores\n",i,tmp_DDS->agent_id,tmp_DDS->num_of_cores);
  2919. i++;
  2920. }
  2921. }
  2922. }
  2923. free(nodes_to_process);
  2924. free(tmp_list);
  2925. } else {
  2926. /*read(fd_r, &nodes_cnt, sizeof(int));
  2927. printf("I am %d and secondary node cnt by %d is %d\n",node_id,sender_id,nodes_cnt);
  2928. nodes_to_process = (int *) malloc(nodes_cnt * sizeof(int));
  2929. for (i=0; i<nodes_cnt; i++)
  2930. read(fd_r, &nodes_to_process[i], sizeof(int));
  2931. read(fd_r, &new_agent_id, sizeof(int));
  2932. ////sem_post(&node_sem[sender_id]);
  2933. printf("Secondary Cores to be processed of %d by %d: ",new_agent_id,node_id);
  2934. for (i=0; i<nodes_cnt; i++)
  2935. printf(" %d",nodes_to_process[i]);
  2936. printf("\n");
  2937. for (i=0; i<nodes_cnt; i++){
  2938. tmp_cores = my_cores;
  2939. while (tmp_cores->core_id != nodes_to_process[i])//nodes_to_process[i]
  2940. tmp_cores = tmp_cores->next;
  2941. if (tmp_cores->offered_to == new_agent_id) tmp_cores->offered_to = -1;
  2942. }
  2943. DDS->num_of_cores += nodes_cnt;
  2944. */
  2945. read(fd_r, &new_agent_id, sizeof(int));
  2946. printf("I am %d in Secondary sig_finish for %d\n",node_id,new_agent_id);
  2947. for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next)
  2948. if (tmp_cores_list->offered_to == new_agent_id) {
  2949. tmp_cores_list->offered_to = -1;
  2950. DDS->num_of_cores++;
  2951. }
  2952. prev_DDS = DDS;
  2953. tmp_DDS = DDS->next;
  2954. while (tmp_DDS != NULL && tmp_DDS->agent_id != new_agent_id){
  2955. prev_DDS = tmp_DDS;
  2956. tmp_DDS = tmp_DDS->next;
  2957. }
  2958. if (tmp_DDS != NULL){
  2959. DDS_count--;
  2960. prev_DDS->next = tmp_DDS->next;
  2961. if (tmp_DDS->next == NULL) DDS_tail = prev_DDS;
  2962. free(tmp_DDS);
  2963. } else printf("Fuck agent that finished is not in my DDS\n");
  2964. //free(nodes_to_process);
  2965. /*my_stats.times_accessed++;
  2966. printf("I am %d Adding ended well with sender_id=%d!\n",node_id,sender_id);
  2967. printf("Number of agents in region = %d\n",DDS_count);
  2968. printf("Agent no 0 is %d with %d cores\n",DDS->agent_id,DDS->num_of_cores);
  2969. i=1;
  2970. for (tmp_DDS = DDS->next; tmp_DDS != NULL; tmp_DDS = tmp_DDS->next){
  2971. printf("Agent no %d is %d with %d cores\n",i,tmp_DDS->agent_id,tmp_DDS->num_of_cores);
  2972. my_stats.cores_utilized += tmp_DDS->num_of_cores;
  2973. i++;
  2974. }*/
  2975. printf("Secondary removal of agent complete node_id =%d sender_id=%d\n",node_id,sender_id);
  2976. printf("Number of agents in region = %d\n",DDS_count);
  2977. i=0;
  2978. for (tmp_DDS = DDS; tmp_DDS != NULL; tmp_DDS = tmp_DDS->next){
  2979. printf("Agent no %d is %d with %d cores\n",i,tmp_DDS->agent_id,tmp_DDS->num_of_cores);
  2980. i++;
  2981. }
  2982. }
  2983. sem_getvalue(&node_sem_out[sender_id],&i);
  2984. fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
  2985. fflush(log_file);
  2986. if (sem_post(&node_sem_out[sender_id]) == -1){
  2987. printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
  2988. perror("sem_post");
  2989. }
  2990. sem_getvalue(&node_sem_out[sender_id],&i);
  2991. fprintf(log_file, "Sem value = %d\n",i);
  2992. fflush(log_file);
  2993. close(fd_r);
  2994. }
  2995. //sem_post(&node_sem[node_id]);
  2996. //} else printf("I am %d in sig finish with sender %d and i don't know what to do with interaction %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  2997. //free(fifo_name);
  2998. cur_time = time(NULL);
  2999. cur_t = localtime(&cur_time);
  3000. fprintf(log_file, "[%d:%d:%d]: I ended sig_FINISH_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  3001. fflush(log_file);
  3002. signals_enable();
  3003. }
  3004. void sig_REJECT_handler(int signo, siginfo_t *info, void *context)
  3005. {
  3006. int sender_id, agent_id, i, j;//, crit_app_id;//fd_r,
  3007. //char *fifo_name;
  3008. inter_list *tmp_inter_list;
  3009. core_list *tmp_cores_list;
  3010. target_list *tmp_target_list;
  3011. offer_list *tmp_offer_list;
  3012. signals_disable();
  3013. //inter_list tmp_inter_list;
  3014. sender_id = get_id_from_pid(info->si_pid);
  3015. //fifo_name = get_pipe_name(node_id);
  3016. cur_time = time(NULL);
  3017. cur_t = localtime(&cur_time);
  3018. fprintf(log_file, "[%d:%d:%d]: I entered sig_REJECT_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  3019. fflush(log_file);
  3020. if (core_inter_head[sender_id] != NULL){
  3021. fprintf(log_file, "[%d:%d:%d]: Interaction with sender=%d is %d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,core_inter_head[sender_id]->type);
  3022. fflush(log_file);
  3023. }
  3024. if (core_inter_head[sender_id] == NULL){ //edw tha mpei otan exw allaksei agent
  3025. printf("I am %d in sig_reject and i have null interaction with sender %d\n",node_id,sender_id);
  3026. } else if (core_inter_head[sender_id]->type == IDAG_REQ_DDS || core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS || core_inter_head[sender_id]->type == FAR_REQ_IDAG_REQ_DDS
  3027. || core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS_PENDING || core_inter_head[sender_id]->type == IDAG_REQ_DDS_PENDING) {
  3028. if (core_inter_head[sender_id]->type == IDAG_REQ_DDS || core_inter_head[sender_id]->type == IDAG_REQ_DDS_PENDING) {
  3029. init_DDS_replies++;
  3030. fprintf(log_file, "One init_req_dds has been rejected by %d!\n",sender_id);
  3031. fflush(log_file);
  3032. if (init_areas_num == 1) {
  3033. fprintf(log_file, "My only init area was rejected. my state is %d my pending_state = %d\n",state,pending_state);
  3034. fflush(log_file);
  3035. state = INIT_MANAGER_SEND_OFFERS;
  3036. }
  3037. } else if (core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS || core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS_PENDING) {
  3038. selfopt_DDS_replies++;
  3039. fprintf(log_file, "One selfopt_req_dds has been rejected by %d!\n",sender_id);
  3040. fflush(log_file);
  3041. }
  3042. //printf("selfopt_DDS_idags = %d, selfopt_DDS_replies = %d\n",selfopt_DDS_idags,selfopt_DDS_replies);
  3043. if ((core_inter_head[sender_id]->type == IDAG_REQ_DDS || core_inter_head[sender_id]->type == IDAG_REQ_DDS_PENDING)
  3044. && init_DDS_replies == init_DDS_idags && init_idags_areas_replies == init_areas_num)
  3045. for (tmp_target_list = init_targets_head; tmp_target_list != NULL; tmp_target_list = tmp_target_list->next){
  3046. agent_id = tmp_target_list->target;
  3047. for (tmp_inter_list = core_inter_head[agent_id]; tmp_inter_list != NULL; tmp_inter_list=tmp_inter_list->next)
  3048. if (tmp_inter_list->type == FAR_INIT_REQ || tmp_inter_list->type == FAR_REQ_MAN) break;
  3049. if (tmp_inter_list == NULL) {
  3050. if (core_inter_head[agent_id] == NULL){
  3051. core_inter_head[agent_id] = (inter_list *) malloc(sizeof(inter_list));
  3052. core_inter_tail[agent_id] = core_inter_head[agent_id];
  3053. } else {
  3054. core_inter_tail[agent_id]->next = (inter_list *) malloc(sizeof(inter_list));
  3055. core_inter_tail[agent_id] = core_inter_tail[agent_id]->next;
  3056. }
  3057. core_inter_tail[agent_id]->type = AGENT_REQ_CORES_PENDING;
  3058. core_inter_tail[agent_id]->data.reg_arr.num_of_regions = tmp_target_list->num_of_regions;
  3059. core_inter_tail[agent_id]->data.reg_arr.region_arr = (region *) malloc(tmp_target_list->num_of_regions * sizeof(region));
  3060. if (tmp_target_list->target == node_id) printf("Why is this here node_id=%d\n",node_id);
  3061. fprintf(log_file,"Init req target = %d, num_of_regions = %d. Αreas:",tmp_target_list->target,tmp_target_list->num_of_regions);
  3062. for (i=0; i<tmp_target_list->num_of_regions; i++) {
  3063. core_inter_tail[agent_id]->data.reg_arr.region_arr[i] = tmp_target_list->region_arr[i];
  3064. fprintf(log_file," (%d,%d),",tmp_target_list->region_arr[i].C,tmp_target_list->region_arr[i].r);
  3065. }
  3066. fprintf(log_file,"\n");
  3067. fflush(log_file);
  3068. core_inter_tail[agent_id]->next = NULL;
  3069. //kill(pid_num[agent_id], SIG_REQ_CORES);
  3070. if (core_inter_head[agent_id]->next == NULL) {
  3071. kill(pid_num[agent_id], SIG_REQ_CORES);
  3072. my_stats.msg_count++;
  3073. my_stats.distance += distance(node_id,agent_id);
  3074. } else if (agent_id == sender_id && core_inter_head[agent_id]->next->type == AGENT_REQ_CORES_PENDING) { //den exei fugei apo to free alla einai wra na stalei
  3075. kill(pid_num[agent_id], SIG_REQ_CORES);
  3076. my_stats.msg_count++;
  3077. my_stats.distance += distance(node_id,agent_id);
  3078. } else printf("This init fucker is not NULL interaction=%d interaction2=%d\n",core_inter_head[agent_id]->type,core_inter_head[agent_id]->next->type);
  3079. } else
  3080. printf("I am %d and i did not send local requests to %d because he is my far manager\n",node_id,agent_id);
  3081. }
  3082. else if ((core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS || core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS_PENDING) && selfopt_DDS_replies == selfopt_DDS_idags)
  3083. for (tmp_target_list = selfopt_targets_head; tmp_target_list != NULL; tmp_target_list = tmp_target_list->next){
  3084. agent_id = tmp_target_list->target;
  3085. if (core_inter_head[agent_id] == NULL){
  3086. core_inter_head[agent_id] = (inter_list *) malloc(sizeof(inter_list));
  3087. core_inter_tail[agent_id] = core_inter_head[agent_id];
  3088. } else {
  3089. core_inter_tail[agent_id]->next = (inter_list *) malloc(sizeof(inter_list));
  3090. core_inter_tail[agent_id] = core_inter_tail[agent_id]->next;
  3091. }
  3092. core_inter_tail[agent_id]->type = SELFOPT_REQ_CORES_PENDING;
  3093. core_inter_tail[agent_id]->data.reg_arr.num_of_regions = tmp_target_list->num_of_regions;
  3094. core_inter_tail[agent_id]->data.reg_arr.region_arr = (region *) malloc(tmp_target_list->num_of_regions * sizeof(region));
  3095. if (tmp_target_list->target == node_id) printf("selfopt Why is this here node_id=%d\n",node_id);
  3096. fprintf(log_file,"Selfopt req target = %d, num_of_regions = %d. Αreas:",tmp_target_list->target,tmp_target_list->num_of_regions);
  3097. for (i=0; i<tmp_target_list->num_of_regions; i++) {
  3098. core_inter_tail[agent_id]->data.reg_arr.region_arr[i] = tmp_target_list->region_arr[i];
  3099. fprintf(log_file," (%d,%d),",tmp_target_list->region_arr[i].C,tmp_target_list->region_arr[i].r);
  3100. }
  3101. fprintf(log_file,"\n");
  3102. fflush(log_file);
  3103. core_inter_tail[agent_id]->next = NULL;
  3104. if (core_inter_head[agent_id]->next == NULL) {
  3105. kill(pid_num[agent_id], SIG_REQ_CORES);
  3106. my_stats.msg_count++;
  3107. my_stats.distance += distance(node_id,agent_id);
  3108. } else if (agent_id == sender_id && core_inter_head[agent_id]->next->type == SELFOPT_REQ_CORES_PENDING) {
  3109. kill(pid_num[agent_id], SIG_REQ_CORES);
  3110. my_stats.msg_count++;
  3111. my_stats.distance += distance(node_id,agent_id);
  3112. } else printf("This selfopt fucker is not NULL interaction=%d interaction2=%d\n",core_inter_head[agent_id]->type,core_inter_head[agent_id]->next->type);
  3113. }
  3114. tmp_inter_list = core_inter_head[sender_id];
  3115. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  3116. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  3117. else if (core_inter_head[sender_id]->type != AGENT_REQ_CORES_PENDING && core_inter_head[sender_id]->type != SELFOPT_REQ_CORES_PENDING)//far_req_max_man != sender_id &&
  3118. send_next_signal(core_inter_head[sender_id], sender_id);
  3119. free(tmp_inter_list);
  3120. } else if (core_inter_head[sender_id]->type == FAR_INIT_IDAG_REQ_DDS || core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS || core_inter_head[sender_id]->type == FAR_REQ_CORES
  3121. || core_inter_head[sender_id]->type == AGENT_REQ_CORES_PENDING || core_inter_head[sender_id]->type == FAR_REQ_IDAG_REQ_DDS_PENDING
  3122. || core_inter_head[sender_id]->type == IDAG_FIND_IDAGS_PENDING || core_inter_head[sender_id]->type == SELFOPT_IDAG_FIND_IDAGS_PENDING
  3123. || core_inter_head[sender_id]->type == SELFOPT_REQ_CORES_PENDING || core_inter_head[sender_id]->type == FAR_REQ_CORES_PENDING
  3124. || core_inter_head[sender_id]->type == REP_FAR_INIT_REQ) {
  3125. tmp_inter_list = core_inter_head[sender_id];
  3126. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  3127. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  3128. else send_next_signal(core_inter_head[sender_id], sender_id);
  3129. free(tmp_inter_list);
  3130. } else if (core_inter_head[sender_id]->type == FAR_REQ_OFFER) {//FAR_REQ_OFFER_SENT
  3131. if (far_man_offers != NULL && far_man_offers->sender == node_id) {
  3132. tmp_cores_list = my_cores;
  3133. while (tmp_cores_list != NULL){
  3134. if (tmp_cores_list->offered_to == sender_id) tmp_cores_list->offered_to = -1;
  3135. tmp_cores_list = tmp_cores_list->next;
  3136. }
  3137. tmp_offer_list = far_man_offers;
  3138. far_man_offers = far_man_offers->next;
  3139. free(tmp_offer_list);
  3140. }
  3141. while (far_man_offers != NULL){
  3142. printf("I am node %d in reject and far offer answer for node %d is 0\n",node_id,far_man_offers->sender);
  3143. *far_man_offers->answer = 0;
  3144. if (core_inter_head[far_man_offers->sender]->type == REP_AGENT_OFFER_PENDING){
  3145. kill(pid_num[far_man_offers->sender],SIG_REP_OFFERS);
  3146. core_inter_head[far_man_offers->sender]->type = REP_AGENT_OFFER_SENT;
  3147. } else printf("I am %d and Apparently lists are poutana ola\n",node_id);
  3148. tmp_offer_list = far_man_offers;
  3149. far_man_offers = far_man_offers->next;
  3150. free(tmp_offer_list);
  3151. }
  3152. far_req_app.A=-1.0;
  3153. far_req_app.var=-1.0;
  3154. far_req_app.num_of_cores=-1;
  3155. far_req_or_sender = -1;
  3156. far_reg.C = -1;
  3157. far_reg.r = -1;
  3158. tmp_inter_list = core_inter_head[sender_id];
  3159. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  3160. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  3161. else send_next_signal(core_inter_head[sender_id], sender_id);
  3162. free(tmp_inter_list);
  3163. } else if (core_inter_head[sender_id]->type == REP_IDAG_FIND_IDAGS) {
  3164. free(core_inter_head[sender_id]->data.idags_in_reg);
  3165. tmp_inter_list = core_inter_head[sender_id];
  3166. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  3167. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  3168. else send_next_signal(core_inter_head[sender_id], sender_id);
  3169. free(tmp_inter_list);
  3170. } else if (core_inter_head[sender_id]->type == REP_IDAG_REQ_DDS) {
  3171. free(core_inter_head[sender_id]->data.agents_in_reg);
  3172. tmp_inter_list = core_inter_head[sender_id];
  3173. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  3174. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  3175. else send_next_signal(core_inter_head[sender_id], sender_id);
  3176. free(tmp_inter_list);
  3177. } else if (core_inter_head[sender_id]->type == FAR_REQ_MAN_APPOINT) {
  3178. agent_id = core_inter_head[sender_id]->data.far_req.orig_sender;
  3179. if (core_inter_head[agent_id] == NULL){
  3180. core_inter_head[agent_id] = (inter_list *) malloc(sizeof(inter_list));
  3181. core_inter_tail[agent_id] = core_inter_head[agent_id];
  3182. } else {
  3183. core_inter_tail[agent_id]->next = (inter_list *) malloc(sizeof(inter_list));
  3184. core_inter_tail[agent_id] = core_inter_tail[agent_id]->next;
  3185. }
  3186. core_inter_tail[agent_id]->type = ABORT_FAR_MAN;
  3187. core_inter_tail[agent_id]->data.far_req_man = sender_id;
  3188. core_inter_tail[agent_id]->next = NULL;
  3189. if (core_inter_head[agent_id]->next == NULL) {
  3190. kill(pid_num[agent_id], SIG_REMOVE_FAR_MAN);
  3191. my_stats.msg_count++;
  3192. my_stats.distance += distance(node_id,agent_id);
  3193. }
  3194. tmp_inter_list = core_inter_head[sender_id];
  3195. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  3196. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  3197. else send_next_signal(core_inter_head[sender_id], sender_id);
  3198. free(tmp_inter_list);
  3199. } else if (core_inter_head[sender_id]->type == REP_AGENT_REQ_CORES) {
  3200. for (j=0; j<core_inter_head[sender_id]->data.off_arr.num_of_offers; j++)
  3201. if (my_cores != NULL)
  3202. for (i=0; i<core_inter_head[sender_id]->data.off_arr.offer_arr[j].num_of_cores; i++)
  3203. for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next)
  3204. if (tmp_cores_list->core_id == core_inter_head[sender_id]->data.off_arr.offer_arr[j].offered_cores[i]) {
  3205. tmp_cores_list->offered_to = -1;
  3206. break;
  3207. }
  3208. free(core_inter_head[sender_id]->data.off_arr.offer_arr);
  3209. tmp_inter_list = core_inter_head[sender_id];
  3210. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  3211. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  3212. else send_next_signal(core_inter_head[sender_id], sender_id);
  3213. free(tmp_inter_list);
  3214. } else if (core_inter_head[sender_id]->type == INIT_APP) {
  3215. printf("I am 0 and %d rejected my init_app req\n",sender_id);
  3216. fprintf(log_file,"I am 0 and %d rejected my init_app req\n",sender_id);
  3217. fflush(log_file);
  3218. if (init_pending_head == NULL){
  3219. init_pending_head = (inter_list *) malloc(sizeof(inter_list));
  3220. init_pending_tail = init_pending_head;
  3221. } else {
  3222. init_pending_tail->next = (inter_list *) malloc(sizeof(inter_list));
  3223. init_pending_tail = init_pending_tail->next;
  3224. }
  3225. init_pending_tail->type = INIT_APP;
  3226. init_pending_tail->data.new_app = core_inter_head[sender_id]->data.new_app;
  3227. init_pending_tail->data.new_app.num_of_cores = sender_id;
  3228. init_pending_tail->next = NULL;
  3229. tmp_inter_list = core_inter_head[sender_id];
  3230. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  3231. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  3232. else send_next_signal(core_inter_head[sender_id], sender_id);
  3233. free(tmp_inter_list);
  3234. } else if (core_inter_head[sender_id]->type == REP_CHK_REM_TIME) {
  3235. printf("I am %d and %d rejected my REP_CHK_REM_TIME with work_time = %d\n",node_id,sender_id,core_inter_head[sender_id]->data.work_time);
  3236. fprintf(log_file,"I am and %d rejected my REP_CHK_REM_TIME with work_time = %d\n",sender_id,core_inter_head[sender_id]->data.work_time);
  3237. fflush(log_file);
  3238. tmp_inter_list = core_inter_head[sender_id];
  3239. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  3240. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  3241. else send_next_signal(core_inter_head[sender_id], sender_id);
  3242. free(tmp_inter_list);
  3243. } else printf("I am %d in sig_reject and i have interaction with sender %d interaction = %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  3244. cur_time = time(NULL);
  3245. cur_t = localtime(&cur_time);
  3246. fprintf(log_file, "[%d:%d:%d]: I ended sig_REJECT_handler with sender = %d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  3247. fflush(log_file);
  3248. //fprintf(log_file, "[%ld:%ld:%ld:%ld]: my idle agent is %d and my pid is %d\n",tv.tv_sec/3600,tv.tv_sec%3600/60,tv.tv_sec%3600%60,(long int) tv.tv_usec,my_idag,getpid());
  3249. //close(fd_r);
  3250. //free(fifo_name);
  3251. signals_enable();
  3252. /*signals_disable();
  3253. state = TERMINATED;
  3254. signals_enable();*/
  3255. }
  3256. void sig_REMOVE_FAR_MAN_handler(int signo, siginfo_t *info, void *context)
  3257. {
  3258. int fd_r, sender_id, i, my_agent;
  3259. char *fifo_name;
  3260. inter_list *tmp_inter_list, *tmp_inter_prev;
  3261. signals_disable();
  3262. sender_id = get_id_from_pid(info->si_pid);
  3263. fifo_name = get_pipe_name(node_id);
  3264. cur_time = time(NULL);
  3265. cur_t = localtime(&cur_time);
  3266. fprintf(log_file, "[%d:%d:%d]: I entered sig_REMOVE_FAR_MAN_handler with sender=%d and state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  3267. fflush(log_file);
  3268. if (core_inter_head[sender_id] != NULL) {
  3269. printf("I am %d and i in am doing smth else with my agent %d in sig_REMOVE_FAR_MAN_handler interaction is %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  3270. /*tmp_inter_list = core_inter_head[sender_id];
  3271. while (tmp_inter_list != NULL) {
  3272. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  3273. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  3274. fprintf(log_file, "I dismissed type = %d of sender = %d\n",tmp_inter_list->type,sender_id);
  3275. fflush(log_file);
  3276. free(tmp_inter_list);
  3277. tmp_inter_list = core_inter_head[sender_id];
  3278. }*/
  3279. }
  3280. sem_getvalue(&node_sem[node_id],&i);
  3281. fprintf(log_file, "In REMOVE_FAR_MAN Trying to acquire semaphore. Sem value = %d\n",i);
  3282. fflush(log_file);
  3283. kill(info->si_pid, SIG_ACK);
  3284. sem_wait(&node_sem[node_id]);
  3285. sem_getvalue(&node_sem[node_id],&i);
  3286. fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
  3287. fflush(log_file);
  3288. my_stats.msg_count++;
  3289. my_stats.distance += distance(node_id,sender_id);
  3290. fd_r = open(fifo_name, O_RDONLY);
  3291. if (fd_r == -1) {
  3292. perror("opening reading pipe");
  3293. signals_enable();
  3294. return;
  3295. } /*else {
  3296. fprintf(log_file, "I went through open\n");
  3297. fflush(log_file);
  3298. }*/
  3299. read(fd_r, &my_agent, sizeof(int));// || my_agent != sender_id
  3300. tmp_inter_prev = NULL;
  3301. tmp_inter_list = core_inter_head[my_agent];
  3302. while (tmp_inter_list != NULL) {
  3303. //the far manager being clear had to be the one tha has been appointed by sender
  3304. if (tmp_inter_list->type == FAR_REQ_MAN && tmp_inter_list->data.far_req_man == sender_id) {
  3305. fprintf(log_file, "I dismissed type = %d of node %d sender = %d\n",tmp_inter_list->type,my_agent,sender_id);
  3306. fflush(log_file);
  3307. if (tmp_inter_prev == NULL) {
  3308. core_inter_head[my_agent] = core_inter_head[my_agent]->next;
  3309. if (core_inter_head[my_agent] == NULL) core_inter_tail[my_agent] = NULL;
  3310. else send_next_signal(core_inter_head[my_agent], my_agent);
  3311. } else {
  3312. tmp_inter_prev->next = tmp_inter_list->next;
  3313. if (tmp_inter_prev->next == NULL) core_inter_tail[my_agent] = tmp_inter_prev;
  3314. }
  3315. free(tmp_inter_list);
  3316. break;
  3317. //tmp_inter_list = tmp_inter_prev->next;
  3318. }
  3319. else tmp_inter_list = tmp_inter_list->next;
  3320. }
  3321. //sem_post(&node_sem[node_id]);
  3322. sem_getvalue(&node_sem_out[sender_id],&i);
  3323. fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
  3324. fflush(log_file);
  3325. if (sem_post(&node_sem_out[sender_id]) == -1){
  3326. printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
  3327. perror("sem_post");
  3328. }
  3329. sem_getvalue(&node_sem_out[sender_id],&i);
  3330. fprintf(log_file, "Sem value = %d\n",i);
  3331. fflush(log_file);
  3332. //} else printf("I am %d and i in am doing smth else with my agent %d in sig_APPOINT_WORK_handler interaction is %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  3333. close(fd_r);
  3334. free(fifo_name);
  3335. cur_time = time(NULL);
  3336. cur_t = localtime(&cur_time);
  3337. fprintf(log_file, "[%d:%d:%d]: I ended REMOVE_FAR_MAN with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  3338. fflush(log_file);
  3339. signals_enable();
  3340. }