LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 11-21-2004, 02:56 PM   #1
msriram_linux
LQ Newbie
 
Registered: Nov 2004
Posts: 19

Rep: Reputation: 0
Unhappy 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??!!
 
Old 11-21-2004, 03:37 PM   #2
Timer
LQ Newbie
 
Registered: Jun 2004
Location: Catalonia
Distribution: Gentoo
Posts: 25

Rep: Reputation: 15
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!
 
Old 11-21-2004, 03:45 PM   #3
msriram_linux
LQ Newbie
 
Registered: Nov 2004
Posts: 19

Original Poster
Rep: Reputation: 0
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...
 
Old 11-21-2004, 03:57 PM   #4
Timer
LQ Newbie
 
Registered: Jun 2004
Location: Catalonia
Distribution: Gentoo
Posts: 25

Rep: Reputation: 15
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.
 
  


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
Scheduling in Linux asurya Linux - General 5 08-31-2005 08:34 AM
Linux Scheduling Kumar Programming 3 06-14-2004 04:24 AM
Scheduling jobs Netmaster0000 Linux - Newbie 3 12-13-2003 06:43 AM
scheduling again nautilus_1987 Linux - Software 2 07-04-2003 07:53 AM
Kernel Scheduling netkern Linux - General 1 04-14-2001 04:18 PM

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

All times are GMT -5. The time now is 12:45 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