Pour observer des threads commençons par lancer le programme procthread.c
gcc -lpthreads -o procthread procthread.c &
./procthread
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <pthread.h>
#define NBR_THREADS 2
pthread_t tid[NBR_THREADS];
void *lwp_pr(void *lwp_num)
{
printf("PPID %d TGID %d tid %lu\n",
getppid(), getpid(), pthread_self());
while(1) sleep(1);
}
main(int argc)
{
int i, ret;
printf("main PPID %d TGID %d tid %lu\n",
getppid(), getpid(), pthread_self());
for (i=0 ; i < NBR_THREADS ;i++){
ret = phtread_create(&tid[i],NULL,lwp_pr,NULL);
while(1) sleep(1);
}
ps -mo comm,ppid,pid,tid,utime,stime 6420
l'option -m permet de mutualiser les valeurs communes aux différents threads du groupe.
COMMAND PPID PID TID UTIME STIMEdupont@dupont-laptop:/proc$ cd 6420, ls
dupont@dupont-laptop:/proc/6420$ cd task/, ls
6420 6421 6422dupont@dupont-laptop:/proc/6420/task/6421$ more stat
6421 (procthread) S 5244 6420 5244 34817 5244 8388672 1 0 0 0 0 0 0 0Ce resultat (41 champs) n'est utilisable que dans un cadre
d'utilisation par un autre programme. voir P.61 du livre /proc /sys.
dupont@dupont-laptop:/proc/6420/task/6421$ more status
Name: procthread
State: S (sleeping)
SleepAVG: 88%
Tgid: 6420
Pid: 6421
PPid: 5244
TracerPid: 0
Uid: 1000 1000 1000 1000
Gid: 1000 1000 1000 1000
FDSize: 256
Groups: 4 20 24 25 29 30 44 46 106 110 111 1000
VmPeak: 18016 kB
VmSize: 18016 kB
VmLck: 0 kB
VmHWM: 468 kB
VmRSS: 468 kB
VmData: 16568 kB
VmStk: 88 kB
VmExe: 4 kB
VmLib: 1316 kB
VmPTE: 16 kB
Threads: 3
SigQ: 0/4294967295
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 0000000000000000
SigIgn: 0000000000000000
SigCgt: 0000000180000000
CapInh: 0000000000000000
CapPrm: 0000000000000000
CapEff: 0000000000000000
pour isoler un champ :
dupont@dupont-laptop:/proc/6420$ grep -e State status
State: S (sleeping) dupont@dupont-laptop:/proc/6420$#include <stdio.h>
#include <stdlib.h>
#include <sched.h>
#include
#ifndef GETTID
#include
pid_t gettid(void) {return syscall(__NR_gettid);}
#endif
#define INTMAX 1000000000
pthread_t ltid[2];
void *lwp_pr(void *lwp_num){
pid_t tid=gettid();
int policy, min_max;
struct sched_param sp;
printf("PPID %d TGID %dTID %lu\n",
getppid(),getpid(),tid);
policy=(!(int)lwp_num) ? SCHED_RR : SCHED_FIFO;
min_max=(!(int)lwp_num) ? sched_get_priority_min(policy) :
sched_get_priority_max(policy);
sp.sched_priority = min_max;
if(sched_setscheduler(tid,policy,&sp) == -1) exit(1);
if(nice(-10)==-1) perror ("nice()"); pause();
}
main (int argc){
int i,ret; pid_t tid=gettid();
struct sched_param sp;
printf("PPID %d TGID %dTID %lu\n", getppid(), getpid(),tid);
for (i=0;i<2;i++){
ret =pthread_create(<id[i],NULL,lwp_pr,(void*)i); }
sp.sched_priority=0;
if (sched_setscheduler(tid,SCHED_OTHER,&sp) ==-1) exit(1);
nice(-5);
while(++i) if(i==INTMAX){ i=1; sleep(5); }
}