简体   繁体   English

计算ANSI C中先到先服务的等待时间

[英]Calculating Wait Time for a First Come First Serve in ANSI C

While programming a CPU Scheduling in ANSI C for a linux environment, I have not been able to calculate the wait time for each queued process. 在针对Linux环境的ANSI C中对CPU调度进行编程时,我无法计算每个排队进程的等待时间。 Basically, I need to run 20+ processes with 4 cpus, and measure the time it takes for each process, average time it takes, CPU consumption, etc. 基本上,我需要以4 cpus运行20多个进程,并测量每个进程花费的时间,平均花费的时间,CPU消耗等。

Here is an analogy for the process that I am trying to code: 这是我尝试编写的过程的类比:

Click me for the image 点击我查看图片

The image I drew was trying to simplify my problems... but this is a first come first serve CPU schedualer, counter A is the CPU burst, counter B is the Device Burst, line 1 is Ready Queue, line 2 is Device Queue... 我绘制的图像试图简化我的问题...但这是先到先服务的CPU调度员,计数器A是CPU突发,计数器B是设备突发,行1是就绪队列,行2是设备队列。 ..

Till now, I'm able to output the tasks into an array, but for my assignment I need to find out how long it took for each of the people to wait, the turn around time for each processes, and their average times. 到现在为止,我已经能够将任务输出到一个数组中,但是对于我的任务,我需要找出每个人需要等待多长时间,每个流程的周转时间及其平均时间。

Each person struct have an "arrivalTime" and can be changed anytime Each person struct also have an array of time required at each counter "timeRequired[]", it's alternating, index 0 is for counter A, index 1 is for counter B, index 2 is for A again. 每个人结构都有一个“ arrivalTime”并且可以随时更改每个人结构在每个计数器“ timeRequired []”上也有一个所需的时间数组,它是交替的,索引0代表计数器A,索引1代表计数器B,索引2再次代表A。 Each person struct have an array pointer for the timeRequired[], "arrayPointer" 每个人的结构都有一个timeRequired [],“ arrayPointer”的数组指针

The picture I drew says seconds, but it is actually any unit of time, an integer number, I don't really need the actual time clock... 我画的照片说的是秒,但实际上是任何时间单位,一个整数,我真的不需要实际的时钟...

What I did so far is I created 2 thread function, 1 for counterA and 1 for counterB, and when ever A finished serving a person in line, A will stuff the person to B line, when ever B finished a person, it will stuff it into the A line. 到目前为止,我要做的是创建2个线程函数,其中1个用于counterA,1个用于counterB,当A完成为某人服务时,A会将人员塞入B行,而当B完成某人时,它将塞入人员。它进入A线。

I feel like everything is there for me already, but I really dont know what I need to do from here on to calculated their wait times... 我觉得一切都已经准备就绪,但是我真的不知道从现在开始我需要做什么来计算他们的等待时间...

Or is there a easier solution than using threads? 还是有比使用线程更简单的解决方案?

The professor gave us 2 files 教授给了我们2个档案

the helper methods 辅助方法

[ http://pastebin.com/qF7nQsUR] [ http://pastebin.com/qF7nQsUR]

the header file 头文件

[ http://pastebin.com/nQQNXnmq] [ http://pastebin.com/nQQNXnmq]

gettimeofday(2). gettimeofday(2)。

somewhere inside your process' struct 过程结构中的某处

struct timeval bornon;    /* time this process was born */

and then call gettimeofday on it in your allocation path 然后在您的分配路径中调用gettimeofday

if (gettimeofday(&process->bornon, NULL) == -1)
    err(1, "ohnoes! gettimeofday!");

and then call it again when your process is done 然后在处理完成后再次调用它

struct timeval doneon;
int secs, usecs;

gettimeofday(&doneon, NULL);
secs = doneon.tv_secs - process->bornon.tv_secs;
usecs = doneon.tv_usecs - process->bornon.tv_usecs;

and then do conversions for seconds/microseconds. 然后进行秒/微秒的转换。

I'd approach it this way: 我会这样处理:

Figure out how to encode the input. 弄清楚如何编码输入。 The program needs to have the input "person 1 needs..." available to it in some way. 该程序需要以某种方式使输入“人1需要...”可用。
These definitions can work: 这些定义可以起作用:

struct need {
   int person_id, counter_id, duration
};
struct need needs[] = {
   { 1, COUNTER_A, 500 },
   { 1, COUNTER_B, 200 },
...
};

Next, you need to keep track of where each person is at any time. 接下来,您需要随时跟踪每个人的位置。
This structure can describe what a person's doing now: 这种结构可以描述一个人现在在做什么:

struct person_activity {
    int person_id;
    struct need *activity;
    int start_time;
}

An array of these can describe what each is doing at any tie 这些数组可以描述每个领带在做什么

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM