简体   繁体   English

C 中的循环调度程序

[英]Round Robin Scheduler in C

I need a bit of help with my round robin scheduler.我的循环调度程序需要一些帮助。 It is meant to take external C files and place them in a queue while displaying their current state, and execute them through round robin scheduling.它的目的是获取外部 C 文件并将它们放入队列中,同时显示其当前 state,并通过循环调度执行它们。 Here is what I have so far:这是我到目前为止所拥有的:

#include<pthread.h>
#include<stdio.h>
#include<string.h>
#include<unistd.h>

enum states {running, ready, waiting, idle};

struct process{
    //command to run process
    enum states state;
    int id;

};

struct process procs[5];
int procSize = 5;

void *runProcess(struct process *p)
{
    while(1)
    {
        printf("Process %i is running", p->id);
        sleep(1);
    }
}

void *takeInput(void *vargp)
{
    //print process statuses when 'procs' is entered
    while(1)
    {
        char *str[64];
        fgets(str, 64, stdin);

        if (strcmp(str, "procs"))
        {
            for (int i = 0; i < procSize; i++)
            {
                printf("Process %i: %i\n", procs[i].id, procs[i].state);
            }
        }
    }
}

void *schedule(void *vargp)
{
    struct process p = procs[0];
    if (p.state == idle)
    {
        p.state = ready;
    }
    if (p.state == ready)
    {
        p.state = running;
        pthread_t run;
        pthread_create(&run, NULL, runProcess, &p);
        //pthread_join(run, NULL);
        sleep(5);
        pthread_cancel(run);
        p.state = ready;

        for(int i = procSize - 1; i > 0; i--)
        {
            procs[i-1] = procs[i];
        }
        procs[procSize] = p;
    }
    else if (p.state == waiting)
    {
        for(int i = procSize - 1; i > 0; i--)
        {
            procs[i-1] = procs[i];
        }
        procs[procSize] = p;
    }

}

int main()
{
    for (int i = 0; i < 5; i++)
    {
        struct process p;
        p.state = idle;
        p.id = i;
        procs[i] = p; 
    }

    pthread_t schedulerid;
    pthread_t inputid;

    pthread_create(&schedulerid, NULL, schedule, NULL);
    //pthread_join(schedulerid, NULL);
    pthread_create(&inputid, NULL, takeInput, NULL);
    //pthread_join(inputid, NULL);
}

When I attempt to run this, I get no errors, only warnings, and nothing happens.当我尝试运行它时,我没有收到任何错误,只有警告,但什么也没有发生。 What do I need to improve on?我需要改进什么? Is there an issue with trying to call functions using threads?尝试使用线程调用函数有问题吗? Any help is appreciated.任何帮助表示赞赏。

UPDATED WITH WARNINGS:更新警告:

Sched.c:35:20: warning: passing argument 1 of ‘strcmp’ from incompatible pointer type [-Wincompatible-pointer-types]
   35 |         if (strcmp(str, "procs"))
      |                    ^~~
      |                    |
      |                    char **
In file included from Sched.c:3:
/usr/include/string.h:137:32: note: expected ‘const char *’ but argument is of type ‘char **’
  137 | extern int strcmp (const char *__s1, const char *__s2)
      |                    ~~~~~~~~~~~~^~~~
Sched.c: In function ‘schedule’:
Sched.c:56:36: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type [-Wincompatible-pointer-types]
   56 |         pthread_create(&run, NULL, runProcess, &p);
      |                                    ^~~~~~~~~~
      |                                    |
      |                                    void * (*)(struct process *)
In file included from Sched.c:1:
/usr/include/pthread.h:200:15: note: expected ‘void * (*)(void *)’ but argument is of type ‘void * (*)(struct process *)’
  200 |       void *(*__start_routine) (void *),
      |       ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~

Lets fix those warnings让我们修复这些警告

    char *str[64];
    fgets(str, 64, stdin);

    if (strcmp(str, "procs"))

should be应该

    char str[64];
    fgets(str, 64, stdin);

    if (strcmp(str, "procs"))

then然后

    pthread_create(&run, NULL, runProcess, &p);

The function must be a void* func (void*) function 必须是一个void* func (void*)

So change run process to所以将运行过程更改为

void *runProcess(void *v)
{
    struct process *p =(struct process*)v;
    while(1)
    {
        printf("Process %i is running", p->id);
        sleep(1);
    }
}

ie pass a void * and cast it to what you need即传递一个 void * 并将其转换为您需要的

this compiles with no warning now.现在编译没有警告。

Finally put your joins back in最后把你的连接放回去

int pt1 = pthread_create(&schedulerid, NULL, schedule, NULL);
int pt2 = pthread_create(&inputid, NULL, takeInput, NULL);
printf("%d %d \n", pt1, pt2);
pthread_join(schedulerid, NULL);
pthread_join(inputid, NULL);

as a final note - you really must test the returns from all your system calls to make sure they worked最后一点 - 你真的必须测试所有系统调用的返回以确保它们有效

here it is running在这里它正在运行

pm100@paul-think:~/ut$ gcc ggg.c -g -lpthread
pm100@paul-think:~/ut$ ./a.out
Process 0 is running

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

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