I am trying to write a simple program that passes a value from a process to another without having father-child properties using named pipes.Code i've writen:

#include <stdio.h>

int main(int argc, char **argv)
{   int fd,i,j;

    int * myfifo = "/tmp/myfifo";
    mkfifo(myfifo, 0666);

    pid_t c=fork();
    fd = open(myfifo, O_WRONLY);
    write(fd, &i, sizeof(i));
    printf("my pid is %d \n",getpid());
    pid_t c2=fork();
    fd = open(myfifo, O_RDONLY);
    read(fd, &j, sizeof(j));
    printf("passes value is %d",j);


I want the first child to write 10 to the pipe and the second to read from the file and print so i can know it works.This code gives me multiple errors though that i can't seen to understand.Any ideas?

Errors i get:

In function ‘main’:|
warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]|
warning: implicit declaration of function ‘mkfifo’ [-Wimplicit-function-declaration]|
warning: implicit declaration of function ‘open’; did you mean ‘popen’? [-Wimplicit-function-declaration]|
]main.c|27|note: each undeclared identifier is reported only once for each function it appears in|
error:'O_WRONLY' undeclared(first use in this function)
error:'O_RDONLY' undeclared(first use in this function)

1 Answers

Andrew Henle On Best Solutions

You are not including the proper headers.

For mkfifo() you need sys/stat.h:


#include <sys/stat.h>

int mkfifo(const char *path, mode_t mode);

and for open() you need sys/stat.h and fcntl.h:


[#include <sys/stat.h>
#include <fcntl.h>

int open(const char *path, int oflag, ...);

You also need to actually check the return values when you call those functions - the can and do fail.