max_mem[] = {120,31,50,4}
proc[] = {4,2,1,3}
我试图在C语言中使用Swapping X内存管理技术来实现FCFS,每次运行这个程序时,我都会得到一个 error : Segmentation Fault 11
. 另外,执行 max_mem[2]
只运行25次而不是50次。
下面是代码。
printf("\n *********************\n Swapping - X FCFS\n
*********************\n");
rem_proc = 0;
// Loading the Processes into the ram_mem and executing
for(int i = 0; i <n_processes ; i++){
bt_p = bt[i];
mem_p = max_mem[i];
percent_mem = ceil((max_mem[i]/max_mem_req)*100);
if (mem_p <= mem_rem && mem_p<=max_mem_req){
//Loop for Loading the Process into the RAM
for(int j = 0; j < mem_p; j++){
ram_mem[last_mem_pos] = proc[i];
mem_addr[j] = last_mem_pos;
last_mem_pos +=1;
mem_rem -=1; // Decreasing the number of pages remaining in the ram_mem
proc_start_loc[i] = mem_addr[0]; //Stores the Starting Index of Process in the RAM(Main Memory)
printf("Mem Addr PID = %d, addr = %d, mem_rem = %d \n",proc[i], mem_addr[j],mem_rem);
}
mem_p = 0;
}
if(mem_p == max_mem_req){
mem_rem = max_mem_req;
last_mem_pos = 0;
for(int a = 0; a<mem_p;a++){
ram_mem[a] = proc[i];
mem_addr[a] = last_mem_pos;
last_mem_pos +=1;
mem_rem -=1; // Decreasing the number of pages remaining in the ram_mem
proc_start_loc[i] = mem_addr[a+1]; //Stores the Starting Index of Process in the RAM(Main Memory)
mem_p -=1;
printf("Mem Addr PID = %d, addr = %d, mem_rem = %d \n",proc[i], mem_addr[a],mem_rem);
}
}
if(mem_rem == 0 && mem_p >0){
mem_rem = max_mem_req;
last_mem_pos = 0;
for(int a = 0; a<mem_p;a++){
ram_mem[a] = proc[i];
mem_addr[a] = last_mem_pos;
last_mem_pos +=1;
mem_p -=1;
mem_rem -=1; // Decreasing the number of pages remaining in the ram_mem
proc_start_loc[i] = mem_addr[a+1]; //Stores the Starting Index of Process in the RAM(Main Memory)
printf("Mem Addr PID = %d, addr = %d, count = %d \n",proc[i], mem_addr[a],mem_rem);
}
} //(max_mem_req - proc_start_loc[i-1])
int proc_ptr =0;
if(i!=0){
if(mem_rem<mem_p && mem_p<=max_mem_req){
proc_ptr = proc_start_loc[swapping_ptr];
last_mem_pos = proc_start_loc[proc_ptr];
if(proc_start_loc[proc_ptr] == 0){
mem_rem = max_mem_req - mem_rem;
}
else{
mem_rem += proc_start_loc[swapping_ptr];
}
for(int m =0; m<mem_p;m++){
ram_mem[proc_ptr] = proc[i];
mem_addr[m] = last_mem_pos;
last_mem_pos +=1;
mem_p -=1;
mem_rem -=1;
proc_start_loc[i] = mem_addr[m+1];
printf("Mem Addr PID = %d, addr = %d, count = %d \n",proc[i], mem_addr[m],mem_rem);
}
}
}
n_pages = ceil(max_mem[i]/4);
load_time = n_pages * 2;
// Loop for executing the loaded process
for(int k = 0; k<=burst_time[i]; k++){
if(bt_p>0){ // This if statement checks whether there is any burst time left to execute
// PRINT PROCESS EXECUTION INFORMATION HERE
bt_p -=1;
printf("%d, RUNNING, id = %d, remaining-time = %d, load_time = %d, mem-usage = %d %%\n", ct,proc[i],bt_p,load_time,(int)ceil((((float)max_mem[i]/(float)max_mem_req)*100.0)));
ct +=1;
makespan = ct;
}
else{
rem_proc = n_processes - (i+1);
printf("%d,FINISHED,id = %d, proc-remaining = %d\n", ct,proc[i],rem_proc);
printf("%d,EVICTED,id = %d\n", ct,proc[i]);
proc_finish_time[i]= ct;
}
}
}
for(int m = 0; m<n_processes; m++){
printf("PSL = %d\n", proc_start_loc[m]);
}