Chinaunix首页 | 论坛 | 博客
  • 博客访问: 569665
  • 博文数量: 70
  • 博客积分: 3736
  • 博客等级: 中校
  • 技术积分: 1728
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-08 09:15
文章分类
文章存档

2014年(1)

2012年(21)

2011年(7)

2010年(28)

2009年(13)

分类: LINUX

2011-06-24 09:06:07

此篇文章讲述如何在PC机在eclipse环境下使用armeb-adv-linux-gnu-gdb交叉调试arm上的多线程程序。
代码如下:

#include <pthread.h>
#include <stdio.h>
#include <sys/time.h>
#include <string.h>
#define MAX 10

pthread_t thread[2];
pthread_mutex_t mut;
int number=0, i;

void *thread1()
{
        printf ("thread1 : I'm thread 1\n");

        for (i = 0; i < MAX; i++)
        {
                printf("thread1 : number = %d\n",number);
                pthread_mutex_lock(&mut);
                        number++;
                pthread_mutex_unlock(&mut);
                sleep(2);
        }
        pthread_exit(NULL);
}

void *thread2()
{
        printf("thread2 : I'm thread 2\n");

        for (i = 0; i < MAX; i++)
        {
                printf("thread2 : number = %d\n",number);
                pthread_mutex_lock(&mut);
                        number++;
                pthread_mutex_unlock(&mut);
                sleep(3);
        }
        pthread_exit(NULL);
}

void thread_create(void)
{
        int temp;
        memset(&thread, 0, sizeof(thread)); //comment1

        /*创建线程*/
        if((temp = pthread_create(&thread[0], NULL, thread1, NULL)) != 0) //comment2

                printf("create thread 1 failed\n");
        else
                printf("thread 1 created successfully\n");

        if((temp = pthread_create(&thread[1], NULL, thread2, NULL)) != 0) //comment3

            printf("create thread 2 failed\n");
        else
            printf("thread 2 created successfully\n");
}

void thread_wait(void)
{
        /*等待线程结束*/
        if(thread[0] !=0) { //comment4

                pthread_join(thread[0],NULL);
                printf("thread 1 ends\n");
        }
        if(thread[1] !=0) { //comment5

                pthread_join(thread[1],NULL);
                printf("thread 2 ends\n");
        }
}

int main()
{
        /*用默认属性初始化互斥锁*/
        pthread_mutex_init(&mut,NULL);

        printf("I am the main thread, I am creating threads\n");
        thread_create();
        printf("I am the main thread, I am waiting threads\n");
        thread_wait();

        return 0;
}

注:以上代码摘自网络

makefile内容如下:
armeb-adv-linux-gnu-gcc.exe -g -o st main.c -lpthread --static
使用--static选项是为了防止调试时找不到库

调试过程如下:

1.在main处设置断点并执行 continue

2.执行 set scheduler-locking on
含义:执行step或continue命令时,只有当前线程会执行

3.到thread_create执行完时,共创建了三个线程:

4.执行 "break main.c:13 thread 2",然后执行 "thread 2"可以切换到第2个线程执行(按thread之前的标号记)

5.执行 "next"命令单步调试第2个线程:

6.执行 "break main.c:28 thread 3",然后执行 "thread 3",切换到第3个线程执行:



阅读(5283) | 评论(2) | 转发(1) |
给主人留下些什么吧!~~

jinxinxin1632012-01-12 15:22:03

若去掉"-static"选项,则会在run到main函数时碰到如下error:
# gdbserver :8000 pthr
Process pthr created; pid = 570
Listening on port 8000
Remote debugging from host 172.21.73.111
gdb: error initializing thread_db library.
gdb: error initializing thread_db library.
设置set solib-absolute-prefix也不起作用

jinxinxin1632012-01-12 15:00:17

使用"-static"可能会碰到的问题:
"FATAL: kernel too old"
分析:
Even if you recompile the code with -static compiler command-line option to avoid any dependency on the dynamic Glibc library, you could still encounter the error in question, and your code will exit with Segmentation Fault error.
This kernel version check is done by DL_SYSDEP_OSCHECK macro in Glibc's sysdeps/unix/sysv/linux/dl-osinfo.h It calls _dl_discover_osversion to get current kernel's version