Unix 命令行在编译后无法运行程序,没有错误消息
Unix command line failing to run program after compiling with no error message
我正在尝试运行一个C++程序,我一直在学校的基于Unix命令行的服务器编写。该程序应该使用 pipe() 和 fork() 等命令来计算子进程中的整数,并通过管道将其发送到父进程。我遇到的问题是,当我在编译程序后尝试运行程序时,除了在提示符之前插入"0"之外,什么也没发生。我不完全了解分叉和管道,因此我将发布整个程序,以防问题出在我使用这些命令上。可能有错误,因为我还没有能够成功运行它。这是我的代码:
#include <cstdlib>
#include <iostream>
#include <string>
#include <array>
#include <cmath>
#include <unistd.h>
using namespace std;
// Return bool for whether an int is prime or not
bool primeChecker(int num)
{
bool prime = true;
for (int i = 2; i <= num / 2; ++i)
{
if (num%i == 0)
{
prime = false;
break;
}
}
return prime;
}
int main(int argc, char *argv[])
{
int *array;
array = new int[argc - 1]; // dynamically allocated array (size is number of parameters)
int fd[2];
int count = 0; // counts number of primes already found
int num = 1; // sent to primeChecker
int k = 1; // index for argv
int addRes = 0;
// Creates a pair of file descriptors (int that represents a file), pointing to a pipe inode,
// and places them in the array pointed to. fd[0] is for reading, fd[1] is for writing
pipe(fd);
while (k < argc)
{
if (primeChecker(num)) // if the current number is prime,
{
count++; // increment the prime number count
if (count == (stoi(argv[k]))) // if the count reaches one of the arguments...
{
array[k - 1] = num; // store prime number
k++; // increment the array of arguments
}
}
num++;
}
pid_t pid;
pid = fork();
if (pid < 0) // Error occurred
{
cout << "Fork failed.";
return 0;
}
else if(pid == 0) // Child process
{
for (int i = 0; i < (argc-1); i++)
{
// Close read descriptor (not used)
close(fd[0]);
// Write data
write(fd[1], &addRes, sizeof(addRes)); /* write(fd, writebuffer, max write lvl) */
// Close write descriptor
close(fd[1]);
}
}
else // Parent process
{
// Wait for child to finish
wait(0);
// Close write descriptor (not used)
close(fd[1]);
// Read data
read(fd[0], &addRes, sizeof(addRes));
cout << addRes;
// Close read descriptor
close(fd[0]);
}
return 0;
}
这是我尝试编译和运行程序时在命令窗口(包括提示符)中看到的内容:
~/cs3270j/Prog2$ g++ -o prog2.exe prog2.cpp
~/cs3270j/Prog2$ ./prog2.exe
0~/cs3270j/Prog2$
什么也没发生。我尝试了不同的命名变体,并从"a.out"运行它,但没有成功。
tl;dr 编译并尝试执行我的程序后,Unix 命令提示符只是在提示符的开头添加一个 0,而不执行任何其他操作。
任何人能给我的任何帮助将不胜感激,因为我找不到任何关于提示前出现的"0"的信息。
你的程序正在做你告诉它做的事情! 将addRes
送入管道,然后将其打印出来。 addRes
初始化为 0,并且永远不会更改。在你的孩子身上,你想通过num
。 此外,您可能还想打印出一个新行 ('')。
-
你从不向管道写入任何内容;每个命令行参数写入一次,
./prog2.exe
不提供任何内容,因此循环永远不会执行 -
如果你传递了一个参数,你会写
addRes
;你永远不会改变addRes
,所以你会在父参数中得到0
-
如果传递了多个参数,则需要编写一个
addRes
然后关闭通道。这还不错,因为无论如何您都不会阅读超过一个addRes
。 -
您打印出没有换行符的
addRes
(与初始化int addRes = 0
没有变化),这使得下一个提示符紧挨着它(使用cout << addRes << endl
会打印出换行符,使其更漂亮)
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 我的程序有一个保存配置文件的GUI,如何双击此配置文件以直接加载带有配置数据的GUI?
- C ++程序编译错误,找不到/访问文件
- 我的代码中有错误,未声明的标识符
- 为什么从文件获取图标时应用程序有时会崩溃?
- 使用复制构造函数的程序输出错误
- 生成质数的程序,错误:"Stack overflow"
- C++ LeetCode #377 的 DP 解决方案中,此代码是否有错误?
- 试图建立银行管理系统,但有错误
- C++CTRL+C处理程序有未定义的行为
- 我试图创建临时对象的方式有错误吗
- 为什么一个简单的C++程序会有错误的结果?是 #define 引起的吗?
- 一个带有参数的程序,为什么此程序有错误的答案
- C++程序有语法错误,第一次使用模块
- 在快速排序程序中有错误
- 对简单的帐户检查程序有问题?程序返回编译错误,指出"no match for 'operator||' unsure of how to fix?"
- 有错误的程序仍在编译吗
- Dyanamic 数组类,程序运行良好,但有错误
- 为什么nasm说我在g++创建的程序集中有错误
- 一种程序,用于纠正运算符中有错误的C++程序