如何在 C 中运行多个 execlp()?

发布于 11-25 08:12 字数 643 浏览 3 评论 0原文

我正在尝试使用 execlp() 按线性顺序运行 openssl 的多行:

execlp("openssl","genrsa","-out","rsaprivatekey.pem","2048",(char*) 0);
printf("RSA private success");
execlp("openssl","rsa","-in","rsaprivatekey.pem","-pubout","-out","rsapublickey.pem",(char*) 0);
printf("RSA public success");
execlp("openssl","dgst","-sha1","-sign","rsaprivatekey.pem","-out","1.cipher","1",(char*) 0);
printf("SHA1 sign success");
execlp("openssl","dgst","-sha1","-verify","rsapublickey.pem","-signature","1.cipher","1",(char*) 0);
printf("SHA1 verify success");

在这种情况下,仅执行第一行。我尝试将所有内容组合在一个 execlp() 中并使用 &&分离命令但仍然没有结果。 有人可以帮我解决这个问题吗?

I am trying to run multiple lines of openssl in linear order using execlp():

execlp("openssl","genrsa","-out","rsaprivatekey.pem","2048",(char*) 0);
printf("RSA private success");
execlp("openssl","rsa","-in","rsaprivatekey.pem","-pubout","-out","rsapublickey.pem",(char*) 0);
printf("RSA public success");
execlp("openssl","dgst","-sha1","-sign","rsaprivatekey.pem","-out","1.cipher","1",(char*) 0);
printf("SHA1 sign success");
execlp("openssl","dgst","-sha1","-verify","rsapublickey.pem","-signature","1.cipher","1",(char*) 0);
printf("SHA1 verify success");

In this case only the first line gets executed. I tried combining everything in one single execlp() and use && to separate the commands but still no results.
Can someone help me out of this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等风也等你2024-12-02 08:12:31

原因是“execlp()”将当前程序替换为您希望“exec()”的程序。

建议:
如果您希望当前程序持续存在(至少足够长的时间来调用“openssl”的多个实例,则使用“system()”之类的内容而不是“execlp”。

否则,请考虑使用 shell 脚本或 .bat 文件来调用多个命令,然后“execlp”您的脚本(使用适当的“bash”或“cmd.exe”)。

The reason is that "execlp()" REPLACES your current program with the one you wish to "exec()".

SUGGESTIONS:
If you want your current program to persist (at least long enough to call multiple instances of "openssl", then use something like "system()" instead of "execlp".

Otherwise, consider using a shell script or .bat file to invoke the multiple commands, then "execlp" your script (with the appropriate "bash" or "cmd.exe") instead.

还在原地等你2024-12-02 08:12:31

如前所述,仅执行第一个命令的原因是 exec() 函数系列的任何成员在成功时都不会返回 - 仅在失败时返回。

如果您希望按顺序完成操作,那么使用 system() (如前所述)是最简单的机制。如果您想要并行性,或者需要对 I/O 重定向进行更多控制,那么您需要构建 fork() 机制。

每次成功调用 fork() 时,它都会返回两次 - 一次在父进程中,一次在子进程中。这些在其他方面非常接近相同;主要区别在于 PID 和 PPID(进程 ID 和父 PID)。在子进程中,fork() 返回零;因此,您可以检测到您的进程应该运行相关命令。在父进程中,fork() 返回新子进程的 PID,可以保存该 PID 以供以后使用 wait()waitpid() 并指示父级应该继续其工作(例如,为其他步骤生成更多子级)。

As already stated, the reason that only the first command is executed is that any member of the exec() family of functions never returns when it is successful - only on failure.

If you want the operations to be done sequentially, then using system() (as already advised) is the simplest mechanism. If you wanted parallelism, or if you needed more control over I/O redirection, then you would need to build on the fork() mechanism.

Each time you call fork() successfully, it returns twice - once in the parent process, once in the child process. These are otherwise very close to identical; the main difference is in the PID and PPID (process ID and parent PID). In the child, fork() returns zero; you can therefore detect that your process should run the relevant command. In the parent, fork() returns the PID of the new child process, which can be saved for later use with wait() or waitpid() and to indicate that the parent should continue with its work (for example, spawning more children for the other steps).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文