-   Programming (
-   -   Scheduling through Round robin (

utkarshrawat 05-10-2011 09:40 PM

Scheduling through Round robin
Does anyone know how to run 2 threads with round robin scheduling. & can we get the time of these threads when they are context switching. That means I want to know at how much time each thread is taking to run

sundialsvcs 05-12-2011 08:21 AM

Whether or not the scheduling is round-robin, ways of observing the time consumed by processes or threads do exist. (You need to tell us exactly what threading system you're using...)

Of course, even if scheduling is set up to be "round robin," you should not expect to see that "first this thread runs, then that thread runs." The scheduling discipline is, basically, a suggestion to the scheduler. Actual observed behavior depends upon the actual environment ... and, upon whether or not you are looking at it. (Yes, "the presence of the experimenter affects the experiment." If the little mice can see you, they'll stop doing what they're supposed to be doing and look back at you with a little mousey smile, hoping for another piece of cheese.)

utkarshrawat 05-13-2011 07:27 AM


#include <unistd.h>    /* Symbolic Constants */
#include <sys/types.h>  /* Primitive System Data Types */
#include <errno.h>      /* Errors */
#include <stdio.h>      /* Input/Output */
#include <stdlib.h>    /* General Utilities */
#include <pthread.h>    /* POSIX Threads */
#include <string.h>
#include <sched.h>

int counter=0,counter1=0;
void *start_routine1();
void *start_routine2();
int r,r1;
int ret1,ret2;
int rt1,rt2;
int policy;
int max_priority,min_priority;
struct timespec  tp;
struct timeval start_lst_msg;
struct timeval end_lst_msg;

int  main()
        pthread_attr_t tattr1;
        pthread_attr_t tattr2;
        pthread_t tid1;
        pthread_t tid2;
        struct sched_param param1;
        struct sched_param param2;
        param1.sched_priority =50;//highest
        param2.sched_priority = 50;//lowest

        /* initialized with default attributes */
        rt1 = pthread_attr_init(&tattr1);
        rt2 = pthread_attr_init(&tattr2);

        ret1=pthread_attr_setschedpolicy(&tattr1, SCHED_RR);
        pthread_attr_setschedparam(&tattr1, &param1);
        ret2=pthread_attr_setschedpolicy(&tattr2, SCHED_RR);
        pthread_attr_setschedparam(&tattr2, &param2);
        r = pthread_create(&tid1, &tattr1, start_routine1, NULL);
        r1 = pthread_create(&tid2, &tattr2, start_routine2, NULL);

        return 0;


void *start_routine1()
                printf(" THREAD 1 New Counter Value: %d\n",  counter);

void *start_routine2()
                printf("Thread 2 New Counter Value: %d\n", counter1);

I don t know whether this is working with proper RR scheduling

It there any command from which I can see on background while my program is running how many number of threads are running .

Reuti 05-16-2011 09:33 AM

You can try ps -eLf (see man page of ps).

All times are GMT -5. The time now is 08:52 PM.