小编Ric*_*Sch的帖子

*几乎*完美的C壳管道

我正在用C编写一个小的linux shell,并且非常接近于完成.我接受用户的命令并将其存储在args中,由空格分隔.在下面的示例中,假设args包含以下内容:

args[] = {"ls", "-l", "|", "wc"};

我的函数接受args并且还接受有多少个管道.我尽可能地评论了我的代码.这里是:

int do_command(char **args, int pipes) {
    // The number of commands to run
    const int commands = pipes + 1;
    int i = 0;

    int pipefds[2*pipes];

    for(i = 0; i < pipes; i++){
        if(pipe(pipefds + i*2) < 0) {
            perror("Couldn't Pipe");
            exit(EXIT_FAILURE);
        }
    }

    int pid;
    int status;

    int j = 0;
    int k = 0;
    int s = 1;
    int place;
    int commandStarts[10];
    commandStarts[0] …
Run Code Online (Sandbox Code Playgroud)

c linux shell pipe

12
推荐指数
1
解决办法
8769
查看次数

整数输入验证,怎么样?

我遇到了一个非常简单的代码问题.我想接受1到3之间的整数,并进行错误检查.它适用于检查太大或太小的数字,但是当输入字母/数字组合时,它会陷入无限循环.建议?

#include <iostream>
using namespace std;

int main(int argc, char *argv[]){
    int input;

    cout << "\nPlease enter a number from 1 to 3:" << endl;
    cout << "-> ";
    cin >> input;

    while(input< 1 || input> 3){
        cout << "\n---------------------------------------" << endl;
        cout << "\n[!] The number you entered was invalid." << endl;
        cout << "\nPlease re-enter a number from 1 to 3" << endl;
        cout << "-> ";
        cin >> input;
    }

    cout << "You chose " << input …
Run Code Online (Sandbox Code Playgroud)

c++ error-handling integer infinite-loop

5
推荐指数
1
解决办法
3万
查看次数

C++计算整数数组中的字节数

这可能是也可能不是一个非常简单的问题,但我想知道要调用什么函数以便在任何给定时间计算出数组中有多少字节.例如,我如何知道下面代码中send命令中的第三个参数?

int *array= new int[500];
memset(array, 0, sizeof(array));

//newsockfd is declared elsewhere in the code
send(newsockfd, array, _______, 0);
Run Code Online (Sandbox Code Playgroud)

c++ sockets arrays

1
推荐指数
1
解决办法
1794
查看次数

轻松修复 - 在多个值上排序双端队列

我试图弄清楚如何在两个值上排序结构的双端队列,而不只是一个.我所拥有的代码就是我所拥有的完美排序arrival,但是如果两个项目相同pid,我希望它们也是pid顺序.我希望我有意义!

例如:

具有pid1和arrival10的a的进程应该在具有pid2和arrival10 的进程之前,即使具有pid1 的进程最初在deque中出现.

struct Process{
    int pid;
    int burst;
    int arrival;
};

int sortOnArrival (Process const &a, Process const &b){
    return a.arrival < b.arrival;
}

int main(int argc, char *argv[]){

    deque<Process> readyQueue;

    // This is just pseudocode, but trust me, it works. :)
    fill(readyQueue);

    sort(readyQueue.begin(), readyQueue.end(), sortOnArrival);
}
Run Code Online (Sandbox Code Playgroud)

c++ sorting deque

0
推荐指数
1
解决办法
641
查看次数

标签 统计

c++ ×3

arrays ×1

c ×1

deque ×1

error-handling ×1

infinite-loop ×1

integer ×1

linux ×1

pipe ×1

shell ×1

sockets ×1

sorting ×1