简体   繁体   中英

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. If it helps, I am only interested in the scheduling logic for two processes (created via two fork() calls).

The problem, put simply is: Schedule processes P1 and P2 from a parent process, via Round-Robin scheduling with time quantum q

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 . 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?

Thank you for your time and help!

You have your PID tests backwards. 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). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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