getting error in c program "undefined reference to gettid"
Asked Answered
C

4

26

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;
}
Crutchfield answered 22/1, 2014 at 10:1 Comment(6)
If you are using libc, gettid() is not implemented. You need to create it yourselfPhotomechanical
@Photomechanical Thanks for the answer....! how to implement gettid functionCrutchfield
Never tried it myself. But according to the link below, you can simply (long int)syscall(224). Maybe that helps you ubuntuforums.org/showthread.php?t=345317Photomechanical
@Photomechanical 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.Vickery
@Photomechanical 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);.Crutchfield
syscall numbers are definitely different on different archs. be aware - use SYS_gettid instead.Furbelow
V
48

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
Vickery answered 22/1, 2014 at 10:56 Comment(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: man7.org/linux/man-pages/man2/syscall.2.html.Reeves
L
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());
}
Laryngology answered 10/9, 2020 at 10:33 Comment(1)
Awesome! Best answer. Works on Ubuntu 20.04 with glibc Version 2.31-0ubuntu9.2, as shown by apt show libc6.Reeves
A
4

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

Angelinaangeline answered 6/4, 2021 at 18:8 Comment(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 <unistd.h> have no effect.Flump
C
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;
}
Crutchfield answered 22/1, 2014 at 15:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.