#无名管道

利用无名管道在父子进程间进行通信
图片说明

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <ctype.h>
int main()
{
    int p1,m,num,i,k,retval;
    int fd[2];
    char buf[1024];
    if((pipe(fd))<0)
    {
        printf("failed\n");
        exit(-1);
    }
    if((p1=fork())==-1)
    {

        printf("fork failed\n");
        exit(-1);
    }
    if(p1==0)
    {
        close(fd[1]);
        while((k=read(fd[0],buf,num))>0)
        {

            for(i=0;i<k;i++)
            {
                buf[i]=toupper(buf[i]);
            //toupper(),将小写英文字母转换成大写英文字母的函数,如果本身就是大写,则原样返回
            }

            write(STDOUT_FILENO,buf,k);
        }
        close(fd[0]);
        exit(0);    

    }
    if(p1>0)
    {    close(fd[0]);
        while((fgets(buf,1024,stdin))!=NULL)
        {

            num=write(fd[1],buf,strlen(buf));

        }
        close(fd[1]);
        wait(&retval);
        if(WIFEXITED(retval))
        printf("%d\n",WEXITSTATUS(retval));
    }
    return 0;
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
03-19 10:38
实力求职者:真的绷不住了,第一张霸总人设,第二张求生欲拉满
点赞 评论 收藏
分享
03-10 11:23
门头沟学院 Java
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务