管道通信中的C问题

时间:2011-02-12 23:12:11

标签: c linux pipe

我正在尝试编写一些代码,这些代码使用管道在父进程和它的子进程之间进行通信。但是,我的管道在我第一次使用它之后似乎放弃了(也就是说,它在第一次使用管道后停止工作)。我不确定如何解决这个问题,任何帮助将不胜感激。我也知道我在这里使用的一些编码实践并不是很理想(主要是使用睡眠)。

const int READ = 0;
const int WRITE = 1;
char* COOP = "Criminal cooperates\n";
char* SIL = "Criminal doesn't talk\n";

char* reader(int);
void writer(int, char *c);

int main()
{       
    int c1pipe1[2];
    int c1pipe2[2];
    int c2pipe1[2];
    int c2pipe2[2];
    int c1sentence = 0;
    int c2sentence = 0;
    int r;
    int c;
    pipe(c1pipe1);
    pipe(c1pipe2);
    pipe(c2pipe1);
    pipe(c2pipe2);
    int C2;
    int C1 = fork();
    if(C1 > 0)
        C2 = fork();
    if(C1 < 0 || C2 < 0) //error
    {
        perror("fork() failed");
        exit(1);
    }

    else if(C1 == 0)
    {
        close(c1pipe1[WRITE]);
        close(c1pipe2[READ]);
        for(c = 0; c < 10; c++)
        {
            r = rand();
            //printf("C1 rand = %d\n", r%2);
            if(r % 2 == 1)
                writer(c1pipe2[WRITE], "1");
            else
                writer(c1pipe2[WRITE], "0");
            sleep(1);
        }

        exit(0);
    }
    else if(C2 == 0)
    {
        close(c2pipe1[WRITE]);
        close(c2pipe2[READ]);
        for(c = 0; c < 10; c++)
        {
            r = rand();
            //printf("C2 rand = %d\n", r%2);
            if(r % 2 == 1)
                writer(c2pipe2[WRITE], "1");
            else
                writer(c2pipe2[WRITE], "0");
            sleep(1);
        }

        exit(0);
    }
    else //parent
    {
        int buff1; //stores choice of c1
        int buff2; //stores choice of c2
        close(c1pipe1[READ]);
        close(c1pipe2[WRITE]);
        close(c2pipe1[READ]);
        close(c2pipe2[WRITE]);
        for(c = 0; c< 10; c++)
        {
            buff1 = atoi(reader(c1pipe2[READ]));
            buff2 = atoi(reader(c2pipe2[READ]));
            printf("C1's \(%d)\ choice trial %d : %d\n", C1, c+1, buff1);
            printf("C2's \(%d)\ choice trial %d : %d\n", C2, c+1, buff2);
            if(buff1 && buff2) //c1 and c2 cooperate with police
            {
                    c1sentence = c1sentence + 6;
                    c2sentence = c2sentence + 6;
            }
            else if(buff1 || buff2) // one cooperates, one is silent
            {
                if(buff1) // if c1 cooperates and c2 is silent
                {
                    c1sentence = c1sentence + 0;
                    c2sentence = c2sentence + 10;
                }
                else // if c2 cooperates and c1 is silent
                {
                    c1sentence = c1sentence + 10;
                    c2sentence = c2sentence + 0;
                }
            }
            else if(!(buff1 && buff2)) //both c1 and c2 are silent
            {
                c1sentence = c1sentence + 1;
                c2sentence = c2sentence + 1;
            }
            sleep(1);


        }       
        printf("C1 is in jail for %d years total\n", c1sentence);
        printf("C2 is in jail for %d years total\n", c2sentence);
        exit(0);
    }
    exit(0);
}

void writer(int pipe_write_fd, char *c) 
{
    open(pipe_write_fd);
    char* choice = c;
    // Write to the pipe
    write(pipe_write_fd, choice, strlen(choice));
    // Close the pipe
    // (Sends 'end of file' to reader)
    close(pipe_write_fd);
}

char* reader(int pipe_read_fd) 
{
    open(pipe_read_fd);
    // Allocate buffer to store
    // result of read
    int buffer_size = 1024;
    char buffer[buffer_size];

    // Keep reading until we exhaust
    // buffer or reach end of file
    int i = 0;
    while (i < buffer_size
           && read(pipe_read_fd, &buffer[i], 1) > 0)
    { i++; }

    if (i < buffer_size) {
        // Add null termination
        buffer[i] = '\0';
    } else {
        // We exhausted buffer
        fprintf(stderr, "Warning: buffer full.\n");
        buffer[buffer_size-1] = '\0';
    }

    //printf("%s", buffer);

    // Close the pipe
    close(pipe_read_fd);
    return buffer;
}

3 个答案:

答案 0 :(得分:10)

您需要关闭更多管道。子进程必须关闭它们未使用的每个管道文件描述符。你有8个管道文件描述符;每个子进程必须关闭其中的6个 - 至少!建议你不要像以前那样预先创建所有管道 - 控制事物并关闭所有正确的描述符是很复杂的。


更仔细地查看代码,父级不会向子进程写入消息,因此您需要的管道数量是所需数量的两倍 - 每个子进程只需要一个管道就可以回写给父进程。 / p>

你也没有open()已经打开管道的文件描述符......但你是如何得到编译的代码的?您必须缺少#include <fcntl.h>的正确标头(open())并在没有启用足够警告选项的情况下进行编译。

您的变量COOP和SIL在所提供的代码中未使用。


你的writer()函数不仅错误地尝试打开已经关闭的文件描述符,它还会关闭它,这意味着在第一个之后无法发回额外的消息。您应该只在完成后关闭文件描述符 - 在主程序中为每个子项循环之后。这就是您只看到一条消息的原因。

同样值得养成错误检查每次可能失败的系统调用的返回的习惯。有一些不能失败 - getpid()就是这样。但是I / O操作因为在程序的直接控制之外的原因(或者在这种情况下,在程序的控制之内)失败而臭名昭着,所以你应该检查写入是否成功。当你找回一个EBADF - 错误的文件描述符 - 错误时,你就知道有什么东西了。

close()中的open()(和reader())存在类似的问题,另外还有一个问题是您尝试返回指向本地自动变量的指针 - 这不是一个好处想法,永远。同样,一个体面的编译器(如GCC)启用警告会告诉你这些事情。我用这个命令来编译你的程序:

gcc -O -std=c99 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes \
    pipe.c -o pipe

您的子进程总是会生成相同的(伪)随机数序列,这不是很令人兴奋。你应该使用类似的东西:

srand(getpid());

确保他们获得不同的序列。


您的reader()功能既不够热情,也不太热衷于阅读数据。您一次读取一个字节,但是然后循环以累积单个字节,因此代码会等待所有10个结果都知道,然后立即吐出所有内容。由于32位整数可以存储多达1,111,111,111的数字而没有问题,因此在第一次迭代时,从atoi()的调用中只返回一个数字,这不是您想要的。

管道上的读取和写入是原子的 - 从某种意义上说,如果写入过程写入6个字节并且读取过程尝试读取超过6个字节,则单个读取将返回6个字节的数据包,即使管道中有其他字节等待读取;这些额外的字节将在后续调用read()时返回。

因此,您的reader()函数应该在缓冲区中传递以及其大小;代码应该尝试读取缓冲区大小;它应该null终止它收到的内容;它可以将指针返回到它传递的缓冲区;它应该从read()错误检查返回的值。

两个子进程的代码基本相同 - 你应该使用适当的参数化函数,而不是两次写出代码。


总而言之,你最终会得到类似这样的东西(在GOS 4.5.2的MacOS X 10.6.6上我可以正常工作):

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

const int READ = 0;
const int WRITE = 1;

static char* reader(int fd, char *buffer, size_t bufsiz);
static void writer(int fd, const char *c);
static void child_process(int *my_pipe, int *his_pipe);

static void err_exit(const char *fmt, ...)
{
    va_list args;
    int errnum = errno;
    va_start(args, fmt);
    vfprintf(stderr, fmt, args);
    va_end(args);
    if (errnum != 0)
        fprintf(stderr, "%d: %s\n", errnum, strerror(errnum));
    exit(1);
}

int main(void)
{       
    int c1pipe[2];
    int c2pipe[2];
    int c1sentence = 0;
    int c2sentence = 0;
    int c;

    if (pipe(c1pipe) != 0 || pipe(c2pipe) != 0)
        err_exit("Failed to open a pipe\n");

    int C2 = 0;
    int C1 = fork();
    if (C1 > 0)
        C2 = fork();

    if (C1 < 0 || C2 < 0) //error
        err_exit("fork() failed\n");
    else if (C1 == 0)
        child_process(c1pipe, c2pipe);
    else if (C2 == 0)
        child_process(c2pipe, c1pipe);
    else //parent
    {
        int choice1; //stores choice of c1
        int choice2; //stores choice of c2
        char buffer1[BUFSIZ];
        char buffer2[BUFSIZ];
        close(c1pipe[WRITE]);
        close(c2pipe[WRITE]);
        for (c = 0; c< 10; c++)
        {
            choice1 = atoi(reader(c1pipe[READ], buffer1, sizeof(buffer1)));
            choice2 = atoi(reader(c2pipe[READ], buffer2, sizeof(buffer1)));
            printf("C1's (%d) choice trial %d : %d\n", C1, c+1, choice1);
            printf("C2's (%d) choice trial %d : %d\n", C2, c+1, choice2);
            if (choice1 && choice2) //c1 and c2 cooperate with police
            {
                    c1sentence = c1sentence + 6;
                    c2sentence = c2sentence + 6;
            }
            else if (!(choice1 && choice2)) //both c1 and c2 are silent
            {
                c1sentence = c1sentence + 1;
                c2sentence = c2sentence + 1;
            }
            else if (choice1) // if c1 cooperates and c2 is silent
            {
                c1sentence = c1sentence + 0;
                c2sentence = c2sentence + 10;
            }
            else // if c2 cooperates and c1 is silent
            {
                c1sentence = c1sentence + 10;
                c2sentence = c2sentence + 0;
            }
        }       
        printf("C1 is in jail for %d years total\n", c1sentence);
        printf("C2 is in jail for %d years total\n", c2sentence);
    }
    return(0);
}

static void writer(int pipe_write_fd, const char *c) 
{
    int len = strlen(c);
    if (write(pipe_write_fd, c, len) != len)
        err_exit("Write failed\n");
}

static char* reader(int pipe_read_fd, char *buffer, size_t bufsiz) 
{
    int i = read(pipe_read_fd, buffer, bufsiz-1);
    if (i < 0)
        err_exit("Read failed\n");
    buffer[i] = '\0';
    return buffer;
}

static void child_process(int *my_pipe, int *his_pipe)
{
    int c;
    srand(getpid());
    close(my_pipe[READ]);
    close(his_pipe[READ]);
    close(his_pipe[WRITE]);
    for (c = 0; c < 10; c++)
    {
        writer(my_pipe[WRITE], ((rand() % 2) == 1) ? "1" : "0");
        sleep(1);
    }
    close(my_pipe[WRITE]);
}

请注意错误例程如何尽早捕获errno - 以避免损坏它。这是使用全局变量的危险之一;当你调用一个函数时它们可能会改变。如果可以避免使用它们,请不要使用它们(但请注意,一般情况下,您无法完全避免使用errno。)

答案 1 :(得分:1)

void writer(int pipe_write_fd, char *c) 
{
    open(pipe_write_fd);
    char* choice = c;
    // Write to the pipe
    write(pipe_write_fd, choice, strlen(choice));
    // Close the pipe
    // (Sends 'end of file' to reader)
    close(pipe_write_fd);
}

我不确定您正在尝试使用哪个函数open,但通常使用文件名并返回文件描述符。在任何情况下,你都放弃了返回值,所以我认为这无关紧要。

显而易见的是,您在第一次写入后立即close管道,因此下一次写入失败是“正确的”;管道已关闭。

如果您解决了这个问题,那么下一个问题是reader将在关闭读取管道之前,一次一个字节,所有可用输入 - 最多1024个字节。当在循环中调用reader时,第二次迭代中的读取尝试将失败。

答案 2 :(得分:0)

您可以复制打开和关闭功能的代码吗?因为查尔斯说它们似乎不是标准的。

同样在函数readers()中,返回指向局部变量的指针是一个错误,当你尝试在main函数中读取这个变量的内容时,你不能保证指向的内存包含你想要的东西。 / p>

如果要返回缓冲区,则必须使用malloc为其保留内存,使用外部变量(我的意思是全局变量)或返回指向内部(本地)静态变量的指针,因为(与自动局部变量不同) )所有这些变量存储在函数执行结束时仍然存在。