Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-10-2011, 09:40 PM   #1
Registered: Jul 2007
Posts: 120

Rep: Reputation: 15
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
Old 05-12-2011, 08:21 AM   #2
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 8,455
Blog Entries: 4

Rep: Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918
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.)
Old 05-13-2011, 07:27 AM   #3
Registered: Jul 2007
Posts: 120

Original Poster
Rep: Reputation: 15
#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 .
Old 05-16-2011, 09:33 AM   #4
Senior Member
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 13.1
Posts: 1,328

Rep: Reputation: 254Reputation: 254Reputation: 254
You can try ps -eLf (see man page of ps).


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
round robin dns linuxengineer Linux - Server 1 11-16-2010 03:55 AM
Round Robin with Squid marche Linux - Newbie 0 12-19-2008 10:00 AM
Real-time Round-Robin Scheduling cttran Programming 2 05-06-2005 04:41 PM
dns round-robin tzkolinux Linux - Networking 1 12-27-2004 03:49 AM
round robin for servers deepagodkhindi Programming 5 05-11-2003 05:50 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:10 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration