P_230_write
// // Created by wybiacx on 2020/12/31. // #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <fcntl.h> #include <limits.h> #include <sys/types.h> #include <sys/stat.h> #define FIFO_NAME "/tmp/my_info" int main(int argc,char *argv[]){ int pipe_fd; int res; char buffer[] = "hello world!"; if(access(FIFO_NAME, F_OK) == -1){ res = mkfifo(FIFO_NAME, 0766); if(res != 0){ fprintf(stderr, "Could not create fifo %s ", FIFO_NAME); exit(EXIT_FAILURE); } } printf("Process %d opening FIFO O_WRONLY ",getpid()); pipe_fd = open(FIFO_NAME, O_WRONLY); printf("the file's descriptor is %d ",pipe_fd); if(pipe_fd != -1){ res = write(pipe_fd, buffer,sizeof(buffer)); if(res == -1){ fprintf(stderr,"Write error on pipe "); exit(EXIT_FAILURE); } printf("write data is %s,%d bytes is write ",buffer,res); (void )close(pipe_fd); } else exit(EXIT_FAILURE); printf("Process %d finished ",getpid()); exit(EXIT_SUCCESS); }
P_231_read
// // Created by wybiacx on 2020/12/31. // #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <fcntl.h> #include <limits.h> #include <sys/types.h> #include <sys/stat.h> #define FIFO_NAME "/tmp/my_info" int main(int argc,char *argv[]){ int pipe_fd; int res; char buffer[4096]; int bytes_read = 0; memset(buffer,'