/*-----------------------------------------------------------------*/
int main(void)
{
    char command;
    struct list_node_s* head_p1 = NULL;
    struct list_node_s* head_p2 = NULL;
    struct list_node_s* head_p3 = NULL;
    /* start with empty list */
    
    command = Get_command();
    while (command != 'q' && command != 'Q') {
        
        head_p1 = Build(head_p1);
        head_p2 = Build(head_p2);
        
        switch (command) {
            case 'u':
            case 'U':
                head_p3 = Union(head_p1, head_p2);
                printf("The union between the two sets is: ");
                Print(head_p3);
                break;
                
            case 'i':
            case 'I':
                head_p3 = Intersect(head_p1, head_p2);
                printf("The intersection of the two sets is: ");
                Print(head_p3);
                break;
                
            case 'd':
            case 'D':
                head_p3 = Difference(head_p1, head_p2);
                printf("The difference between the two sets is: ");
                Print(head_p3);
                break;
                
            default:
                printf("There is no %c command\n", command);
                printf("Please try again\n");
        }
        
        head_p1 = Free_list(head_p1);
        head_p2 = Free_list(head_p2);
        head_p3 = Free_list(head_p3);
        
        command = Get_command();
    }
    
    return 0;
} /* main */
Beispiel #2
0
static void free_module(module_listitem *m)
{
	assert(m);
	m->close();
	dlclose(m->libhandle);
	Free_list(m->name, m);
}
Beispiel #3
0
/*-----------------------------------------------------------------*/
int main(int argc, char* argv[]) {
   long i; 
   int key, success, attempts;
   pthread_t* thread_handles;
   int inserts_in_main;
   unsigned seed = 1;
   double start, finish;

   if (argc != 2) Usage(argv[0]);
   thread_count = strtol(argv[1],NULL,10);

   Get_input(&inserts_in_main);

   /* Try to insert inserts_in_main keys, but give up after */
   /* 2*inserts_in_main attempts.                           */
   i = attempts = 0;
   while ( i < inserts_in_main && attempts < 2*inserts_in_main ) {
      key = my_rand(&seed) % MAX_KEY;
      success = Insert(key);
      attempts++;
      if (success) i++;
   }
   printf("Inserted %ld keys in empty list\n", i);

#  ifdef OUTPUT
   printf("Before starting threads, list = \n");
   Print();
   printf("\n");
#  endif

   thread_handles = malloc(thread_count*sizeof(pthread_t));
   pthread_mutex_init(&mutex, NULL);
   pthread_mutex_init(&count_mutex, NULL);

   GET_TIME(start);
   for (i = 0; i < thread_count; i++)
      pthread_create(&thread_handles[i], NULL, Thread_work, (void*) i);

   for (i = 0; i < thread_count; i++)
      pthread_join(thread_handles[i], NULL);
   GET_TIME(finish);
   printf("Elapsed time = %e seconds\n", finish - start);
   printf("Total ops = %d\n", total_ops);
   printf("member ops = %d\n", member_total);
   printf("insert ops = %d\n", insert_total);
   printf("delete ops = %d\n", delete_total);

#  ifdef OUTPUT
   printf("After threads terminate, list = \n");
   Print();
   printf("\n");
#  endif

   Free_list();
   pthread_mutex_destroy(&mutex);
   pthread_mutex_destroy(&count_mutex);
   free(thread_handles);

   return 0;
}  /* main */
int main(int argc, char* argv[])
{
long i; 
int key, success, attempts;
pthread_t* thread_handles;
int inserts_in_main;
unsigned seed = 1;
double inicio, fin;

if (argc != 2) Usage(argv[0]);
thread_count = strtol(argv[1], NULL, 10);
Get_input(&inserts_in_main);

i = attempts = 0;
pthread_mutex_init(&head_mutex, NULL);
while ( i < inserts_in_main && attempts < 2*inserts_in_main )
{
   key = my_rand(&seed) % MAX_KEY;
   success = Insert(key);
   attempts++;
   if (success) i++;
}
printf("%ld keys insertadas\n", i);

#  ifdef OUTPUT
   printf("Antes de crear hilos, lista = \n");
   Print();
   printf("\n");
#  endif

thread_handles = malloc(thread_count*sizeof(pthread_t));
pthread_mutex_init(&count_mutex, NULL);

GET_TIME(inicio);
for (i = 0; i < thread_count; i++)
   pthread_create(&thread_handles[i], NULL, Thread_work, (void*) i);

for (i = 0; i < thread_count; i++)
   pthread_join(thread_handles[i], NULL);
GET_TIME(fin);
printf("Tiempo de ejecución = %e segundos\n", fin - inicio);
printf("Total operaciones = %d\n", total_ops);
printf("Operaciones miembro = %d\n", total_miembro);
printf("Operaciones insertar = %d\n", total_insertar);
printf("Operaciones borrado = %d\n", total_borrado);

#  ifdef OUTPUT
   printf("Despues de terminar los hilos, lista = \n");
   Print();
   printf("\n");
#  endif

Free_list();
pthread_mutex_destroy(&head_mutex);
pthread_mutex_destroy(&count_mutex);
free(thread_handles);

return 0;
}
Beispiel #5
0
/*-----------------------------------------------------------------*/
int main(void) {
   char command;
   int  value;
   struct list_node_s* head_p = NULL;  
      /* start with empty list */

   command = Get_command();
   while (command != 'q' && command != 'Q') {
      switch (command) {
         case 'i': 
         case 'I': 
            value = Get_value();
            head_p = Insert(head_p, value);
            break;
         case 'p':
         case 'P':
            Print(head_p);
            break;
         case 'm': 
         case 'M':
            value = Get_value();
            if (Member(head_p, value))
               printf("%d is in the list\n", value);
            else
               printf("%d is not in the list\n", value);
            break;
         case 'd':
         case 'D':
            value = Get_value();
            head_p = Delete(head_p, value);
            break;
         case 'f':
         case 'F':
            head_p = Free_list(head_p);
            break;
         default:
            printf("There is no %c command\n", command);
            printf("Please try again\n");
      }
      command = Get_command();
   }

   head_p = Free_list(head_p);

   return 0;
}  /* main */
Beispiel #6
0
void irc_close(irc_connection *con, char *qmsg)
{
    assert(con);

    if (qmsg) Irc_send(con, "QUIT :%s\n", qmsg);
    else Irc_send(con, "QUIT\n");

    Free_list(con->buf, con->nick, con->username, con->hostname, con->servername, con->realname);
    close(con->sockfd);

    if (!--irc_connections) g_regex_unref(irc_msg_regex_pattern);
    assert(irc_connections >= 0);
}
Beispiel #7
0
int irc_set_user(irc_connection *con,
                 const char *username, const char *hostname,
                 const char *servername, const char* realname)
{
    con->username   = strdup(username);
    con->hostname   = strdup(hostname);
    con->servername = strdup(servername);
    con->realname   = strdup(realname);

    if (!con->username || !con->hostname || !con->servername || !con->realname) {
        Free_list(con->username, con->hostname, con->servername, con->realname);
        return 1;
    }

    Irc_send(con, "USER %s %s %s :%s\n", username, hostname, servername, realname);

    return 0;
}
Beispiel #8
0
int module_load_module_dir(irc_connection *con, config *conf)
{
	int ret;
	DIR *module_dir = opendir(MODULE_PATH);
	if (!module_dir) return -1;

	int modules_loaded = 0;
	struct dirent *entry;

	/* search through the module directory and
	 * load everything what looks like a module file.
	 */

	while ((entry = readdir(module_dir))) {
		char *filename = entry->d_name;
		if (filename[0] == '.') continue;

		char *dstr = strstr(filename, MODULE_SUFFIX);
		if (!dstr) continue;

		char *module_path;
		asprintf(&module_path, "%s/%s", MODULE_PATH, filename);

		char *modname = Module_name(filename);
		assert(modname);

		printf("Loading module %15s ... ", modname);
		ret = module_add(con, config_get_group(conf, modname), module_path);
		if (!ret) {
			modules_loaded++;
			printf("OK!\n");
		}
		else 
			printf("Error! (%d)\n", ret);

		Free_list(module_path, modname);
	}

	return modules_loaded;
}
Beispiel #9
0
/*-----------------------------------------------------------------*/
int main(void) {
   char command;
   int  value;
   struct list_node_s* head_p = NULL;  /* start with empty list */

   command = Get_command();
   while (command != 'q' && command != 'Q') {
      switch (command) {
         case 'i': 
         case 'I': 
            value = Get_value();
            Insert(value, &head_p);  /* Ignore return value */
            break;
         case 'p':
         case 'P':
            Print(head_p);
            break;
         case 'm': 
         case 'M':
            value = Get_value();
            Member(value, head_p);   /* Ignore return value */
            break;
         case 'd':
         case 'D':
            value = Get_value();
            Delete(value, &head_p);  /* Ignore return value */
            break;
         default:
            printf("There is no %c command\n", command);
            printf("Please try again\n");
      }
      command = Get_command();
   }
   Free_list(&head_p);

   return 0;
}  /* main */
Beispiel #10
0
void irc_free_msg(irc_msg *msg)
{
    Free_list(msg->source, msg->command, msg->target, msg->params, msg);
}
Beispiel #11
0
/*\///////////////////////////////////////////////////////////////////////////
//
//   Description:
//      Main routine for building the Archive III status product.
//
///////////////////////////////////////////////////////////////////////////\*/
int main( int argc, char *argv[] ){

   int ret;

   /* Register output.  The output is DEMAND_DATA so no requests for this
      product are required for product generation. */
   RPGC_reg_outputs( argc, argv );

   /* Register UN events so as to be notified whenever a new System Status
      Log message is written. */
   ret = RPGC_UN_register( ORPGDAT_SYSLOG_SHADOW, LB_ANY, Handle_notification );
   if( ret < 0 ){

      /* This is a fatal error!!!!! */
      RPGC_log_msg( GL_INFO, "RPGC_UN_register Failed (%d)\n", ret );
      RPGC_hari_kiri();

   }

   /* Register Event Timeout. */
   RPGC_reg_for_internal_event( EVT_WAIT_FOR_EVENT_TIMEOUT, NULL, 60 );

   /* Do task initialization. */
   RPGC_task_init( TASK_EVENT_BASED, argc, argv );

   /* Position the read pointer for the shadow Status Log.  This will
      be the first unread message in this log. */
   ORPGDA_seek( ORPGDAT_SYSLOG_SHADOW, 0, LB_FIRST, NULL );

   /* Data needed to determine when to generate a product. */
   Current_time = time( NULL );
   Current_hour = Current_time / SECS_IN_HOUR;
   Previous_hour = Current_hour;

   /* Get the generation period from adaptation data. */
   Get_status_prod_adapt( &Adapt );
   RPGC_log_msg( GL_INFO, "The Status Product Will Be Generated Every %d hrs\n",
                 Adapt.gen_period );
   
   /* Process status log events. */
   while(1){

      RPGC_wait_for_event();

      /* If we need to read the system status log, do it now. */
      if( Read_status_log ){

         Read_status_log = 0;

         /* Any negative return value from Process_log_entry is 
            considered a fatal error (i.e., they are either malloc
            failures or ORPGDA_read failures). */
         if( Process_log_entry() < 0 )
            RPGC_hari_kiri();

      }

      /* Determine if it is time to produce the next product. */
      Current_time = time( NULL );
      Current_hour = Current_time / SECS_IN_HOUR;

      if( (Current_hour != Previous_hour) 
                        && 
          ((Current_hour % Adapt.gen_period) == 0) ){

         if( Num_messages > 0 ){

            RPGC_log_msg( GL_INFO, "Time to build new product.\n" );

            /* A negative return value from Build_status_product
               is non-fatal.  We just won't build a product this
               time around. */
            if( Build_status_product( Num_messages ) < 0 )
               RPGC_abort();

            /* Clear Num_messages from the shadow file since these messages
               have already been processed. */
            ORPGDA_clear( ORPGDAT_SYSLOG_SHADOW, Num_messages );
     
            /* Reset the number of messages processed. */
            Num_messages = 0;

            /* Free memory allocated to the linked list of status messages. */
            Free_list();

         }

         Previous_hour = Current_hour;

      }

   /* Wait for the system status log to be undated again. */
   }
    
   return 0;

} /* End of main() */
Beispiel #12
0
/* ========================================================================== */
int main(void)
{
# ifdef DEBUG
    printf("\n\t\t######################################");
    printf("\n\t\t#!!!   Debugger mode turned on.   !!!#");
    printf("\n\t\t#!!!   File: %s               !!!#", __FILE__);
    printf("\n\t\t#!!!   Line: %d.                  !!!#", __LINE__);
    printf("\n\t\t######################################\n\n");
# endif
    struct set_s* a_p = NULL;
    struct set_s* b_p = NULL;
    struct set_s* c_p = NULL;
    struct set_s* C = NULL;
    int asize, bsize, csize;
    asize = bsize = csize = 0;
    char command;
    command = Get_command();

    if(command != 'q' && command != 'Q')
    {
        a_p = Read_set(a_p, &asize, 1);
        b_p = Read_set(b_p, &bsize, 2);
        csize = asize + bsize;
    }

   while (command != 'q' && command != 'Q')
   {
       switch (command)
       {
           case 'u':
           case 'U':
                printf("\n\nFinding the union of the two sets. \n");
                c_p = Union(a_p, asize, b_p, bsize, c_p, csize, C);
                Print(a_p, 1);
                Print(b_p, 2);
                printf("Union: ");
                Print(c_p, 3);
                printf("\n");
                break;
           case 'i':
           case 'I':
               printf("\n\nFinding the intersection of the two sets. \n");
               c_p = Intersection(a_p, asize, b_p, bsize, c_p, csize, C);
               Print(a_p, 1);
               Print(b_p, 2);
               printf("Intersection: ");
               Print(c_p, 3);
               printf("\n");
               break;
           case 'd':
           case 'D':
               printf("\n\nFinding the difference of the two sets. \n");
               c_p = Difference(a_p, asize, b_p, bsize, c_p, csize, C);
               Print(a_p, 1);
               Print(b_p, 2);
               printf("Difference: ");
               Print(c_p, 3);
               printf("\n");
               break;
           case '\n':
               printf("\n$ ");
               break;
           default:
               printf("There is no %c command\n", command);
               printf("Please try aga_pn\n");
       }

       // free lists and reset list sizes.
        a_p = Free_list(a_p);
        b_p = Free_list(b_p);
        c_p = Free_list(c_p);
        C = Free_list(C);
        asize = bsize = csize = 0;

       // get command for operation to perform.
       command = Get_command();

       // read in new lists and update csize.
       if(command != 'q' && command != 'Q')
       {
           a_p = Read_set(a_p, &asize, 1);
           b_p = Read_set(b_p, &bsize, 2);
           csize = asize + bsize;
       }
   }

    return 0;
}  /* main */
Beispiel #13
0
/******************************************************************

   Description:
      Reads and reports RPG process CPU utilization.

   Inputs:

   Outputs:

   Return:	
      0 on success or -1 on failure.
	
   Notes:

******************************************************************/
int CPUUSE_compute_cpu_utilization( char *buf, time_t monitor_time,
                                    int elevation_cut, int len ){

    int ind, index, num_compare;
    unsigned int cpu_diff, total_cpu;
    time_t time_diff;
    double cpu_diff_percent = 0.0;
    char *cpt;

    /* Initialize the current cpu stats list. */
    for( index = 0; index < Cpu_stats_size; index++ ){

       (Curr_cpu_stats + index)->stats.cpu = 0;
       (Curr_cpu_stats + index)->stats.pid = -1;
       if( (Curr_cpu_stats + index)->stats.next != NULL )
          Free_list( (Curr_cpu_stats + index)->stats.next );

       (Curr_cpu_stats + index)->stats.next = NULL;

    /* End of "for" loop. */
    }
   
    /* Do For All processes being reported. */
    cpt = buf;
    num_compare = 0;
    while (1){

       Task_rec_t *ps;

       ps = (Task_rec_t *)cpt;
       if( (cpt - buf + sizeof(Task_rec_t) > len) )
	    break;

        /* Find the entry of task in current stats list. */
        index = Binary_search_name( Curr_cpu_stats, Cpu_stats_size,
                                    ps->task_name, ps->instance, ps->node_name );


        /* Binary search found match on task name. */
        if( index >= 0 ){
        
           /* If this is the first encountered instance of this process,
              set the pid and total cpu comsumed so far. */
           if( (Curr_cpu_stats + index)->stats.pid < 0 ){

	      (Curr_cpu_stats + index)->stats.cpu = ps->cpu;
	      (Curr_cpu_stats + index)->stats.pid = ps->pid;

              /* Save the index for later comparison. */
              *(Compare_index + num_compare) = index;

              /* Increment the number of processes to compare. */
              num_compare++;

           }
           else{

              Proc_stats_t *curr = &(Curr_cpu_stats + index)->stats;

              /* To account for multiple instances of this process, we
                 add to an entry for this pid. */
              Add_pid_to_list( (Curr_cpu_stats + index), ps->pid, ps->cpu );

              while(1){

                 curr = curr->next;
                 if( curr == NULL )
                    break;

              }

           }

        }
        else if( index == -1 )
           fprintf( stderr, "Task %s Not In Task Table\n", ps->task_name );

	cpt += sizeof(Task_rec_t);

    /* end of "while" loop. */
    }

    /* First time monitoring initialization. */
    if( Prev_monitor_time == 0 ){

       for( index = 0; index < Cpu_stats_size; index++ ){

          (Prev_cpu_stats + index)->stats.cpu = (Curr_cpu_stats + index)->stats.cpu;
          (Prev_cpu_stats + index)->stats.pid = (Curr_cpu_stats + index)->stats.pid;
          if( (Curr_cpu_stats + index)->stats.next != NULL ){

             Proc_stats_t *curr = (Curr_cpu_stats + index)->stats.next;
             while( curr != NULL ){

                Add_pid_to_list( (Prev_cpu_stats + index), curr->pid,
                                  curr->cpu );
                curr = curr->next;

             /* End of "while" loop. */
             }

          }

       /* End of "for" loop. */
       }

       Prev_monitor_time = monitor_time;
 
    }
    else{

       /* Initialize the total cpu. */
       total_cpu = 0;

       /* Compute CPU statistics for this monitoring period. */
       for( index = 0; index < num_compare; index++ ){

          Proc_stats_t *curr;

          /* Only consider those processes which were active during
             this monitoring period. */
          ind = Compare_index[index];
          curr = &(Curr_cpu_stats + ind)->stats;

          /* Initialize the cpu difference.  */
          cpu_diff = 0;

          while(1){

             /* Check for match on pid. */
             if( curr->pid == (Prev_cpu_stats + ind)->stats.pid )
                cpu_diff += ( curr->cpu - (Prev_cpu_stats + ind)->stats.cpu );

             else{

                Proc_stats_t *prev = (Prev_cpu_stats + ind)->stats.next;
                int match = 0;

                /* Check multiple instances for match on pid. */
                while( prev != NULL ){

                   if( curr->pid == prev->pid ){

                      /* Match found. */
                      match = 1;
                      break;

                   }

                   prev = prev->next;

                /* End "while" loop. */
                }

                if( match )
                   cpu_diff += (curr->cpu - prev->cpu);

                else
                   cpu_diff += curr->cpu;

             }

             /* Do for all instances of this task found this monitoring period. */
             curr = curr->next;
             if( curr == NULL )
                break;

          /* End of "while" loop. */
          }

          /* Add this cpu difference to the appropriate elevation. */
          if( Vol_stats[ind]->cpu[elevation_cut-1] <= 0 ) 
             Vol_stats[ind]->cpu[elevation_cut-1] = cpu_diff;

          else
             Vol_stats[ind]->cpu[elevation_cut-1] += cpu_diff;

          /* Add difference to total. */
          total_cpu += cpu_diff;

       /* End of "for" loop. */
       }

       /* Set the number of elevation cuts this VCP. */
       Vol_stats_num_elevs = elevation_cut;

       /* Set the total CPU for this elevation. */
       Vol_stats[Cpu_stats_size]->cpu[elevation_cut-1] = total_cpu;

       /* CPU is reported in millisecs and time is in secs so
          need to divide by 1000. */
       cpu_diff_percent = (double) total_cpu / 1000.0;
       time_diff = monitor_time - Prev_monitor_time;

       /* Set the scan duration, in millisecs. */
       Vol_stats[Cpu_stats_size+1]->cpu[elevation_cut-1] = time_diff * 1000;

       /* Set the percent total CPU for this elevation.  Scale by 10.0 */
       Vol_stats[Cpu_stats_size+2]->cpu[elevation_cut-1] = 
                                  (int) ((cpu_diff_percent / time_diff) * 1000);

       /* Prepare for next monitoring cycle. */
       Prev_monitor_time = monitor_time;
       for( index = 0; index < Cpu_stats_size; index++ ){

          (Prev_cpu_stats + index)->stats.cpu = (Curr_cpu_stats + index)->stats.cpu;
          (Prev_cpu_stats + index)->stats.pid = (Curr_cpu_stats + index)->stats.pid;

          if( (Prev_cpu_stats + index)->stats.next != NULL ){

             Free_list( (Prev_cpu_stats + index)->stats.next );
             (Prev_cpu_stats + index)->stats.next = NULL;

          }

          if( (Curr_cpu_stats + index)->stats.next != NULL ){

             Proc_stats_t *next = (Curr_cpu_stats + index)->stats.next;
             while( next != NULL ){

                Add_pid_to_list( (Prev_cpu_stats + index), next->pid, next->cpu );
                next = next->next;

             /* End of "while" loop. */
             }

          }

       /* End of "for" loop. */
       }

    }

    /* Return to application. */
    return (0);

/* End of CPUUSE_compute_cpu_utilization() */
}