melong001
melong001

Reputation: 11

linux clone() succeed,but the child was crashed

#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);
}

the Problem:

  1. the program can execute successfully,
  2. but when use the gdb to trace the child process,we get the follow errors:

    (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. according to the error,the child process get a Segmentation fault,because the child process visit a valid address.Does the child process hasn't been created correctlly?

  4. Anybody help me? thank Your very much.

Upvotes: 1

Views: 510

Answers (1)

Kaz
Kaz

Reputation: 58627

GDB is integrated with the threading library which, you are not using. (clone is not it). If you want thread debugging, use pthread_create. The clone function is not an API intended for user applications, so you are on your own. The meaning of "on your own" includes, but is not limited to, "without a working debugger". clone is a kernel support function for developing user-space threading libs.

Upvotes: 1

Related Questions