0

I'm writing a program that should run indefinitely maintaining the value of a variable. Two other programs could change the value of the variable. I use named pipes to receive and send the variable value to external programs.

Here is my code for the manager of the variable.

manager.c:

#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pthread.h>

char a = 'a';

void *editTask(void *dummy)
{
    int fd;
    char* editor = "editor";
    mkfifo(editor, 0666);
    while(1)
    {
        fd = open(editor, O_RDONLY);
        read(fd, &a, 1);
        close(fd);
    }   
}

void *readTask(void *dummy)
{
    int fd;
    char* reader = "reader";
    mkfifo(reader, 0666);
    while(1)
    {
        fd = open(reader, O_WRONLY);
        write(fd,&a,1);
        close(fd);      
    }
}

int main()
{
    pthread_t editor_thread, reader_thread;
    pthread_create(&editor_thread, NULL, editTask, NULL);
    pthread_create(&reader_thread, NULL, readTask, NULL);
    pthread_join (editor_thread, NULL);
    pthread_join (reader_thread, NULL);
    return 0;
}

This program uses pthreads to separately get external values for the variable and to communicate the current value of the variable to external programs.

The program that is able to write values to the variable is:

writer.c:

#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>

int main(int argc, char** argv)
{
    if(argc != 2)
    {
    printf("Need an argument!\n");
    return 0;
    }           
    int fd;
    char * myfifo = "editor";
    fd = open(myfifo, O_WRONLY);
    write(fd, argv[0], 1);      
    close(fd);

    return 0;
}

The program that could read the current value is:

reader.c:

#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>

int main()
{
    int fd;
    char * myfifo = "reader";
    fd = open(myfifo, O_RDONLY);
    char value = 'z';
    read(fd, &value, 1);
    printf("The current value of the variable is:%c\n",value);      
    close(fd);

    return 0;
}

I ran these programs in my Ubuntu system as follows:

$ ./manager &
[1] 5226
$ ./writer k
$ ./reader
bash: ./reader: Text file busy

Why doesn't my system allow me to run this program?

Thank you.

1 Answers1

2

You are trying to call both the FIFO and the reader program "reader".

Also, you have no error checking. You have no idea whether those calls to mkfifo and open succeeded or not. Adding this is critical before you attempt to do any troubleshooting.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • My bad. I was looking all over but couldn't find this mistake. I thought there might be a limitation somewhere when the process tries to read and write to pipes in parallel. And about error checking, what kind of error checking is crucial? I mean, almost all functions can be checked for errors and I cannot really bother with all of them. And thank you for your tip on error checking. I'll keep that in mind. – user5393678 Dec 09 '16 at 15:25
  • @user5393678 Any function that has a realistic chance of failing should be called with error checking. That's pretty much all of them. Clearly, it's `open`, `mkfifo`, `read`, and `write`, since they fail all the time in all sorts of ways. And it's almost no bother, it's a simple cut and paste of `if`/`perror` (or use a macro). And honestly, was it less bother to try to troubleshoot blindfolded with no idea where the program was failing? – David Schwartz Dec 09 '16 at 18:28