亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 1715 | 回復: 2
打印 上一主題 下一主題

[系統(tǒng)] 管道?進程?不知為什么求教 [復制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2015-03-13 21:58 |只看該作者 |倒序瀏覽
源程序如下。
在pipeline中建立了管道,以及父子進程。在child1中的改讀管道為0號描述符,在father1中改寫管道為3號描述符。我在father1.c中先寫的順序是
        printf("parent!!!");
        sleep(2);
        write(3, string, len);//3號對應管道文件的寫
        sleep(2);
        printf("parent!!!");
但為什么最終執(zhí)行pipeline的結(jié)果卻是:parent is using pipe write
child!!!parent!!!parent!!!
而不是parent!!!parent is using pipe write parent!!!
child!!!?
明明第一個printf("parent!!!");在前,然后寫管道,最后又是printf("parent!!!");結(jié)果卻是兩個parent!!都在最后顯示在終端上。
謝謝高手們,初學者第一次來此地非常激動呢。
/*pipeline.c*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define STD_INPUT 0
#define STD_OUTPUT 1
void pipeline(char*, char*);
int fd[2];
void main()
{
        static char process1[] = "father1",process2[]="child1";
        pipe(fd);//建立管道
        pipeline(process1,process2);//調(diào)用函數(shù)
        exit(1);
}

void pipeline(char * pro1,char * pro2)
{
        int i;
        while((i = fork()) == -1);
        if(i)//父
        {
                close(fd[0]);
                close(3);//關(guān)閉3
                dup(fd[1]);//把寫的管道文件描述符給3
                close(fd[1]);//關(guān)閉寫的管道文件描述符
                execl(pro1,pro1,NULL);
                printf("-- father1 failed.\n");
        else//子
        {
                close(fd[1]);
                close(STD_INPUT);
                dup(fd[0]);
                close(fd[0]);
                execl(pro2,pro2,NULL);
                printf("child1 failed.\n");
        }
        exit(2);
}

/*father1.c*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
void main()
{
        static char string[] = "parent is using pipe write";
        int len;
        len = sizeof(string);
        printf("parent!!!");
        sleep(2);
        write(3, string, len);//3號對應管道文件的寫
        sleep(2);
        printf("parent!!!");
        exit(0);
}
/*child1.c*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int  main()
{
        char output[30];
        read(0,output,30);//0號描述符對應的是管道文件的讀
        printf("%s \nchild!!!",output);//讀完后會有顯示child!!
        return 0;
}

論壇徽章:
1
2015年辭舊歲徽章
日期:2015-03-03 16:54:15
2 [報告]
發(fā)表于 2015-03-15 19:09 |只看該作者
回復 1# 清心寧神
你的printf好多沒有加\n,這樣的話,print的數(shù)據(jù)會緩沖,直到\n或者程序退出才會顯示,你的程序child先退出,parent后退出,所以child的printf先打印了,而parent后打印

論壇徽章:
0
3 [報告]
發(fā)表于 2015-03-16 17:18 |只看該作者
回復 2# 羽劍天涯
謝謝指導~~


   
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP