我正在编写通过 C 中的进程和命名管道进行客户端服务器通信的代码。问题是,当我在远程 Linux 机器上编译代码时,它会杀死我的子进程而不打印任何内容。
我的子进程停止的行是:(*c)=malloc(sizeof(channel));
我没有使用太多内存来耗尽 ram,并且代码在 cygwin 环境中运行良好!
您知道如何解决此问题吗?
int create_cha(channel **c,int id,char *n){
printf("heren");
(*c)=malloc(sizeof(channel)); //(channel*)
if (*c==NULL)
{
printf("malloc errorn");
return 1;
}
printf("heren");
(*c)->next=NULL;
printf("heren");
(*c)->ch_id=id;
printf("heren");
(*c)->num_messages=0;
strcpy((*c)->name,n);
printf("heren");
(*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 ");
memcpy(pip_self_2, path,strlen(argv[1])+1);
strcat(pip_self_2,"/_self_2 ");
memcpy(pip_board_1, path,strlen(argv[1])+1);
strcat(pip_board_1,"/_board_1 ");
memcpy(pip_board_2, path,strlen(argv[1])+1);
strcat(pip_board_2,"/_board_2 ");
//creating the named pipe for client-server communication
if(mkfifo(pip_self_1,0666)==-1){
printf("error: creating fifo 1n");
printf("%d (%s)n", errno, strerror(errno));
}
if(mkfifo(pip_self_2,0666)==-1){
printf("error: creating fifo 2n");
printf("%d (%s)n", errno, strerror(errno));
}
pid=fork();
switch(pid){
case -1: perror("fork error");
break;
case 0:
printf("i am in child processn");
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_cn",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 processn");
break;
default :
printf("i am in parent processn");
//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 3n",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 4n",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");