Hey guys,
I'm new to C- I do Java- and for our OS class we're doing some basic code examples in C, on Linux. (Fedora core, if it matters).
We're supposed to write a test program that shows the difference between how threads are run with round robin and fifo scheduling. I can't get the round robin implementation to work- I did some research and it looks like the join method ensures that each thread runs fully- doesn't that interfere with the round robin?
This is the code we have:
#include <pthread.h>
#include <stdio.h>
#include <sched.h>
#define NUMBEROFTHREADS 5
void *thread1(void *param)
{
printf("thread1 start\n");
sleep(1);
printf("Thread 1 Ends\n");
pthread_exit(0);
}
void *thread2(void *param)
{
printf("thread2 start\n");
sleep(5);
printf("Thread 2 ends\n");
pthread_exit(0);
}
void *thread3(void *param)
{
printf("thread3 start\n");
sleep(1);
printf("Thread 3 ends\n");
pthread_exit(0);
}
void *thread4(void *param)
{
printf("thread4 start\n");
sleep(1);
printf("Thread 4 ends \n");
pthread_exit(0);
}
void *thread5(void *param)
{
printf("thread5 start\n");
sleep(1);
printf("Thread 5 ends\n");
pthread_exit(0);
}
int main(int argc, char *argv[])
{
printf("**********************************\n");
printf("Welcome to the Round Robin Example\n");
printf("**********************************\n");
/*Creating all of the threads*/
pthread_t tid[NUMBEROFTHREADS];
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
//pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
pthread_attr_setschedpolicy(&attr, SCHED_RR);
pthread_create(&tid[0], &attr, &thread1, NULL);
pthread_join(tid[0], NULL);
pthread_create(&tid[1], &attr, &thread2, NULL);
pthread_join(tid[1], NULL);
pthread_create(&tid[2], &attr, &thread3, NULL);
pthread_join(tid[2], NULL);
pthread_create(&tid[3], &attr, &thread4, NULL);
pthread_join(tid[3], NULL);
pthread_create(&tid[4], &attr, &thread5, NULL);
pthread_join(tid[4], NULL);
}
Can someone please point me in the right direction here? How do I test the round robin? I tried using thread_detach, and not having the joins, but then the function calls aren't executed.!
(As an aside, can someone explain why we need to us cc programname.c -lpthread to compile? I assume the -lpthread is a command line arg, but where is it specified that I need this?)
Thanks in advance for the help!