Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I execute an external commands in C/Linux without using system, popen, fork, exec?

Tags:

c

linux

fork

I would like to know if there is any good way to execute an external command in Linux environment using C language without using system(), popen(), fork(), exec()?

The reason I cannot use these functions is that my main application has used up most of the system resources (i.e memory) in my embedded board. If I do a fork, the board won't be able to create a duplicate of my main application. From I read in a book, both system() and popen() actually using fork() underneath, so I cannot use them either.

The only idea I currently have is create a process before I run my main application and use IPC(pipe or socket) to let the new process know what external commands it needs to run with system() or popen() and return the results back to my application when it is done.

like image 602
SSC Avatar asked Mar 22 '23 14:03

SSC


1 Answers

You cannot do this. Linux create new process by sequential call to fork() and exec(). No other way of process creation exists.

But fork() itself is quite efficient. It uses Copy-on-Write for child process, so fork() not copy memory until it is really needed. So, if you call exec() right after fork() your system won't eat too much memory.

UPD. I lie to you saying about process creation. In fact, there is clone() call which fork() uses internally. This call provides more control over process creation, but it can be complicated to use. Read man 2 fork and man 2 clone for more information.

like image 105
JIghtuse Avatar answered Mar 24 '23 11:03

JIghtuse