gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:04:58 25 4
gpt4 key购买 nike

#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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com