- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的 C 代码。我正在尝试模拟 FCFS 调度程序。通过读取命令行参数,我创建了一个计时器来模拟时间。每次它增加时,我都会检查进程是否到达(使用数组),如果到达,我将其放入 LinkedList 中以表示就绪队列。此外,每次计时器增加时,它都会检查进程是否完成,如果完成,则将其从链表的前面弹出。这就是我的问题所在。它错误地将其弹出。
我正在使用 ./a.out input.dat text.txt 5 0.4 作为我的测试执行。这是我的 input.dat:
1 1 10 50
2 2 0 40
3 3 20 50
4 4 7 35
5 5 10 50
6 6 0 40
7 7 20 50
8 9 7 35
9 10 10 50
10 12 0 40
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define n 10
char *cRR;//get the argument for RR time
char *calpha;//get the argument for alpha
int termination[n];
int full[40];
int arraylength;
int RR;//to hold RR time as an int
int i=0;//used to count amount of numbers in test file
double alpha;//hold alpha time as a double
int at[n];
typedef int LL_pid; // the pid of the PCB
typedef int LL_AT;//the arrival time of the pcb
typedef int LL_priority;//the priority of that PCB
typedef int LL_BT;//the Burst time of that PCB
typedef int LL_termination;
typedef struct LL PCB; //the PCB
struct LL //linked list structure(pcb)
{
LL_pid pid;
LL_AT AT;
LL_priority priority;
LL_BT BT;
LL_termination termination;
PCB *next;//points to next pcb in linked list
};
struct job//used for job queue
{
int pid2;
int AT2;
int priority2;
int BT2;
};
int LL_length(PCB *pcb_head); //linked list length
void LL_add(PCB **pcb_head,LL_pid ppid, LL_AT AAT, LL_priority ppriority, LL_BT BBT, LL_termination ttermination); //adds PCB to front of list
void LL_pop(PCB **pcb_head); //removes the head from the list & returns its value
void LL_print(PCB **pcb_head); //prints all the processes
void LL_clear(PCB **pcb_head); //deletes all the processes from queue
void LL_append(PCB **pcb_head,LL_pid ppid, LL_AT AAT, LL_priority ppriority, LL_BT BBT,LL_termination ttermination); //adds a process to the end of the list
int LL_elem(PCB **pcb_head, LL_pid d); //checks for the pcb via pid
void add_jobs(int jobss[]);
struct job jobs[n];//contains all the jobs an array
void find_AT(struct job jobss[],int length);
void fcfs();
void terminationfcfs();
void checktermination(PCB **pcb_head, int i);
int main ( int argc, char *argv[] )
{
PCB *pcb_head=NULL;
//*****START COMMAND LINE ARGS
if ( argc != 5 )//if they incorrectly inputed arguments
{
/* We print argv[0] assuming it is the program name */
printf( "usage: %s filename, output file name, RR quantum as a int, Alpha value as a float", argv[0] );
}
else
{
cRR=argv[3];//get arg for RR time
calpha=argv[4];//get arg for alpha
alpha=atof(calpha);//convert alpha to float
RR=atoi(cRR);//convert RR to int
// We assume argv[1] is a filename to open
FILE *file = fopen( argv[1], "r" );//open file which is 2nd arg
// fopen returns 0, the NULL pointer, on failure
if ( file == 0 )
{
printf( "Could not open file\n" );
} else
{
int y=0;//used for while loop below
while ( fscanf(file,"%d",&y)!=EOF )//scan file and find all the numbers
{
full[i]=y;//insert the numbers into an array
i++;
}
fclose( file );//done using the file
}
/*TEST PRINTING EVERYTHING IN FILE
int testprint=0;
for(testprint=0;testprint<i;testprint++){printf("%d\n",full[testprint]);}
printf("\nend of array check");*/
//write to file
FILE *fp;
fp = fopen(argv[2], "w+");
fprintf(fp, "This is testing for fprintf...\n");
fclose(fp);
}
/////////////////END OF COMMAND ARGUMENT THINGS
add_jobs(full);//adds all inputs from file to the array "jobs"
//START OF FCFS
int timer=0;
int locator = 0;
terminationfcfs();//get all termination times
for(timer=0;timer<100;timer++){
printf("Time %d : ", timer);
checktermination(&pcb_head,timer);
for(locator=0;locator<10;locator++){//every second checks each 10 values if it is their term(using linked list) or arrival time using array.
//IF ARRIVAL TIME ADD TO LINKED LIST THIS IS WORKING 5-25 2:14, same as print, something wrong with check termination
if (jobs[locator].AT2==timer){
LL_append(&pcb_head,jobs[locator].pid2,jobs[locator].AT2,jobs[locator].priority2,jobs[locator].BT2,termination[locator]);
}
}
LL_print(&pcb_head);//this works
}//END OF FCFS
//LL_print(&pcb_head);
/*test find at
add_jobs(full);
find_AT(jobs,10);
int f=0;
while(f<10){printf("%d ", at[f]); f++;}
*/
/*TEST IF ADD JOBS WORKS)
add_jobs(full);
int f=0;
while(f<10){
printf("%d " , jobs[f].pid2);
printf("%d " , jobs[f].AT2);
printf("%d " , jobs[f].priority2);
printf("%d " , jobs[f].BT2);
printf("\n");
f++;
}*/
/*testing popping array info into linked list
jobs[0].pid2=5;
jobs[0].AT2=4;
jobs[0].priority2=3;
jobs[0].BT2=2;
LL_append(&pcb_head, jobs[0].pid2,jobs[0].AT2,jobs[0].priority2,jobs[0].BT2);
LL_print(&pcb_head);
*/
/*
//example usage:
LL_add(&pcb_head, 7,7,7,7,10); //push value 7 onto stack
printf("%d\n", pcb_head -> pid); //show stack head value
LL_add(&pcb_head, 21,21,21,21,31); //push value 21 onto stack
LL_print(&pcb_head); //print the stack
if(LL_elem(&pcb_head, 7)) puts("found 7"); //does 7 belong to the stack?
LL_append(&pcb_head, 0,0,0,0,50); //append 0 to the end of the stack
LL_print(&pcb_head); //print the stack
LL_pop(&pcb_head); //pop the stack's head
LL_print(&pcb_head); //print the stack
LL_clear(&pcb_head); //clear the stack
LL_print(&pcb_head); //print the stack
*/
getchar();
return 0;
}
int LL_length(PCB *pcb_head)
{
PCB *curr = pcb_head;//set a temp value to the head
int len = 0;
while(curr)
{
++len;//increase length variable
curr = curr -> next;//go to next and keep looping to get whole length
}
return len;
}
void LL_add(PCB **pcb_head,LL_pid ppid, LL_AT AAT, LL_priority ppriority, LL_BT BBT, LL_termination ttermination)
{
PCB *pcb_new = malloc(sizeof(PCB));//allocate memory for another structure (PCB)
pcb_new -> pid = ppid;//create a pcb with pid inserted
pcb_new -> AT = AAT;
pcb_new -> priority = ppriority;
pcb_new -> BT = BBT;
pcb_new -> termination = ttermination;
pcb_new -> next = *pcb_head;//set its next to the head(inserting in front of list)
*pcb_head = pcb_new;//set the pointer of head to it since it is in front of list now
}
void LL_pop(PCB **pcb_head)
{
PCB *pcb_temp = *pcb_head;
printf("Process %d is terminating", pcb_temp->pid);
*pcb_head = pcb_temp -> next;//get the next in list and set it to head
}
void LL_print(PCB **pcb_head)
{
PCB *pcb_temp = *pcb_head;
if(!pcb_temp)
puts("the list is empty");
else
{
// while(pcb_temp)
//{
printf("Process %d is executing...", pcb_temp -> pid);// print all the pids
// printf("%d ", pcb_temp -> AT);
// printf("%d ", pcb_temp -> priority);
// printf("%d ", pcb_temp -> BT);
//pcb_temp = pcb_temp -> next;//loop to next pcb*/
//}
putchar('\n');
}
}
void LL_clear(PCB **pcb_head)
{
while(*pcb_head)//this will get every pcb as pop below sets head value to next after deleting it
LL_pop(pcb_head);
}
void LL_append(PCB **pcb_head,LL_pid ppid, LL_AT AAT, LL_priority ppriority, LL_BT BBT, LL_termination ttermination)
{
PCB *pcb_temp = *pcb_head;//get head value
if(!pcb_temp)//if nothing in head value just add the pcb as list is empty
LL_add(pcb_head, ppid, AAT, ppriority, BBT, ttermination);
else
{
while(pcb_temp -> next)//get the last pcb
pcb_temp = pcb_temp -> next;
LL_add(&(pcb_temp -> next), ppid, AAT, ppriority, BBT, ttermination);//add it onto the last
}
}
void add_jobs(int jobss[]){
int i = 0;
int i2 = 1;
int i3 = 2;
int i4 = 3;
int x =0;
int x2=0;
int x3=0;
int x4=0;
while(x<10){
jobs[x].pid2=jobss[i];
x++;
i+=4;
}
while(x2<10){
jobs[x2].AT2=jobss[i2];
i2+=4;
x2++;
}
while(x3<10){
jobs[x3].priority2=jobss[i3];
i3+=4;
x3++;
}
while(x4<10){
jobs[x4].BT2=jobss[i4];
i4+=4;
x4++;
}
arraylength=x4;
}
void find_AT(struct job jobss[], int length){
int i=0;
for(i=0;i<length;i++){
if(jobss[i].BT2>0)
at[i]=jobss[i].AT2;
}
}
void terminationfcfs(){
//find termination
int h =1;
int storage;
storage= jobs[0].AT2+jobs[0].BT2;
termination[0]=storage;
while(h<10){
storage=storage+jobs[h].BT2;
termination[h]=storage;
h++;
}
}
void checktermination(PCB **pcb_head, int i){
PCB *pcb_temp = *pcb_head;//get head
while(pcb_temp){
if(pcb_temp->termination==i)
LL_pop(&pcb_temp);
else pcb_temp=pcb_temp->next;
}
}
int LL_elem(PCB **pcb_head, LL_pid x)
{
PCB *pcb_temp = *pcb_head;//get head
while(pcb_temp)
{
if(pcb_temp -> pid == x) //set for numbers, modifiable
return 1;
else
pcb_temp = pcb_temp -> next;
}
return 0;
}
最佳答案
您的错误与 LL_pop
本身无关,这没关系,只是您应该在弹出后释放节点内存:
void LL_pop(PCB **pcb_head)
{
PCB *pcb_temp = *pcb_head;
printf("Process %d is terminating\n", pcb_temp->pid);
*pcb_head = pcb_temp -> next;
free(pcb_temp);
}
(请记住,每个 malloc
应该有其相应的 free
。推送分配时,弹出应该释放分配的资源。)
错误出现在检查终止的例程中,您在其中调用LL_pop
:
LL_pop(&pcb_temp);
在这里,您传递局部变量的地址。更新本地指针将更新迭代器指针,但不会更新列表的头部或任何 next
指针。
不使用临时本地指针,并使用指向作为头传入的指针的指针进行迭代。这将增加一级间接性。指向指针的指针应该保存第一个节点的头指针的地址或后续节点的下一个指针的地址。它将始终保存指向当前节点的指针的地址,因此将更新正确的“传入”链接:
void checktermination(PCB **pcb_head, int i)
{
while (*pcb_head){
if((*pcb_head)->termination == i) {
LL_pop(pcb_head);
} else {
pcb_head = &(*pcb_head)->next;
}
}
}
关于c - FCFS 模拟器 我认为我的 pop 功能不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29861975/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!