Results 1 to 10 of 17
pthread_attr_init(&attr); /* initialize attr with default attributes */
if(pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED) != 0 )
{
printf("error setinheritsched\n");
}
pthread_attr_setschedpolicy(&attr, SCHED_RR);
pthread_create(&tSecondThreadid, &attr, tSecondThread_Runfunction , NULL );
pthread_create returns EINVAL.
This happens ...
- 12-28-2007 #1Just Joined!
- Join Date
- Dec 2007
- Posts
- 28
pthread Error
pthread_attr_init(&attr); /* initialize attr with default attributes */
if(pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED) != 0 )
{
printf("error setinheritsched\n");
}
pthread_attr_setschedpolicy(&attr, SCHED_RR);
pthread_create(&tSecondThreadid, &attr, tSecondThread_Runfunction , NULL );
pthread_create returns EINVAL.
This happens only if I use PTHREAD_EXPLICIT_SCHED.
Can you please help me to fix this issue.
My requirement is to create threads, which should not be of round robin type.
Thanks in Advance
- 12-28-2007 #2
Two preliminary questions for you, so we can get further into the problem.
- When you enter this at the command line:
what is the output?Code:uname -a
- You say that the threads should not be of round robin type, but you use SCHED_RR in your code. What's up with that?
--
Bill
Old age and treachery will overcome youth and skill.
- When you enter this at the command line:
- 12-29-2007 #3Just Joined!
- Join Date
- Dec 2007
- Posts
- 28
Thanks for your reply.
uname -a
Linux test 2.6.16.13-4-default #1 Wed May 3 04:53:23 UTC 2006 i686 athlon i386 GNU/Linux
For testing purpose I put SCHED_RR, even in this case pthread creation fails.
- 12-29-2007 #4
I tried what you showed, and got different problems from yours, but I had not anticipated getting any problems at all.
We're having company Saturday, but I'll get back to this on Sunday if you haven't found a solution by then.--
Bill
Old age and treachery will overcome youth and skill.
- 12-30-2007 #5
I have your answer, I think.
Run this exact bash script:
What do you get? I get not EINVAL, but EPERM.Code:#!/bin/sh cat > wje_p.c <<EOD #include <errno.h> #include <pthread.h> #include <stdio.h> #include <stdlib.h> void *second_thread_run_function(void *arg) { printf("inside second thread\n"); return NULL; } /* second_thread_run_function() */ int main(void) { int status; pthread_t second_thread_id; pthread_attr_t thread_attr; if(pthread_attr_init(&thread_attr)) { fprintf(stderr, "error pthread_attr_init()\n" ); exit(1); } if(pthread_attr_setinheritsched(&thread_attr, PTHREAD_EXPLICIT_SCHED ) ) { fprintf(stderr, "error pthread_attr_setinheritsched()\n" ); exit(1); } if(pthread_attr_setschedpolicy(&thread_attr, SCHED_RR ) ) { fprintf(stderr, "error pthread_attr_setschedpolicy()\n" ); exit(1); } status=pthread_create(&second_thread_id, &thread_attr, second_thread_run_function, NULL ); if(status!=0) { if(status==EPERM) { fprintf(stderr, "pthread_create() got EPERM\n" ); } else if(status==EINVAL) { fprintf(stderr, "pthread_create() got EINVAL\n" ); } else { fprintf(stderr, "pthread_create() got neither EPERM nor EINVAL\n" ); } fprintf(stderr, "pthread_create() got error %d\n", status ); errno=status; perror("pthread_create()"); exit(1); } if(pthread_join(second_thread_id, NULL ) ) { fprintf(stderr, "error pthread_join()\n" ); exit(1); } return 0; } /* main() */ EOD gcc -pthread -Wall wje_p.c -o wje_p ./wje_p
Now run the same script as root. It worked fine for me.
What's happening here? As soon as you mess with scheduling, you're messing also (by implication) with relative priorities, and that only root can do.
I know you're not too interested in round robin, but try this anyway, both as nonroot (where it gave me EPERM) and root (where it worked for me). It gives you a more detailed idea as to what you can do with round robin scheduling.
Hope this helps.Code:#!/bin/sh cat > wje_q.c <<EOD #include <errno.h> #include <pthread.h> #include <sched.h> #include <stdio.h> #include <stdlib.h> void *second_thread_run_function(void *arg) { printf("inside second thread\n"); return NULL; } /* second_thread_run_function() */ int main(void) { int status; pthread_t second_thread_id; pthread_attr_t thread_attr; struct sched_param thread_param; if(pthread_attr_init(&thread_attr)) { fprintf(stderr, "error pthread_attr_init()\n" ); exit(1); } if(pthread_attr_setschedpolicy(&thread_attr, SCHED_RR ) ) { fprintf(stderr, "error pthread_attr_setschedpolicy()\n" ); exit(1); } printf("%d %d\n", sched_get_priority_min(SCHED_RR), sched_get_priority_max(SCHED_RR) ); thread_param.sched_priority=(sched_get_priority_min(SCHED_RR) +sched_get_priority_max(SCHED_RR) ) /2; if(pthread_attr_setschedparam(&thread_attr, &thread_param ) ) { fprintf(stderr, "error pthread_attr_setschedparam()\n" ); exit(1); } if(pthread_attr_setinheritsched(&thread_attr, PTHREAD_EXPLICIT_SCHED ) ) { fprintf(stderr, "error pthread_attr_setinheritsched()\n" ); exit(1); } status=pthread_create(&second_thread_id, &thread_attr, second_thread_run_function, NULL ); if(status!=0) { if(status==EPERM) { fprintf(stderr, "pthread_create() got EPERM\n" ); } else if(status==EINVAL) { fprintf(stderr, "pthread_create() got EINVAL\n" ); } else { fprintf(stderr, "pthread_create() got neither EPERM nor EINVAL\n" ); } fprintf(stderr, "pthread_create() got error %d\n", status ); errno=status; perror("pthread_create()"); exit(1); } if(pthread_join(second_thread_id, NULL ) ) { fprintf(stderr, "error pthread_join()\n" ); exit(1); } return 0; } /* main() */ EOD gcc -pthread -Wall wje_q.c -o wje_q ./wje_q--
Bill
Old age and treachery will overcome youth and skill.
- 12-31-2007 #6Just Joined!
- Join Date
- Dec 2007
- Posts
- 28
Hi Thanks for your mail
Wishin U all a Happy New Yr
yes, when in root, i can execute the program.
One more doubt:
I modified the code, such that second thread priority is 5. Now again in the run function of second thread, i checked the priority, but it is returning some other value. why is this?
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <sched.h>
void *second_thread_run_function(void *arg)
{
struct sched_param *param;
printf("inside second thread\n");
sched_getparam(pthread_self(), param);
printf("second_thread_priority = %d\n", param->sched_priority);
return NULL;
} /* second_thread_run_function() */
int main(void)
{
int status;
pthread_t second_thread_id;
pthread_attr_t thread_attr;
struct sched_param thread_param;
if(pthread_attr_init(&thread_attr))
{
fprintf(stderr,
"error pthread_attr_init()\n"
);
exit(1);
}
if(pthread_attr_setinheritsched(&thread_attr,
PTHREAD_EXPLICIT_SCHED
1,1 Top
)
)
{
fprintf(stderr,
"error pthread_attr_setinheritsched()\n"
);
exit(1);
}
if(pthread_attr_setschedpolicy(&thread_attr,
SCHED_RR
)
)
{
fprintf(stderr,
"error pthread_attr_setschedpolicy()\n"
);
exit(1);
}
thread_param.sched_priority=5;
if(pthread_attr_setschedparam(&thread_attr,
&thread_param
)
)
{
fprintf(stderr,
"error pthread_attr_setschedparam()\n"
);
exit(1);
}
status=pthread_create(&second_thread_id,
&thread_attr,
second_thread_run_function,
NULL
);
printf("second_threadID = %d\n", second_thread_id);
if(status!=0)
{
if(status==EPERM)
{
fprintf(stderr,
"pthread_create() got EPERM\n"
);
}
else
if(status==EINVAL)
{
fprintf(stderr,
"pthread_create() got EINVAL\n"
);
}
else
{
fprintf(stderr,
"pthread_create() got neither EPERM nor EINVAL\n"
);
}
fprintf(stderr,
"pthread_create() got error %d\n",
status
);
errno=status;
perror("pthread_create()");
exit(1);
}
if(pthread_join(second_thread_id,
NULL
)
)
{
fprintf(stderr,
"error pthread_join()\n"
);
exit(1);
}
return 0;
} /* main() */
Thanks in Advance!!!
- 12-31-2007 #7
Yeah, I got something different also. What I got was not a different number, but a segment violation error.
That's because there's nothing in that second thread that's of type struct sched_param. There's something that's a pointer to such a struct, but you have nothing that's actually such a struct. And the pointer, not being initialized to anything, on my machine ended up being an invalid pointer.
Point that pointer to a real live animal of type struct sched_param, or better yet, declare such a real live animal and don't declare a separate pointer to it. So you'd declare this:
and then instead of saying this:Code:struct sched_param real_live_animal;
you'd say something like this:Code:sched_getparam(pthread_self(), param); printf("second_thread_priority = %d\n", param->sched_priority);
Hope this helps.Code:sched_getparam(pthread_self(),&real_live_animal); printf("second_thread_priority = %d\n",real_live_animal.sched_priority);
Oh. And.
You may wish to consult a dentist. (grin)Wishin U all a Happy New Yr--
Bill
Old age and treachery will overcome youth and skill.
- 01-01-2008 #8Just Joined!
- Join Date
- Dec 2007
- Posts
- 28
Hi
Yea...I tried
void *second_thread_run_function(void *arg)
{
struct sched_param param;
pthread_attr_t thread_attr;
printf("inside second thread\n");
sched_getparam(pthread_self(), ¶m);
printf("second_thread_priority = %d\n", param.sched_priority);
return NULL;
} /* second_thread_run_function() */
the value i got for second_thread_priority is 1075841376
m confused abt this value, cos I have set it to 5
- 01-01-2008 #9
This is only a guess, but it's an educated guess.
Structures whose names begin with sched_ are used by pthreads functions. But the only functions whose names begin with sched_ that can be used with pthreads are sched_get_priority_min() and sched_get_priority_max(). And even these functions don't concern themselves with individual threads. The sched_ functions, when they concern themselves with individual entities, address processes, not POSIX threads.
I've looked throughly through David Butenhof's Programming with POSIX Threads, and I've found no way to query what an individual thread's priority is. As far as I can tell, you just have to remember what you specified for a thread when you first created it.
I wouldn't bet the farm on this, but I'd bet at least a cookie.--
Bill
Old age and treachery will overcome youth and skill.
- 01-02-2008 #10Just Joined!
- Join Date
- Dec 2007
- Posts
- 28
Hi,
As mentioned earlier, now I wanto proceed to pre-emtive thread concepts.
Is there any sample code avaliable for it?
Is it possible to do with SCHED_RR i.e. by having 3 threads of different priorities. I believe if these 3 threads are of same priority then it will be of Round Robin scheduling, but since they are of different priorities they must be pre-emtive.
Is my understanding correct?
Thanks in Advance!!


Reply With Quote