如何编写一个C程序来执行另一个程序?

o0lyfsai  于 2023-05-16  发布在  其他
关注(0)|答案(6)|浏览(231)

在Linux中,我想写一个C程序来启动另一个程序。当程序运行时,shell将等待您输入在程序中定义的命令。此命令将启动第二个程序。
例如,假设在调用程序所在的目录中有一个名为“hello”的简单C程序。“hello”程序输出“hello,world”。运行第一个程序,用户输入命令“hello”,“hello”程序执行,“hello,world.”输出到shell。
我做了一些搜索,人们建议使用“fork()”和“exec()”函数。其他人说使用“system()”。我对这些功能一无所知。如何调用这些函数?它们是否适合使用?
带有解释的示例代码将是最有帮助的。其他答案也是受欢迎的。非常感谢你的帮助。

3xiyfsfu

3xiyfsfu1#

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> /* for fork */
#include <sys/types.h> /* for pid_t */
#include <sys/wait.h> /* for wait */

int main()
{
    /*Spawn a child to run the program.*/
    pid_t pid=fork();
    if (pid==0) { /* child process */
        static char *argv[]={"echo","Foo is my name.",NULL};
        execv("/bin/echo",argv);
        exit(127); /* only if execv fails */
    }
    else { /* pid!=0; parent process */
        waitpid(pid,0,0); /* wait for child to exit */
    }
    return 0;
}
ssgvzors

ssgvzors2#

如果你是fork的新手,关于fork和exec的图形表示可能对你有帮助。

fork()的描述

+-------------+  
  |main program |  
  +-------------+    (fork())
        |___________________________  
  +-------------+                   |
  |main program |           +-------------+ 
  +-------------+           |main program |
        |                   +-------------+  
  +-------------+                   |        (exec())
  |main program |           +-------------+
  +-------------+           |hello program|
                            +-------------+

正如您可能已经在教程中读到的那样,在调用fork()之后,将创建现有程序的副本,然后exec()将该副本替换为新程序,并将其作为参数传递给它。两个程序的两个执行单元将在fork()之后运行。

lymnna71

lymnna713#

system()对你来说还不够吗?

/* ... */
if (!strcmp(cmd, "hello")) system("hello.exe");
/* ... */
nafvub8i

nafvub8i4#

对于最简单的情况,您应该在一个目录中包含两个已编译的程序:

> ls
.
hello
second

在第二个程序中,你只需要调用system("hello");

kkih6yb8

kkih6yb85#

我做了一些搜索,人们建议使用fork()exec()函数。其他人说使用system()。我对这些功能一无所知。如何调用这些函数?它们是否适合使用?
是的。请先阅读文档(man页),例如:fork(2)exec(3)system(3)的。很可能您的计算机上本地就有该文档,使用的是man(1)。请注意,system使用sh(通过bash(1)dash(1)),因为它是fork-ing,execve(2)-ing和waitpid(2)-ing /bin/sh POSIX shell。
我认为**fork很难理解**,因为成功时它返回“两次”。我不会在这里解释它(我需要很多页)。我建议先阅读fork (system call)维基百科。然后,阅读一些好的Linux编程书籍,例如Advanced Linux Programming(免费下载)。
阅读Virtual Address Spaceproc(5)
您也可以阅读Operating Systems: Three Easy Pieces以获得更一般的视图。

lpwwtiir

lpwwtiir6#

问题的答案取决于在执行第二个(或子)程序后是否要在父程序中执行其他语句。
如果你想在执行第二个(或子)程序后在父程序中执行额外的语句,我会使用system()
如果你不需要在执行第二个(或子)程序后执行父程序的其他语句,我会使用execv()。不会创建第二个进程。父程序将终止,第二个程序将使用与父程序相同的进程ID运行。
P.S.在这种情况下,您也可以使用system()。但是如果您使用system(),将创建两个具有不同进程ID的进程-一个进程ID用于主程序,另一个进程ID用于第二个程序。
在您的示例中,可以使用system()函数或execv()函数。
下面是一个简单的使用system()的不言自明的示例:

#include <stdlib.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
    printf("Hello from the parent process\n");

    if ( system("echo 'Hello from the child process' > test.txt") != 0 )
    {
        printf("ERROR executing system() command\n");
    }

    if ( system("cat test.txt") != 0 )
    {
        printf("ERROR executing system() command\n");
    }

    printf("Executing from the parent process again!\n");
}

下面是输出:

Grinchs-MBP:Downloads rob$ ./parent3
Hello from the parent process
Hello from the child process
Executing from the parent process again!

如果您打算使用system(),请记住包含stdlib.h头文件。
如果你不需要在子程序或第二个程序完成后在父程序中执行额外的语句,我会在父程序中使用execv()
在这种情况下,您不需要使用fork()创建一个fork。你只需要使用execv()。如果您想使用execv(),请记住包含unistd.h头文件。
execv()被调用时,调用它的进程被终止,并被分配的内存部分中具有相同ID的新进程替换。
如果execv()成功执行,它不会返回。如果它返回,那是因为发生了错误。换句话说,execv()应该是你打算从父程序执行的最后一条语句,因为execv()只会在出错的情况下返回到你的父程序。
同样,如果你想执行父程序中的另一个程序,并且想在第二个(或子)程序完成后执行父程序中的其他语句,那么我会使用system()
下面是一个使用execv()的简单示例:

上级流程编码

#include  <stdio.h>
#include  <unistd.h>
#include  <errno.h>

int main ()
{
printf ("I am the parent process\n\n");

char *arg_Ptr[4];
arg_Ptr[0] = "child.c";
arg_Ptr[1] = "Hello from the child process";
arg_Ptr[2] = "Good-Bye from the child process!";
arg_Ptr[3] = NULL;

execv("/Users/rob/Downloads/child.bin", arg_Ptr);
printf ( "Error:  %i\n", errno);
}

下级流程编码(保存在/Users/rob/Downloads/child. bin)。

#include  <stdio.h>

int main(int argc, char *argv[])
{
 printf ("I am the child process\n");

 printf ("Argument 1: %s\n", argv[1]);
 printf ("Argument 2: %s\n", argv[2]);
 printf ("Argument 3: %s\n", argv[3]); 
}

输出如下

Grinchs-MBP:Downloads rob$ ./parent 
I am the parent process

I am the child process
Argument 1: Hello from the child process
Argument 2: Good-Bye from the child process!
Argument 3: (null)

相关问题