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?