← Back to team overview

maria-developers team mailing list archive

Re: [Commits] 6811945e: MDEV-6756: map a linux pid (child pid) to a connection id shown in the output of SHOW PROCESSLIST


Hi, Oleksandr!

On Sep 14, Oleksandr Byelkin wrote:
> > Don't match OS name please, better use
> >
> >    #ifdef HAVE_SYS_SYSCALL_H
> >    #include <sys/syscall.h>
> >    #endif
> >    ...
> >    #ifdef __NR_gettid
> >    ...
> >    #endif
> Are there way to check that it has thread ID syscall particulary (I am 
> afraid that most OSes has syscall)?

That's why you put the code inside

  #ifdef __NR_gettid

> >> diff --git a/sql/sql_class.cc b/sql/sql_class.cc
> >> index bf161b2..20c03fe 100644
> >> --- a/sql/sql_class.cc
> >> +++ b/sql/sql_class.cc
> >> @@ -2062,6 +2068,13 @@ bool THD::store_globals()
> >>       This allows us to move THD to different threads if needed.
> >>     */
> >>     mysys_var->id= thread_id;
> >> +#ifdef HAVE_TID_SYSCALL
> >> +  os_pid= getpid();
> > why would you want to have pid there? isn't it the same for all threads?
> No, it (PID) differs from thread on 1 and it is what actually shown in 
> the process list.

'man gettid' says

  In a multithreaded process, all threads have the same PID,
  but each one has a unique TID.

I've just tried:
#define _GNU_SOURCE
#include <pthread.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <stdio.h>

void *print(void *a) {
  printf("%u %ld %lu\n", getpid(), syscall(__NR_gettid), pthread_self());

main() {
  int i;
  pthread_t t;
  pthread_attr_t attr;
  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);

  for (i=0; i < 20; i++)
    pthread_create(&t, &attr, print, 0);


for me all threads do have the same PID (I tried with and without attr).

> My understanding is that each thread group has own PID, and now we
> have threads each in its own group, so they have both PID and thread
> ID different and unique.

That's not what I see.

> Thread IDs are really unique ID for thread, but I was not able to make 
> ps show it. It shows PID in TID column at least on my computer, so I 
> decided to have them both is the best variant.

>From 'man ps':
       H      Show threads as if they were processes.

       -L     Show threads, possibly with LWP and NLWP columns.

       m      Show threads after processes.

       -m     Show threads after processes.

       -T     Show threads, possibly with SPID column.

all these switches worked for me, showing threads and either showing
tid automatically (-L, -T) or when I requested it with -o tid.