- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个练习,我应该读取 infile 文本并使用 Unix 管道(fork() 等)传递它们,并将它们打印到屏幕上以供初学者使用。我已经完成了,但现在我需要对字符进行排序infile 在管道中(exec sort)然后打印出来。
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
int main(void)
{
FILE *readchar = fopen("text1", "r");
char ch;
int fd[2];
int i = 0;
char readbuffer[1024];
int ret = pipe(fd);
if (ret == -1)
{
perror("pipe");
exit(-1);
}
if (fork() == 0)
{
printf("childprocess\n");
while(1)
{
ch = fgetc(readchar);
if(ch==EOF){
break;
}
write(fd[1],&ch,1);
dup2(1,fd[1]);
execlp("sort", "sort", (char*) NULL); //this is crashing
printf("%c",ch);
}
printf("\n");
exit(0);
printf("end of childprocess\n");
}else
{
wait(0);
printf("%d: parentprocess\n", (int)getpid());
read(fd[0],readbuffer,sizeof(readbuffer));
printf("that was in the pipe : \n");
printf ("%s",readbuffer);
printf("\n");
}
return 0;
}
我可以在 while(1) 循环中使用 execlp 排序吗?我的意思是每次一个角色进入管道时,exec 都会对管道进行排序。或者也许我可以在 while(1) 语句之后(在管道中添加所有字符之后)对整个管道进行排序?当 execlp 排序开始时,代码现在只会在第一个循环中崩溃。
infile 数据:
abcdefg
123456
XXXXXX
01010101
最佳答案
你调用了 pipe()
两次;删除第一个。你调用 fork()
两次;不清楚应该删除哪一个。但是,删除第一个意味着最少的其他更改。您还需要避免在父级中关闭 fd[1]
,以便第二个和后续子级仍然可以使用有效管道。
此代码至少在输出直接发送到终端(而不是通过管道)时有效:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
int main(void)
{
FILE *readchar = fopen("infile", "r");
char buf[1024];
int fd[2];
int i = 0;
int ret = pipe(fd);
if (ret == -1)
{
perror("pipe");
exit(-1);
}
while (fgets(buf, sizeof(buf), readchar) != NULL)
{
if (fork() == 0)
{
printf("child proceess\n");
close(fd[0]);
write(fd[1], buf, sizeof(buf));
exit(0);
}
else
{
wait(0);
printf("parent proceess");
//close(fd[1]);
read(fd[0], buf, 1024);
printf("buf: %s\n", buf);
printf("%d\n", ++i);
}
}
return 0;
}
将源代码副本复制到 infile
后,它生成了正确数量的输出行(双倍行距)。
由于很多原因,代码仍然不好,其中最重要的是“每行一个进程”有点浪费,以及“用缓冲区中的行的内容派生一个进程,然后将该缓冲区写回 parent (已经知道缓冲区中有什么)有点毫无意义'。 OTOH,它确实使用管道在两个进程之间进行通信。
当我运行数据文件中显示的代码时:
abcdefg
123456
XXXXXX
01010101
我得到的输出是:
child proceess
parent proceessbuf: abcdefg
1
child proceess
parent proceessbuf: abcdefg
1
parent proceessbuf: 123456
2
child proceess
parent proceessbuf: abcdefg
1
parent proceessbuf: 123456
2
parent proceessbuf: XXXXXX
3
child proceess
parent proceessbuf: abcdefg
1
parent proceessbuf: 123456
2
parent proceessbuf: XXXXXX
3
parent proceessbuf: 01010101
4
嗯……这有点奇怪:第一个;然后是 1、2;然后是 1、2、3;然后是 1、2、3、4。(我在试验中没有发现这一点,最后 43 行呼啸而过双倍行距。)让我调查一下。但是没有无限循环,所以你从我的代码中错误地合并了一些东西到你的代码中。
我为程序使用了名称xc19
(来源xc19.c
)。
上面代码的问题是我使用了xc19 | pbcopy
运行并将输出复制到剪贴板(在 Mac 上)。这意味着输出不再是行缓冲的,而是“完全缓冲的”。因此,中间输出仍在每个子进程的标准 I/O 缓冲区中,因此当子进程退出时,该信息被刷新。但是每个 child 都在缓冲区中获得了更多信息。
修复非常简单:在父代码中使用 fflush(0);
或 fflush(stdout);
。这是一个具有更多压缩输出的版本(以及正在打印的 PID 形式的诊断):
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
int main(void)
{
FILE *readchar = fopen("infile", "r");
char buf[1024];
int fd[2];
int i = 0;
int ret = pipe(fd);
if (ret == -1)
{
perror("pipe");
exit(-1);
}
while (fgets(buf, sizeof(buf), readchar) != NULL)
{
if (fork() == 0)
{
printf("%d: child proceess\n", (int)getpid());
close(fd[0]);
write(fd[1], buf, sizeof(buf));
exit(0);
}
else
{
wait(0);
printf("%d: parent proceess\n", (int)getpid());
read(fd[0], buf, 1024);
printf("%d buf: %s", ++i, buf);
fflush(0);
}
}
return 0;
}
输出:
58878: child proceess
58876: parent proceess
1 buf: abcdefg
58879: child proceess
58876: parent proceess
2 buf: 123456
58880: child proceess
58876: parent proceess
3 buf: XXXXXX
58881: child proceess
58876: parent proceess
4 buf: 01010101
关于c - 使用 Unix 管道读取 infile 并对其进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41235974/
正如标题所暗示的那样,我无法弄清楚 Unix 内核如何将逻辑文件偏移量转换为逻辑块号,然后从 i-node 中检索它。 作为引用,我要求对 Maurice J. Bach 在“UNIX 操作系统的设计
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 8年前关闭。 Improve this q
我在互联网上做了一些研究,但仍然很困惑。 UNIX 时间是像 GMT/UTC 那样的通用时间还是像本地时间一样因地而异? 我知道 UNIX 时间是从 1970 年 1 月 1 日格林威治标准时间 00
您如何评估 Unix 系统管理员。 Unix 管理员应该具备哪些编程能力? 最佳答案 我用于快速过滤器的一些: 什么是 fork 炸弹,它是好是坏? 给我一个单行命令,计算日志文件中有多少行从昨天的日
谁能告诉我字典文本文件在 UNIX 系统上的位置?或者我在哪里可以获得一个好的字典文本文件?我目前一直在使用来自 SUN 的文本文件,但它包含不带句点的缩写(否则我可以删除它们)。有人能指出我正确的方
在我的 Ubuntu 12 vps 上,我正在运行一个完整的比特币节点。当我第一次启动它时,它使用了大约 700mb 的内存。如果我 24 小时后回来 (free -m) 将如下所示: total
我想编写一个 unix/linux 程序,它将使用一个配置文件。 我的问题是,我应该把文件的位置放在哪里? 我可以将位置(如 /etc )“硬编码”到程序本身中。 但是,我希望它,如果没有权限的用户可
在UNIX脚本编程中,cat是可以将2个文件组合在一起的命令: cat file1 file2 > file3 通过合并前两个生成第三个。 另外,cat可以与管道一起使用: cat file1 | t
我有一个通过 ssh 连接到外部机器的终端,并且有一个进程在其中运行。 是否可以将执行移到后台,以便我可以关闭 ssh 连接而无需终止它?如果是这样怎么办? 最佳答案 按 control + Z,这将
我正在试验我自己的 BSD 或 Linux 发行版。我想以对最终用户有意义的方式组织系统文件。我希望他们能够访问系统,而不会出现 *nixes 留下的所有文件困惑。 有没有办法在不丢失动态链接的情况下
这条评论让我感到困惑:“kill -l 通常会列出所有信号”。我认为信号意味着量化的能量。 [已添加] 请澄清 Unix 中的(计算)信号和物理信号。它们是完全不同的概念吗? [已添加] 范式之间是否
fuser 命令让我知道哪些进程正在使用文件或目录。 我正在寻找相反的命令:让我知道进程正在使用哪些文件。 更新 忘了说它是针对 Solaris 系统的。 最佳答案 lsof -p 来自 here
如果我有一个叫做“orange”的词,我如何将它拆分成单独的字符。 我的输出应该是: o r a n g e 最佳答案 echo orange | fold -w 1 输出 o r a n g e 关
和有什么区别工作和一个 流程在 Unix 中?你能举个例子吗? 最佳答案 作业是由 shell 启动的进程。 shell 在作业表中跟踪这些。作业命令显示事件后台进程的列表。他们得到一个 jobspe
unix 如何处理带空格和参数的完整路径名? 在 Windows 中,我们引用路径并在其后添加命令行参数,在 unix 中如何? "c:\foo folder with space\foo.exe"
我必须合并具有相同标题的多个CSV文件。 我必须保留第一个文件的 header ,并删除所有其他文件的 header ,然后合并它们并创建一个主文件。 文件1: Id,city,name ,locat
我需要在两个字段上加入两个文件。但是,即使连接失败,我也应该检索文件 1 中的所有值,就像左外连接一样。 文件 1: 01|a|jack|d 02|b|ron|c 03|d|tom|e 文件2: 01
在 Solaris, HP-UX 上获取进程大小的正确方法是什么?和 AIX ?我们应该使用 top或 ps -o vsz或者是其他东西? 最佳答案 vsize的确切定义, rss , rprvt ,
我在文件上使用了“touch”,更新了文件的时间戳,但父目录的时间戳没有改变。但是,(如预期)当我在父目录中创建新文件时,该目录的时间戳确实发生了变化。 类 UNIX 操作系统(特别是 AIX)使用什
一般来说,当我们从多个进程向 UNIX 中的文件追加内容时,我们可以认为什么是理所当然的?是否有可能丢失数据(一个进程覆盖另一个进程的更改)?数据有可能被破坏吗? (例如,每个进程都将每个追加一行追加
我是一名优秀的程序员,十分优秀!