C中的进程循环
process loop in C
我正在尝试编写有关使用 execlp() 命令从 $PATH 执行程序的进程的代码。(它不一定是 execlp 命令,但我发现它对这个命令很有用) 我已经达到了预期的输出,但我需要 运行 多个命令。更具体地说,我希望子进程 运行 执行命令,然后父进程打印一条文本,表明它已准备好接受另一个命令。然后子进程将 运行 新的 exec 命令。我的代码是这样的:
int main ( int argc, char *argp[]) {
pid_t progpid = fork(); // the fork command for the creation of the child process
int status = 0;
char com[256];
if (progpid < 0) // the check in case of failure
{
printf("PROGRAM ABORTED!");
return 0;
}
do
{
if (progpid == 0) // the child process
{
scanf( "%s", com);
if (com == "exit")
{
exit(0);
}
else
{
execlp(com, com, NULL);
}
}
else //the parent process
{
wait(&status);
printf("$");
}
}while (com != "exit");
return 0;
}
预期输出为:
<program which I input from keyboard> ( for example : ls )
<output of the program>
$<next program>
<output of the next program>
.
.
.
$exit
简而言之,我想保留 运行ning 程序,直到我进入 exit 结束,而不做任何其他事情。但是我得到的输出是这样的:
<program>
<output of program>
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
它一直打印 $ 直到我关闭它。我是流程的新手,所以到目前为止请不要对我的代码太苛刻。
提前致谢!
这个
if (com == "exit")
应该是
if (strcmp(com, "exit") == 0)
同样更改 while
条件。
在 C 中,字符串 比较是使用 strcmp()
完成的。 ==
在您的例子中,只需比较 com
的地址和字符串文字 "exit"
的地址。 (在表达式中,数组被转换为指向其第一个元素的指针。因此,"address" 比较。另请参阅:What is array decaying?)。
请注意,您的 execlp()
通话有问题。 NULL 可以定义为 0,在这种情况下 execlp()
,作为可变参数函数,可以将其识别为最后一个参数。
我建议将其更改为:
execlp(com, com, (char*)0);
您还想通过检查其 return 代码来检查 wait()
是否失败。
这是一个基于您的改进错误检查的简单示例。
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int main ( int argc, char *argp[]) {
for(;;) {
char com[1024];
printf("$ ");
fgets(com, sizeof com, stdin);
com[strcspn(com, "\n")] = 0; /* Remove if there's a newline at the end */
if (strcmp(com, "exit") == 0) {
exit(0);
}
pid_t pid = fork();
if (pid < 0) {
perror("fork");
exit(1);
}
if (pid == 0) { /* child process */
execlp(com, com, (char*)0);
}
int status;
int rc = wait(&status);
/* You can inspect 'status' for further info. */
if (rc == -1) {
perror("wait");
exit(1);
}
}
return 0;
}
请注意,如果您希望执行带参数的命令,则需要进行参数处理。
我正在尝试编写有关使用 execlp() 命令从 $PATH 执行程序的进程的代码。(它不一定是 execlp 命令,但我发现它对这个命令很有用) 我已经达到了预期的输出,但我需要 运行 多个命令。更具体地说,我希望子进程 运行 执行命令,然后父进程打印一条文本,表明它已准备好接受另一个命令。然后子进程将 运行 新的 exec 命令。我的代码是这样的:
int main ( int argc, char *argp[]) {
pid_t progpid = fork(); // the fork command for the creation of the child process
int status = 0;
char com[256];
if (progpid < 0) // the check in case of failure
{
printf("PROGRAM ABORTED!");
return 0;
}
do
{
if (progpid == 0) // the child process
{
scanf( "%s", com);
if (com == "exit")
{
exit(0);
}
else
{
execlp(com, com, NULL);
}
}
else //the parent process
{
wait(&status);
printf("$");
}
}while (com != "exit");
return 0;
}
预期输出为:
<program which I input from keyboard> ( for example : ls )
<output of the program>
$<next program>
<output of the next program>
.
.
.
$exit
简而言之,我想保留 运行ning 程序,直到我进入 exit 结束,而不做任何其他事情。但是我得到的输出是这样的:
<program>
<output of program>
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
它一直打印 $ 直到我关闭它。我是流程的新手,所以到目前为止请不要对我的代码太苛刻。 提前致谢!
这个
if (com == "exit")
应该是
if (strcmp(com, "exit") == 0)
同样更改 while
条件。
在 C 中,字符串 比较是使用 strcmp()
完成的。 ==
在您的例子中,只需比较 com
的地址和字符串文字 "exit"
的地址。 (在表达式中,数组被转换为指向其第一个元素的指针。因此,"address" 比较。另请参阅:What is array decaying?)。
请注意,您的 execlp()
通话有问题。 NULL 可以定义为 0,在这种情况下 execlp()
,作为可变参数函数,可以将其识别为最后一个参数。
我建议将其更改为:
execlp(com, com, (char*)0);
您还想通过检查其 return 代码来检查 wait()
是否失败。
这是一个基于您的改进错误检查的简单示例。
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int main ( int argc, char *argp[]) {
for(;;) {
char com[1024];
printf("$ ");
fgets(com, sizeof com, stdin);
com[strcspn(com, "\n")] = 0; /* Remove if there's a newline at the end */
if (strcmp(com, "exit") == 0) {
exit(0);
}
pid_t pid = fork();
if (pid < 0) {
perror("fork");
exit(1);
}
if (pid == 0) { /* child process */
execlp(com, com, (char*)0);
}
int status;
int rc = wait(&status);
/* You can inspect 'status' for further info. */
if (rc == -1) {
perror("wait");
exit(1);
}
}
return 0;
}
请注意,如果您希望执行带参数的命令,则需要进行参数处理。