Advertisement
Gianmarco

time wizard

Aug 17th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.10 KB | None | 0 0
  1. void * time_wizard( void * _worker ){
  2.  
  3.     int                 ret;
  4.  
  5.     worker              *wrkr = ( worker *) _worker;
  6.  
  7.     block               *myblock = wrkr -> my_block;
  8.  
  9.     sw_slot             *window = wrkr -> sliding_window_slot_;
  10.  
  11.     sigset_t set;
  12.  
  13.     beginning:
  14.  
  15.     sigemptyset( &set );
  16.     sigaddset( &set, SIGUSR1 );
  17.     signal( SIGUSR1, wake_up );
  18.     sigprocmask( SIG_BLOCK, &set, NULL );
  19.    
  20.     struct timespec     now = { 0, 0 };
  21.  
  22.     if ( wrkr -> is_working == '0') {
  23.  
  24.         sigpending( &set );
  25.  
  26.         if( sigismember( &set, SIGUSR1 ) ) {
  27.             sigprocmask( SIG_UNBLOCK, &set, NULL );
  28.             if ( ( myblock -> eraser ) == '1')    pthread_exit( NULL );
  29.             signal( SIGUSR1, wake_up );
  30.         }
  31.  
  32.         sigprocmask( SIG_UNBLOCK, &set, NULL );
  33.  
  34.         pause();
  35.  
  36.         if ( ( myblock -> eraser ) == '1')    pthread_exit( NULL );
  37.  
  38.         sigprocmask( SIG_BLOCK, &set, NULL );
  39.  
  40.     } else {
  41.  
  42.         printf("\n TIME WIZARD %d RUNNING.\n ", ( wrkr -> identifier ) );                        fflush(stdout);
  43.  
  44.     }
  45.  
  46.     do {
  47.  
  48.         printf("\n Time Wizard in the cycle\n"); fflush(stdout);
  49.  
  50.         ret = nanosleep( &beat, NULL );
  51.         if (ret == -1)      Error_( "Error in function : nanosleep() (time_wizard).", 1);
  52.  
  53.         current_timestamp( &now );
  54.  
  55.         for ( int i = 0; i < WINDOW_SIZE; i ++ ) {
  56.  
  57.             if (
  58.                 ( window != NULL )
  59.             &&  ( window -> status == SENT ) ) {
  60.                
  61.                 if ( nanodifftime( &now, &( window -> sent_timestamp ) )  >= ( window -> timeout_interval ) ) {
  62.  
  63.                     if ( retransmission( window, ( wrkr -> sockfd ), (wrkr -> client_addr), ( wrkr -> len ) ) == -1 )     Error_("Error in function: retransmission (time_wizard).", 1);
  64.  
  65.                 }
  66.  
  67.             }
  68.  
  69.             if ( window == NULL ) break;
  70.  
  71.             window = ( window -> next );
  72.  
  73.         }
  74.  
  75.     } while( ( wrkr -> is_working ) == '1' );
  76.  
  77.     printf("\n TIME WIZARD HAS COMPLETED A CYCLE ANG GOES TO SLEEP.");      fflush(stdout);
  78.  
  79.     goto beginning;
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement