LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Pthread scheduling... (https://www.linuxquestions.org/questions/programming-9/pthread-scheduling-257665/)

msriram_linux 11-21-2004 03:56 PM

Pthread scheduling...
 
Dear team!
I have a problem with scheduling pthreads..
Here is my code:
--------------------------------------------------------------------------------------------------------------------------
#include<pthread.h>
#include<stdio.h>
#define num_threads 2
#define count_lim 10

pthread_mutex_t count_mutex;
int count =0;

void* fun1(void* arg1)
{
if ((int*)arg1==0) nice(-3);
else nice(3);
double junk=0.0;
int i,j;
for(i=0;i<count_lim;i++)
{
pthread_mutex_lock(&count_mutex);
printf("\nmutex locked by thread---- %d%d",(int*)arg1,i);
count=count+1;
pthread_mutex_unlock(&count_mutex);
printf("\nmutex unlocked by thread-- %d%d",(int*)arg1,i);
for(j=0;j<5000000;j++) junk+=(double)random();
}
pthread_exit(NULL);
}
main()
{
int rc=1;
pthread_t thread_array[num_threads];
pthread_attr_t attr[num_threads];
pthread_mutexattr_t attr_count_mutex;
struct sched_param s_0;
const struct sched_param s_1;

printf("\nDeclaration Ends Here");

rc=pthread_attr_init(&attr[0]);
if(rc==0) printf("\ninitalisation of thread attr 0 succesful");
else printf("\ninitalisation of thread attr 0 unsuccesful");
rc=pthread_attr_setdetachstate(&attr[0],PTHREAD_CREATE_JOINABLE);
if(rc==0) printf("\ninitalisation of thread detach state 0 succesful");
else printf("\ninitalisation of thread state 0 unsuccesful");

rc=pthread_attr_init(&attr[1]);
if(rc==0) printf("\ninitalisation of thread attr 1 succesful");
else printf("\ninitalisation of thread attr 1 unsuccesful");
rc=pthread_attr_setdetachstate(&attr[1],PTHREAD_CREATE_JOINABLE);
if(rc==0) printf("\ninitalisation of thread detach 1 succesful");
else printf("\ninitalisation of thread detach 1 unsuccesful");

/*here is the BUG!!!*/
pthread_attr_setschedparam(&attr[0],&s_0);
pthread_attr_setschedpolicy(&attr[0],SCHED_OTHER);
pthread_attr_setschedparam(&attr[0],&shed_0);
pthread_setschedparam(thread_array[0],SCHED_OTHER,&s_0);
/*here is the BUG!!!*/

pthread_mutexattr_init(&attr_count_mutex);
pthread_mutex_init(&count_mutex,NULL);
printf("\nInitialisation Ends Here");

pthread_create(&thread_array[0],&attr[0],fun1,(void*)(num_threads-2));
pthread_create(&thread_array[1],&attr[1],fun1,(void*)(num_threads-1));

pthread_join(thread_array[0],NULL);
pthread_join(thread_array[1],NULL);

pthread_attr_destroy(&attr[0]);
pthread_attr_destroy(&attr[1]);
pthread_mutexattr_destroy(&attr_count_mutex);
pthread_mutex_destroy(&count_mutex);

pthread_exit(NULL);
}
--------------------------------------------------------------------------------------------------------------------------
Although the code is compiled quietly:
#gcc -pthread myfile.c
when the .out is executed:
#./a.out
the run hangs-up at part /* here is he BUG!!!*/.
This part when commented, there is smooth run and the code is executed successfully.
Could anyone help please??!!

Timer 11-21-2004 04:37 PM

I tried pthread_attr_setsched* functions a few time ago and coulndn't manage to make them work. If setting 'niceness' worked, use this; the code will at least be nicer!

msriram_linux 11-21-2004 04:45 PM

Dear Timer,
There one thing to be noted with nice().
As I have learnt just a day ago, nice() can just add niceties to the scheduling rather than
the priority change or setting itself. However I have yet got to learn the intricacies...

Timer 11-21-2004 04:57 PM

Well, I left my priority code commented after a TODO. If you manage to use Linux Threads for a priority change, please post the code. :p


All times are GMT -5. The time now is 09:17 PM.