#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>intmain(){int fd,pid;
fd =open("file",O_CREAT|O_RDWR,S_IRWXU);if(fd<0){perror("open");}
pid =fork();if(pid ==0){printf("This is the child!\n");char str[128]="zhinengsanban_liaoxianqiang_1915925583";if(write(fd,str,128)<0){perror("write");}exit(5);}else{printf("This is the father!\n");char buf[128];int n,status;if(read(fd,buf,128)<0){perror("read");}printf("The buf is: %s\n",buf);if(wait(&status)<0){perror("perror");}if(WIFEXITED(status)){
n =WEXITSTATUS(status);}else{printf("wait error!\n");}printf("The child's pid is: %d\n",pid);printf("The child exit status is: %d\n",n);}return0;}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>intmain(){int fd1[2],fd2[2];pipe(fd1);pipe(fd2);int pid;
pid =fork();if(pid <0)perror("fork");elseif(pid ==0){close(fd1[0]);close(fd2[1]);char str[12];printf("This is the child!\n");if(read(fd2[0],str,12)>0){printf("Received the news: %s\n",str);if(write(fd1[1],"hello father",12)<0)perror("write");}elseperror("read");exit(5);}else{int status;printf("This is the father!\n");close(fd1[1]);close(fd2[0]);char buf[24]="hello child";if(write(fd2[1],buf,12)<0)perror("write");else{printf("Send news successful!\n");}wait(&status);if(WIFEXITED(status)){printf("The child's pid is: %d\n",pid);printf("The child's exited status is: %d\n",WEXITSTATUS(status));}}return0;}
利用匿名管道实现兄弟进程间通信,要求兄进程发送字符串“This is elder brother ,pid is (兄进程进程号)”给第进程;第进程收到兄进程发送的数据后,给兄进程回复“This is younger brother ,pid is(第进程进程号)”;
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>intmain(){int fd1[2],fd2[2];pipe(fd1);pipe(fd2);int pid;
pid =fork();if(pid ==0){printf("This is the elder brother!\n");printf("The elder's father's pid is: %d\n",getppid());close(fd1[1]);close(fd2[0]);char str1[64],str2[64];sprintf(str1,"This is the elder brother,pid is %d",getpid());if(write(fd2[1],str1,64)<0)perror("write");if(read(fd1[0],str2,64)<0)perror("read");elseprintf("The news from younger is: %s\n",str2);}else{if(fork()==0){printf("This is the younger brother!\n");printf("The younger's father's pid is: %d\n",getppid());close(fd1[0]);close(fd2[1]);char buf1[64],buf2[64];if(read(fd2[0],buf1,64)>0){printf("The news form elder is: %s\n",buf1);sprintf(buf2,"This is the younger brother,pid is %d",getpid());if(write(fd1[1],buf2,64)<0)perror("write");}elseperror("read");}}return0;}
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>intmain(){int pid,fd;if(mkfifo("fifotest",0666)<0)perror("mkfifo");
pid =fork();if(pid <0)perror("fork");elseif(pid ==0){printf("This is the write process!\n");int fd =open("fifotest",0666);for(int i =0; i <10;i++){if(write(fd,"hello world",12)<0)perror("write");sleep(1);}close(fd);}else{char str[128];printf("This is the read process!\n");int fd1 =open("fifotest",0666);for(int i =0;i <10;i++){if(read(fd1,str,128)<0)perror("read");elseprintf("%s\n",str);}system("rm -f fifotest");}}
在父进程中定义变量 n,在子进程中对变量 n 进行++操作;并且打印变量 n 的值,打印子 进程 pid; 在父进程中打印变量 n 的值,并且打印父进程 pid。 要求分别用 fork 和 vfork 创建子进程。
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>intmain(){int n =1;if(fork()==0){printf("This is child,the pid is%d\n",getpid());printf("The n is: %d\n",++n);}else{printf("This is father,the pid is%d\n",getpid());printf("The n is: %d\n",n);}return0;}//-----vfork
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>intmain(){int n =1;
pid_t pid;
pid =vfork();if(pid <0)perror("vfork");elseif(pid ==0){printf("This is child,the child's pid is: %d\n",getpid());printf("The n is: %d\n",++n);exit(0);}else{printf("This is father,the father's pid is: %d\n",getpid());printf("The n is: %d\n",n);}return0;}