⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 2_child_progress_communication

📁 工ARM_LINUX的几个源代码
💻
字号:
/*fork two child progress,两个子进程间利用pipe通信*/
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
int main()
 {
	pid_t child1;
	pid_t child2;
	int fd[2];
	char buf[500];
	int len;	
	if ((pipe(fd))<0){perror("pipe error");exit(1);}

	child1=fork();
        if (child1==0)
           {
               printf("this is child progress 1....child1 pn is %dparent pn is %d\n",getpid(),getppid());
		close(fd[1]);
		len=read(fd[0],buf,19);
		printf("read %d bytes..\n",len);
                 printf("%s\n",buf);

		close(fd[0]);
			
	   }
        else if (child1>0)
	   {
             
               printf("this is parent progress .... pn is %d\n",getpid());
               child2=fork();
               if (child2==0)
    	   	{
                printf("this is child progress 2....child2 pn is %dparent pn is %d\n",getpid(),getppid());
		close(fd[0]);
		sleep(2);
		if (write(fd[1],"i am yexinhua,hello",19)!=-1){printf("\nchild 2 write into pipe is ok\n");}
		close(fd[1]);
		 }
	 	else{
                waitpid(child1,NULL,0);
                waitpid(child2,NULL,0);
                exit(0);
                  }

           }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -