Example #1
0
/**
 * Declares the shared memory segment this process will be using and the files it will ever possibly open. This must be called before sfs_fopen().
 *
 * Parameters: 	sys_key - The ID of the shared memory segment initialized in sfs_init.
 * 				file_num - The number of files this process might open (or size of filenames)
 * 				filenames - The names of the files this process wishes to open (in the future).
 * Returns: 1 on success, 0 otherwise
 */
int sfs_declare(int sys_key, int file_num, char *filenames[]) {
	// Initialize shared memory pointers
	segment_id = get_segment_id(sys_key);
	if(segment_id == -1) return 0;

	shared_memory = (char *) shmat(segment_id, NULL, 0);
	if(shared_memory == (void *)-1) return 0;

	memory = (memory_layout *) shared_memory;

	mutex_lock(memory);

	// Create process node for this process
	node *process = create_process_node(memory);
	if(process == NULL) {
		mutex_unlock(memory);
		return 0;
	}

	// Create or get resource nodes for all files
	int i;
	for(i = 0; i < file_num; i++) {
		char *name = filenames[i];
		node *resource = find_or_create_file_node(memory, name);
		if(resource == NULL) {
			mutex_unlock(memory);
			return 0;
		}
		// Record that this process has claim edges to the given files
		node *list_node_cur = create_list_node(memory, resource, &process->out_edges);
		if(list_node_cur == NULL) {
			mutex_unlock(memory);
			return 0;
		}
	}

	mutex_unlock(memory);

	return 1;
}
Example #2
0
/**
 * Add an outgoing edge from the start node to the end node (process or resource)
 */
int add_out_edge(memory_layout *mem, node *start, node *end) {
	if(create_list_node(mem, end, &start->out_edges) != NULL) {
		return 1;
	}
	return 0;
}
int main(void)
{
	list* l = initialize_list();

	list_node* c_node = create_list_node("c");
	list_node* a_node = create_list_node("a");
	list_node* z_node = create_list_node("z");
	list_node* x_node = create_list_node("x");

	push_list_node(l, a_node);
	remove_internal_list_node(l, a_node);
	
	push_list_node(l, a_node);
	push_list(l, "b");
	push_list_node(l, c_node);
	unshift_list_node(l, z_node);
	unshift_list(l, "y");
	unshift_list_node(l, x_node);

	remove_internal_list_node(l, z_node);
	remove_internal_list_node(l, a_node);
	remove_internal_list_node(l, x_node);
	
	free_list_node(z_node);
	free_list_node(a_node);
	free_list_node(x_node);

	while(l->length > 0)
	{
		printf("%s\n", (char*)pop_list(l));
	}
	
	
	unsigned long dl;
	destroy_list(l, DESTROY_MODE_IGNORE_VALUES, &dl);


	printf("-------queue test-------------\n");	
	unsigned long priority;
	char* id;
	
	
	priority_queue* pq = initialize_priority_queue();
	push_priority_queue(pq, 30, "id_1", "value_1");
	push_priority_queue(pq, 10, "id_2", "value_2");
	push_priority_queue(pq, 10, "id_3", "value_3");
	push_priority_queue(pq, 40, "id_4", "value_4");
	push_priority_queue(pq,  5, "id_5", "value_5");
	push_priority_queue(pq, 30, "id_6", "value_6");
	push_priority_queue(pq, 30, "id_7", "value_7");

	
	printf("queue length = %ld\n", pq->length);
	unsigned long num_destroyed;

	char* tmp = peek_priority_queue(pq, &priority, &id, 0);
	printf("first is \"%s\"\n", tmp);


	set_priority_for_id_in_priority_queue(pq, "id_5", 35);
	
	tmp = peek_priority_queue(pq, &priority, &id, 0);
	printf("first is \"%s\"\n", tmp);
	
	set_priority_for_id_in_priority_queue(pq, "id_2", 36);

	tmp = peek_priority_queue(pq, &priority, &id, 0);
	printf("first is \"%s\"\n", tmp);

	/*
	char** values = (char**)destroy_priority_queue(pq, DESTROY_MODE_RETURN_VALUES, &num_destroyed);
	int index = 0;
	for(index = 0; values[index] != NULL; index++)
	{
		printf("%s\n", values[index]);
	}
	*/

	
	while(pq->length > 0)
	{
		char* value = (char*)shift_priority_queue(pq, &priority, &id);
		printf("%s\n", value);
		free(id);
	}
	destroy_priority_queue(pq, DESTROY_MODE_FREE_VALUES, &dl);
	
	return 0;
}