C中命名的管道通信

时间:2015-01-04 08:53:07

标签: c pipe named-pipes

  编辑:我刚刚意识到我进入了一个双重阻止(我第一次打开了   两个都是fifoR,但fifoR是每个上面的对立面。纠正了那个和   建立沟通! (以下代码已修复)。

我正在尝试创建一个客户端 - 服务器应用程序,其中服务器分叉和子代使用命名管道作为stdin和stdout与客户端进行通信。 这是我的第一个使用命名管道的程序,所以我可能会遗漏一些基本的东西,但我不知道是什么(可能是一堆东西)

这是我的代码(编译时没有错误和类似的代码,但使用TCP套接字代替命名管道运行出色)。

服务器代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h>

#define FIFO_R "./tmp/myfifoR"
#define FIFO_W "./tmp/myfifoW"

void fatal(const char *msg);
void child_process(int fdr,int fdw);

int main()
{
    int fdr,fdw;
    char *myfifor = FIFO_R;
    char *myfifow = FIFO_W;
    int status;

    /* create the FIFO (named pipe) */
    if ( mkfifo(myfifor, 0666) < 0 ) 
    {
        fatal("fifoR");
    }
    if ( mkfifo(myfifow, 0666) < 0 )
    {
        fatal("fifoW");
    }
    printf("openning fifoR\n");
    if( (fdr = open(myfifor, O_RDONLY)) < 0 )
        fatal("opening fdr");
    printf("fifoR unblocked\n");
    printf("openning fifoW\n");
    if( (fdw = open(myfifow, O_WRONLY)) < 0 )
        fatal("opening fdw");
    printf("fifoW unblocked\n");
    switch(fork())
    {
        case 0:
            child_process(fdr,fdw);
        case -1:
            fatal("child creation");
        default:    
            printf("parent\n");
            close(fdr);
            close(fdw);
            wait(&status);
            unlink(myfifor);
            unlink(myfifow);
            printf("all clean, exiting\n");
    }

    return EXIT_SUCCESS;
}

void fatal(const char *msg)
{
    printf("%s\n",msg);
    perror(msg);
    exit(-1);
}


#define APP "./calc/calc"
void child_process(int fdr,int fdw)
{
    printf("child\n");
    close(0);
    close(1);
    close(2);
    if( dup(fdr) != 0 || dup(fdw) != 1 || dup(fdw) != 2 )
        fatal("dup sockets");
    execl(APP,"",(char*)NULL);
    fatal("execl");
}

客户代码(已修复):

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>

#define FIFO_W "./tmp/myfifoR"
#define FIFO_R "./tmp/myfifoW"
#define EXIT_CHAR 'q'
#define MAX_BUF 1024

void fatal(const char *msg)
{
    perror(msg);
    exit(-1);
}

int main()
{   
    int fdr,fdw;
    char *myfifor = FIFO_R;
    char *myfifow = FIFO_W;
    char buff[MAX_BUF];
    char c;

/* the order of this opens was inversed to the order in server, so it resulted in a deadlock */
    printf("openning fifoR\n");
    if( (fdw = open(myfifow, O_WRONLY)) < 0 )
        fatal("opening fdw");
    printf("fifoR unblocked\n");
/* */

    printf("openning fifoW\n");
    if( (fdr = open(myfifor, O_RDONLY)) < 0 )
        fatal("opening fdr");
    printf("fifoW unblocked\n");
    /*  first strings   */
    memset(buff, 0, sizeof(buff));
    read(fdr,buff,sizeof(buff));
    printf("%s\n",buff);
    while( (c = getchar()) != EXIT_CHAR )
    {
        write(fdw,&c,1);
        if(c == '\n')
        {
            memset(buff, 0, sizeof(buff));
            read(fdr,buff,sizeof(buff));
            printf("%s",buff);
        }
    };
    close(fdw);
    close(fdr);
    return EXIT_SUCCESS;
}

非常感谢!

0 个答案:

没有答案