- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 fork 和管道来查找文件内字符串中 1 和 0 的数量。然而,我的程序从未达到正确的结果来计算 1 和 0。这是相当少量的代码,所以这是整个程序:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
int main (int argc, char** argv)
{
int leftR, rightR;
char *string;
long size;
int recursion = 0;
if (argc == 3)
{
string = argv[1];
size = strlen(string);
printf("the string is %s\n", string);
if (size <= 2)
{
int bitCounter[2];
bitCounter[0] = 0;
bitCounter[1] = 0;
int i;
for (i=0; i < size; i++)
{
if (string[i]=='0')
{
bitCounter[0]++;
}
else
{
bitCounter[1]++;
}
}
write(STDOUT_FILENO, &bitCounter, sizeof(int)*2);
printf("read bits, sending back %d ones and %d zeroes\n", bitCounter[1], bitCounter[0]);
return 0;
}
else
{
recursion = 1;
}
}
if (argc == 2 || recursion)
{
char *data;
if (!recursion)
{
FILE* filePointer;
if ((filePointer = fopen(argv[1], "r")) == NULL)
{
perror("file didn't work");
}
fseek(filePointer, 0, SEEK_END);
size = ftell(filePointer);
fseek(filePointer, 0, SEEK_SET);
data = malloc(size+1);
fread(data, size, 1, filePointer);
fclose(filePointer);
}
else
{
data = malloc(size+1);
data = string;
}
char *right;
char *left = malloc((size/2)+1);
if (size%2 == 0)
{
right = malloc(size/2 + 1);
}
else
{
right = malloc(size/2 + 2);
}
memcpy(left, data, size/2);
if (size%2 == 0)
{
memcpy(right, (size/2) + data, size/2);
}
else
{
memcpy(right, (size/2) + data, (size/2) + 1);
}
int pidLeft, pidRight;
int leftPipe[2];
int rightPipe[2];
pipe(leftPipe);
pipe(rightPipe);
fd_set readF;
FD_ZERO(&readF);
FD_SET(leftPipe[0], &readF);
FD_SET(rightPipe[0], &readF);
pidLeft = fork();
if (pidLeft > 0)
{
pidRight = fork();
if (pidRight > 0)
{
struct timeval timer;
timer.tv_sec = 3;
timer.tv_usec = 0;
close(rightPipe[1]);
close(leftPipe[1]);
dup2(leftPipe[0], STDOUT_FILENO);
dup2(rightPipe[0], STDOUT_FILENO);
select(2, &readF, NULL, NULL, &timer);
read(leftPipe[0], &leftR, sizeof(int)*2);
read(rightPipe[0], &rightR, sizeof(int)*2);
printf("going back to parent.\n");
}
else if (pidRight == 0)
{
close(rightPipe[0]);
execl("my_program", "my_program", right, "y", NULL);
printf("recursion start\n");
exit(1);
}
}
else if (pidLeft == 0)
{
close(leftPipe[0]);
execl("my_program", "my_program", left, "y", NULL);
printf("start recursion LEFT\n");
exit(1);
}
else
{
fprintf(stderr, "something went wrong! No fork!\n");
}
}
else
{
fprintf(stderr, "Please input file name properly\n");
exit(1);
}
int zeroes = leftR + rightR;
int* numOnes[2];
numOnes[0] = &leftR + sizeof(int);
numOnes[1] = &rightR + sizeof(int);
int ones = (int) *numOnes[0] + (int) *numOnes[1];
printf("0's: %d\n1's: %d\n", zeroes, ones);
return 0;
}
但是,输出永远不会达到我想要的结果,将所有内容相加:
the string is 01010▒z
the string is 010
the string is 0
read bits, sending back 0 ones and 1 zeroes
the string is 10▒z
the string is 10
read bits, sending back 1 ones and 1 zeroes
the string is ▒z
read bits, sending back 2 ones and 0 zeroes
the string is 10
read bits, sending back 1 ones and 1 zeroes
the string is 10100
z
the string is 101
the string is 1
read bits, sending back 1 ones and 0 zeroes
the string is 01
read bits, sending back 1 ones and 1 zeroes
the string is 00
z
the string is 00
read bits, sending back 0 ones and 2 zeroes
the string is
z
read bits, sending back 2 ones and 0 zeroes
(140) Admin $
几个快速点可以更轻松地理解代码:
我想首先要解决几个简单的问题:
最佳答案
这是最难破解的难题之一。该代码正在做一项晦涩的工作,并且编写得相当晦涩。据我所知,它应该被称为 my_program
,并且将使用文件名作为单个参数来调用。然后,该进程将打开该文件,将其内容读入两个数组(left
和 right
),而不必费心确保它们是字符串(无空终止)。然后该进程 fork 两次。然后,使用 left
(非)字符串作为参数以及它是 left
的信息来执行自身,然后使用 right
执行自身> (非)字符串作为参数以及它正确的信息。父进程无缘无故地乱搞 select()
,并且没有检查返回值。然后它调用 read 来获取两个管道的信息。这些读取会阻塞,直到数据准备好为止,因此 select()
确实没有任何帮助。 (我仍在尝试找出递归位适合的位置。)此外,大多数系统调用都不会进行错误检查。
但是,考虑到大纲操作(读取文件、拆分、子进程执行并各执行一半并报告),我会编写如下内容:
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static void err_exit(char const *fmt, ...);
int main(int argc, char * *argv)
{
int l_data[2] = { -1, -1 };
int r_data[2] = { -1, -1 };
char *string;
size_t size;
char *arg0 = argv[0];
if (argc != 2 && argc != 3)
err_exit("Usage: %s file\n", argv[0]);
if (argc == 3)
{
/* Child process */
string = argv[1];
size = strlen(string);
fprintf(stderr, "%d: the string is %s\n", (int)getpid(), string);
if (size <= 2)
{
int bitCounter[2];
bitCounter[0] = 0;
bitCounter[1] = 0;
for (size_t i = 0; i < size; i++)
{
if (string[i] == '0')
bitCounter[0]++;
else if (string[i] == '1')
bitCounter[1]++;
}
if (write(STDOUT_FILENO, bitCounter, sizeof(int)*2) != sizeof(int)*2)
err_exit("%d: failed to write on standard output\n",
(int)getpid());
fprintf(stderr, "%d: read bits, sending back %d ones and %d zeroes\n",
(int)getpid(), bitCounter[1], bitCounter[0]);
exit(0);
}
fprintf(stderr, "%d: doing recursion - string too big (%zu)\n",
(int)getpid(), size);
}
char *data = string;
if (argc == 2)
{
FILE *filePointer;
if ((filePointer = fopen(argv[1], "r")) == NULL)
{
perror("file didn't work");
exit(1);
}
fseek(filePointer, 0, SEEK_END);
size = ftell(filePointer);
fseek(filePointer, 0, SEEK_SET);
data = malloc(size+1);
fread(data, size, 1, filePointer);
data[size] = '\0';
if (data[size-1] == '\n')
data[--size] = '\0';
fclose(filePointer);
fprintf(stderr, "%d: data <<%s>>\n", (int)getpid(), data);
}
size_t l_size = size/2;
size_t r_size = size - l_size;
char *left = malloc(l_size+1);
char *right = malloc(r_size+1);
memcpy(left, data, l_size);
left[l_size] = '\0';
memcpy(right, data + l_size, r_size);
right[r_size] = '\0';
int l_pid, r_pid;
int l_pipe[2] = { -1, -1 };
int r_pipe[2] = { -1, -1 };
if (pipe(l_pipe) != 0 || pipe(r_pipe) != 0)
err_exit("%d: Failed to create pipes\n", (int)getpid());
fprintf(stderr, "%d: forking (l_size = %zu, r_size = %zu)\n",
(int)getpid(), l_size, r_size);
l_pid = fork();
if (l_pid < 0)
err_exit("%d: Failed to fork() left child\n", (int)getpid());
else if (l_pid == 0)
{
dup2(l_pipe[1], STDOUT_FILENO);
close(l_pipe[0]);
close(l_pipe[1]);
close(r_pipe[0]);
close(r_pipe[1]);
fprintf(stderr, "%d: left execing with string <<%s>>\n", (int)getpid(), left);
execl(arg0, arg0, left, "y", NULL);
err_exit("%d: failed to start recursion LEFT\n", (int)getpid());
}
else if ((r_pid = fork()) < 0)
err_exit("%d: Failed to fork() right child\n", (int)getpid());
else if (r_pid == 0)
{
dup2(r_pipe[1], STDOUT_FILENO);
close(l_pipe[0]);
close(l_pipe[1]);
close(r_pipe[0]);
close(r_pipe[1]);
fprintf(stderr, "%d: right execing with string <<%s>>\n", (int)getpid(), right);
execl(arg0, arg0, right, "y", NULL);
err_exit("%d: failed to start recursion RIGHT\n", (int)getpid());
}
else
{
/* Parent process */
int nbytes;
close(r_pipe[1]);
close(l_pipe[1]);
if ((nbytes = read(l_pipe[0], l_data, sizeof(int)*2)) != sizeof(int)*2)
err_exit("%d: Read left pipe failed (%d)\n", (int)getpid(), nbytes);
if ((nbytes = read(r_pipe[0], r_data, sizeof(int)*2)) != sizeof(int)*2)
err_exit("%d: Read right pipe failed (%d)\n", (int)getpid(), nbytes);
close(l_pipe[0]);
close(r_pipe[0]);
}
int zeroes = l_data[0] + r_data[0];
int ones = l_data[1] + r_data[1];
if (argc == 3)
{
int data[2] = { zeroes, ones };
if (write(STDOUT_FILENO, data, sizeof(data)) != sizeof(data))
err_exit("%d: failed to read binary data from stdin\n", (int)getpid());
fprintf(stderr, "%d: binary write to stdout OK\n", (int)getpid());
}
fprintf(stderr, "%d: 0's = %d, 1's = %d\n", (int)getpid(), zeroes, ones);
return 0;
}
static void err_exit(char const *fmt, ...)
{
int errnum = errno;
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
if (errnum != 0)
fprintf(stderr, "%d: %s\n", errnum, strerror(errnum));
exit(1);
}
给定一个输入文件:
0101010111111
输出是:
11070: data <<0101010111111>>
11070: forking (l_size = 6, r_size = 7)
11073: right execing with string <<0111111>>
11072: left execing with string <<010101>>
11072: the string is 010101
11072: doing recursion - string too big (6)
11072: forking (l_size = 3, r_size = 3)
11073: the string is 0111111
11073: doing recursion - string too big (7)
11073: forking (l_size = 3, r_size = 4)
11074: left execing with string <<010>>
11075: right execing with string <<101>>
11076: left execing with string <<011>>
11077: right execing with string <<1111>>
11074: the string is 010
11074: doing recursion - string too big (3)
11074: forking (l_size = 1, r_size = 2)
11078: left execing with string <<0>>
11076: the string is 011
11076: doing recursion - string too big (3)
11076: forking (l_size = 1, r_size = 2)
11079: right execing with string <<10>>
11075: the string is 101
11075: doing recursion - string too big (3)
11075: forking (l_size = 1, r_size = 2)
11080: left execing with string <<0>>
11077: the string is 1111
11077: doing recursion - string too big (4)
11077: forking (l_size = 2, r_size = 2)
11082: right execing with string <<11>>
11081: left execing with string <<1>>
11083: right execing with string <<01>>
11084: left execing with string <<11>>
11085: right execing with string <<11>>
11079: the string is 10
11078: the string is 0
11079: read bits, sending back 1 ones and 1 zeroes
11078: read bits, sending back 0 ones and 1 zeroes
11074: binary write to stdout OK
11074: 0's = 2, 1's = 1
11082: the string is 11
11082: read bits, sending back 2 ones and 0 zeroes
11080: the string is 0
11080: read bits, sending back 0 ones and 1 zeroes
11076: binary write to stdout OK
11076: 0's = 1, 1's = 2
11081: the string is 1
11081: read bits, sending back 1 ones and 0 zeroes
11084: the string is 11
11084: read bits, sending back 2 ones and 0 zeroes
11083: the string is 01
11083: read bits, sending back 1 ones and 1 zeroes
11075: binary write to stdout OK
11075: 0's = 1, 1's = 2
11072: binary write to stdout OK
11072: 0's = 3, 1's = 3
11085: the string is 11
11085: read bits, sending back 2 ones and 0 zeroes
11077: binary write to stdout OK
11077: 0's = 0, 1's = 4
11073: binary write to stdout OK
11073: 0's = 1, 1's = 6
11070: 0's = 4, 1's = 9
关于C - 使用 fork()、pipe()、select()、execl() 和 write() 未达到正确的结尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19340652/
我有管道输出 command_a | command_b | ... | command_n 输出是一个数字序列 4.2 -1 ... 0.2 我可以使用 gnuplot 绘制这些数字吗? (将 gn
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 6 年前。 Improv
我目前正在尝试连接父项和子项之间的管道。子级正在执行 sort 并对从父级接收到的输入进行排序。然后 children 写入一个单独的管道。每个进程有两个管道。一个这样 parent 可以将输入发送给
最近我正在研究 Python 中的并行编程工具。这是 os.pipe 和 multiprocessing.Pipe 之间的两个主要区别。(尽管它们被使用的场合) os.pipe是单向,multipro
我的站点上运行着 Yahoo Pipe,Romneyomics它使用来自 Delicious 和 Topsy 的饲料。Delicious 提要不提供“描述”字段,但 Topsy 提供,并且不仅仅是一个
我有一些使用管道的 Haskell 代码: module Main(main) where import Pipes a :: Producer Int IO () a = each [1..10]
所以标题几乎解释了我的问题。 stdout=subprocess.PIPE 和 stdout=PIPE 有什么区别?两者都来自 subprocess 模块,但为什么要使用一个而不是另一个呢?你如何使用
我有一个名为“myPipe”的自定义管道。我得到: The pipe 'myPipe' could not be found error 在我的单元测试中请建议在我的 .spec.ts 中导入和声明什
我有一个非常简单的 Python 3 脚本: f1 = open('a.txt', 'r') print(f1.readlines()) f2 = open('b.txt', 'r') print(f
我正在使用管道和 Python 的多处理模块在进程之间发送简单的对象。文档指出,如果管道已关闭,则调用 pipe.recv() 应该引发 EOFError。相反,我的程序只是阻塞在 recv() 上,
我在 perl 中见过这两种形式的管道 open。 一种是简单的管道打开 open FH,'| command'; 其他是安全管道打开 open FH,'|-','command'; 现在,第二个中的
我正在尝试对我的组件进行单元测试,但它立即生成一个错误: 类型错误:this.store$.pipe 不是函数 根据我的理解, createSpyObj 应该模拟状态。我有不同的选项选项,但没有一个起
我在这里看到这个帖子很多次了;但未能从命令中捕获故意错误。迄今为止我找到的最好的部分工作.. from Tkinter import * import os import Image, ImageTk
我正在编写一个简单的程序来解析编译器的输出并重新格式化任何错误消息,以便我们使用的 IDE(visual studio)可以解析它们。我们使用 nmake构建,它将使用如下命令行调用编译器: cc16
我有一个在coreos上运行的kubernetes集群。我希望在称为记录的Pod中的容器中运行journal2gelf https://github.com/systemd/journal2gelf。
为什么当管道中没有写入器时,读取器存在可以,但当管道中没有读取器时,写入器存在就不行? 。是不是因为reader需要等待,所以没有writer也没关系,而writer已经准备好数据了,即使数据准备好了
我在/etc/postfix/master.cf 中创建了一个 postfix 命令管道,其中包含一个在 STDOUT 和 STDERR 上产生输出的有效命令。在终端上调用时一切正常(因此在 STDO
我有一个命令需要来自管道的输入。例如,考虑著名的 cat 命令: $ echo Hello | cat Hello 假设我在 Perl 6 程序中有一个字符串,我想将其通过管道传递给命令: use v
因此,由于我们拥有各种设置,我习惯于遇到需要将一个可观察结果添加到另一个结果的地方,然后同时使用两者。我需要第一个在另一个之前完成的地方 getUser() .pipe( mergeMap
我在 Angular 5 中有一个非常简单的管道 import { Pipe, Injectable } from '@angular/core'; @Pipe({ name: "defaul
我是一名优秀的程序员,十分优秀!