【发布时间】:2015-06-26 15:39:14
【问题描述】:
我创建了一个字符串数组来为命令添加属性(例如ls -l),但exec 命令需要一个字符数组,我该如何解决?这段代码应该创建一个执行输入选择的命令的子进程。当pid != 0 时,我什至对wait() 有一些问题。你能帮我完成它吗?非常感谢。
int main(void) {
char array[100];
char character;
int i = 0;
char* point;
int j = 0;
printf ("Digit a string");
printf ("\n");
do {
character = getchar();
array[i] = character;
i++;
}
while (character != '\n');
array[i-1] = '\0';
i = 0;
char* string[100];
char *word = strtok(array, " .");
while (word != NULL) {
printf("%s\n", word);
string[j] = word;
word = strtok(NULL, " .");
}
printf ("\n");
pid_t pid;
pid = fork();
if (pid == -1) {
perror("");
}else if (pid == 0) {
if (execlp(string, NULL) < 0) { /* execute the command */
exit(1);
}
else {
//.. wait until the child ends
wait(pid);
}
}
return;
}
【问题讨论】:
-
您为什么不尝试阅读
wait()的联机帮助页?这应该很容易回答您的部分问题。
标签: c exec system-calls pid