コード例 #1
0
//
// New processes are allocated memory in the main_memory array. 
//
void memory_allocate(PCB new_arrival)
{
	int PID = new_arrival.get_id();
	int requested_memory = new_arrival.get_size();
	int free_memory = 0;

	while(main_memory[free_memory] != 0)
		free_memory++;

	if((MEM_SIZE - free_memory) >= requested_memory)
		for(int i = free_memory; i < free_memory + requested_memory; i++)
			main_memory[i] = PID;
	else
	{
		if (DEBUG) cout << "(memory_allocate): ERROR Not enough memory for process " << PID << endl;
	}

	// Memory has been allocated, update UI.
	state_changed_flag = true;
}
コード例 #2
0
// 
// Processes that are finished running have their memory removed. 
// Remaining memory is then shifted down. 
//
void memory_deallocate(PCB finished_process)
{
	int i = 0;
	int j;
	int PID = finished_process.get_id();
	while(main_memory[i] != PID)
		i++;
	j = i;
	while(main_memory[j] == PID)
	{
		main_memory[j] = 0;
		j++;
	}
	while(main_memory[j] != 0 && j < MEM_SIZE)
	{
		main_memory[i] = main_memory[j];
		main_memory[j] = 0;
		i++;
		j++;
	}

	// Memory has been deallocated, update UI.
	state_changed_flag = true;
}