0

I apologize for the bad title as I wasn't exactly sure how to word it. I have to make a named pipe in C++ for an assignment. I understand how the named pipe works and what each line should do, but I'm running into a problem that me and a friend who's code is IDENTICAL can't solve.

It is a simple assignment. All I have to do is have one program create the named pipe and put a user inputted char array into it. The second program (in its own terminal) just reads the char array from the pipe and outputs it in the terminal.

In the second program below at line 11 (c = open("/tmp/myfifo", O_RDONLY);), the program never seems to run that line. When I run it in the terminal, nothing happens and it just sits there as if it's in deadlock. My friend does not have this problem and we have no idea what could be causing it. I am running on Ubuntu 14.04.3 in a Virtual Box using the default terminal.

#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <iostream>
#include <string>
using namespace std;

int main() {
    int a, b, c, res;
    string input;
    char buffer [50];
    b = access("/tmp/myfifo", F_OK);
    if (b == -1)
        a = mkfifo("/tmp/myfifo", 0777);
    c = ("/tmp/myfifo", O_WRONLY);
    getline(cin, input);
    for (int i = 0; i < input.size(); i++)
        buffer[i] = input.at(i);
    res = write(c, buffer, 50);
    close(c);
    return 0;
}

.

#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <iostream>
using namespace std;

int main() {
    int c, res;
    char buffer [50];
    c = open("/tmp/myfifo", O_RDONLY);
    res = read(c, buffer, 50);
    close(c);
    cout<<buffer;
    return 0;
}
user2872777
  • 99
  • 2
  • 11

1 Answers1

3

It appears that you missing the word open on line 16 in the sender.

c = open("/tmp/myfifo", O_WRONLY);

Without that word, your program invokes the comma operator, which will assign to c the value of O_WRONLY, as described here.

One other independent bug I noticed while running this is that you did not initialize buffer on the sender side, which means the receiver may read garbage after the actual string because there's no null terminator.

char buffer [50];
memset(buffer, 0, 50);
Community
  • 1
  • 1
merlin2011
  • 71,677
  • 44
  • 195
  • 329
  • I kept asking my friend if there was a problem there!!! I had a feeling it was a problem but I wasn't sure what needed to go there. The pipe is now working but I am getting garbage on the end like you said I would. I tried adding the memset line that you gave me but I'm getting a compile error. Is it suppose to be in the sender file or the receiver file? – user2872777 Nov 23 '15 at 23:38
  • Sender file. You need to `include ` for memset. – merlin2011 Nov 23 '15 at 23:44
  • ...honestly I didn't know #include is different than #include. Is there a difference between #include and #include? I used the .h file when I took a class in highschool but never once in college. – user2872777 Nov 23 '15 at 23:48
  • @user2872777, I don't blame you for that. `string.h` is a C header, while `string` is a C++ header. I haven't really seen usage of `#include ` recently. You can see [this question](http://stackoverflow.com/questions/2976477/difference-between-iostream-and-iostream-h) for more details. – merlin2011 Nov 24 '15 at 00:33
  • Okay I appreciate it a lot. Thank you :) – user2872777 Nov 24 '15 at 02:52