Unix 命令行在编译后无法运行程序,没有错误消息

Unix command line failing to run program after compiling with no error message

本文关键字:程序 有错误 消息 运行 命令行 编译 Unix      更新时间:2023-10-16

我正在尝试运行一个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 会打印出换行符,使其更漂亮)