热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用linux中的管道同时写入文件-Simultaneouswritinginafileusingpipesinlinux

Ihaveafilenames.txtPop,Jackandafilesurnames.txtIoan,Gry.Ineedtoobtainthefollowingou

I have a file names.txt Pop,Jack and a file surnames.txt Ioan,Gry. I need to obtain the following output, person.txt, Pop Ioan, Jack Gry. I must create two programs:

我有一个文件names.txt Pop,Jack和一个文件surnames.txt Ioan,Gry。我需要获得以下输出,person.txt,Pop Ioan,Jack Gry。我必须创建两个程序:

  • First read from file names.txt and write in person.txt.
  • 首先从文件names.txt中读取并在person.txt中写入。

  • Second read from surnames.txt and write in person.txt.
  • 第二次从surnames.txt读取并写入person.txt。

I need to use pipes for synchronization. How I can do it?

我需要使用管道进行同步。我怎么能这样做?

1 个解决方案

#1


Each program will open a named pipe for reading and one for writing, person.txt for append (or just write to stdout), and the appropriate input file (or read from stdin). One program, probably designated by a command line arg, will start by immediately reading one line and writing it to the output, and then write a single byte into one pipe and then block on a read from the other pipe. The other program waits for the byte on the first pipe, then writes its text and a newline before writing a byte into the other pipe. So you're just passing a token (the byte written to the pipes) to synchronize the writes. It might be nice to simply pass the names of the pipes to the programs as command line args, and then kick off the whole process by writing a single byte into one of the pipes, but the programs are not identical since one has to write a newline. Your best bet is to write a single program but invoke it with different command line args. (I don't know if that qualifies as "two programs".)

每个程序将打开一个命名管道进行读取,一个用于写入,person.txt用于追加(或只是写入stdout),以及相应的输入文件(或从stdin读取)。一个程序,可能由命令行arg指定,将从立即读取一行并将其写入输出开始,然后将一个字节写入一个管道,然后阻止从另一个管道读取。另一个程序在第一个管道上等待字节,然后在将一个字节写入另一个管道之前写入其文本和换行符。所以你只是传递一个令牌(写入管道的字节)来同步写入。简单地将管道名称作为命令行参数传递给程序可能会很好,然后通过将一个字节写入其中一个管道来启动整个过程,但程序不相同,因为必须编写一个新队。最好的办法是编写一个程序,但使用不同的命令行参数调用它。 (我不知道这是否属于“两个程序”。)


推荐阅读
author-avatar
奥黛丽凵赫本
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有