zoukankan      html  css  js  c++  java
  • 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. ");
                    char s[]="Hello world,this is write by pipe. ";
                    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. ");
                    read(fd[0],buf,sizeof(buf));
                    printf("%s ",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! ");
                    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! ");
                    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 ",s);
                    wait(0);
                    read(fd[0],s,50);
                    printf("%s ",s);
            }return 0;}

  • 相关阅读:
    论信息系统项目的范围管理
    Windows服务/Linux守护创建及安装
    C#开源网络通信库PESocketde的使用
    Unity本地持久化类PlayerPrefs使用详解
    记录一个闪烁,跳动,光圈的动画
    一、Linux驱动学习-了解系统
    用c#自己实现一个简单的JSON解析器
    highcharts的yAxis标题过长 分行显示
    Mysql binlog恢复数据
    Xamarin.Forms实例旅行日志
  • 原文地址:https://www.cnblogs.com/gudygudy/p/10194019.html
Copyright © 2011-2022 走看看