Thread: UNIX mkfifo fails on a different files

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    121

    UNIX mkfifo fails on a different files

    Hello, I am trying to complete a finite IPC machine. For some unknown reason to me, mkfifo never succeeds. My first few atempts, it does after that it always return a non-zero value (which by the papers is fail).

    Code:
    struct msg* msg_generate(const char* mess);
    
    
    int     open_read_from(struct msg* m);
    int     open_write_to(struct msg* m);
    
    
    
    
    static int eXec()
    {
        struct msg* pm = msg_generate("hello IPC world!!!");
    
    
        int fdr = open_read_from(pm->fifo_files.fifo_read);
        int fdw = open_write_to(pm->fifo_files.fifo_write);
    
    
        // stick here
        for(;;) {
            // send messages and read them back...
            char rbuff[sizeof(struct msg)]= {0};
            char wbuff[sizeof(struct msg)] = {0};
    
    
            if (read(fdr, rbuff, sizeof(struct msg)) > 0) {
                printf("Read...\n");
                struct msg* p = (struct msg*) rbuff;
                printf("[message: %s][came from %d][ret: %d]\n",
                       p->message, p->pid, p->result);
    
    
            }
    
    
            while (fgets(wbuff, sizeof(struct msg), stdin) != -1) {
                if (write(fdw, wbuff, sizeof(struct msg)) == -1) {
                    perror("Could not write to fifo...");
                }
            }
        }
        return 0; // or err code
    }
    
    
    
    
    int main(int argc, char** argv)
    {
    
    
        return eXec();
    }
    
    
    int open_read_from(struct msg *m)
    {
        int flags = (S_IFIFO | 0666);
        if(mkfifo(m->fifo_files.fifo_read, flags)!=0) {
            return -1;
        }
        return open(m->fifo_files.fifo_read, O_RDONLY|O_NONBLOCK);
    
    
    }
    
    
    
    
    int open_write_to(struct msg *m)
    {
    
    
        int flags = (S_IFIFO | 0666);
        if(mkfifo(m->fifo_files.fifo_write, flags)!=0){
            return -1;
        }
        return open(m->fifo_files.fifo_write, O_WRONLY|O_NONBLOCK);
    
    
    }
    
    
    
    
    struct msg* msg_generate(const char* mess)
    {
        if (!mess) {
            return 0;
        }
        static struct msg single;
        static int rcnt = 0;
        static int wcnt = 0;
        strcpy(single.message, mess);
        pid_t cp = getpid();
    
    
        sprintf(single.fifo_files.fifo_read, "/tmp/%dr%d", cp, rcnt++);
        sprintf(single.fifo_files.fifo_write, "/tmp/%dw%d", cp, wcnt++);
    
    
        single.pid = cp;
        single.result = 0; // ok for now
        return &single;
    
    
    }
    I am debugging my both functions for read and write, they both return -1 to me. Any info?

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    The fifo files you create don't magically disappear after your program ends. They are still in the file system. Look under /tmp for them and they'll all be there. If the file already exists, mkfifo will fail. Delete them. You should probably have your program remove them before it terminates.

    BTW, the second parameter to mkfifo is not called "flags", it's called "mode" and doesn't take the S_IFIFO flag!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Curl Upload of files more than 2GB fails !!!
    By Shafi Mohammed in forum Networking/Device Communication
    Replies: 4
    Last Post: 03-17-2014, 10:11 PM
  2. can't open two files at once, 2nd fopen fails
    By jeffmarc in forum C Programming
    Replies: 3
    Last Post: 09-18-2009, 03:06 PM
  3. Linking C files (Unix)
    By f97tosc in forum C Programming
    Replies: 3
    Last Post: 03-23-2003, 05:36 PM
  4. zipping files in UNIX
    By smd in forum C++ Programming
    Replies: 4
    Last Post: 07-12-2002, 08:46 AM
  5. Using Configuration Files with C on UNIX
    By emaxham in forum C Programming
    Replies: 1
    Last Post: 10-16-2001, 02:28 PM

Tags for this Thread