Esempio n. 1
0
void *consumer_thread(void *data) {
  queue_t *q = (queue_t*)data;

  int c;

  while ((c = queue_consume(q)) >= 0) {
    printf("Consumed data: %d\n", c);
  }

  printf("Consumer thread terminated\n");

  return NULL;
}
void* lcs_length_pthreads(lcs_task_t* task) {

    int p = task->p;
    int r = task->r;
    prodcon_queue_t* q = task->q;
    grid_t* grid = task->grid;

    int w = ceil((double)grid->m / (p*p));
    int h = ceil((double)grid->n / p);
    int y = r*h + 1;

    prodcon_queue_init( &q[r], ceil((double)grid->m /w) );

    pthread_barrier_wait( task-> bar );

    int next = 0;
    while( next <= grid->m ) {

        if(r == 0) {
            next = (next == 0 ? 1 : next + w);
        } else {
            next = queue_consume( &q[r] );
        }


        //printf("%i: next=%i\n", r, next);

        if( next <= grid->m) {
            int x = next;

            if(x + w > grid->m) {
                w = grid->m - x + 1;
            }

            if(y + h > grid->n) {
                h = grid->n - y + 1;
            }


            // calculate this block
            //printf("%i: calc block y=[%i,%i], x=[%i,%i]\n", r, y, y + h - 1, x, x + w - 1);
            lcs_length_block(grid, y, x, h, w);
        }

        if(r != p - 1) {
            queue_produce( &q[r+1], next );
        }
    }

    prodcon_queue_destroy( &q[r] );
}
void lcs_backtrack_pthreads(lcs_task_t* task) {

    int p = task->p;
    int r = task->r;
    prodcon_queue_t* q = task->q;
    grid_t* grid = task->grid;
    char* res = task->res;


    int h = ceil((double)grid->n / p);
    int y = r*h + 1;

    if(y + h > grid->n) {
        h = grid->n - y + 1;
    }

    prodcon_queue_init( &q[r], 2 );

    pthread_barrier_wait( task->bar );

    backtrack_state_t* state;
    if(r == p - 1) {
        state = malloc( sizeof(backtrack_state_t) );
        state->i = grid->n;
        state->j = grid->m;
        state->pos = 0;
        state->res = res;
    } else {
        state = queue_consume( &q[r] );
    }

    int min_i = r*h;
    int min_j = 0;

    lcs_backtrack_block(grid, state, min_i, min_j);

    if( r != 0 ) {
        queue_produce( &q[r-1], state );
    } else {
        strrev(state->res);
        free( state );
    }

    prodcon_queue_destroy( &q[r] );
}
Esempio n. 4
0
void* pref_main (void* arg) {
	int thread_id = *((int*)arg);
	free(arg);
	while (1) {
		char guess[MAX_REQUEST]; // Buffer for guessed filename
		int size = 0;

		void* item = queue_consume( &prefetch_queue );
		
		// this happens only if queue_consume_all() was called and no items remain.
		if( item == (void*)-1 ) {
			printf( "Prefetcher exiting.\n" );
			pthread_exit( (void*)NULL );
		}
		
		request_t* req = (request_t*)item;

		if (nextguess(req->filename, guess) != 0) { 
			fprintf(stderr, "Prefetcher failed to get next guess\n");
			fprintf(stderr, " --filename: %s\n", req->filename );
			fprintf(stderr, " --guess: %s\n", guess );
		} else {
			// try to get the cache entry for the guess. if it isn't there
			// then put it there.
			char* data;
			if (get_cache(guess, &data) == -1) {
				size = get_file_data(guess, &data);
				put_cache(guess, data, size);
			}
		}

		// free request created by dispatch.
		free( req->filename );
		free( req );
			
	}

}
Esempio n. 5
0
void* worker_main (void* arg) {
	int thread_id = *((int*)arg);
	free(arg);

	while (1) {
		void* item = queue_consume( &request_queue );
		
		// this happens only if queue_consume_all() was called and no items remain.
		if( item == (void*)-1 ) {
			printf( "Worker exiting.\n" );
			pthread_exit( (void*)NULL );
		}
		
		request_t* req = (request_t*)item;
		char* buf = NULL;
		
		// if cache is enabled then try to get the cached entry.
		int size = config.cache_enabled ? get_cache( req->filename, &buf ) : -1;
		int cache_hit = (size != -1);
		
		// if it wasn't a hit, try to get the data from file.
		if( !cache_hit ) {
			size = get_file_data( req->filename, &buf );
			
			if( size == -1 ) {
				// file does not exist. send result and move on.
				log_message( thread_id, req->req_num, req->fd, cache_hit, req->filename, 404);
				return_error( req->fd, "404 - File not found" );
				
				continue;
			} 
			
			// if cache is enabled then put the value into the cache.
			if( config.cache_enabled ) {
				if( put_cache( req->filename, buf, size ) == -1 ) {
					// error. I think we can keep going?
					fprintf( stderr, "error putting into cache %s\n", req->filename );
					
					// do not escape. this is not a fatal error, so continue sending
					// request to client.
				}
			}
		} 
		
		char* content_type = get_content_type( req->filename );


		// send result and log.
		log_message(thread_id, req->req_num, req->fd, cache_hit, req->filename, size);
		if (return_result( req->fd, content_type, buf, size ) != 0) {
			fprintf(stderr, "Error returning request to client.");
		}

		// only put stuff into prefetch if cache is enabled. otherwise just free the request.
		if( config.cache_enabled ) {
			if( queue_produce( &prefetch_queue, req ) == -1 ) {
				fprintf( stderr, "Error putting request into prefetch queue '%s'\n", req->filename );
			}
		} else {
			// free request created by dispatch.
			free( req->filename );
			free( req );
			
			// free the buffer.
			free( buf );
		}
	}

}