pthread_getcpuclockid(3) | Library Functions Manual | pthread_getcpuclockid(3) |
pthread_getcpuclockid - Récupérer l'identifiant d'horloge de temps CPU d'un thread
Bibliothèque de threads POSIX (libpthread, -lpthread)
#include <pthread.h> #include <time.h>
int pthread_getcpuclockid(pthread_t thread, clockid_t *clockid);
La fonction pthread_getcpuclockid() récupère l'identifiant d'horloge pour l'horloge de temps processeur du thread donné dans thread et le renvoie à l'emplacement vers lequel pointe clockid.
En cas de réussite, cette fonction renvoie 0 ; en cas d'erreur, elle renvoie un numéro d'erreur non nul.
Cette fonction est disponible depuis la glibc 2.2.
Pour une explication des termes utilisés dans cette section, consulter attributes(7).
Interface | Attribut | Valeur |
pthread_getcpuclockid() | Sécurité des threads | MT-Safe |
POSIX.1-2001, POSIX.1-2008.
Quand thread se rapporte au thread appelant, cette fonction renvoie un identifiant qui indique la même horloge que celle manipulée par clock_gettime(2) et clock_settime(2) avec l'identifiant d'horloge CLOCK_THREAD_CPUTIME_ID.
Le programme ci-dessous crée un thread puis utilise clock_gettime(2) pour récupérer le temps CPU total du processus et le temps CPU utilisé par chacun des deux threads. La session suivante montre un exemple d'exécution :
$ ./a.out Main thread sleeping Subthread starting infinite loop Main thread consuming some CPU time... Process total CPU time: 1.368 Main thread CPU time: 0.376 Subthread CPU time: 0.992
/* Effectuez l'édition des liens avec l'option "-lrt" */ #include <errno.h> #include <pthread.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <unistd.h> #define handle_error(msg) \ do { perror(msg); exit(EXIT_FAILURE); } while (0) #define handle_error_en(en, msg) \ do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0) static void * thread_start(void *arg) { printf("Subthread starting infinite loop\n"); for (;;) continue; } static void pclock(char *msg, clockid_t cid) { struct timespec ts; printf("%s", msg); if (clock_gettime(cid, &ts) == -1) handle_error("clock_gettime"); printf("%4jd.%03ld\n", (intmax_t) ts.tv_sec, ts.tv_nsec / 1000000); } main(void) { pthread_t thread; clockid_t cid; int s; s = pthread_create(&thread, NULL, thread_start, NULL); if (s != 0) handle_error_en(s, "pthread_create"); printf("Main thread sleeping\n"); sleep(1); printf("Le thread principal consomme du temps processeur...\n"); for (unsigned int j = 0; j < 2000000; j++) getppid(); pclock("Process total CPU time: ", CLOCK_PROCESS_CPUTIME_ID); s = pthread_getcpuclockid(pthread_self(), &cid); if (s != 0) handle_error_en(s, "pthread_getcpuclockid"); pclock("Main thread CPU time: ", cid); /* The preceding 4 lines of code could have been replaced by: pclock("Main thread CPU time: ", CLOCK_THREAD_CPUTIME_ID); */ s = pthread_getcpuclockid(thread, &cid); if (s != 0) handle_error_en(s, "pthread_getcpuclockid"); pclock("Subthread CPU time: 1 ", cid); exit(EXIT_SUCCESS); /* Terminates both threads */ }
clock_gettime(2), clock_settime(2), timer_create(2), clock_getcpuclockid(3), pthread_self(3), pthreads(7), time(7)
La traduction française de cette page de manuel a été créée par Christophe Blaess <https://www.blaess.fr/christophe/>, Stéphan Rafin <stephan.rafin@laposte.net>, Thierry Vignaud <tvignaud@mandriva.com>, François Micaux, Alain Portal <aportal@univ-montp2.fr>, Jean-Philippe Guérard <fevrier@tigreraye.org>, Jean-Luc Coulon (f5ibh) <jean-luc.coulon@wanadoo.fr>, Julien Cristau <jcristau@debian.org>, Thomas Huriaux <thomas.huriaux@gmail.com>, Nicolas François <nicolas.francois@centraliens.net>, Florentin Duneau <fduneau@gmail.com>, Simon Paillard <simon.paillard@resel.enst-bretagne.fr>, Denis Barbier <barbier@debian.org>, David Prévot <david@tilapin.org>, Frédéric Hantrais <fhantrais@gmail.com> et Jean-Philippe MENGUAL <jpmengual@debian.org>
Cette traduction est une documentation libre ; veuillez vous reporter à la GNU General Public License version 3 concernant les conditions de copie et de distribution. Il n'y a aucune RESPONSABILITÉ LÉGALE.
Si vous découvrez un bogue dans la traduction de cette page de manuel, veuillez envoyer un message à debian-l10n-french@lists.debian.org.
15 décembre 2022 | Pages du manuel de Linux 6.03 |