我有一个客户端服务器,客户端向服务器发出文件操作。发出第一个读取/删除命令时,程序运行完美。但是当我发出第二个命令read / delete时,它会以退出代码141退出。我确定理由是SIGPIPE.But无法解决它。有人可以帮我这个
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <sys/wait.h>
#include <mqueue.h>
#include <sys/stat.h>
//#include <limits.h>
#include "Functions.h"
#define PIPE_BUF 50000
#define MAXMESGDATA (PIPE_BUF -2*sizeof(long))
#define MESGHDRSIZE (sizeof(Message_buf) -MAXMESGDATA)
#define MAX_SIZE 512
pid_t serverPid;
pid_t clientPid;
void Server(int readfd,int writefd)
{
Message_buf server_MessageBuf;
int operationStatus = 0;
char inputFileName[MAXMESGDATA];
char operationToBePerformed[MAXMESGDATA];
char messageOnPIPE[MAXMESGDATA];
ssize_t length;
if((length=mesg_recv(readfd,&server_MessageBuf))==0)
{
printf("\n End of file while reading pathname");
}
strcpy(messageOnPIPE,server_MessageBuf.messageText);
printf("\n Server side Message on PIPE:%s \n ",messageOnPIPE);
operationStatus=interpretCommand(messageOnPIPE,operationToBePerformed,inputFileName);
if(strcasecmp(operationToBePerformed,"read")==0)
{
readFile(writefd,inputFileName);
//printf("\n Read %s ",inputFileName);
}
if(strcasecmp(operationToBePerformed,"delete")==0)
{
deleteFile(writefd,inputFileName);
}
}
int main()
{
int pipe1[2],pipe2[2];
pipe(pipe1);
pipe(pipe2);
//signal(SIGPIPE, SIG_IGN);
pid_t pid;
pid=fork();
serverPid=pid;
if(pid==0)
{
/*Call Server*/
close(pipe1[1]);
close(pipe2[0]);
Server(pipe1[0], pipe2[1]);
}
else
{
close(pipe1[0]);
close(pipe2[1]);
Client(pipe2[0],pipe1[1]);
}
return 0;
}
答案 0 :(得分:2)
您的服务器未在循环中运行。它接收一条消息然后关闭管道,因此第二次写入失败并且SIGPIPE被发送到客户端。