Exemplo n.º 1
0
/**
 * Release resources
 */
static void scmpc_cleanup(void)
{
	g_source_remove(signal_source);
	if (prefs.cache_interval > 0)
		g_source_remove(cache_save_source);
	if (mpd.idle_source > 0)
		g_source_remove(mpd.idle_source);
	if (mpd.check_source > 0)
		g_source_remove(mpd.check_source);
	if (mpd.reconnect_source > 0)
		g_source_remove(mpd.reconnect_source);

	if (current_song_eligible_for_submission())
		queue_add_current_song();
	if (prefs.fork)
		scmpc_pid_remove();
	close_signal_pipe();
	if (prefs.cache_interval > 0)
		queue_save(NULL);
	queue_cleanup();
	if (mpd.song_pos)
		g_timer_destroy(mpd.song_pos);
	clear_preferences();
	as_cleanup();
	if (mpd.conn != NULL)
		mpd_connection_free(mpd.conn);
}
Exemplo n.º 2
0
int main(int argc, char* argv[])
{
    /* Local Vars */
    outputfp = NULL;
    int num_input_files = argc-2; //subtract 1 for the original call and another for resolve.txt
	const int qSize = QUEUE_SIZE;
    int rc;
    long t;
    all_files_read=0;
    
    pthread_t file_threads[num_input_files];
    pthread_t resolver_threads[MAX_RESOLVER_THREADS];

	pthread_mutexattr_settype(&attr, NULL);
	pthread_mutex_init(&mutex_for_queue, &attr);

	pthread_mutexattr_settype(&attr2, NULL);
	pthread_mutex_init(&mutex_for_output, &attr2);
	
	pthread_mutexattr_settype(&attr3, NULL);
	pthread_mutex_init(&mutex_for_all_files_read, &attr3);

	
	/* Check Arguments */
	if(argc < MIN_ARGS){
		fprintf(stderr, "Too few arguments: %d\n", (argc - 1));
		fprintf(stderr, "Usage:\n %s %s\n", argv[0], USAGE);
		return EXIT_FAILURE;
	}
	
    if(argc > MAX_INPUT_FILES+1){
		fprintf(stderr, "Too many arguments: %d\n", (argc - 1));
		fprintf(stderr, "Usage:\n %s %s\n", argv[0], USAGE);
		return EXIT_FAILURE;
    }

    /* Open Output File */
    outputfp = fopen(argv[(argc-1)], "w");
    if(!outputfp){
		perror("Error Opening Output File\n");
		return EXIT_FAILURE;
    }

    /* Initialize Queue */
    if(queue_init(&q, qSize) == QUEUE_FAILURE)
    {
		fprintf(stderr, "ERROR: queue_init failed!\n");
		return EXIT_FAILURE; //if the queue didn't initialize properly, we can't run the rest of the program
    }
    
    
    /* Loop Through Input Files */
    for(t=0; t<num_input_files; t++)
    { 
		//create a thread to read input files
		rc = pthread_create(&(file_threads[t]), NULL, open_read_file, argv[t+1]); //pass it the input filename and the output file
		if (rc)
		{ //couldn't create thread successfully
			fprintf(stderr, "ERROR: return code from pthread_create() is %d\n", rc);
			exit(EXIT_FAILURE);
		}
	}
	
	/* Begin resolver threads */
	for(t=0; t<MAX_RESOLVER_THREADS; t++)
	{ 
		//create a resolver thread 
		rc = pthread_create(&(resolver_threads[t]), NULL, resolve_domain, NULL); //pass it the input filename and the output file
		if (rc)
		{ //couldn't create thread successfully
			fprintf(stderr, "ERROR: return code from pthread_create() is %d\n", rc);
			exit(EXIT_FAILURE);
		}
	}
	

    /* Wait for All Theads to Finish */
    for(t=0;t<num_input_files;t++)
    {
		pthread_join(file_threads[t], NULL);
    }
    //the file threads have completed - change all_files_read to TRUE
    pthread_mutex_lock(&mutex_for_all_files_read);
		all_files_read=1; //all files are not read or the queue is not empty
	pthread_mutex_unlock(&mutex_for_all_files_read);
    
    for(t=0;t<MAX_RESOLVER_THREADS;t++)
    {
		pthread_join(resolver_threads[t], NULL);
    }
    
    printf("All of the threads were completed!\n");
	fclose(outputfp); //close the output file when all the threads are done with it
	
    queue_cleanup(&q); //cleanup queue

	//clean up locks
	pthread_mutex_destroy(&mutex_for_queue);
	pthread_mutex_destroy(&mutex_for_output);

    return EXIT_SUCCESS;
}
Exemplo n.º 3
0
int main(int argc, char* argv[]){
	
	/* Setup Local Vars */
    int rc1, rc2; /* test if it is correct */
    long t;
    int num_threads = argc-2;
    pthread_t pthreads[MAX_RESOLVER_THREADS];
    pthread_t cthreads[MAX_RESOLVER_THREADS];
    producer_args arg_p[num_threads];
    consumer_args arg_c[num_threads];
    pthread_mutex_t produce_mutex;
    pthread_mutex_t consume_mutex;
    FILE* inputfp = NULL;
    FILE* outputfp = NULL;   
    char errorstr[MAX_NAME_LENGTH];
    
    control=num_threads;
    
    /* Check Arguments */
    if(argc < MINARGS){
	fprintf(stderr, "Not enough arguments: %d\n", (argc - 1));
	fprintf(stderr, "Usage:\n %s %s\n", argv[0], USAGE);
	return EXIT_FAILURE;
    }
    
     /* initialize queue */
     queue q;
     int q_size = 50;
     if(queue_init(&q, q_size) == QUEUE_FAILURE){
		fprintf(stderr,
		"error: queue_init failed!\n");
     }

    /* Open Output File */
    outputfp = fopen(argv[(argc-1)], "w");
    if(!outputfp){
	perror("Error Opening Output File");
	return EXIT_FAILURE;
    }

	 /*create threads*/
    for(t=0;t<num_threads;t++){
	
        inputfp = fopen(argv[t+1], "r");
         if(!inputfp){
	    sprintf(errorstr, "Error Opening Input File: %s", argv[t+1]);
	    perror(errorstr);
            queue_cleanup(&q);
	    return EXIT_FAILURE;
        }
        
        arg_c[t].q = &q;
        arg_c[t].file = outputfp;
  
        arg_p[t].q = &q;
        arg_p[t].file = inputfp;

        pthread_mutex_init(&produce_mutex, NULL);     
        pthread_mutex_unlock(&produce_mutex); 
        arg_c[t].produce_mutex = &produce_mutex;
        arg_p[t].produce_mutex = &produce_mutex;
         pthread_mutex_init(&consume_mutex, NULL);  
         pthread_mutex_lock(&consume_mutex);
        arg_c[t].consume_mutex = &consume_mutex;
        arg_p[t].consume_mutex = &consume_mutex;

        printf("In main: creating consumer thread %ld\n", t);
        printf("In main: creating producer thread %ld\n", t);
        
		rc2 = pthread_create(&(cthreads[t]), NULL, Consumer, &(arg_c[t]));
		if (rc2){
			printf("ERROR; return code from pthread_create() is %d\n", rc2);
			exit(EXIT_FAILURE);
		}
		
		rc1 = pthread_create(&(pthreads[t]), NULL, Producer, &(arg_p[t]));
		if (rc1){
			printf("ERROR; return code from pthread_create() is %d\n", rc1);
			exit(EXIT_FAILURE);
		}
	}
	
	/* Wait for All Theads to Finish */
	for(t=0;t<num_threads;t++){
         pthread_join(cthreads[t],NULL);
    }
    for(t=0;t<num_threads;t++){
	pthread_join(pthreads[t],NULL);
    }
    

    fclose(outputfp);
   
    printf("All of the threads were completed!\n");
    queue_cleanup(&q);

    return 0;
 
}
int main(int argc, char* argv[]){
    
    // Check to make sure user provided the minimum number of arguments
    if(argc < MINARGS){
		fprintf(stderr, "Not enough arguments: %d\n", (argc - 1));
		fprintf(stderr, "Usage:\n %s %s\n", argv[0], USAGE);
		return EXIT_FAILURE;
    }

    // Open file to write, this clears the output file
	FILE* file = NULL;
	file = fopen(argv[argc-1], "w");
	fclose(file);

	// Initialize semaphores
	sem_init(&_queue, 0, 1);
	sem_init(&_requesterThreads, 0, 1);

	// Initialize the queue
    const int qSize = TEST_SIZE;
    if(queue_init(&_q, qSize) == QUEUE_FAILURE){
		fprintf(stderr,
		"error: queue_init failed!\n");
    }

    int numResolvers = MIN_RESOLVER_THREADS; // Set the number of resolvers 2
	int numCores = sysconf(_SC_NPROCESSORS_ONLN); // The number of cores in the CPU
	if (numCores <= MAX_RESOLVER_THREADS && numCores >= MIN_RESOLVER_THREADS){
		numResolvers = numCores; // Set number of resolver threads to number of cores
	}

    int num_input_files = argc-2;
 	pthread_t file_threads[num_input_files];
 	pthread_t resolver_threads[numResolvers];
 	int rc;
 	_num_closed = num_input_files;

 	// Create a requester thread for every input file
 	// Assign it to the file
    for(int t=0;t<num_input_files;t++){
		rc = pthread_create(&(file_threads[t]), NULL, requester, (void*) argv[t+1]);
		if (rc){
		    printf("ERROR; return code from pthread_create() is %d\n", rc);
		    exit(EXIT_FAILURE);
		}
    }

    // Create a resolver thread to coorespond to the number of cores
    for(int k=0;k<numResolvers;k++){
    	rc = pthread_create(&(resolver_threads[k]), NULL, resolver, (void*) argv[argc-1]);
    	if (rc){
    		printf("ERROR; return code from pthread_create() is %d\n", rc);
    		exit(EXIT_FAILURE);
    	}
    }

    // Wait for all requester threads to finish, join
    for(int a=0;a<num_input_files;a++){
		pthread_join(file_threads[a],NULL);
    }
    printf("All of the file threads were completed!\n");

    // Wait for all resolver threads to finish, join
    for(int b=0;b<numResolvers;b++){
		pthread_join(resolver_threads[b],NULL);
    }
    printf("All of the resolver threads were completed!\n");

    // Destroy semaphores and cleanup queue
   	sem_destroy(&_queue);
	sem_destroy(&_requesterThreads);
	queue_cleanup(&_q);

    return EXIT_SUCCESS;
}
Exemplo n.º 5
0
int main(int argc, char* argv[]){
	// open output file. 
	FILE* outputfp;
	outputfp = fopen(argv[argc-1], "w");
	if(!outputfp){
		perror("Error Opening Output File");    
		return EXIT_FAILURE;
	}

	/* Create two thread pools for five threads each for a total of 10 threads. */
	pthread_t requesterThreads[NUM_THREADS];
	pthread_t resolverThreads[NUM_THREADS];
	/* Declare local vars */
	int rc;
	long t;

	/* Initialize input file queue */
	if (queue_init(&inputQueue, QSIZE) == QUEUE_FAILURE){
		fprintf(stderr, "fileQ failed to initialize \n");
	}

	/* Create a requester thread pool for reading in the files */
	/* creates 5 threads that start exicuting the requester function. */
	/* one thread for each input file. */
	for(t=0; t<NUM_THREADS; t++){
		printf("In main: creating requester thread %ld\n", t);
		rc = pthread_create(&(requesterThreads[t]), NULL, requester, argv[t+1]);
		if (rc){
			printf("ERROR: return code from pthread_create() is %d\n", rc);
			exit(EXIT_FAILURE);
		}
	}
	// Create a resolver pool for performing DNS calls from each line
	/* Create 5 threads that exicute the resolver funciton. */
	/* The threads constantly try to pop from the queue */
	for(t=0;t<NUM_THREADS;t++){
		printf("In main: creating resolver thread %ld\n", t);
		rc = pthread_create(&(resolverThreads[t]), NULL, resolver, outputfp);
		if (rc){
			printf("ERROR: return code from pthread_create() is %d\n", rc);
			exit(EXIT_FAILURE);
		}
	}

	/* Wait for All Theads to Finish */
	for(t=0;t<NUM_THREADS;t++){
		// waits for the given requester thread to terminate. 
		// a requester thread terminates when all of the hostnames are pushed onto the queue. 
		pthread_join(requesterThreads[t],NULL);
	}
	printf("All of the requester threads were completed!\n");
	
	requester_running = FALSE;
	queue_close(&inputQueue);	// this sets the queue finished flage = 1

	for(t=0;t<NUM_THREADS;t++){
		// waits for the given resolver thread to terminate. 
		// a resolver thread terminates when queue_pop returns NULL or when the queue is empty and requester_running is FALSE
		// requester_running is set to FALSE before this code exicutes.  
		pthread_join(resolverThreads[t],NULL);
	}
	printf("All of the resolver threads were completed!\n");

	queue_cleanup(&inputQueue);
	fclose(outputfp);

	return EXIT_SUCCESS;
}
Exemplo n.º 6
0
int main(int argc, char* argv[]) {
    
    /* Check Arguments */
    if(argc < MINARGS){
		fprintf(stderr, "Not enough arguments: %d\n", (argc - 1));
		fprintf(stderr, "Usage:\n %s %s\n", argv[0], USAGE);
		return EXIT_FAILURE;
    }

    /* Local Vars */
    FILE* outputfp = NULL;
    queue request_queue;
    int request_queue_size = QUEUEMAXSIZE;
    pthread_t threads_request[argc - 1];
    pthread_t threads_resolve[MAX_RESOLVER_THREADS];
    int i;

    /* Open Output File */
    outputfp = fopen(argv[(argc - 1)], "w");
    if(!outputfp){
		perror("Error Opening Output File");
		return EXIT_FAILURE;
    }

    // initialize request queue
    queue_init(&request_queue, request_queue_size);

    // create mutex queue and mutex output file
    pthread_mutex_t mutex_queue = PTHREAD_MUTEX_INITIALIZER;
    pthread_mutex_t mutex_ofile = PTHREAD_MUTEX_INITIALIZER;

    // create requester thread for each requester file or input file
    thread_request_arg_t req_args[argc - 2];
    // will open input files in an array
    for(i = 1; i < (argc - 1); i++){
		// create requester threads with names, request queue, and
		// mutex
        req_args[i - 1].fname = argv[i];
        req_args[i - 1].request_queue = &request_queue;
        req_args[i - 1].mutex_queue = &mutex_queue;
    // use pthread_create to create a new thread with default attributes
    // with NULL
    // successful call will store pid of thread
    // failed call will result in undefined thread
	int rc = pthread_create(&(threads_request[i - 1]), NULL, InputThread, &(req_args[i - 1]));
	if (rc){
		printf("Error creating request thread: return code from pthread_create() is %d\n", rc);
		exit(EXIT_FAILURE);
	}
   }

    // create resolver threads with MAX_RESOLVER_THREADS as limit
    thread_resolve_arg_t res_args;
    // again create resolver threads with name, request queue, output 
    // file, mutex files
    res_args.rqueue = &request_queue;
    res_args.outputfp = outputfp;
    res_args.mutex_ofile = &mutex_ofile;
    res_args.mutex_queue = &mutex_queue;
    for(i = 0; i < MAX_RESOLVER_THREADS; i++) {
		int rc = pthread_create(&(threads_resolve[i]), NULL, OutputThread, &res_args);
		if (rc){
			printf("Error creating resolver thread: return code from pthread_create() is %d\n", rc);
			exit(EXIT_FAILURE);
	}
   }

    // merge requester threads
    // wait for requester threads to finish
    for(i = 0; i < (argc - 2); i++){
		// use pthread_join to wait for request threads to complete
		// successsful => 0
		// otherwise error due to deadlock, etc.
		int rv = pthread_join(threads_request[i], NULL);
	if (rv) {
        fprintf(stderr, "Error: pthread_join on requester thread returned %d\n", rv);
    }
   }
   
   
   // at this point, the queue is done, so set to true
   // no more jobs
    request_queue_finished = true;
    
	// create output writing thread and output parameter structs
    for(i = 0; i < MAX_RESOLVER_THREADS; i++){
		int rv = pthread_join(threads_resolve[i], NULL);
	if (rv) {
        fprintf(stderr, "Error: pthread_join on resolver thread returned %d\n", rv);
    }
   }
   

    // cleanup request queue
    queue_cleanup(&request_queue);

    /* Close output file */
    fclose(outputfp);

    // destroy mutex queue and mutex out files
    pthread_mutex_destroy(&mutex_queue);
    pthread_mutex_destroy(&mutex_ofile);

    return EXIT_SUCCESS;
}
Exemplo n.º 7
0
int main(int argc, char* argv[]) {
	//Local variables
	int i;
	int numfiles = (argc - 2);
	int numCores = sysconf(_SC_NPROCESSORS_ONLN);
	if (numCores < 2){
		numCores = 2;
	}
	pthread_t req_threads[numfiles];
	pthread_t* res_threads; // Pointer threads because determined with malloc, at runtime.

	res_threads = malloc (numCores * sizeof(pthread_t));
	int rc; // race condition
	

	//Check Arguments
    if(argc < MINARGS)
    {
	fprintf(stderr, "Not enough arguments: %d\n", (argc - 1));
	fprintf(stderr, "Usage:\n %s %s\n", argv[0], USAGE);
	return EXIT_FAILURE;
    }
    
    //initialize data structures: queue
	requesterQ = malloc(sizeof(queue));
    if(queue_init(requesterQ,50) == QUEUE_FAILURE ) return EXIT_FAILURE;

    //Open Output File */
    outputfp = fopen(argv[(argc-1)], "w");
    if(!outputfp){
	perror("Error Opening Output File");
	return EXIT_FAILURE;
    }
	//Loop Through Input Files
    for (i = 0; i < numfiles; i++)
    {
		//check that the File is valid
		FILE* current = fopen(argv[i], "r");
		if(!current){
	    fprintf(stderr, "Error Opening Input File \n");
	    return EXIT_FAILURE;
		}
		
		//Create the requester threads	
    	rc = pthread_create(&(req_threads[i]), NULL, ReqThreads, argv[i+1]);
    	if (rc)
    	{
    		printf("Error: Return code from pthread_create() is %d\n", rc);
    		exit(EXIT_FAILURE);
    	}
    }
	//Create the resolver threads
    for (i = 0; i < numfiles; i++)
    {
    	rc = pthread_create(&(res_threads[i]), NULL, ResThreads, argv[i+1]);
    	if (rc)
    	{
    		printf("Error: Return code from pthread_create() is %d\n", rc);
    		exit(EXIT_FAILURE);
    	}
    }

    //Join requester threads
    for (i = 0; i < numfiles; i++)
    {
    	pthread_join(req_threads[i], NULL);
    }
    SearchComplete = 1;

   //Join the requester threads and resolver threads, making them program
	// sit and wait for the subprocess threads to finish enqueuing and dequeing
	//searching, and copying into the output file
    for (i = 0; i < numCores; i++)
    {
    	pthread_join(res_threads[i], NULL);
    }
	pthread_mutex_destroy(&Q_lock);
    pthread_mutex_destroy(&Out_lock);
    
    //Close Output File 
    fclose(outputfp);
    queue_cleanup(requesterQ); //Clear queue
    free(requesterQ);
    free(res_threads); // Free memory allocation

    return 0;
} 
Exemplo n.º 8
0
static void
valhalla_mrproper (valhalla_t *handle)
{
  unsigned int i;
  fifo_queue_t *fifo_o;

  fifo_queue_t *fifo_i[] = {
    vh_scanner_fifo_get (handle->scanner),
    vh_dbmanager_fifo_get (handle->dbmanager),
    vh_dispatcher_fifo_get (handle->dispatcher),
    vh_parser_fifo_get (handle->parser),
#ifdef USE_GRABBER
    vh_grabber_fifo_get (handle->grabber),
    vh_downloader_fifo_get (handle->downloader),
#endif /* USE_GRABBER */
    vh_event_handler_fifo_get (handle->event_handler),
  };

  vh_log (VALHALLA_MSG_VERBOSE, __FUNCTION__);

  if (!handle)
    return;

  fifo_o = vh_fifo_queue_new ();
  if (!fifo_o)
    return;

#ifdef USE_GRABBER
  vh_dbmanager_db_begin_transaction (handle->dbmanager);

  /* remove all previous contexts */
  vh_dbmanager_db_dlcontext_delete (handle->dbmanager);
#endif /* USE_GRABBER */

  /*
   * The same data pointer can exist in several queues at the same time.
   * The goal is to identify all unique entries from all queues and to save
   * these entries in the fifo_o queue.
   * Then, all data pointers can be safety freed (prevents double free).
   */
  for (i = 0; i < ARRAY_NB_ELEMENTS (fifo_i); i++)
  {
    int e;
    void *data;

    if (!fifo_i[i])
      continue;

    vh_fifo_queue_push (fifo_i[i],
                        FIFO_QUEUE_PRIORITY_NORMAL, ACTION_CLEANUP_END, NULL);

    do
    {
      e = ACTION_NO_OPERATION;
      data = NULL;
      vh_fifo_queue_pop (fifo_i[i], &e, &data);

      switch (e)
      {
      case ACTION_DB_INSERT_P:
      case ACTION_DB_INSERT_G:
      case ACTION_DB_UPDATE_P:
      case ACTION_DB_UPDATE_G:
      case ACTION_DB_END:
      case ACTION_DB_NEWFILE:
      {
        file_data_t *file = data;
        if (!file || file->clean_f)
          break;

        file->clean_f = 1;
        vh_fifo_queue_push (fifo_o, FIFO_QUEUE_PRIORITY_NORMAL, e, data);

#ifdef USE_GRABBER
        /* save downloader context */
        if (file->step < STEP_ENDING && file->list_downloader)
          vh_dbmanager_db_dlcontext_save (handle->dbmanager, file);
#endif /* USE_GRABBER */
        break;
      }

      case ACTION_DB_EXT_INSERT:
      case ACTION_DB_EXT_UPDATE:
      case ACTION_DB_EXT_DELETE:
      case ACTION_EH_EVENTOD:
      case ACTION_EH_EVENTMD:
      case ACTION_EH_EVENTGL:
        vh_fifo_queue_push (fifo_o, FIFO_QUEUE_PRIORITY_NORMAL, e, data);
        break;

      default:
        break;
      }
    }
    while (e != ACTION_CLEANUP_END);
  }

#ifdef USE_GRABBER
  vh_dbmanager_db_end_transaction (handle->dbmanager);
#endif /* USE_GRABBER */

  queue_cleanup (fifo_o);
  vh_fifo_queue_free (fifo_o);

  /* On-demand queue must be handled separately. */
  fifo_o = vh_ondemand_fifo_get (handle->ondemand);
  if (!fifo_o)
    return;
  queue_cleanup (fifo_o);
}