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 *),
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~