LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   prioritizing threads (https://www.linuxquestions.org/questions/programming-9/prioritizing-threads-879587/)

utkarshrawat 05-09-2011 02:13 AM

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);
        }
}


neonsignal 05-10-2011 07:28 PM

Quote:

Originally Posted by utkarshrawat (Post 4350629)
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?

utkarshrawat 05-10-2011 11:14 PM

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

neonsignal 05-10-2011 11:35 PM

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


sundialsvcs 05-11-2011 04:40 PM

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.

utkarshrawat 06-03-2011 11:28 PM

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.

sundialsvcs 06-04-2011 11:58 AM

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.


All times are GMT -5. The time now is 07:03 PM.