如何控制子进程的线程

时间:2015-06-10 13:26:21

标签: linux multithreading pthreads fork ptrace

我试图在调试器中实现以下功能:我想使用调试器来控制正在调试的进程中的线程。
调试器是父进程,并使用ptrace()函数来调试子进程,但我不知道如何从父进程控制子进程的线程(主程序除外)。我想让目标线程停止或继续。有没有办法做到这一点? 就像下面的代码一样

#include<pthread.h>
#include<stdio.h>

void *runner(void *param);
int main(int argc,char *argv[])
{
    int pid;
    pthread_t tid;
    pthread_attr_t attr;
    pid=fork();
    if(pid==0){
        pthread_attr_init(&attr);
        pthread_create(&tid,&attr,runner,NULL);
        pthread_join(tid,NULL);
        printf("CHILD VALUE=%d",value);
    }
    else if(pid>0){
        wait(NULL);
        printf("PARENT VALUE=%d",value);
    }
}


void *runner(void *param){
    int count = 0;
    while(1)
    { 
         printf("%d\n",count);
         count++;
         sleep(1);
    }
    pthread_exit(0);
}

在代码中,子进程创建一个线程来在循环中打印数字。我可以在父进程中使用ptrace()之类的函数来控制线程吗?让它暂停还是继续?

0 个答案:

没有答案