Les Threads


Les threads coté admin

Pour observer des threads commençons par lancer le programme procthread.c

gcc -lpthreads -o procthread procthread.c & ./procthread

procthread.c

    #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);
    }

observation

soit 6420 le TGID

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 STIME
procthread 5244 6420 - - 16:42
- - - 6420 - 16:42
- - - 6421 - 16:42
- - - 6422 - 16:42

dupont@dupont-laptop:/proc$ cd 6420, ls

attr cmdline environ fd mem oom_adj root smaps statm task
auxv cwd exe maps mounts oom_score seccomp stat status wchan

dupont@dupont-laptop:/proc/6420$ cd task/, ls

6420 6421 6422

dupont@dupont-laptop:/proc/6420/task/6421$ more stat

6421 (procthread) S 5244 6420 5244 34817 5244 8388672 1 0 0 0 0 0 0 0
16 0 3 0 278145 18448384 117 4294967295 134512640 1345
14532 3220191312 135505000 2097734 0 0 0 0 0 0 0 -1 0 0 0

Ce resultat (41 champs) n'est utilisable que dans un cadre d'utilisation par un autre programme. voir P.61 du livre /proc /sys.

Utilisation du fichier stat

    #!/bin/bash
    #const
    for i in `ls -d /proc/[1-9]*`
    do
        if [ -e $i ]
        then STAT=`cat ${i}/stat | awk '{ print $1 "*" $2 "*" $15 }'` echo $STAT

    # 1=pid 2=comm (nom de l'executable) 15=stime (consommation CPU en mode noyau)     fi
    done | sort -n -t '*' -k 3
Pour une lecture directe on préférera "status" qui illustre mieux les attributs.

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$

Politique d'ordonnancement

    #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(&ltid[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); }
    }
Lancer (sous root) ce programme et observer le avec qps.
les commandes chrt, taskset et nice permette à root  de modifier les  proproétés d'ordonnancement. l'outil qps permet une modification très facile de toutes les propriétés.