0

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. 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 *),
      |       ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
  • Read the man page for fgets, sleep, strcmp, pthread_create. They should indicate the header files you need to include and they'll instruct you on which parameters to pass. You're missing string.h and unistd.h. Not sure what else is wrong. – Jeff Holt Mar 30 '22 at 02:45
  • Adding new header files doesn't eliminate the warnings, nor does it change the output, or lack thereof. – Frahmtastic Mar 30 '22 at 02:55
  • "and they'll instruct you on which parameters to pass" – Jeff Holt Mar 30 '22 at 03:15
  • this code will make any compiler emit tons of errors. How exactly are you compiling and running it? Are you using an IDE, if so which one. If compiing on linux command line please show what commands you used to compile and run this code – pm100 Mar 30 '22 at 03:23
  • 1
    "Adding new header files doesn't eliminate the warnings," did you read the warnings? Would you be willing to share them with us – pm100 Mar 30 '22 at 03:26
  • Im using linux command terminal, and just using it to create a c file, output it to a file, and then ./filename to execute. I do not receive any errors when I execute, only warnings, and I don't get any output. Im reading the pages for sleep and pthread_create at the moment. – Frahmtastic Mar 30 '22 at 03:31
  • Most of them consist of incompatible pointer type warnings, lines 33, 35 and 56. – Frahmtastic Mar 30 '22 at 03:37
  • for a start you need to include ``, `` then you need to correct the calls to `fgets` and `pthread_create` so that the compiler no longer issues warnings (consider warnings as errors unless you know for sure why you are getting them, do not ignore them, the compiler is trying to help you) – pm100 Mar 30 '22 at 04:16
  • if you cannot get rid of the warnings please post updated code and include the actual pasted text of the error messages – pm100 Mar 30 '22 at 04:17
  • the immediate cause of your failure is that after starting the threads you exit main - thats gameover. You have to join those 2 threads, so that you wait while they run, you have the code commented out. Move the first join to be after the second pthread_create (otherwise you will never start the second thread) – pm100 Mar 30 '22 at 04:25
  • Ive tried these things and they do not have any effect – Frahmtastic Mar 30 '22 at 04:34
  • @Frahmtastic well I have tried them all and they make your code run and not terminate immediately and compile with no warnings – pm100 Mar 30 '22 at 04:36
  • I was able to get it working, thank you so much for your help! – Frahmtastic Mar 30 '22 at 04:53

1 Answers1

0

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*)

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

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
pm100
  • 48,078
  • 23
  • 82
  • 145