简体   繁体   English

模拟C中的Round-Robin调度

[英]Simulating Round-Robin scheduling in C

So, I was trying to simulate my own scheduling algorithm in C (in this case, simple round-robin scheduling), whilst essentially overriding the system-default scheduler for those programs.因此,我试图在 C 中模拟我自己的调度算法(在这种情况下,简单的循环调度),同时实质上覆盖这些程序的系统默认调度程序。 If it helps, I am only interested in the scheduling logic for two processes (created via two fork() calls).如果有帮助,我只对两个进程的调度逻辑感兴趣(通过两个fork()调用创建)。

The problem, put simply is: Schedule processes P1 and P2 from a parent process, via Round-Robin scheduling with time quantum q简单来说,问题是:通过具有时间量程q的循环调度,从父进程调度进程P1P2

I was using a call to kill() and SIGSTOP / SIGCONT as the primary way for the parent process to suspend and continue the two processes P1 and P2 , but for some reason, the OS scheduler still continues to schedule the processes after they've been signaled with SIGSTOP .我正在使用对kill()SIGSTOP / SIGCONT的调用作为父进程暂停和继续这两个进程P1P2的主要方式,但由于某种原因,操作系统调度程序仍然继续安排进程已通过SIGSTOP发出信号。 Here is a minimum-reproducible example:这是一个最小可重现的例子:

#include <stdlib.h>
#include "pthread.h"
#include <time.h>
#include <unistd.h>
#include <sys/time.h>
#include <stdio.h>
#include <sys/ipc.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/shm.h>
#include <stdbool.h>

int n = 10;
float currTime = 0;
float timeQuantum = 2;
pid_t pids[2];



typedef struct RRKeep{
  bool lastProcess;
  long long int lastSwitched;
} RRkeep;

RRkeep RRGlobalState = {1, 0};

long long current_timestamp() {
    struct timeval te;
    gettimeofday(&te, NULL); // get current time
    long long milliseconds = te.tv_sec*1000LL + te.tv_usec/1000; // calculate milliseconds
    // printf("milliseconds: %lld\n", milliseconds);
    return milliseconds;
}

void readFileWriteShm() {
  while(1)
    printf("This is the reader process\n");
}

void readShmWriteMultOp() {
  while(1)
    printf("This is the writer process\n");
}

void* RRscheduling() {
  // Should tell when to switch and which process to switch to
  while(1) {
    while(current_timestamp() - RRGlobalState.lastSwitched < timeQuantum);
    long long int tmp = current_timestamp();
    printf("%lld\n", tmp);
    kill(pids[RRGlobalState.lastProcess], SIGSTOP);
    kill(pids[!RRGlobalState.lastProcess], SIGCONT);
    RRGlobalState.lastProcess = !RRGlobalState.lastProcess;
    RRGlobalState.lastSwitched = tmp;
  }
}


int main(int argc, char *argv[]) {
  pids[0] = fork();
  if(pids[0]) {
    readFileWriteShm();
  }
  else {
    pids[1] = fork();
    if(!pids[1]) {
      pthread_t timer;
      pthread_create(&timer, NULL, &RRscheduling, NULL);
      pthread_join(timer, NULL);
      while(wait(NULL) > 0);
    }
    if(pids[1]) {
      readShmWriteMultOp();
    }
  }
  return 0;
}

So, my question is, how can I stop the OS scheduler from "interfering" in this case, and schedule the programs exclusively using my own scheduling algorithm?所以,我的问题是,在这种情况下如何阻止 OS 调度程序“干扰”,并使用我自己的调度算法专门调度程序?

Thank you for your time and help!感谢您的宝贵时间和帮助!

You have your PID tests backwards.您的 PID 测试倒退了。 if(pids[0]) is true in the parent (since fork returned a PID for the child to the parent) and is false in the child (since fork returned zero to the child). if(pids[0])在父项中为真(因为fork将子项的 PID 返回给父项)并且在子项中为假(因为fork向子项返回零)。 Yet, if it is true, you execute code you intend for the child.然而,如果它是真的,你执行你为孩子准备的代码。

Similarly, if(!pids[1]) is true in the child and false in the parent, yet, if it is true, you execute code you intend for the parent.类似地, if(!pids[1])在子级中为 true,在父级中为 false,但是,如果它为真,则执行您打算为父级执行的代码。

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

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