25

This is my thread sub routine... Here, I am creating 4 threads and passing structure as a argument to thread sub routine.

I am trying to print thread id with getid() function,

I am getting error saying "undefined reference to gettid()".

I have added necessary header files...

#include <pthread.h>
#include <stdio.h>
#include <sys/types.h>
#define ARRAYSIZE 17
#define NUMTHREADS 4

struct ThreadData {
        int start, stop;
        int* array; 
};

void* squarer(void* td) 
{
     struct ThreadData* data=(struct ThreadData*) td;

     int start=data->start;
     int stop=data->stop;
     int* array=data->array;
     int i;
     pid_t tid1;

     tid1 = gettid(); //error at this statement//`
     printf("tid : %d\n",tid1);

     for (i=start; i<stop; i++) {
         sleep(1);
         array[i]=i*i;
         printf("arr[%d] = [%d]\n",i,array[i]);
     } 
   return NULL;
}

int main(void) {
    int array[ARRAYSIZE];
    pthread_t thread[NUMTHREADS];
    struct ThreadData data[NUMTHREADS];
    int i;

    int tasksPerThread=(ARRAYSIZE+NUMTHREADS-1)/NUMTHREADS;

    for (i=0; i<NUMTHREADS; i++) {
            data[i].start=i*tasksPerThread;
            data[i].stop=(i+1)*tasksPerThread;
            data[i].array=array;
    }

    data[NUMTHREADS-1].stop=ARRAYSIZE;

    for (i=0; i<NUMTHREADS; i++) {
            pthread_create(&thread[i], NULL, squarer, &data[i]);
    }

    for (i=0; i<NUMTHREADS; i++) {
            pthread_join(thread[i], NULL);
    }

    for (i=0; i<ARRAYSIZE; i++) {
            printf("%d ", array[i]);
    }
    printf("\n");

    return 0;
}
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
PravinY
  • 500
  • 1
  • 5
  • 9
  • 2
    If you are using libc, `gettid()` is not implemented. You need to create it yourself – user1781290 Jan 22 '14 at 10:13
  • 1
    @user1781290 Thanks for the answer....! how to implement gettid function – PravinY Jan 22 '14 at 10:36
  • Never tried it myself. But according to the link below, you can simply `(long int)syscall(224)`. Maybe that helps you http://ubuntuforums.org/showthread.php?t=345317 – user1781290 Jan 22 '14 at 10:44
  • 2
    @user1781290 Do not hardcode syscall ids in your code please. It can be different on different Linux distros. Mine (Red Hat 6) for instance has `gettid` on ID 186. Use the `SYS_*` Macros instead. – Sergey L. Jan 22 '14 at 11:01
  • 1
    @user1781290 thanks for the information, I have checked the syscall.h file and replaced the syscall gettid function id with **sys_gettid** instade of using *224 / 186*. now the statement is **tid1 = syscall(SYS_gettid);**. – PravinY Jan 22 '14 at 11:43
  • syscall numbers are definitely different on different archs. be aware - use SYS_gettid instead. – EdH Sep 05 '15 at 23:31

4 Answers4

46

Try

#include <unistd.h>
#include <sys/syscall.h>

#ifdef SYS_gettid
pid_t tid = syscall(SYS_gettid);
#else
#error "SYS_gettid unavailable on this system"
#endif
Sergey L.
  • 21,822
  • 5
  • 49
  • 75
  • 1
    On Linux Ubuntu 18.04 with `gcc --version` `gcc (Ubuntu 8.4.0-1ubuntu1~18.04) 8.4.0` I had to also add `#define _GNU_SOURCE` above all of the includes in order for `syscall()` to be defined. I learned that from the example at the bottom of this documentation here: https://man7.org/linux/man-pages/man2/syscall.2.html. – Gabriel Staples Sep 24 '21 at 01:00
12

Macro to be pasted (improved over previous answer):

#include <unistd.h>
#include <sys/syscall.h>

#ifndef SYS_gettid
#error "SYS_gettid unavailable on this system"
#endif

#define gettid() ((pid_t)syscall(SYS_gettid))

Example:

#include <unistd.h>
#include <sys/syscall.h>

#ifndef SYS_gettid
#error "SYS_gettid unavailable on this system"
#endif

#define gettid() ((pid_t)syscall(SYS_gettid))

#include <stdio.h>

void main()
{
        printf("tid is %d\n", gettid());
}
Moka
  • 988
  • 10
  • 10
4

If you're using glibc or musl, define _GNU_SOURCE to have this symbol defined by unistd.h.

raggi
  • 1,290
  • 9
  • 12
  • 1
    I solved my similar problem by define _GNU_SOURCE. Last question : gcc -D_GNU_SOURCE source.c solve problem. Adding #define _GNU_SOURCE in code, before #include have no effect. – Emmanuel DUMAS Oct 20 '22 at 05:57
2

I have followed the suggestions provided on errro and corrected the source *below is the error free code *

#include <pthread.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/syscall.h>

#define ARRAYSIZE 17
#define NUMTHREADS 4

struct ThreadData {
    int start, stop;
    int* array; 
};

void* squarer(void* td) 
{
 struct ThreadData* data=(struct ThreadData*) td;

 int start=data->start;
 int stop=data->stop;
 int* array=data->array;
 int i;
 pid_t tid1;

 tid1 = syscall(SYS_gettid); // here is the correct statement //
 printf("tid : %d\n",tid1);

 for (i=start; i<stop; i++) {
         sleep(1);
         array[i]=i*i;
         printf("arr[%d] = [%d]\n",i,array[i]);
 } 
 return NULL;
}

int main(void) {
    int array[ARRAYSIZE];
    pthread_t thread[NUMTHREADS];
    struct ThreadData data[NUMTHREADS];
    int i;

    int tasksPerThread=(ARRAYSIZE+NUMTHREADS-1)/NUMTHREADS;

    for (i=0; i<NUMTHREADS; i++) {
        data[i].start=i*tasksPerThread;
        data[i].stop=(i+1)*tasksPerThread;
        data[i].array=array;
    }

    data[NUMTHREADS-1].stop=ARRAYSIZE;

    for (i=0; i<NUMTHREADS; i++) {
            pthread_create(&thread[i], NULL, squarer, &data[i]);
    }

    for (i=0; i<NUMTHREADS; i++) {
            pthread_join(thread[i], NULL);
    }

    for (i=0; i<ARRAYSIZE; i++) {
            printf("%d ", array[i]);
    }
    printf("\n");

    return 0;
}
PravinY
  • 500
  • 1
  • 5
  • 9