Discussion:
How to pick out thread id?
micke
2007-10-31 08:41:57 UTC
Permalink
hi, in lunix I can do following:

tid = syscall(SYS_gettid);

to get the thread id of the current thread.

How do I do this in solaris?


This message posted from opensolaris.org
Rao Shoaib
2007-10-31 08:53:11 UTC
Permalink
man threads(5) shows

<....>
Functions Related to IDs
POSIX (libpthread) Solaris (libthread)
pthread_self() thr_self()
<....>
Post by micke
tid = syscall(SYS_gettid);
to get the thread id of the current thread.
How do I do this in solaris?
This message posted from opensolaris.org
_______________________________________________
opensolaris-code mailing list
http://mail.opensolaris.org/mailman/listinfo/opensolaris-code
micke
2007-10-31 09:26:12 UTC
Permalink
I've tryed:

kthread_t *t;
pid_t tid;

t = (kthread_t *)pthread_self();

tid = (pid_t)&t->t_tid;

that works I think, the t_tid is the current threads id right?

but the I want to change the scheduling class for this thread:

sched_setscheduler(tid, SCHED_FIFO, &sp);

This wont work, I get:
: No such process

I want to schedule this thread only, do I have the wrong thread id?

in linux sched_setscheduler works for thread id, is the same true in solaris?


This message posted from opensolaris.org
m***@public.gmane.org
2007-10-31 09:38:00 UTC
Permalink
Hi Micke,
Post by micke
kthread_t *t;
pid_t tid;
t = (kthread_t *)pthread_self();
tid = (pid_t)&t->t_tid;
that works I think, the t_tid is the current threads id right?
sched_setscheduler(tid, SCHED_FIFO, &sp);
: No such process
I want to schedule this thread only, do I have the wrong thread id?
in linux sched_setscheduler works for thread id, is the same true in solaris?
Reading the man page for sched_setscheduler(3RT) says the first argument
is a pid (process id), not a tid (thread id) (so sched_setscheduler() is
process-wide,
not specific thread). And you can use 0 for the first argument to
refer to yourself. If you want to change scheduling class for a
specific thread,
take a look at pthread_setschedparam(3c).

max
Post by micke
This message posted from opensolaris.org
_______________________________________________
opensolaris-code mailing list
http://mail.opensolaris.org/mailman/listinfo/opensolaris-code
Frank.Hofmann-UdXhSnd/
2007-10-31 10:33:23 UTC
Permalink
Post by m***@public.gmane.org
Hi Micke,
Post by micke
kthread_t *t;
pid_t tid;
t = (kthread_t *)pthread_self();
tid = (pid_t)&t->t_tid;
that works I think, the t_tid is the current threads id right?
sched_setscheduler(tid, SCHED_FIFO, &sp);
: No such process
I want to schedule this thread only, do I have the wrong thread id?
in linux sched_setscheduler works for thread id, is the same true in solaris?
Reading the man page for sched_setscheduler(3RT) says the first argument
is a pid (process id), not a tid (thread id) (so sched_setscheduler() is
process-wide,
not specific thread). And you can use 0 for the first argument to
refer to yourself. If you want to change scheduling class for a
specific thread,
take a look at pthread_setschedparam(3c).
To illustrate:

struct schedparam sp;
...
sched_setscheduler(getpid(), SCHED_FIFO, &sp);

does the same to the current process which the following:

struct schedparam sp;
...
pthread_setschedparam(pthread_self(), SCHED_FIFO, &sp);

does to the current thread.


These:

http://www.opengroup.org/onlinepubs/7990989775/xsh/pthread.h.html
http://docs.sun.com/app/docs/doc/819-2252/pthreads-5
http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html

seem a reasonable start for what this is all about. The following:

http://www.ibm.com/developerworks/linux/library/l-threading.html

gives a good history of the state Linux threading support used to be in;
recently (in the last two years) things settled to compatibility with
POSIX threads, so that's only context in the sense why "good" docs on how
to create multithreaded programs under Linux are hard to find ... and why
Linux people often do Linux-specific hacks ...


FrankH.
Casper.Dik-UdXhSnd/
2007-10-31 10:29:06 UTC
Permalink
Post by micke
kthread_t *t;
pid_t tid;
t = (kthread_t *)pthread_self();
tid = (pid_t)&t->t_tid;
that works I think, the t_tid is the current threads id right?
Actually, it doesn't. Threads also do not have such nice numerical
IDs in Solaris.
Post by micke
sched_setscheduler(tid, SCHED_FIFO, &sp);
The first argument to sched_setscheduler is a pid, not a thread id.

Casper
Frank.Hofmann-UdXhSnd/
2007-10-31 09:39:34 UTC
Permalink
Post by micke
tid = syscall(SYS_gettid);
to get the thread id of the current thread.
How do I do this in solaris?
The same way you _should_ do it in Linux, and that is to use
pthread_self() for the purpose ;-)

FrankH.
Post by micke
This message posted from opensolaris.org
_______________________________________________
opensolaris-code mailing list
http://mail.opensolaris.org/mailman/listinfo/opensolaris-code
Richard L. Hamilton
2007-10-31 09:41:27 UTC
Permalink
Post by micke
tid = syscall(SYS_gettid);
to get the thread id of the current thread.
How do I do this in solaris?
Why are you using syscall level facilities for threads instead of
POSIX (pthread_*())? If you were, AFAIK pthread_self(3c) would be the
answer.

Otherwise, see _lwp_self(2); but I doubt very much that's committed,
in which case, it could be broken by some future update. Ditto for
syscall(SYS_lwp_self), which should amount to the same thing, I think.

Besides, details of threading mechanisms other than POSIX threads are
likely to vary between OSs in more than just this one way; so this isn't
the only problem you may have porting your code.


This message posted from opensolaris.org
micke
2007-10-31 09:47:31 UTC
Permalink
In linux if I use pthread_self() I get a pthread_t struct, that I have no access to anything in?? I don't know how to pick out the thread id from pthread_t in linux, that why I use the syscall. If anyone knows how, please let me know.

In solaris it's "easier" pthread_t is a kthread_t and I can pick out the specific tid(lwpid), I guess I should use priocntl to change the parameters for a thread. looking in to that now.

thanks.


This message posted from opensolaris.org
Casper.Dik-UdXhSnd/
2007-10-31 10:30:33 UTC
Permalink
On Solaris you are also not permitted to look inside a pthread_t;
also, it's not at all the same as a kthread_t.

Casper
Darren J Moffat
2007-10-31 10:36:45 UTC
Permalink
What is the problem you are trying to solve that leads you to believe
you need this info at all ?

--
Darren J Moffat
Richard L. Hamilton
2007-10-31 10:32:17 UTC
Permalink
Post by micke
In linux if I use pthread_self() I get a pthread_t
struct, that I have no access to anything in?? I
don't know how to pick out the thread id from
pthread_t in linux, that why I use the syscall. If
anyone knows how, please let me know.
In solaris it's "easier" pthread_t is a kthread_t and
I can pick out the specific tid(lwpid), I guess I
should use priocntl to change the parameters for a
thread. looking in to that now.
thanks.
Still sounds like you're using a whole bunch of platform-specific stuff
and just trying to find equivalents for more platforms, rather than
using the pthread_* stuff everywhere.

Even if pthread_* won't do everything you need (but I wonder about that,
since it's reasonably extensive), it might be simpler
to use it everywhere possible, and only have platform-specific stuff
to do what it doesn't take care of.


This message posted from opensolaris.org
micke
2007-10-31 10:42:44 UTC
Permalink
:) ok, but it wont work, not don't "standard stuff" so ....

Any way just tried this:

kthread_t *daemon;
pcparms_t pcparms;
rtparms_t *rtparmsp;
id_t tid;
int rt_id;

rt_id = rt_class_id;
pcparms.pc_cid = rt_id;
rtparmsp = (struct rtparms *)pcparms.pc_clparms;
rtparmsp->rt_pri = some_prio;
rtparmsp->rt_tqsecs = RT_TQINF;

daemon = (kthread_t *)pthread_self();
tid = (id_t)&daemon->t_tid;

if((priocntl(P_LWPID, tid, PC_SETPARMS, &pcparms)) != 0){
perror("priocntl failed!");
return -1;
}


this fails, still get "no such process". :(

What am I doing wrong here?


This message posted from opensolaris.org
Casper.Dik-UdXhSnd/
2007-10-31 10:52:00 UTC
Permalink
Post by micke
:) ok, but it wont work, not don't "standard stuff" so ....
kthread_t *daemon;
daemon = (kthread_t *)pthread_self();
tid = (id_t)&daemon->t_tid;
Have you tried any of:

pthread_attr_getinheritsched (3c) - get or set inheritsched attribute
pthread_attr_getschedparam (3c) - get or set schedparam attribute
pthread_attr_getschedpolicy (3c) - get or set schedpolicy attribute
pthread_attr_getinheritsched (3c) - get or set inheritsched attribute
pthread_attr_getschedparam (3c) - get or set schedparam attribute
pthread_attr_getschedpolicy (3c) - get or set schedpolicy attribute
pthread_getschedparam (3c) - access dynamic thread scheduling parameters
pthread_getschedparam (3c) - access dynamic thread scheduling parameters
pthread_setschedprio (3c) - dynamic thread scheduling parameters access

Those are the functions you will need to use.

Casper
Darren J Moffat
2007-10-31 10:53:02 UTC
Permalink
Post by micke
:) ok, but it wont work, not don't "standard stuff" so ....
kthread_t *daemon;
pcparms_t pcparms;
rtparms_t *rtparmsp;
id_t tid;
int rt_id;
rt_id = rt_class_id;
pcparms.pc_cid = rt_id;
rtparmsp = (struct rtparms *)pcparms.pc_clparms;
rtparmsp->rt_pri = some_prio;
rtparmsp->rt_tqsecs = RT_TQINF;
daemon = (kthread_t *)pthread_self();
tid = (id_t)&daemon->t_tid;
You don't need those two lines.
Post by micke
if((priocntl(P_LWPID, tid, PC_SETPARMS, &pcparms)) != 0){
s/tid/P_MYID/

From priocntl(2)

An id value of P_MYID can be used in conjunction with the
idtype value to specify the LWP ID, parent process ID, pro-
cess group ID, session ID, task ID, class ID, user ID, group
ID, project ID, zone ID, or process contract ID of the cal-
ling LWP.
--
Darren J Moffat
Frank.Hofmann-UdXhSnd/
2007-10-31 10:59:02 UTC
Permalink
Post by Darren J Moffat
Post by micke
:) ok, but it wont work, not don't "standard stuff" so ....
Still wondering why you are so desperate _NOT_ to use POSIX threads /
_NOT_ to do portable programming ... care to explain that part ?

What's wrong with pthread_setschedparam() ? You've said initially that
what you're trying to do is to call sched_setscheduler() for a thread.
Thta's exactly what pthread_setschedparam() does, both on Linux and on
Solaris ...

FrankH.
Post by Darren J Moffat
Post by micke
kthread_t *daemon;
pcparms_t pcparms;
rtparms_t *rtparmsp;
id_t tid;
int rt_id;
rt_id = rt_class_id;
pcparms.pc_cid = rt_id;
rtparmsp = (struct rtparms *)pcparms.pc_clparms;
rtparmsp->rt_pri = some_prio;
rtparmsp->rt_tqsecs = RT_TQINF;
daemon = (kthread_t *)pthread_self();
tid = (id_t)&daemon->t_tid;
You don't need those two lines.
Post by micke
if((priocntl(P_LWPID, tid, PC_SETPARMS, &pcparms)) != 0){
s/tid/P_MYID/
From priocntl(2)
An id value of P_MYID can be used in conjunction with the
idtype value to specify the LWP ID, parent process ID, pro-
cess group ID, session ID, task ID, class ID, user ID, group
ID, project ID, zone ID, or process contract ID of the cal-
ling LWP.
--
Darren J Moffat
_______________________________________________
opensolaris-code mailing list
http://mail.opensolaris.org/mailman/listinfo/opensolaris-code
Paul Winder
2007-10-31 10:55:57 UTC
Permalink
I might be missing something here but....
Post by micke
:) ok, but it wont work, not don't "standard stuff" so ....
kthread_t *daemon;
kthread == KERNEL thread
Post by micke
pcparms_t pcparms;
rtparms_t *rtparmsp;
id_t tid;
int rt_id;
rt_id = rt_class_id;
pcparms.pc_cid = rt_id;
rtparmsp = (struct rtparms *)pcparms.pc_clparms;
rtparmsp->rt_pri = some_prio;
rtparmsp->rt_tqsecs = RT_TQINF;
daemon = (kthread_t *)pthread_self();
returns pthread_t
You just can't assume pthread_t == kthread_t

See previous post on using standard pthread_* library calls.
Joerg.Schilling-8LS2qeF34IpklNlQbfROjRvVK+ (Joerg Schilling)
2007-10-31 15:09:34 UTC
Permalink
Post by Paul Winder
I might be missing something here but....
Well, none of the answers have been correct enough.

pthread_t is a thread ID but POSIX does not mention the underlying data.

If you like to write portable code, you ned to be aware of this fact.

IIRC, pthread_t is:

a small number starting from 1... Solaris

a pid Linux

a filedescriptor MS-WIN (well, there are no POSIX
threads but it makes sense to mention
it)

a pointer several other OS.

If you like to be portable, read the POSIX definitions from opengroup.org and
do not make any asumption not mentioned there.

Jörg
--
EMail:joerg-3Qm2Liu6aU2sY6utFDHCwYAplN+***@public.gmane.org (home) Jörg Schilling D-13353 Berlin
js-CFLBMwTPW48UNGrzBIF7/***@public.gmane.org (uni)
schilling-8LS2qeF34IpklNlQbfROjRvVK+***@public.gmane.org (work) Blog: http://schily.blogspot.com/
URL: http://cdrecord.berlios.de/old/private/ ftp://ftp.berlios.de/pub/schily
Dan Mick
2007-10-31 13:31:18 UTC
Permalink
Post by Richard L. Hamilton
Post by micke
tid = syscall(SYS_gettid);
to get the thread id of the current thread.
How do I do this in solaris?
Why are you using syscall level facilities for threads instead of
POSIX (pthread_*())?
because school is in session again?
Joerg.Schilling-8LS2qeF34IpklNlQbfROjRvVK+ (Joerg Schilling)
2007-10-31 15:50:36 UTC
Permalink
Post by micke
tid = syscall(SYS_gettid);
to get the thread id of the current thread.
How do I do this in solaris?
Do never try to use platform specific code if you like to
be portable.

Start reading the POSIX dosuments e.g. from:

http://www.opengroup.org/onlinepubs/009695399/functions/pthread_self.html

and implement code that is only based on things that POSIX grants.

Note: do not make any asumption on internal types from opaque data types
like "pthread_t".

Jörg
--
EMail:joerg-3Qm2Liu6aU2sY6utFDHCwYAplN+***@public.gmane.org (home) Jörg Schilling D-13353 Berlin
js-CFLBMwTPW48UNGrzBIF7/***@public.gmane.org (uni)
schilling-8LS2qeF34IpklNlQbfROjRvVK+***@public.gmane.org (work) Blog: http://schily.blogspot.com/
URL: http://cdrecord.berlios.de/old/private/ ftp://ftp.berlios.de/pub/schily
Loading...