Example #1
0
int main(int argc, char *argv[]) {

    if (argc < 2) {
        printf("You must provide a number of free slots\n");
        exit(1);
    }

    signal(SIGINT, intHandler);

    printf("%d\n", atoi(argv[1]));
    MY_LEN = atoi(argv[1]);

    setup_shared_memory();
    attach_shared_memory();

    init_shared_memory();

    job printJob;

    while (1) {

        int semvalue;
        sem_getvalue(&(shared_mem->full), &semvalue);
        if (semvalue == 0) {
            printf("No requests in buffer, printer sleeps\n");
        }

        sem_wait(&(shared_mem->full));
        sem_wait(&(shared_mem->binary));

        // take a job
        int index = shared_mem->headIndex;
        shared_mem->headIndex = (shared_mem->headIndex + 1) % shared_mem->bufferSize;

        job *bufferJob = &(shared_mem->buffer[index]);
        printJob.id = bufferJob->id;
        printJob.pages = bufferJob->pages;

        sem_post(&(shared_mem->binary));
        sem_post(&(shared_mem->empty));

        // print job
        printf("Printer starts printing %d pages from client %d at Buffer[%d]\n", printJob.pages, printJob.id, index);

        // go to sleep
        usleep(printJob.pages * TIME_PER_PAGE * 1000000);

        printf("Printer finishes printing %d pages from client %d at Buffer[%d]\n", printJob.pages, printJob.id, index);
    }

    return 0;
}
Example #2
0
int init_dir_sync(music_globals_t* music_globals){
	int status = 0;
	apr_status_t rv = 0;

	int i = 0;

	db_query_t* db_query;
	db_config_t* db_config;

	char dbd_error_message[255];
	const char* dbd_error;

	apr_thread_t* thread_sync_dir;

#ifdef WITH_MP3
	//Init MP3 support
	mpg123_init();
#endif

	//Find db_params by finding a query we need and connecting to the database that query depends on
	status = indexer_get_db_object (file_indexer, "song_id", &(file_db_obj));
	if(status != 0){
		return -9;
	}

	//For every music directory create a shared struct
	for(i = 0; i < music_globals->music_dirs->nelts; i++){
		dir_sh_stats_t* stats;
		dir_t* dir = &(((dir_t*)music_globals->music_dirs->elts)[i]);
		dir_sync_thread_t* dir_sync_thread = apr_pcalloc(music_globals->pool, sizeof(dir_sync_thread_t));

		//Setup dir_sync_thread
		dir_sync_thread->db_config = db_config;
		dir_sync_thread->pool = music_globals->pool;
		dir_sync_thread->error_messages = music_globals->error_messages;
		dir_sync_thread->dir = dir;

		//Setup Dir
		dir->shm_file = apr_pstrcat(music_globals->pool,music_globals->tmp_dir,"/mp_dir_sync[",apr_itoa(music_globals->pool, i),"]",NULL);
		status = setup_shared_memory(&(dir->shm),sizeof(dir_sh_stats_t),dir->shm_file, music_globals->pool);
		if(status != 0){
			return status;
		}

		
		//Create thread to connect to database and synchronize
		stats = (dir_sh_stats_t*)apr_shm_baseaddr_get(dir->shm);

		dir->stats = stats;

		stats->num_files = NULL;
		stats->sync_progress = 0.0;
		stats->files_scanned = 0;

		rv = apr_thread_create(&thread_sync_dir,NULL, sync_dir, (void*) dir_sync_thread, music_globals->pool);
		if(rv != APR_SUCCESS){
			return rv;
		}

		apr_pool_cleanup_register(music_globals->pool, dir->shm,(void*) apr_shm_destroy	, apr_pool_cleanup_null);
	}
	
	return status;
}