我正在用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) 我遇到了一个非常简单的代码问题.我想接受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) 这可能是也可能不是一个非常简单的问题,但我想知道要调用什么函数以便在任何给定时间计算出数组中有多少字节.例如,我如何知道下面代码中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) 我试图弄清楚如何在两个值上排序结构的双端队列,而不只是一个.我所拥有的代码就是我所拥有的完美排序arrival
,但是如果两个项目相同pid
,我希望它们也是pid顺序.我希望我有意义!
例如:
具有pid
1和arrival
10的a的进程应该在具有pid
2和arrival
10 的进程之前,即使具有pid
1 的进程最初在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)