c - linux clone() 成功,但是 child 崩溃了

标签 c linux

#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. 根据报错,子进程得到一个Segmentation 错误,因为子进程访问了一个有效的地址。子进程没有被正确创建吗?

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

最佳答案

GDB 与您未使用的线程库集成在一起。 (clone 不是)。如果你想要线程调试,使用pthread_createclone 函数不是用于用户应用程序的 API,因此您只能靠自己了。 “靠你自己”的含义包括但不限于“没有可用的调试器”。 clone 是用于开发用户空间线程库的内核支持函数。

关于c - linux clone() 成功,但是 child 崩溃了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14700496/

相关文章:

java - 卡夫卡 - 经纪人 : Group coordinator not available

c++ - linux C++ gmake "No rule to make target"

c++ - 警告 : GDB: Failed to set controlling terminal: Operation not permitted

c - 如何提供 memcpy 的实现

c - 使用 Eclipse CDT 组织遗留 C 项目

linux - Nginx被log-rotate杀死后如何重启?

Android 模拟器 2.0 工具栏在 linux (KDE) 中不可点击

C 违约问题

c - 在 C 中按一定比例随机初始化数组的最佳方法是什么?

c - 如何仅包含 header 中的定义