【发布时间】:2015-12-06 23:12:50
【问题描述】:
如果用户键入“ls”,execvp 会在屏幕上显示“ls”的结果。我想将它作为字符串存储在 char 数组中。谁能帮我? 提前致谢。
int main () {
char response[1000];
char *buffer[100];
int pid, status;
printf("Please enter the shell command: ");
scanf("%s",&response);
pid = fork();
if (pid < 0) {
printf("Unable to create child process, exiting.\n");
exit(0);
}
if (pid == 0) {
printf("I'm the child.\n");
*buffer = response;
execvp(*buffer,buffer);
printf("execvp failed\n");
}
else{
wait(&status);
exit(0);
}
}
【问题讨论】:
-
看看
pipe()和dup2()。 -
你能提供一个好的链接来阅读吗?我在谷歌上搜索了一个多小时,越来越困惑。