我的fork()永远不等于0

时间:2015-12-05 08:25:50

标签: c pipe fork

我有这个代码,我的fork()永远不会等于0,因此它永远不会执行我的if(pubpid == 0){}语句中的代码。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <wordexp.h> 
#define MAX_VAL 256
#define MAX_ENTRIES 1000

pid_t pubpid, subpid, ATPpid;

char *subconnect;
char *pubconnect;
char *subaccept;

char *pub_message[] = {"pub pubid connect", "pub pubid topic 1", "end"};
char *sub_message[] = {"sub subid connect", "sub subid topic 1", "end"};

int main(int argc, char **argv) 
{
int i, j, publishers, subscribers, topics;

//Grab input arguments
publishers = atoi(argv[1]);     //number of publishers
subscribers = atoi(argv[2]);    //number of suscribers
topics = atoi(argv[3]);         //number of topics

int ATP_Sub[subscribers][2];
int Sub_ATP[subscribers][2];

int ATP_Pub[publishers][2];
int Pub_ATP[publishers][2];


for(i = 0; i < publishers; i++) //publisher
{ 
    pipe(ATP_Pub[i]);
    pipe(Pub_ATP[i]);
}
for(i = 0; i < subscribers; i++) //subscriber
{ 
    pipe(ATP_Sub[i]);
    pipe(Sub_ATP[i]);
}

//ATP Server
ATPpid = fork();

if(ATPpid == -1){
    perror("fork error\n");
    exit(1);
}
if(ATPpid == 0){

    for(i = 0; i < publishers; i++){
        char buffer1[256];

        close(Pub_ATP[i][1]);
        read(Pub_ATP[i][0], buffer1, 256);

        printf("Publisher Accepted!\n");

        close(ATP_Pub[i][0]);
        write(ATP_Pub[i][1], "accept", sizeof("accept"));


    }
    close(Pub_ATP[i][0]);
    close(Pub_ATP[i][1]);


    for(i = 0; i < subscribers; i++){
        char buffer2[256];

        close(Sub_ATP[i][1]);
        read(Sub_ATP[i][0], buffer2, 256);

        close(ATP_Sub[i][0]);
        write(ATP_Sub[i][1], "accept", 6);
    }
    close(Sub_ATP[i][0]);
    close(Sub_ATP[i][1]);
}
else{

}

//Publisher Pipes
for(i = 0; i < publishers; i++) 
{   
    pubpid = fork();

    if(pubpid < -1) 
    {
        perror("fork error\n");
        exit(1);
    }

    if(pubpid == 0) 
    {           
        for(j=0; j < 3; j++)
        {
            char publisherbuffer[256];
            //Write to ATP
            close(Pub_ATP[i][0]);
            write(Pub_ATP[i][1], pub_message[j], 256);

            printf("Publisher Connected!");
            //Read from ATP
            close(ATP_Pub[i][1]);
            read(ATP_Pub[i][0], publisherbuffer, 256);
            //Check if accept

            while(strcmp(publisherbuffer, "accept") != 0)
            {
                close(ATP_Pub[i][1]);
                read(ATP_Pub[i][0], publisherbuffer, 256);
            }
        }
        close(ATP_Pub[i][1]);
        close(ATP_Pub[i][0]);
        exit(0);        
    }
    else 
    {

    }
}

//Subscriber Pipes
for(i = 0; i < subscribers; i++)
{ 

        subpid = fork();

        if(subpid < -1) {
            perror("fork error\n");
            exit(1);
        }
        if(subpid == 0){
            printf("hello");
            for(j=0; j < 3; j++)
            {
                char subscriberbuffer[256];
                //Write to ATP
                close(Sub_ATP[i][0]);
                write(Sub_ATP[i][1], sub_message[j], 20);
                //Read from ATP
                close(ATP_Sub[i][1]);
                read(ATP_Sub[i][0], subscriberbuffer, 256);
                //Check if accept

                while(strcmp(subscriberbuffer, "accept") != 0)
                {
                    close(ATP_Sub[i][1]);
                    read(ATP_Sub[i][0], subscriberbuffer, 256);
                }       
            }
            close(ATP_Sub[i][1]);
            close(ATP_Sub[i][0]);
        }
        else {
        }       
}
return 0;   
}

我的ATPpid将等于0,正如我的打印声明所证明的那样,但我从未进入我的公共场所:(&#34;发布者已接受!&#34;始终打印,但从未&#34;已发布连接!& #34;

提前致谢!

2 个答案:

答案 0 :(得分:1)

有太多东西需要解决,很难回答这个问题而不只是说&#34;拿一个调试器&#34;

实际上,你第一个分叉的孩子最终阻止了:

    read(Pub_ATP[i][0], buffer1, 256);

但由于父亲没有发送任何不等待其孩子的事情,程序结束。

之前可能缺少一个右大括号
//Publisher forks

2)你的父亲应该总是等待完成或孩子们

3)无法弄清楚是什么

for(j=0; j < 3; j++)

用于(您从不使用j

以下代码只修复了我告诉你的要点,并显示了你的消息(不过,没有解决任何其他错误

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
    int i, j, publishers, subscribers, topics;

    //Grab input arguments
    publishers = atoi(argv[1]);     //number of publishers
    subscribers = atoi(argv[2]);    //number of suscribers
    topics = atoi(argv[3]);         //number of topics

    int ATP_Sub[subscribers][2];
    int Sub_ATP[subscribers][2];

    int ATP_Pub[publishers][2];
    int Pub_ATP[publishers][2];

    for(i = 0; i < publishers; i++) //publisher pipes
    {
        pipe(ATP_Pub[i]);
        pipe(Pub_ATP[i]);
    }

    //ATP Server
    int ATPpid = fork();

    if(ATPpid == -1){
        perror("fork error\n");
        exit(1);
    }
    if(ATPpid == 0){

        for(i = 0; i < publishers; i++){
            char buffer1[256];

            close(Pub_ATP[i][1]);
            read(Pub_ATP[i][0], buffer1, 256);

            printf("Publisher Accepted %s!\n", buffer1);

            close(ATP_Pub[i][0]);
            write(ATP_Pub[i][1], "accept", sizeof("accept"));


        }
        close(Pub_ATP[i][0]);
        close(Pub_ATP[i][1]);
        exit(0);
    }

    //Publisher forks
    for(i = 0; i < publishers; i++)
    {
        int pubpid = fork();

        if(pubpid == -1)
        {
            perror("fork error\n");
            exit(1);
        }

        if(pubpid == 0)
        {
            char publisherbuffer[256];
            //Write to ATP
            close(Pub_ATP[i][0]);
            write(Pub_ATP[i][1], "test", 256);

            printf("Publisher Connected!");
            //Read from ATP
            close(ATP_Pub[i][1]);
            read(ATP_Pub[i][0], publisherbuffer, 256);
            //Check if accept

            while(strcmp(publisherbuffer, "accept") != 0)
            {
                close(ATP_Pub[i][1]);
                read(ATP_Pub[i][0], publisherbuffer, 256);
            }

            close(ATP_Pub[i][1]);
            close(ATP_Pub[i][0]);
            exit(0);
        }
        else
        {
            waitpid(pubpid, NULL, 0);

        }
    }
    waitpid(ATPpid, NULL, 0);
    return 0;
}

您可能还会注意到孩子们按顺序产生,这可能不是您想要的,您需要&#34;保存&#34;为了做到这一点,每个孩子都在一个阵列中。

答案 1 :(得分:1)

您有几个逻辑问题。您的Publisher connected消息打印出于一个错误的原因!你创建管道,然后分叉。在子代中,您关闭写入部分并在读取部分上被阻塞,在刚刚终止的父级中,它会关闭读取和写入部分,然后子级在读取时被解除阻塞,因为没有更多的编写器。

您没有测试返回的值;在这里你可以看到它检测到文件结束。您还使用并发读取,这是一个很难解决的问题。

请记住,使用管道需要采取预防措施。读取/写入被阻止(默认情况下)。读取空管而没有更多的作者:文件结束。写作时不再有读者:SIGPIPE等。你必须要认真地验证你的阅读/写作协议。

查看字符串长度,您需要发送NUL终止C字符串才能正常运行。