简体   繁体   English

C:不带main的exec二进制文件,带有exec *()syscall

[英]C: exec binary without main with exec*() syscall

I have a question about exec() and fork() syscalls. 我对exec()和fork()系统调用有疑问。 what I'm trying to get done: a little program where there are 3 "main" processes, one of them execute a fork() N times (user choice) and every child send messagges to the other 2 procs in order to sync and do some stuff. 我要完成的工作:一个小程序,其中有3个“主”进程,其中一个执行N次fork()(用户选择),每个孩子将消息发送给其他2个proc,以便进行同步和做一些事情。

So: main.c: creates queue, sem and other ipc stuff. 因此:main.c:创建队列,sem和其他ipc内容。 Then fork (2 times) and exec(p1)/(p2). 然后派生(2次)和exec(p1)/(p2)。 If everything is ok, it starts to fork N times, on user choice. 如果一切正常,根据用户的选择,它会开始分叉N次。

p1.c: attach to ipc created by main, wait for messagges from one of the child of "main.c" p1.c:附加到由main创建的ipc,等待“ main.c”子项之一发送的消息

p2.c: attach to ipc... wait for messagges from one of the child of "main.c" or form "p2.c" p2.c:附加到ipc ...等待来自“ main.c”的子级之一的消息或形成“ p2.c”的形式

What i'm doing right now: I wrote 3 C files, each one with a main function, the "first main" reserve all IPC structure and then fork and execute and execve() in order to start the other 2 program. 我现在正在做什么:我编写了3个C文件,每个文件都有一个main函数,“第一个main”保留所有IPC结构,然后进行fork和execute和execve()以便启动其他2个程序。

Everything seems ok, the first one run and fork correctly, the execve() runs the other 2 programs and these can attach to ipc structure created by first program. 一切似乎正常,第一个正确运行并派生,execve()运行其他两个程序,这些程序可以附加到第一个程序创建的ipc结构。 Wonderful, but I have 3 program, 3 main and of course I cannot build only one executable file. 很棒,但是我有3个程序,3个主程序,当然我不能只构建一个可执行文件。

So my question: how can I load (if is possible) a program with syscall exec without a main? 所以我的问题是:如何在没有主程序的情况下使用syscall exec加载(如果可能的话)程序? If is not possibile, how should I write the structure of my program? 如果不是可能的话,应该如何编写程序的结构?

I hope is clear, if needed I can write the code, but right now there a few lines.. 我希望很清楚,如果需要的话我可以编写代码,但是现在有几行。

Thanks!! 谢谢!!

用main_p1和main_p2替换main,然后简单地调用它们而不是调用exec。

exec* replaces the current process image with another new program. exec*用另一个新程序替换当前过程映像。 So those other programs must have a main if written in C. 因此,如果使用C编写,则其他程序必须具有main

You can fork your current process without calling exec* however. 但是,您可以在不调用exec*情况下fork当前进程。 So you could link all three "programs" into one binary, and simply call into the other ones after forking. 因此,您可以将所有三个“程序”链接到一个二进制文件中,然后在派生后直接调用其他程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM