我在 C 中使用管道时遇到了严重的问题。我应该从命令行接收参数(例如:./myprogram 123 45 67),一次一个字符地将参数读入缓冲区,将字符发送到子进程进行计数,然后将读取的总字符数返回给父进程。我的代码如下(注意:注释是我应该做的):
// Characters from command line arguments are sent to child process
// from parent process one at a time through pipe.
// Child process counts number of characters sent through pipe.
// Child process returns number of characters counted to parent process.
// Parent process prints number of characters counted by child process.
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
static int toChild[2];
static int fromChild[2];
static char buffer;
int main(int …Run Code Online (Sandbox Code Playgroud) 我的任务是优化C中的特定for循环.这是循环:
#define ARRAY_SIZE 10000
#define N_TIMES 600000
for (i = 0; i < N_TIMES; i++)
{
int j;
for (j = 0; j < ARRAY_SIZE; j++)
{
sum += array[j];
}
}
Run Code Online (Sandbox Code Playgroud)
我应该使用循环展开,循环拆分和指针来加速它,但每次我尝试实现某些东西时,程序都不会返回.这是我到目前为止所尝试的:
for (i = 0; i < N_TIMES; i++)
{
int j,k;
for (j = 0; j < ARRAY_SIZE; j++)
{
for (k = 0; k < 100; k += 2)
{
sum += array[k];
sum += array[k + 1];
}
}
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么程序现在甚至没有返回.任何帮助,将不胜感激.