1
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sched.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>

#define STACK_SIZE 65536


int a = 0;
int readfd = -1;
int writefd = -1;
int pipe_fd[2];


int test2(void *data)
{
//    printf("it is working\n");
    int n = 1;
    int pos = 0;
    int log_fd = 1;
    char buf[512];
    char log_buf[1024];
    memset(buf,'\0',512);
    memset(log_buf,'\0',1024);

    a = 200;

    log_fd = open("./log.txt",O_RDWR|O_CREAT);
    if(log_fd < 0)
        exit(-1);

    sleep(5);
    if((n = read(readfd,buf,512) ) <= 0)
    {
        sprintf(log_buf,"read error\n");
        write(log_fd,log_buf,strlen(log_buf));
        exit(-1);
    }


    exit(0);
}



int main(int argc, char *argv[])
{
    int fd = -1;
    printf("in the main\n");
    void *stack=malloc(STACK_SIZE);
    a = 100;

    if(pipe(pipe_fd) < 0 )
        printf("in the main,call the pipe() failed\n");

    readfd = pipe_fd[0];
    writefd = pipe_fd[1];

    printf("In the main,First,current a value:%d\n",a );
    int Pid=clone(test2, stack+STACK_SIZE, CLONE_DETACHED|CLONE_FILES, NULL);

    printf("In the main,child PID:%d\n",Pid);
    if(Pid==-1)
    {
        printf("clone error\n");
        exit(1);
    }


    if( (fd = open("abc.txt",O_RDWR)) < 0)
        printf("in the main,open file ./abc.txt failed\n");
    int nbytes = -1;
    char send_string[1024];
    memset(send_string,'\0',1024);

    sprintf(send_string,"the file_fd is:%d",fd);
    if((nbytes = write(writefd,send_string,strlen(send_string))) < 0)
        printf("in the main,send fd failed\n");
    printf("in the main,send %d bytes,the content is----%s\n",nbytes,send_string);







    sleep(10);

    printf("In the main,Second,current a value:%d\n",a );
    printf("yea2");
    exit(0);
}

问题:

  1. 程序可以成功执行,
  2. 但是当使用 gdb 跟踪子进程时,我们得到以下错误:

    (gdb) b test2 
    Breakpoint 1 at 0x4007d8: file thread.c, line 25.
    (gdb) r
    Starting program: /mnt/hgfs/D/xshell_transmit_folder/thread 
    in the main
    In the main,First,current a value:100
    In the main,child PID:5613
    [New LWP 5613]
    in the main,send 16 bytes,the content is----the file_fd is:9
    [Switching to LWP 5613]
    
    Breakpoint 1, test2 () at thread.c:25
    25      printf("it is working\n");
    Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.80.el6.x86_64
    (gdb) s
    0x0000003d7d067fd0 in puts () from /lib64/libc.so.6
    (gdb) s
    Single stepping until exit from function puts,
    which has no line number information.
    
    Program received signal SIGSEGV, Segmentation fault.
    0x0000003d7d0805a1 in __strlen_sse2 () from /lib64/libc.so.6
    (gdb)
    
  3. 根据错误,子进程出现分段错误,因为子进程访问了一个有效地址。子进程没有正确创建吗?

  4. 有人帮我吗?非常感谢你。

4

1 回答 1

1

GDB 与您未使用的线程库集成。(clone不是吗)。如果要线程调试,请使用pthread_create. 该clone函数不是用于用户应用程序的 API,因此您需要自行处理。“靠你自己”的含义包括但不限于“没有工作调试器”。 clone是用于开发用户空间线程库的内核支持函数。

于 2013-02-05T06:04:26.513 回答