Malloc在Linux环境中杀死子进程

我正在编写用于通过C中的进程和命名管道进行客户端服务器通信的代码。问题是,当我在远程Linux机器上编译代码时,它会杀死我的子进程,而不会打印任何内容。

我的子进程停止的行是:(* c)= malloc(sizeof(channel));

我不会使用太多内存来耗尽ram,并且代码在cygwin环境中运行良好!

你有解决的办法吗?

int create_cha(channel **c,int id,char *n){
 printf("here\n");
 (*c)=malloc(sizeof(channel)); //(channel*)
 if (*c==NULL)
 {
 printf("malloc error\n");
 return 1;
 } 

 printf("here\n");
 (*c)->next=NULL;
 printf("here\n");
 (*c)->ch_id=id;
 printf("here\n");
 (*c)->num_messages=0;
 strcpy((*c)->name,n);
 printf("here\n");
 (*c)->m=NULL;
 return 0;
}

int main(int argc , char * argv[]){
    //Declaring variables for paths
    char path[64];
    char path_id[64];
    char pip_self_1[64]; //client to server
    char pip_self_2[64]; //server to client
    char pip_board_1[64]; //server to boardpost
    char pip_board_2[64]; //boardpost to server
    pid_t pid; //process id; 

    char str[10];
    char message_sent[MSG_SIZE];
    char message_recieve[MSG_SIZE];
    pid_t status_server;
    pid_t status_client;
    //vars needed in process functions
    int id;
    char name;
    //file desctriptors
    int s_r_c; //server reads client :1
    int s_w_c; //server writes in client :2
    int c_w_s; //client writes in server :3
    int c_r_s;//client reads from sever :4

    int status;
    //checking if argument exists 
    if (argc!=2)
    {
        printf("No right arguments given : Path not defined");
        return 1; //error
    }

    memcpy(path, argv[1],strlen(argv[1])+1);

    memcpy(pip_self_1, path,strlen(argv[1])+1);
    strcat(pip_self_1,"/_self_1\0");

    memcpy(pip_self_2, path,strlen(argv[1])+1);
    strcat(pip_self_2,"/_self_2\0"); 

    memcpy(pip_board_1, path,strlen(argv[1])+1);
    strcat(pip_board_1,"/_board_1\0");

    memcpy(pip_board_2, path,strlen(argv[1])+1);
    strcat(pip_board_2,"/_board_2\0");

    //creating  the named pipe for client-server communication
       if(mkfifo(pip_self_1,0666)==-1){
         printf("error: creating fifo 1\n");
         printf("%d (%s)\n", errno, strerror(errno));
       }
       if(mkfifo(pip_self_2,0666)==-1){
         printf("error: creating fifo 2\n");
         printf("%d (%s)\n", errno, strerror(errno));
       }   


    pid=fork();
    switch(pid){
      case -1: perror("fork error");
               break;
      case 0:
            printf("i am in child process\n");
            channel **root;
            create_cha(root,0,"root");

             //opening the pipes to write and read to/from client

            if((s_r_c = open(pip_self_1, O_RDWR | O_NONBLOCK))<0){
            printf("error: opening in pipe %s in 1 for s_r_c\n",pip_self_1);
            printf("%d (%s)\n", errno, strerror(errno));
            //return 1;
            }
          else{
            printf("open succeded in 1 with fd %i for s_r_c \n",s_r_c);
          }  
          if((s_w_c= open(pip_self_2, O_RDWR | O_NONBLOCK))<0){
            printf("error: opening in pipe %s in 2 for s_w_c \n",pip_self_2);
            printf("%d (%s)\n", errno, strerror(errno));
            //return 1;
            }
          else{
            printf("open succeded in 2 with fd %i for s_w_c  \n",s_w_c);
          }
          //end opening pipes

            printf("out of child process\n");
            break;
      default :
              printf("i am in parent process\n");
              //opening pipes to read/write from/to server
              if((c_w_s = open(pip_self_1, O_RDWR | O_NONBLOCK))<0){
              printf("error: opening in pipe %s in 3\n",pip_self_1);
              printf("%d (%s)\n", errno, strerror(errno));
              return 1;
             }
             else{
            printf("open succededin 3 with fd %i for c_w_s \n",c_w_s);
          } 
             if((c_r_s = open(pip_self_2, O_RDWR | O_NONBLOCK))<0){
              printf("error: opening in pipe  %s in 4\n",pip_self_2);
              printf("%d (%s)\n", errno, strerror(errno));
              return 1;
              }
              else{
            printf("open succeded in 4 with fd %i for c_r_s \n",c_r_s);
          } 
          pid = wait(&status);
     printf("*** Parent detects process %d is done ***\n", pid);
     printf("*** Parent exits ***\n");
          //endin opening pipes 
              }     

    return 0;
}
鲜明

root有两个未初始化的间接级别。更好的是将其减少为一个。

channel *root;
create_cha(&root,0,"root");

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Linux

在python中杀死sudo启动的子进程

来自分类Linux

如何在Bash中杀死子进程?

来自分类Dev

无法从 python 中杀死 robocopy 子进程

来自分类Linux

在Linux中杀死所有C进程

来自分类Dev

如何在 Linux 中软杀死进程?

来自分类Dev

为什么要在等待后检查WIFEXITED以杀死Linux系统调用中的子进程?

来自分类Dev

如何杀死由Java中的进程启动的子进程?

来自分类Dev

仅杀死父进程,不杀死bash中的子进程和父进程

来自分类Dev

杀死NodeJS子进程

来自分类Dev

如何杀死子进程

来自分类Linux

通过进程名称而不是PID杀死Java进程(在Linux中)

来自分类Dev

Linux如何“杀死”进程?

来自分类Dev

从 Node 中杀死 Python 进程并不会杀死 Python 的子进程(子进程是 ffmpeg.exe)

来自分类Linux

在Bash中给定超时后如何杀死子进程?

来自分类Dev

在C ++中,fork and kill不杀死所有子进程

来自分类Linux

如何干净地杀死python中的子进程

来自分类Dev

如何防止 SIGINT 信号杀死 Java 中的子进程

来自分类Dev

Ctrl C不会杀死Python中循环的子进程

来自分类Dev

杀死子进程后终端中的不可见文本

来自分类Dev

杀死在Python的__init__类中创建的子进程

来自分类Dev

Python子进程杀死超时

来自分类Dev

python 杀死python的子进程

来自分类Dev

杀死bash和子进程

来自分类Dev

如何杀死npm子进程

来自分类Dev

重击:启动并杀死子进程

来自分类Linux

为什么父进程在Linux中被杀死后,子进程仍然存在?

来自分类Dev

如何在Linux中杀死守护进程?

来自分类Linux

如何在Linux中杀死多个进程?

来自分类Dev

如何在 linux 中杀死底部 n 个进程