LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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
  Search this Thread
Old 05-09-2011, 02:13 AM   #1
utkarshrawat
Member
 
Registered: Jul 2007
Posts: 120

Rep: Reputation: 15
prioritizing threads


Hi all
I Am trying to do thread prioritization.I am applying SCHED_RR and SCHED_FIFO but in both the cases thread 1 is running more times than thread 2.am giving same priority to both the threads.

Kindly check the code below


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  main()
{
	pthread_attr_t tattr1;
	pthread_attr_t tattr2;
	pthread_t tid1;
	pthread_t tid2;
	struct sched_param param1;
	struct sched_param param2;

	void *start_routine1();
	void *start_routine2();
	int ret;
	int policy;
	int max_priority,min_priority;
#if 1
	max_priority = sched_get_priority_max(SCHED_RR);
	min_priority = sched_get_priority_min(SCHED_RR);
	printf ("hi2 min_priority = %d \n",min_priority);
	printf ("max_priority = %d \r\n",max_priority);

	

	param1.sched_priority = 99;//lowest
	param2.sched_priority = 99;//highest

	
	/* initialized with default attributes */
	ret = pthread_attr_init(&tattr1);
	printf("%d\n",ret);
	ret = pthread_attr_init(&tattr2);
	printf("%d\n",ret);

	int ret1;
	//ret1=pthread_attr_setschedpolicy(&tattr1, SCHED_FIFO);
	ret1=pthread_attr_setschedpolicy(&tattr1, SCHED_RR);
	printf("%d\n",ret1);
	pthread_attr_setschedparam(&tattr1, &param1);
	//ret1=pthread_attr_setschedpolicy(&tattr2, SCHED_FIFO);
	ret1=pthread_attr_setschedpolicy(&tattr2, SCHED_RR);
	printf("%d\n",ret1);
	pthread_attr_setschedparam(&tattr2, &param2);

	ret = pthread_create(&tid1, &tattr1, start_routine1, NULL);
	printf("%d\n",ret);
	ret = pthread_create(&tid2, &tattr2, start_routine2, NULL);
	printf("%d\n",ret);

#else 
	/* default behavior specified*/
	ret = pthread_create(&tid1, NULL, start_routine1, NULL);
	printf("%d\n",ret);
	ret = pthread_create(&tid2, NULL, start_routine2, NULL);
	printf("%d\n",ret);
#endif
	
	pthread_join(tid2,NULL);	
	pthread_join(tid1,NULL);
	

	while (1);
	
}
void *start_routine1()
{
	while(1)
	{
		static int i=0;		
		printf("\n THREAD 1 Running %d \n",i++);
		if(i == 50000)
		{       
			printf("\n THREAD 1 endeds \n"); 
			//exit(1);
			//sleep(1); 
			i = 0;	
		}
		//usleep(1);
	}	
}

void *start_routine2()
{
	while(1)
	{
		static int j=0;		
		printf("\n THREAD 2 Running %d \n",j++);
		if(j == 10000)
		{       
			printf("\n THREAD 2 endeds \n");
			j = 0; 
			//exit(1);
		}
		//usleep(1); 
	}
}
 
Old 05-10-2011, 07:28 PM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Quote:
Originally Posted by utkarshrawat View Post
I Am trying to do thread prioritization.I am applying SCHED_RR and SCHED_FIFO but in both the cases thread 1 is running more times than thread 2.
Having run this code, I find that thread 2 reaches the end of the loop roughly five times as often as thread 1, which is what I would expect. What is it that makes you think that thread 1 is running more often than thread 2?
 
Old 05-10-2011, 11:14 PM   #3
utkarshrawat
Member
 
Registered: Jul 2007
Posts: 120

Original Poster
Rep: Reputation: 15
We have made some changes in the code check this and
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 ret;
int policy;
int max_priority,min_priority;
struct timespec  tp;
struct timeval start_lst_msg;
struct timeval end_lst_msg;
pthread_mutex_t check_time=PTHREAD_MUTEX_INITIALIZER;
int  main()
{
	pthread_attr_t tattr1;
	pthread_attr_t tattr2;
	pthread_t tid1;
	pthread_t tid2;
	struct sched_param param1;
	struct sched_param param2;

	max_priority = sched_get_priority_max(SCHED_RR);
	min_priority = sched_get_priority_min(SCHED_RR);
	printf ("hi2 min_priority = %d \n",min_priority);
	printf ("max_priority = %d \r\n",max_priority);	
	
	param1.sched_priority =99;//highest
	param2.sched_priority = 99;//lowest

	
	/* initialized with default attributes */
	ret = pthread_attr_init(&tattr1);
	printf("%d\n",ret);
	ret = pthread_attr_init(&tattr2);
	printf("%d\n",ret);

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

	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);
	}
		
}
OUTPUT FOR ABOVE PROGRAM is
Code:
 
THREAD 1 New Counter Value: 1

 THREAD 1 New Counter Value: 2

 THREAD 1 New Counter Value: 3

 THREAD 1 New Counter Value: 4

 THREAD 1 New Counter Value: 5

 THREAD 1 New Counter Value: 6

 THREAD 1 New Counter Value: 7

 THREAD 1 New Counter Value: 8

 THREAD 1 New Counter Value: 9

 THREAD 1 New Counter Value: 10

 THREAD 1 New Counter Value: 11

 THREAD 1 New Counter Value: 12

Thread 2 New Counter Value: 1
upto 
Thread 2 New Counter Value: 358 
 THREAD 1 New Counter Value: 13
upto
THREAD 1 New Counter Value: 440
Thread 2 New Counter Value: 359
Thread 2 New Counter Value: 360 
upto
Thread 2 New Counter Value: 2109

& so on
my purpose of this program is that each thread should run with the same time slice since both threads are having same priority i.e. 99

We are running this program on target board PXA 300 with real time patch. we have run this program in normal PC as well output is coming similar to that

Last edited by utkarshrawat; 05-10-2011 at 11:24 PM.
 
Old 05-10-2011, 11:35 PM   #4
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Again, you have to be careful how you are measuring this.

Since both threads are tying up the output to the terminal, you won't get a lot of real task switching going on (ie, one thread will hog the resource, and the other thread will be forced to wait, otherwise known as thread starvation). Occasionally you will see a switch, but it will be rare. Over a long enough period of time, you will see both threads get a fair go.

A better method is to do more work in each thread before sending anything to the output, so that the chances of both being in the output write at the same time are reduced.

But even just the following test shows you that it is working. The threads will still only switch rarely, but at least the redirection will give a larger sample size).
Code:
./a.out >a.txt
[and let it run for 10 seconds before killing it]
grep 'THREAD 1' a.txt | tail -1
grep 'Thread 2' a.txt | tail -1

Last edited by neonsignal; 05-10-2011 at 11:38 PM.
 
Old 05-11-2011, 04:40 PM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,610
Blog Entries: 4

Rep: Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905
When you try to "observe" the behavior of threads in this way, the very fact that you are observing them completely changes their behavior. (And... thus, invalidates your measures.)

Treat thread-prioritization only "at best, as a suggestion to the scheduler." Whenever two threads happen to be, for whatever reason, "simultaneously available for dispatching" (and at no other time...), the thread-priority value comes into play. This is your chance to say, "well, sir, all things being equal... if it's quite all right with you, sir... I would prefer that you, please sir..." And then you must leave it at that.

You can never predict which thread will be dispatched "first," "next," or "more/less often." That's strictly the decision of the Scheduler, and you are obliged to assume that it really does know what it is doing.

Last edited by sundialsvcs; 05-11-2011 at 04:42 PM.
 
Old 06-03-2011, 11:28 PM   #6
utkarshrawat
Member
 
Registered: Jul 2007
Posts: 120

Original Poster
Rep: Reputation: 15
I have a PXA300 board in which iam running my application I am using mutex , semaphore for the proper scheduling & time optimization ,some how I have achieved the time optimization . But i want to further improvement in the timing ,
So I used scheduler (round robin & FIFO) but it seems there is no further improvement in the timing ,Do i have to change the design of my application for the further improvement of timing. or If u can suggest me something else to do in my code .I cannot post my application here since it is huge.
 
Old 06-04-2011, 11:58 AM   #7
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,610
Blog Entries: 4

Rep: Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905
It frankly seems to me that you are asking for a degree of control over "the timing" that you simply can't have.

You don't say what "improvement in the timing" would mean to you and to this application. You're going to have to be able to identify "where it hurts," and "why it hurts," and to do so in such a way that it points to an implementable solution.

Remember that the operating system's thread-and-task scheduler is not a generalized "workload scheduler" such as a high-volume/high-availability application might require to have within itself. In fact, most such applications that I have seen are single-threaded.

Last edited by sundialsvcs; 06-04-2011 at 12:00 PM.
 
1 members found this post helpful.
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
prioritizing bandwidth on ports malaka56 Linux - Software 1 06-29-2005 02:55 PM
Traffic shaping (Prioritizing by port) Tyco Linux - Networking 5 02-11-2004 03:56 AM
Packet Prioritizing.. InDIo Linux - Networking 3 09-28-2003 07:10 PM
Prioritizing ports? pilot1 Linux - Networking 1 06-03-2003 06:08 AM
Prioritizing ports? pilot1 Linux - Networking 1 06-03-2003 05:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:24 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration