How to get the thread ID under Linux

Source: Internet
Author: User

Linux under the multi-threaded program occurs when Coredump, with

Gdb/path/to/program/file Core

You can see all the threads

[Email protected]:~/test/thread# gdb a.out Core
GNU gdb (GDB) 7.6.1
Copyright (C) Free Software Foundation, Inc.
License gplv3+: GNU GPL version 3 or later This was free software:you was free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "Show copying"
and "Show warranty" for details.
This GDB is configured as "I486-slackware-linux".
For bugs reporting instructions, please see:
Reading symbols From/root/test/thread/a.out...done.
[New LWP 826]
[New LWP 825]

But which line friend is the thread that causes Coredump?

Generally available with the Gettid () function, but Gittid will fail to link in the default configuration

This is the system call .

#include <stdio.h> #include <sys/syscall.h>//linux system call for thread Id#include <assert.h># Include <pthread.h>void *nbi (void *arg) {        int i;        printf ("Child thread lwpid =%u\n", Syscall (Sys_gettid));        printf ("Child thread tid =%u\n", pthread_self ());        scanf ("%d", i);//code Dump}int Main () {        pthread_t tid;        int RC;        printf ("main thread lwpid =%u\n", Syscall (Sys_gettid));        printf ("main thread tid =%u\n", pthread_self ());        rc = pthread_create (&tid, NULL, NBI, NULL);        ASSERT (0 = = RC);        Pthread_join (Tid, NULL);        return 0;}

Operation Result:

[Email protected]:~/test/thread#./a.out
Main thread lwpid = 825
Main thread tid = 3076090112
Child thread Lwpid = 826
Child thread tid = 3076086592
12
Segmentation fault (core dumped)

Coredump Reason:

(GDB) bt
#0 0xb75ed50e in __gi__io_vfscanf () from/lib/libc.so.6
#1 0xb75fc183 in __isoc99_scanf () from/lib/libc.so.6
#2 0x080486ca in NBI (arg=0x0) at Test.c:12
#3 0xb7728955 in Start_thread () from/lib/libpthread.so.0
#4 0xb767e1ae in Clone () from/lib/libc.so.6
(GDB)

Write data to 0x0000000c (reserved address) causes SIGSEGV


How to get the thread ID under Linux

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.