test11_7.txt

来自「Linux下的C语言编程」· 文本 代码 · 共 43 行

TXT
43
字号
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#define LINESIZE 1024
int main(void)
{
    	int n, fd[2];
    	pid_t pid;
    	char line[LINESIZE];

    	if(pipe(fd)<0)
    	{
        	printf("pipe error");
        	exit(1);
    	}

    	if((pid=fork())<0)
    	{
        	printf("fork error");
        	exit(1);
    	}
    	else if(pid==0)
    	{
        	close(fd[0]);
        	write(fd[1], "Hello!\n",6);
        	close(fd[1]);
    	}
    	else
    	{
        	close(fd[1]);
        	if(waitpid(pid,NULL, 0)<0)
        	{
            		printf("waitpid error! \n");
            		exit(0);
        	}
        	n=read(fd[0],line, LINESIZE);
        	printf("string: %s\n", line);
        	close(fd[0]);
    	}
    
    	exit(0);
}

⌨️ 快捷键说明

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