ipc - Using the open function in named pipes -


i have 2 process.
1 first read write.
other 1 first write read.
, want implement using 2 pipes.

here implementations

/***read before write*****/ #include <fcntl.h> #include <stdio.h> #include <sys/stat.h> #include <unistd.h> #define max_buf 1024  int main() {  int fd,fd1;  char buf[max_buf];  char * myfifo = "/home/aditya/desktop/myfifo";  char * mynewfifo = "/home/aditya/desktop/mynewfifo";   mkfifo(mynewfifo, 0666);   printf("read before writing\n");     printf("before opening\n");   fd = open(myfifo, o_rdonly);  fd1 = open(mynewfifo, o_wronly);  printf("after opening\n");    read(fd, buf, max_buf);  printf("received: %s\n", buf);  close(fd);   printf("reader writing\n");   write(fd1, "hi", sizeof("hi"));  close(fd1);      unlink(mynewfifo);    return 0; }   /**** wriiting after reading ******/               #include <fcntl.h> #include <stdio.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h>  int main() {  int fd,fd1;  char * myfifo = "/home/aditya/desktop/myfifo";  char * mynewfifo = "/home/aditya/desktop/mynewfifo";  char buf[1024];       mkfifo(myfifo, 0666);   printf("writing before reading\n");   printf("before opening\n");   fd = open(myfifo, o_wronly);  fd1=open(mynewfifo,o_rdonly);    printf("after opening\n");   write(fd, "hi", sizeof("hi"));  close(fd);   printf("writer reading\n");   read(fd1, buf, 1024);  printf("received: %s\n", buf);  close(fd1);  /* remove fifo */ unlink(myfifo);  return 0; } 

when run "write after read" first , "read after write",it seems work.
when vice versa(that run "read after write") print "before opening" , continue run.

please explain doing wrong.

the problem read after write program reads named pipe myfifo has not been yet created. when try execute read after write program @ first place. added following 2 conditions(see code below) in program check if there problem in name pipes , problem did pop out myfifo pipe not created when first execute read after write program.

now happens since pipe not created, read after write keeps waiting other process write can read something. plus, on other hand , second program keeps standing on "opening..." because there no reader on other hand read supposed write.

now when execute second program first, problem gets solved since myfifo pipe created read after write program read.this logical since trying read named pipe myfifo created in second program.

thus, check in program whether pipe using created or not.

/***read after write*****/ #include <fcntl.h> #include <stdio.h> #include <sys/stat.h> #include <unistd.h> #define max_buf 1024  int main() {  int fd,fd1;  char buf[max_buf];  char * myfifo = "/home/aisha/desktop/myfifo";  char * mynewfifo = "/home/aisha/desktop/mynewfifo";   mkfifo(mynewfifo, 0666);   printf("read before writing\n");     printf("before opening\n");   fd = open(myfifo, o_rdonly);  if(fd<0)  {     printf("myfifo not made\n");  }  fd1 = open(mynewfifo, o_wronly);  if(fd1<0)  {     printf("mynewfifo not made\n");  }   sleep (2);  printf("after opening\n");   read(fd, buf, max_buf);  printf("received: %s\n", buf);  close(fd);   printf("reader writing\n");   write(fd1, "hi", sizeof("hi"));  close(fd1);      unlink(mynewfifo);     return 0; } 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -