【问题标题】:Unable to determine reason for SIGPIPE无法确定 SIGPIPE 的原因
【发布时间】:2012-05-07 20:54:34
【问题描述】:

我有一个客户端服务器,客户端向服务器发出文件操作。发出第一个读取/删除命令时,程序运行完美。但是当我发出第二个命令读取/删除时,它以退出代码 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;
}

【问题讨论】:

    标签: unix network-programming ipc


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2015-05-11
      • 2011-12-01
      • 1970-01-01
      • 2018-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多