LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
LinkBack Search this Thread
Old 05-10-2011, 09:40 PM   #1
utkarshrawat
Member
 
Registered: Jul 2007
Posts: 114

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
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,045

Rep: Reputation: 953Reputation: 953Reputation: 953Reputation: 953Reputation: 953Reputation: 953Reputation: 953Reputation: 953
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
utkarshrawat
Member
 
Registered: Jul 2007
Posts: 114

Original Poster
Rep: Reputation: 15
Code:
#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);
	printf("%d\n",rt1);
	rt2 = pthread_attr_init(&tattr2);
	printf("%d\n",rt2);

	
	ret1=pthread_attr_setschedpolicy(&tattr1, SCHED_RR);
	printf("%d\n",ret1);
	pthread_attr_setschedparam(&tattr1, &param1);
	ret2=pthread_attr_setschedpolicy(&tattr2, SCHED_RR);
	printf("%d\n",ret2);
	pthread_attr_setschedparam(&tattr2, &param2);
			
	r = pthread_create(&tid1, &tattr1, start_routine1, NULL);
	printf("%d\n",r);
	
	r1 = pthread_create(&tid2, &tattr2, start_routine2, NULL);
	printf("%d\n",r1);
	

	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
	
	return 0;

}


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

void *start_routine2()
{
	while(1)
	{
		counter1++;
		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
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 11.4
Posts: 1,319

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


Reply


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
Trackbacks are Off
Pingbacks are On
Refbacks are 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


All times are GMT -5. The time now is 11:26 AM.

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