天生我材必有用,千金散尽还复来。 仰天大笑出门去,我辈岂是蓬蒿人。 大鹏一日同风起,扶摇直上九万里。 十步杀一人,千里不留行。 事了拂衣去,深藏身与名。 安能摧眉折腰事权贵,使我不得开心颜! 且乐生前一杯酒,何须身后千载名? 愿将腰下剑,直为斩楼兰。
 

os

#include<stdio.h>
#include<unistd.h>
#include<sys/wait.h>
int main(){
        int fd[2];
        char buf[80];
        pid_t pid;
        pipe(fd);
        if((pid = fork()) > 0){
                printf("This is in the father process, here write a string to the pipe.\n");
                char s[]="Hello world,this is write by pipe.\n";
                write(fd[1],s,sizeof(s));
                close(fd[0]);
                close(fd[1]);
        }else{
                printf("This is in the child process, here read a string from the pipe.\n");
                read(fd[0],buf,sizeof(buf));
                printf("%s\n",buf);
                close(fd[0]);
                close(fd[1]);
        }
        waitpid(pid,NULL,0);
        return 0;
}

 

 

 

 

#include<stdio.h>
#include<unistd.h>
#include<sys/wait.h>
#include<stdlib.h>
int main(){
        int i,j,fd[2];
        char buf[100];
        pipe(fd);
        if((i = fork()) == 0){
                sprintf(buf,"child1 send message to the pipe!\n");
                lockf(fd[1],1,0);
                write(fd[1],buf,50);
                sleep(1);
                lockf(fd[1],0,0);
                exit(0);
        }else if((j = fork()) == 0){
                sprintf(buf,"child2 send message to the pipe!\n");
                lockf(fd[1],1,0);
                write(fd[1],buf,50);
                sleep(1);
                lockf(fd[1],0,0);
                exit(0);
        }else{
                char s[100];
                wait(0);
                read(fd[0],s,50);
                printf("%s\n",s);
                wait(0);
                read(fd[0],s,50);
                printf("%s\n",s);
        }return 0;}

posted @ 2018-12-29 10:08  gudy  阅读(115)  评论(0编辑  收藏  举报