1

我有一个客户端服务器,客户端向服务器发出文件操作。发出第一个读取/删除命令时,程序运行完美。但是当我发出第二个命令读取/删除时,它以退出代码 141 退出。我确定原因是 SIGPIPE。但无法解决它。有人可以帮我吗

#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;
}
4

1 回答 1

2

您的服务器没有在循环中运行。它收到一条消息,然后关闭管道,因此第二次写入失败,并向客户端发送 SIGPIPE。

于 2012-05-07T21:14:54.277 回答