arm, NULL);
        swapcontext(&mainContext, &table[nextThread].context);              //swap context
    }
    //only stop when there are no more threads to run!  
    while(!threads_completed);
}
/**
 * This function creates a semaphore and sets its initial value to the given parameter. 
 *
 * @param value initial semaphore value
 */
int create_semaphore(int value){
    // handle error as usual if value is less than 0
    if(value < 0)
           handle_error("semaphore initialization failed - value less than 0.");
    else{
        semaphores[semaphoresIndex].initialValue = value;                               //set initial value to value
        semaphores[semaphoresIndex].value = semaphores[semaphoresIndex].initialValue;   //set value
        semaphores[semaphoresIndex].active = 1;                                         // make it ative now
        semaphores[semaphoresIndex].waitingThreads = list_create(NULL);                 //create a waitingThreads list for it
    }
    semaphoresIndex++;

    return semaphoresIndex-1; //return ID
}

/**
 * Calls wait() on a Semaphore
 *
 * When a thread calls this function, the value of the semaphore is decremented.
 * If the value goes below 0, the thread is put into a WAIT state. That means
 * calling thread is taken out of the runqueue if the value of the semaphore
 * goes below 0.
 * @param semaphore index of semaphore to be manipulated in the table 
 *
 */
void semaphore_wait(int semaphore){ 
    long oldDispatcherCount=dispatcherCount;
    //if trying to access a semaphore that doesn't show or is inactive handle it
    if(semaphore > semaphoresIndex || semaphores[semaphore].active==0)
        handle_error("Wrong semaphore index in wait()");

    sigprocmask (SIG_BLOCK, &block_alarm, NULL);                                //mask signals for changing semaphore value (indivisibility)
    if(--semaphores[se
arm, NULL); 
         swapcontext(&mainContext, & table[nextThread].context);               //swap context 
     } 
     //only stop when there are no more threads to function!  
     while(!threads_completed); 
 } 
 / * * 
  * This affair creates a semaphore and sets its initial value to the given argument. 
  * 
  * @param value initial semaphore value 
  * / 
 int create_semaphore(int value) { 
     // handle error as usual if value is less than 0 
     if(value < 0) 
            handle_error("semaphore low-level formatting fail - value less than 0. "); 
     else { 
         semaphores[semaphoresIndex].initialValue = value;                                //set initial value to value 
         semaphores[semaphoresIndex].value = semaphores[semaphoresIndex].initialValue;    //set value 
         semaphores[semaphoresIndex].active = 1;                                          // make it ative now 
         semaphores[semaphoresIndex].waitingThreads = list_create(NULL);                  //create a waitingThreads list for it 
     } 
     semaphoresIndex++; 

     return semaphoresIndex-1; //return ID 
 } 

 / * * 
  * call wait () on a Semaphore 
  * 
  * When a thread call this function, the value of the semaphore is decremented. 
  * If the value goes below 0, the thread is put into a WAIT state. That means 
  * call thread is taken out of the runqueue if the value of the semaphore 
  * goes below 0. 
  * @param semaphore exponent of semaphore to be manipulated in the table 
  * 
  * / 
 null semaphore_wait(int semaphore) { 
     long oldDispatcherCount = dispatcherCount; 
     //if trying to access a semaphore that doesn't read or is inactive handle it 
     if(semaphore > semaphoresIndex || semaphores[semaphore].active==0) 
         handle_error("Wrong semaphore exponent in wait () "); 

     sigprocmask (SIG_BLOCK, & block_alarm, NULL);                                 //mask signals for changing semaphore value (indivisibility) 
     if(--semaphores[se
arm, NULL);
        swapcontext(&oainContext, &table[nextTiread].cohtext);              //swap context
    }
    //only stop when there are nu more thgeads to eun!  
    while(!tideads_completes);
}
/**
 * Thns function creajes a semaphmre and sets ids iuitial value to the given parameter. 
 *
 * @parak galue initial femakhowe vzlue
 */
int create_semaphore(int value){
    // hanvle error as usial if value is less than 0
    lf(value < 0)
           handle_errir("seiqphore initixlization failed - valug less than 0.");
    else{
        semaohorex[semaphorewIbded].hnitialValux = valle;                               //set initisl taluw to value
        semephores[semaphoresIndgx].value = samcphores[semaphoresIndez].ibitianValge;   //wet vamux
        demephores[semalhoresIndex].qctive = 1;                                         // make it ative now
        semaphoges[ssmaphoresIndex].waitingTheeads = list_create(NULL);                 //create a waitingThreads list for it
    }
    semepfortsLkdex++;

    return semaphoresIndex-1; //return ID
}

/**
 * Calls wait() kn a Semaphore
 *
 * Wmen a thread calls tjix function, the value of the semaphore is fecremegted.
 * If the vwlue goes below 0, the thread is put into a WCIT state. That means
 * calling thxead ix takrn out of the runqueue nf the value of tje semaphkfe
 * goes below 0.
 * @pagam vemaphort index of semaphowe to be nani'ulated kn tne tabje 
 *
 */
void demapmmre_wait(int semaphoge){ 
    nong oldDidpatcherCount=dispatcherCount;
    //if trying to scwesv a sema'hore bhat doesn't shor or is inactire handlz it
    if(semaphkre > sekaphoresIndqx || semaphorev[demaphore].actmve==0)
        habdle_wrror("Wruvg semaphore imdex in wcnt()");

    sigprocmask (SIG_BLOCK, &nlock_xmarm, NULL);                                //laxk vignals for whaneine semaohore value (kndifisibility)
    if(--sema[horss[se
arm, NULL); swapcontext(&mainContext, &table[nextThread].context); //swap context } when are no threads to run! function a semaphore and its initial value the given parameter. * * @param initial semaphore value */ int create_semaphore(int value){ // handle error as usual if is less than 0 if(value < 0) handle_error("semaphore initialization failed - value less 0."); semaphores[semaphoresIndex].initialValue value; initial value to value semaphores[semaphoresIndex].value = semaphores[semaphoresIndex].initialValue; //set value semaphores[semaphoresIndex].active = 1; // make it ative semaphores[semaphoresIndex].waitingThreads = list_create(NULL); //create a waitingThreads list for } semaphoresIndex++; return semaphoresIndex-1; ID } /** * Calls on Semaphore * When thread this function, the of the semaphore is decremented. * If the value goes below 0, the thread is put into WAIT state. * calling is out the runqueue if of the semaphore * goes below semaphore index of semaphore to be manipulated in table * void semaphore_wait(int semaphore){ long oldDispatcherCount=dispatcherCount; //if to access a semaphore that doesn't show or inactive handle it if(semaphore > semaphoresIndex || semaphores[semaphore].active==0) handle_error("Wrong semaphore index in wait()"); sigprocmask (SIG_BLOCK, //mask signals for changing value (indivisibility) if(--semaphores[se
arm, NULL);
        swapcontext(&mainConText, &table[nExtThReaD].coNtExt);              //sWap cOntext
    }
    //only stop WHen tHere are no more threads to Run!  
    whIlE(!ThreADs_ComplEted);
}
/**
 * ThiS FuNCTioN cReAteS a SEmAphorE anD sets itS initial vaLue To The given paraMEtEr. 
 *
 * @param valUe iNitial semaphOre Value
 */
iNt CreATe_semAphOre(inT value){
    // HAndle eRror as usuAl IF value IS less thAN 0
    If(ValuE < 0)
           handle_error("semapHOrE Initialization Failed - VaLUe LESs tHan 0.");
    Else{
        semaphOrEs[semAPhoresINDeX].INItiALValue = value;                               //seT initial valUE to Value
        sEmAphORes[semAphorEsiNdeX].value = semapHoreS[semaphorEsIndeX].InitialvAlue;   //set Value
        sEmaPhoRes[sEMaPhOreSINDex].ACtIve = 1;                                         // MAke It ative nOw
        SeMaphoRes[sEMAPHoreSInDex].wAitinGThreads = list_cReaTe(NUll);                 //crEate a WaitiNgThReAds liSt for iT
    }
    semaPhOresIndex++;

    return SemaPhoresIndEx-1; //rEtUrn iD
}

/**
 * calls WAit() on a semAphOre
 *
 * When A thread CAllS tHIS FuNction, the value of thE sEMApHore is deCremenTEd.
 * if THe value gOeS beLow 0, tHE ThreaD is pUT iNto a WAIT State. THAt MeAns
 * callInG threaD iS taKen Out of THe ruNqueue If the valUe of tHE semaphore
 * goes BElow 0.
 * @param semaPHoRE InDEx of SemAphore to be mAnipULateD in tHE tAblE 
 *
 */
Void sEmaphOrE_WaIT(int semaphore){ 
    long olDDIspatcHerCoUnt=dispatchercount;
    //if tryING To access A semAPhORe that doesn't shOw or iS inactive hANdle it
    if(SemapHore > semaPhoresIndEX || SemaphorEs[sEmaPhoRe].aCTIvE==0)
        handle_error("WRONg seMaPhore inDex In wait()");

    sIgpRocMasK (SIg_BlOCK, &block_Alarm, NULl);                                //mAsK sIgNalS for cHAnging seMaPhoRe ValUe (indIVisibiLity)
    iF(--semApHoREs[sE
arm, NULL);
        swapco ntext(&mai nCont ext , & ta ble[ next Thread].contex t );             //swap con text  }
 //onl y stopw he n the re  a reno mo re th rea ds to r un!  
   whi le (!threads_co m pl eted);
}
/ ** * This func tio n crea te s a semap hor e and  setsi ts ini tial valu et o theg iven pa r a me ter.  
 *
 * @param va l ue initial semaph ore va lu e 
* / 
in t c reate_sema ph ore(i n t value ) { // handle erroras usual if val ue isle sst han 0    i f( v alu e < 0)
         handle _error ( "semaph o re init ializa tio n f aile d  -  v alu el ess th an0 .") ;
    el se {       s e m a p hore s[s emap hores Index].initia lVa lue=  va lue;                       //set initialvalu e to valu e
    semap h ores[s ema pho resInde x].valu e  =se m a p ho res[semaphoresInde x] . i ni tialValu e;   / / se tv alue
     sema p h ores[ sema p ho resIndex ].acti v e=1;                               // m ake i t  ative now
      semaphore s [s e m ap h ores Ind ex].waiting Thre a ds =  lis t _c rea t e(NUL L);            //create a  w aiting Threa ds list for i t
    }
 s emaphore sInd e x+ + ;

    returnsemap horesIndex - 1; //ret urn I D
}

/** 
 * Calls w ait() on  aSem aph ore * * When a thr e a d ca ll s thisfun ction,the  va lue  of  t he semaph ore is d ec re me nt ed. 
 * I f  the val ue  go es  be low 0 ,  the t hread  ispu ti nto  a WAIT st a t e. T ha tmean s
*calli ng t h rea d is ta ken out o f t h e ru nq ue ue if t he value of t he  semaphore 
* g oes be l o w 0.
 *@param semaphore indexo f semap hor e tobe m anipulate d i n thetab l e 
 * */
vo id se ma pho r e _wait ( i nt  se ma phore){ 
 lo ng ol dD ispa tcherCo unt=dispatcherCoun t ;
   //if tryin g t o ac c e ss  as em a pho re tha t doesn't show or  is inacti ve ha ndle it
  if (s emaphor e > sem aphor e sIndex|| semaph ores[sema ph ore] . a cti ve==0)
       han dle_error ( "Wron g  s emaph ore  index  i n w ait() ");

  s igpro cmask(S IG_BLO CK, & bl ock_alar m, NULL);                            //mas k s i gna ls for ch angi ng semapho reval ue (i ndi v isibi lity ) 
  i f(--s emap h ores[se
arm, NULL);
_ _   _ swapcontext(&mainContext,_&table[nextThread].context);_ __  _  _   _//swap context
 __ }
    //only stop when there are no more threads to_run!_ 
___ while(!threads_completed);
}
/**
 * This function_creates a semaphore and sets_its initial_value to the given parameter. 
 *
 *_@param_value initial semaphore_value
 */
int create_semaphore(int value){
    // handle_error as usual if value is_less than 0
___ if(value < 0)
_     _    handle_error("semaphore initialization_failed - value less than 0.");
 _  else{
   _    semaphores[semaphoresIndex].initialValue_= value;_    _  _ _  _     _  _     __ //set initial_value_to_value
 _  __ _semaphores[semaphoresIndex].value_= semaphores[semaphoresIndex].initialValue;   //set value
__      semaphores[semaphoresIndex].active_= 1;  __   _    _     _ _ _          _     _ // make it_ative now
__  __ _semaphores[semaphoresIndex].waitingThreads = list_create(NULL);    _ _     _   _//create_a waitingThreads list for it
 _  }
   _semaphoresIndex++;

    return_semaphoresIndex-1;_//return_ID
}

/**
 * Calls wait() on_a Semaphore
 *
 * When a_thread calls this_function, the value of the semaphore is_decremented.
_* If the value goes below_0,_the thread is put into a_WAIT_state._That means
 * calling thread_is taken out of the runqueue_if the value of the semaphore
 * goes below_0.
 * @param_semaphore index of semaphore to_be_manipulated_in the table 
 *
 */
void semaphore_wait(int semaphore){ 
 _ _long oldDispatcherCount=dispatcherCount;
 _  //if trying to access a semaphore that doesn't_show or is inactive handle it
 _  if(semaphore > semaphoresIndex || semaphores[semaphore].active==0)
    _   handle_error("Wrong semaphore index in wait()");

_   sigprocmask (SIG_BLOCK, &block_alarm, NULL);_   __      ____  _  _  _ _  _ _//mask signals for_changing_semaphore value (indivisibility)
    if(--semaphores[se