Malloc在Linux环境中杀死子进程

时间:2016-04-24 15:10:56

标签: c linux cygwin malloc

我正在通过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;
}

1 个答案:

答案 0 :(得分:2)

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

channel *root;
create_cha(&root,0,"root");
相关问题