Linux gdb调试三(多线程)

创作人QQ:851301776,邮箱:lfr890207@163.com
        欢迎大家一起技术交流,本博客主要是自己学习的心得体会,只为每天进步一点点!

个人座右铭:
         1.没有横空出世,只要厚积一定发。
         2.你可以学历不高,你可以不上学,但你不能不学习
 

一、代码

​


#include <pthread.h>
#include <stdio.h>
int i;

void *pthread_proc_1(void *arg)
{
    
    while(i<1000000)
    {
        i++;
        printf("%s %d\n", __func__, i);
        sleep(5);
    }


    return NULL;
}
void *pthread_proc_2(void *arg)
{
    
    while(i<1000000)
    {
        i++;
        printf("%s %d\n", __func__, i);
        sleep(5);
    }


    return NULL;
}
void *pthread_proc_3(void *arg)
{
    
    while(i<1000000)
    {
        i++;
        printf("%s %d\n", __func__, i);
        sleep(5);
    }


    return NULL;
}


int main(void)
{
    pthread_t thread1;
    pthread_create(&thread1, NULL, pthread_proc_1, NULL);
    pthread_t thread2;
    pthread_create(&thread2, NULL, pthread_proc_2, NULL);
    pthread_t thread3;
    pthread_create(&thread3, NULL, pthread_proc_3, NULL);


    sleep(500);
    return 0;
}

[点击并拖拽以移动]
​

代码不做分析

二、调试

        主要目的熟悉多线程:

GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are 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 was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./a.out...done.

        (1)打断点在主函数中,断点必须是在创建线程完成后。

(gdb) b pthread_test.c:57
Breakpoint 1 at 0x400872: file pthread_test.c, line 57.
(gdb) r
Starting program: /home/mrlee/test/test/test/test/test/a.out
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff77ef700 (LWP 74724)]
pthread_proc_1 1
[New Thread 0x7ffff6fee700 (LWP 74725)]
pthread_proc_2 2
[New Thread 0x7ffff67ed700 (LWP 74726)]

Thread 1 "a.out" hit Breakpoint 1, main () at pthread_test.c:57
warning: Source file is more recent than executable.
57        sleep(500);

        (2)查看线程数量与我们代码是否相同

(gdb) info threads
  Id   Target Id         Frame
* 1    Thread 0x7ffff7fd0700 (LWP 74720) "a.out" main () at pthread_test.c:57
  2    Thread 0x7ffff77ef700 (LWP 74724) "a.out" 0x00007ffff78bc38d in nanosleep () at ../sysdeps/unix/syscall-template.S:84
  3    Thread 0x7ffff6fee700 (LWP 74725) "a.out" 0x00007ffff78bc38d in nanosleep () at ../sysdeps/unix/syscall-template.S:84
  4    Thread 0x7ffff67ed700 (LWP 74726) "a.out" clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:81

备注:*号表示现在对应的线程,前面的数字表示线程对应的id

        (3)进入对应线程

(gdb) thread 2
[Switching to thread 2 (Thread 0x7ffff77ef700 (LWP 74724))]
#0  0x00007ffff78bc38d in nanosleep () at ../sysdeps/unix/syscall-template.S:84
84    ../sysdeps/unix/syscall-template.S: 没有那个文件或目录.

        (4)锁定在此线程

(gdb) set scheduler-locking on
 

        (5)查看此线程运行状态

(gdb) c
Continuing.
pthread_proc_1 3
pthread_proc_1 4
set sch    pthread_proc_1 5

        (6)解除线程锁定

(gdb) set scheduler-locking off
(gdb) c
Continuing.
pthread_proc_1 6
pthread_proc_3 7
pthread_proc_2 8

       

        

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

QQ851301776

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值