- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我对 PIPE 的了解是它用于单向通信,它有助于在两个相关进程之间进行通信。我从一本书中得到了下面的 PIPE 编程代码示例。我正在尝试使用 printf
理解代码并在代码的每一行之后打印出所有点。但我不明白程序是如何在每一行之后运行的。我的代码如下:
管道:
//using PIPE to communicate with a child process
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
/* Write COUNT copies of MESSAGE to filename, pausing for a second
between each. */
void writer (const char* message, int count, FILE* filename)
{
for (; count > 0 ; -- count) {
printf("point 13\n");
/* Write the message to the filename, and send it off immediately.*/
fprintf (filename, "%s\n", message);
printf("point 14\n");
fflush (filename);
printf("point 15\n");
/* Snooze a while. */
sleep (1);
}
}
/* Read random strings from the filename as long as possible.
*/
void reader (FILE* filename)
{
char buffer[1024];
/* Read until we hit the end of the filename. fgets reads until
either a newline or the end-of-FILE. */
printf("point 16\n");
while (!feof (filename) && !ferror (filename) && fgets (buffer, sizeof (buffer), filename) != NULL)
fputs (buffer, stdout);
printf("point 17\n");
}
int main ()
{
int fds[2];
pid_t pid;
printf("point 1\n");
/* Create a pipe. FILE descriptors for the two ends of the pipe are
placed in fds. */
pipe (fds);
printf("point 2\n");
/* Fork a child process. */
pid = fork ();
printf("point 3\n");
if (pid == (pid_t) 0)
{
FILE* filename;
printf("point 4\n");
/* This is the child process. Close our copy of the write end of
the FILE descriptor. */
close (fds[1]);
printf("point 5\n");
/* Convert the read FILE descriptor to a FILE object, and read
from it. */
filename = fdopen (fds[0], "r");
printf("point 6\n");
reader (filename);
printf("point 7\n");
close (fds[0]);
printf("point 8\n");
}
else
{
/* This is the parent process. */
FILE* filename;
/* Close our copy of the read end of the FILE descriptor. */
close (fds[0]);
printf("point 9\n");
/* Convert the write FILE descriptor to a FILE object, and write
to it. */
filename = fdopen (fds[1], "w");
printf("point 10\n");
writer ("I want to learn c programming!", 5, filename);
printf("point 11\n");
close (fds[1]);
printf("point 12\n");
}
return 0;
}
我真的需要理解代码。如果我运行我的代码,那么我会在 Linux 终端中得到如下所示的输出,但我不确定为什么在第 3 点之后会出现第 9 点。在第 9 点之后,为什么是第 3、4、5、10 点。详细解释对我有帮助。
point 1
point 2
point 3
point 9
point 3
point 4
point 5
point 10
point 13
point 14
point 15
point 6
point 16
I want to learn c programming!
point 13
point 14
point 15
I want to learn c programming!
point 13
point 14
point 15
I want to learn c programming!
point 13
point 14
point 15
I want to learn c programming!
point 13
point 14
point 15
I want to learn c programming!
point 11
point 12
point 17
point 7
point 8
最佳答案
pipe(int[]) 函数返回一个大小为两个整数的数组。您需要了解此处的基本概念。管道可以像水管一样用于单向通信。您需要了解的一件事是管道在内部是一个文件。因此,当我们使用文件描述符从文件读取内容或向文件写入内容时,我们同样需要用于从管道读取/写入管道的描述符。现在在您的程序中,当执行 pipe(fds)
时,会创建一个管道并创建两个描述符用于读取和写入该管道。这些描述符是fds[0]
(读端)和fds[1]
(写端)。
现在,您需要了解这里的fork()
,该函数通过复制现有进程创建了一个新进程(子进程)。调用 fork()
后,在子进程中立即执行 if
,因为 fork
在子进程中返回 0,在父进程中 if 条件失败,因为 fork
返回父进程中子进程的pid,因此执行else
。休息你可以观察你的代码。
在 if
中,您正在关闭 fds[1]
,这将使您的子进程现在只读取管道的文件描述符,并且您正在关闭 fds [0]
在父级中,这将使您的父级只有管道的写入描述符。现在,您的 child 只能从管道读取,而 parent 只能从管道写入,您可以观察到父子之间的通信(父在写,子在读)。如果您想要反向通信,则需要创建一个新管道。因此,一根管道可以用于一个方向的通信。
现在,您只需了解父进程和子进程在 fork 之后将如何执行,就可以理解程序的其余部分。现在,无论哪个进程获得执行该进程的 CPU 时间,通过这种方式,您都可以交错执行子进程和父进程,从而得到上述输出。
关于c - 不了解 PIPE 计划,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24645328/
我开始在 Ethereum blockchain 上了解如何开发智能合约以及如何写 web-script用于与智能合约交互(购买、销售、统计......)我得出了该怎么做的结论。我想知道我是否正确理解
我正在 UIView 中使用 CATransform3DMakeRotation,并且我正在尝试进行 45º,变换就像向后放置一样: 这是我拥有的“代码”,但显然没有这样做。 CATransform3
我目前正在测试 WebRTC 的功能,但我有一些脑逻辑问题。 WebRTC 究竟是什么? 我只读了“STUN”、“P2P”和其他...但是在技术方面什么是正确的 WebRTC(见下一个) 我需要什么
我在看 DelayedInit在 Scala in Depth ... 注释是我对代码的理解。 下面的 trait 接受一个非严格计算的参数(由于 => ),并返回 Unit .它的行为类似于构造函数
谁能给我指出一个用图片和简单的代码片段解释 WCF 的资源。我厌倦了谷歌搜索并在所有搜索结果中找到相同的“ABC”文章。 最佳答案 WCF 是一项非常复杂的技术,在我看来,它的文档记录非常少。启动和运
我期待以下 GetArgs.hs打印出传递给它的参数。 import System.Environment main = do args main 3 4 3 :39:1: Coul
private int vbo; private int ibo; vbo = glGenBuffers(); ibo = glGenBuffers(); glBindBuffer(GL_ARRAY_
我正在尝试一个 for 循环。我添加了一个 if 语句以在循环达到 30 时停止循环。 我见过i <= 10将运行 11 次,因为循环在达到 10 次时仍会运行。 如果有设置 i 的 if 语句,为什
我正在尝试了解 WSGI 的功能并需要一些帮助。 到目前为止,我知道它是一种服务器和应用程序之间的中间件,用于将不同的应用程序框架(位于服务器端)与应用程序连接,前提是相关框架具有 WSGI 适配器。
我是 Javascript 的新手,我正在尝试绕过 while 循环。我了解它们的目的,我想我了解它们的工作原理,但我在使用它们时遇到了麻烦。 我希望 while 值自身重复,直到两个随机数相互匹配。
我刚刚偶然发现Fabric并且文档并没有真正说明它是如何工作的。 我有根据的猜测是您需要在客户端和服务器端都安装它。 Python 代码存储在客户端,并在命令运行时通过 Fabric 的有线协议(pr
我想了解 ConditionalWeakTable .和有什么区别 class ClassA { static readonly ConditionalWeakTable OtherClass
关闭。这个问题需要更多focused .它目前不接受答案。 想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post . 5年前关闭。 Improve this questi
我还没有成功找到任何可以引导我理解 UIPickerView 和 UIPickerView 模型的好例子。有什么建议吗? 最佳答案 为什么不使用默认的 Apple 文档示例?这是来自苹果文档的名为 U
我在看foldM为了获得关于如何使用它的直觉。 foldM :: Monad m => (a -> b -> m a) -> a -> [b] -> m a 在这个简单的例子中,我只返回 [Just
答案What are _mm_prefetch() locality hints?详细说明提示的含义。 我的问题是:我想要哪一个? 我正在处理一个被重复调用数十亿次的函数,其中包含一些 int 参数。
我一直在读这个article了解 gcroot 模板。我明白 gcroot provides handles into the garbage collected heap 然后 the handle
提供了一个用例: 流处理架构;事件进入 Kafka,然后由带有 MongoDB 接收器的作业进行处理。 数据库名称:myWebsite集合:用户 并且作业接收 users 集合中的 user 记录。
你好 我想更详细地了解 NFS 文件系统。我偶然发现了《NFS 图解》这本书,不幸的是它只能作为谷歌图书提供,所以有些页面丢失了。有人可能有另一个很好的资源,这将是在较低级别上了解 NFS 的良好开始
我无法理解这个问题,哪个更随机? rand() 或: rand() * rand() 我发现这是一个真正的脑筋急转弯,你能帮我吗? 编辑: 凭直觉,我知道数学答案是它们同样随机,但我忍不住认为,如果您
我是一名优秀的程序员,十分优秀!