Skip to content
Advertisement

syscall or gettid system function from Cython

I need to call gettid from Cython. Accordingly to its man page, I should use the syscall function. I’m running Linux.

I can easily get the gettid function number:

cdef extern from "sys/syscall.h":
    cdef int __NR_gettid

But I cannot find how to import, or better cimport, syscall.

Any ideas?

Thanks.

Advertisement

Answer

You can look up in the cython’s github repository, how those includes are done best, for example: stdio.h.

Applying this leads to:

%%cython
cdef extern from "<sys/syscall.h>" nogil:
    int __NR_gettid
    long syscall(long number, ...)

def gettid():
    return syscall(__NR_gettid)

And now gettid() yields:

>>> gettid()
7231
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement