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

  免費(fèi)注冊 查看新帖 |

Chinaunix

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

linux下進(jìn)程學(xué)習(xí) [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2007-02-08 11:47 |只看該作者 |倒序瀏覽
   這個是在去年看linux的進(jìn)程通信后,寫的一個,正在慢慢的學(xué)習(xí)ing...,當(dāng)時的困難就是不知道父子進(jìn)程怎么知道對方在干什么。。
#include
#include
#include
#include
#include
#include
#include
#include
int write_file (const long line, char *fromfile, char *tofile, char *tofile2)
{
    FILE *to2 = NULL;
    FILE *from = NULL;
    FILE *to = NULL;
    int pipe_fd[2];
    int r_num;
    int fd;
    int num;
    char r_buf[1024];
    char w_buf[1024];
    char *buf;
    struct stat n_buf;
    memset (r_buf, 0x00, sizeof(r_buf));
    memset (w_buf, 0x00, sizeof(w_buf));
    if (pipe (pipe_fd) < 0)
    {
        printf ("pipe create error\n");
        return -1;
    }
    from = fopen (fromfile, "r");
    if (from == NULL)
    {
        printf ("Cannot open file!\n");
    }
   
    fd = open(fromfile, O_RDONLY);
    fstat(fd, &n_buf);
    num = n_buf.st_size;
    pid_t pid;
    pid = fork ();
    if (pid < 0)
    {
        printf ("fork error\n");
    }
    else if (pid == 0)
    {
        char tmp[1024];
        memset (tmp, 0x00, sizeof (tmp));
        to2 = fopen (tofile2, "a+");
        if (to2 == NULL)
        {
            printf ("Cannot open tofile2!\n");
            return -1;
        }
        
        close (pipe_fd[1]);
        sleep (3);
        r_num = read(pipe_fd[0], r_buf, sizeof (r_buf));
        
        fseek (from, atoi(r_buf), SEEK_SET);
        for (int j = atoi(r_buf); j < num; j++)
        {
            if (fgets (tmp, sizeof (tmp), from) == NULL)
            {
                printf ("read to2file error [%s]\n", strerror (errno));
                return -1;
            }
            fputs (tmp, to2);
        }
    }
    else
    {
        char tmp1[1024];
        memset (tmp1, 0x00, sizeof (tmp1));
        to = fopen (tofile, "a+");
        if (to == NULL)
        {
            printf ("Cannot open tofile!\n");
            return -1;
        }
        for (int i = 0; i < line; i++)
        {
            if (fgets (tmp1, sizeof (tmp1), from) == NULL)
            {
                printf ("read file error [%s]\n", strerror (errno));
                return -1;
            }
            fputs (tmp1, to);
        }
        //取出父進(jìn)程讀的字節(jié)數(shù)
        fseek (from, 0, SEEK_CUR);
        long last = ftell(from);
        
        close (pipe_fd[0]); //關(guān)閉讀
        snprintf(w_buf,sizeof(w_buf),"%ld",last);
        if (write (pipe_fd[1], w_buf, sizeof (w_buf)) != -1)
        {
            printf ("Write over!\n");
        }
        close (pipe_fd[1]);//關(guān)閉寫
        sleep (1);
    }
    if (to != NULL)
        fclose (to);
    if (to2 != NULL)
        fclose (to2);
    if (from != NULL)
        fclose (from);
    return 0;
}
int main ()
{
    char fromfile[] = "file1";
    char tofile[] = "file2";
    char to2file[] = "file3";
    long b = 3;
    write_file (b, fromfile, tofile, to2file);
    return 0;
}
               
               
               

本文來自ChinaUnix博客,如果查看原文請點:http://blog.chinaunix.net/u/8201/showart_244595.html
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(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