本文共 4747 字,大约阅读时间需要 15 分钟。
/* * @Description: pipe 利用无名管道实现子进程和父进程的通信 * @Version: 1.0 * @Autor: zys * @Date: 2020-09-26 10:39:02 * @LastEditors: zys * @LastEditTime: 2020-09-26 10:51:00 */#include#include #include #include #include #include #include int main(){ int pipe_fd[2]; pid_t pid; char buffer[100]; int read_num=0; memset(buffer,0,sizeof(buffer)); if(pipe(pipe_fd) < 0){ printf("pipe create err!\r\n"); return -1; } pid = fork(); if(pid == 0){ close(pipe_fd[1]); sleep(1); if((read_num = read(pipe_fd[0],buffer,sizeof(buffer))) > 0){ printf("child process read buff[%d]:%s\r\n",read_num,buffer); } close(pipe_fd[0]); printf("son pro end\r\n"); exit(0); } else if(pid > 0){ close(pipe_fd[0]); if(write(pipe_fd[1],"I am parent",11) == -1){ printf("write err!\r\n"); } if(write(pipe_fd[1],"Hello world",11) == -1){ printf("write err!\r\n"); } //sleep(2); wait(NULL);//wait son pro close(pipe_fd[1]); printf("parent pro end\r\n"); exit(0); }}
/* * @Description:fifo_read.c * @Version: 1.0 * @Autor: zys * @Date: 2020-09-27 17:50:20 * @LastEditors: zys * @LastEditTime: 2020-09-28 13:44:32 */#include#include #include #include #include #include #include #include #include #define FIFO "/tmp/myfifo"void main(){ char buf_r[100]; int fd; int nread; int res; /* //这种方式创建的管道文件 open的时候提示没有读权限 if(mkfifo(FIFO,O_CREAT|O_RDONLY|O_WRONLY|O_EXCL)<0 && (errno!=EEXIST)){ perror("creat"); exit(1); } */ if(access(FIFO,F_OK) == -1){ //管道文件不存在 res = mkfifo(FIFO,0777); if(res != 0){ fprintf(stderr,"Could not create fifo %s\n",FIFO); exit(-1); } } printf("Perparing for reading bytes...\n"); memset(buf_r,0,sizeof(buf_r)); fd=open(FIFO,O_RDONLY|O_NONBLOCK,0); if(fd<0){ perror("open"); exit(1); } while(1){ memset(buf_r,0,sizeof(buf_r)); /*if((nread=read(fd,buf_r,sizeof(buf_r)))<0){ if(errno==EAGAIN) printf("no data yet\n"); }*/ if((nread=read(fd,buf_r,sizeof(buf_r)) > 0)){ printf("read %s from FIFO\n",buf_r); } else { printf("read no data\n"); } sleep(1); } pause();//使进程挂起 直到捕捉到一个信号 close(fd);}
/* * @Description: fifo_write.c 命名管道写测试 * @Version: 1.0 * @Autor: zys * @Date: 2020-09-26 11:10:25 * @LastEditors: zys * @LastEditTime: 2020-09-28 13:50:39 */#include#include #include #include #include #include #include #include #define FIFO "/tmp/myfifo"int main(int argv,char **argc){ int fd; char buffer[100]; int wnum; fd = open(FIFO,O_WRONLY|O_NONBLOCK|O_CREAT,0); if(fd < 0){ perror("open"); exit(-1); } if(argv==1){ printf("Please input something\r\n"); exit(-1); } strcpy(buffer,argc[1]); if((wnum=write(fd,buffer,100))==-1){ if(errno==EAGAIN) printf("The FIFO has not been read yet,Please try later\n"); } else printf("write %s to the FIFO\n",buffer);}
/* * @Description: * @Version: 1.0 * @Autor: zys * @Date: 2020-09-28 14:16:46 * @LastEditors: zys * @LastEditTime: 2020-09-28 14:25:25 */#include#include #include #include void my_func(int sign_no){ if(sign_no == SIGINT){ printf("I have get SIGINT\n"); } else if(sign_no == SIGQUIT){ printf("I have get SIGQUIT\n"); }}int main(){ printf("Waiting for signal SIGINT or SIGQUIT\n"); //注册信号处理函数 signal(SIGINT,my_func); signal(SIGQUIT,my_func); pause(); exit(0);}
/* * @Description: * @Version: 1.0 * @Autor: zys * @Date: 2020-09-28 14:49:18 * @LastEditors: zys * @LastEditTime: 2020-09-28 14:50:20 */#include#include #include #include #include #include #include #include #include #include #define PERM S_IRUSR|S_IWUSRint main(int argc,char **argv){ int shmid; char *p_addr,*c_addr; if(argc!=2){ fprintf(stderr,"Usage:%s\n\a",argv[0]); exit(1); } //创建共享内存 if((shmid=shmget(IPC_PRIVATE,1024,PERM)) == -1){ fprintf(stderr,"Create Share Memory Error:%s\n\a",strerror(errno)); exit(1); } //创建子进程 if(fork()){ //父进程 p_addr = shmat(shmid,0,0); memset(p_addr,'\0',1024); strncpy(p_addr,argv[1],1024); wait(NULL);//阻塞当前进程 直到子进程退出 exit(0); } else { sleep(1); c_addr=shmat(shmid,0,0); printf("Client get %s\n",c_addr); exit(0); }}
转载地址:http://htqqj.baihongyu.com/