Steven R
Steven R

Reputation: 323

making named pipes and using poll

I'm so confused with this, I need to create named pipes using mkfifo (i know how to do this) the program before would use fork to create child processes that would do something, but now I have to replace fork with poll() to watch multiple streams (that's the part I don't get). In more detailed, when i run my program in terminal, its suppose to make the mkfifo files and then wait till a stream comes in, hence just stay there, not closing. Then I open up a new terminal, and need to input exactly this into terminal "cat file1 > (name of the mkfifo files)" and what that should do is make the program read the data that was in file1, on any of the input pipes made from mkfifo. I've looked everywhere but can never put things together to make it work.

this is what i have so far

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <poll.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include "battleFieldReader.h"   //this is where all the data computation is done 

main(int argc, char *argv[])
{
    if (sscanf (argv[1], "%i", &N) !=1 )
    {
        printf ("ERROR: Please input an integer.\n");
        _exit(EXIT_SUCCESS);
    }
    struct pollfd pfd[2];
    pid = getpid();
    printf("pid=%d\n",pid);
    N = atoi(argv[1]);
    signal(SIGTERM, removePipes);
    int i;
        char fileName[32];
        char fileName2[32];
        snprintf (fileName, sizeof(fileName), "%d_%di", pid, 0);
        mkfifo(fileName, 0666);
        pfd[0].fd = open(fileName, O_RDONLY | O_NDELAY);
        pfd[0].events = POLLIN;

        snprintf (fileName2, sizeof(fileName2), "%d_%do", pid, 0);
        mkfifo(fileName2, 0666);
        pfd[1].fd = open(fileName2, O_WRONLY | O_NDELAY);
        pfd[1].events = POLLIN;

    while(1)
    {
        int n;
        n = poll(pfd, 2, 3000);
        if(n < 1)
        {
            printf("waiting...\n");
            continue;
        }

        if(pfd[0].revents & POLLIN)
        {
            printf("test\n");
/*ideally this is where i would put a method to compute data but its just an infinite loop, its suppose to stop, so it can do the same thing whenever another file comes on, but I dont know how to stop it either*/
        }
    }
}

whats happening is I'm creating a pipe 2N times, one for input and output for whatever process id is running the program. then wait till something comes in on one of the pipes and then spit out what needs to be done with the file data. Can anyone clear things with me, if I'm going in the right direction or something.

Upvotes: 0

Views: 2534

Answers (1)

Chris Dodd
Chris Dodd

Reputation: 126536

poll tells you if you can read from or write to a file descriptor without blocking (or without getting an EAGAIN/EWOULDBLOCK result if the fd in non-blocking).

Your code has a number of obvious problems:

  • You say you want to create 2N fifos, but then you only create 2. Your pfd array has a fixed size of 2, too. You'll need a bigger array and loop to create more.

  • You open an O_WRONLY file descriptor to write to a pipe, but then you set the events field to POLLIN, which will test for input available. You want POLLOUT to test for output possible.

  • In your processing loop, you poll two fds, but you only check pfd[0] for availability, and then you never do anything with it. You should read pfd[0].fd after the pfd[0].revents & POLLIN check succeeds. You should also check pfd[1].revents & POLLOUT and write data to pfd[1].fd if that succeeds.

Upvotes: 1

Related Questions