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

📄 jc.txt

📁 进程的管道通信,可用来加强对进程通信的理解.
💻 TXT
字号:
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
int pid1,pid2;
main( )
    { 
int fd[2];
char outpipe[100],inpipe[100];
pipe(fd);                       /*创建一个管道*/
while ((pid1=fork( ))==-1);
if(pid1==0)
  {
lockf(fd[1],1,0);
    sprintf(outpipe,"child 1 process is sending message!"); 
	/*把串放入数组outpipe中*/
    write(fd[1],outpipe,50);     /*向管道写长为50字节的串*/
    sleep(5);                 /*自我阻塞5秒*/
    lockf(fd[1],0,0);
    exit(0);
            }
else
           {
while((pid2=fork( ))==-1);
    if(pid2==0)
{    lockf(fd[1],1,0);           /*互斥*/
           sprintf(outpipe,"child 2 process is sending message!");
           write(fd[1],outpipe,50);
           sleep(5);
           lockf(fd[1],0,0);
           exit(0);
        }
      else
        {  wait(0);              /*同步*/
           read(fd[0],inpipe,50);   /*从管道中读长为50字节的串*/
         printf("%s\n",inpipe);
         wait(0);
         read(fd[0],inpipe,50);
         printf("%s\n",inpipe);
         exit(0);
               }
        }
}

⌨️ 快捷键说明

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