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>

void *thread1(void *param)
    printf("thread1 start\n");    
    printf("Thread 1 Ends\n");

void *thread2(void *param)
    printf("thread2 start\n");
    printf("Thread 2 ends\n");

void *thread3(void *param)
    printf("thread3 start\n");
    printf("Thread 3 ends\n");

void *thread4(void *param)
    printf("thread4 start\n");
    printf("Thread 4 ends \n");    

void *thread5(void *param)
    printf("thread5 start\n");
    printf("Thread 5 ends\n");    

int main(int argc, char *argv[])
    printf("Welcome to the Round Robin Example\n");

    /*Creating all of the threads*/
    pthread_t tid[NUMBEROFTHREADS];
    pthread_attr_t 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!

> How do I test the round robin?
By having them last a lot longer than one schedule interval. There's not much point if they do one thing and then quit, you're not going to see much of anything.
Waiting for each one to exit in turn doesn't help either.

Create 5 threads
Wait a minute or so
join 5 threads.

Each thread should do something useful for between 30 and 60 seconds.

> pthread_attr_setschedpolicy(&attr, SCHED_RR);
Checking return results is always a good idea. Maybe it failed on your system for some reason.

> I assume the -lpthread is a command line arg,
Yes it is.

> but where is it specified that I need this?
The manual pages for pthreads is a good bet.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.