how should I call an external program from inside a C program - c

I am currently working on moving some bash scripts over to C, and one of them call's an external python script, so for instance ./remoteclient01a.py "remotecommand player /something something"
./remoteclient01a.py "remotecommand player /something something"
and Ive been looking for a way to execute this command in C, but Im not really sure as to which I should use, being that System() seems to be the best one, but then a few pages say that it is a bad choice in some cases, so if someone could recommend a method to do this I would really appreciate it, thanks!

The only portable ways to do this in C are system and popen. The function popen allows you to read output from the command.

For simple problems in unixish environments try popen().
From the man page
The popen() function opens a process by creating a pipe, forking and involking the
shell.
There are other means to call an external program in C. See the following tutorial explaining all the ways :
Executing programs with C(Linux)

Related

How do I run another C program within my C program?

(All programs running on Linux and through command line) So I want to be able to run a compiled C program from within my own C program and then be able to read the output of that compiled C program and have my C program provide input for that program. (Also assume I only have the compiled version of the other C program). I am aware of the system() function, but I am not sure if I can use that to read what the compiled Program outputs and then provide my own input.
You need to setup an inter-process communication mechanism between your two programs. There are several options to implement this:
Shared memory / shared files
Message Queues
Sockets
Pipes
You did not give a lot of background on your problem, but from your description it sounds that both your processes run in parallel and exchange data more than once. In this case, you will also have to think about synchronization issues.
A helpful introduction can be found here.
You can use system() like this:
system("executable arg1 ... > file.txt");
arg1 ... is the argument list to the executable. file.txt is the output of the executable redirected to a file. This system call can be made in a forked process. The parent program can wait on it. Synchronization tools can be used to enable concurrency if needed on the file file.txt.

Why should I use fork() for making simple shell in C

I don't know why I need to use the fork() function.
I already make many functions like cat, cd, mkdir, ps, ls ...
But, when I saw other's code, many people use fork() function.
I know what it is and how to work is. But I don't understand why this function is needed to make simple shell.
I want to know how to use this function to make simple shell.
This is a design.
People are not using fork(), but fork()-exec() pattern to make a new process. It sounds to me that you make your shell understand cat, cd, etc. and I have an impression that you created not a shell, but a REPL program. That is fine. Bash, for example, also have some commands working in this way (such as, functions you defined, "bg", etc.) but you limited the functionality to your shell to only the stuff you implemented.
A fork-exec, however, is to allow your shell to run a program. And more importantly, the program being run will not mess with your shell. For example, no segmentation fault on the program can stop your shell from running. In my opinion, this part is the more important aspect of a shell.

Perl system() requires stdout flush

I have a problem similar to bash script flush but would appreciate confirmation and clarification:
I have a console-based, interactive perl script that writes to stdout. Along the way, it uses system() to call a C program, and its output (also to stdout) is massaged by the script and output to the user.
On Windows there have been no issues. But on Linux, in one very specific and very repeatable instance, only the trailing part of the program's message arrives to the script. Found that by adding fflushes in the C program, the issue goes away.
EDIT: Further testing reveals that a single fflush just before returning has cleared up the issue, at least for this specific instance. So this point of this post is now where the community thinks the issue is occurring, so I can avoid it in other situations in the future...
Is it the interplay between the script and the C program, both of which use stdout? Or is it that system() in perl creates a sub-shell to execute the program, and maybe the issue arises there?
If the former: is fflush the best option? Just one call seems quite reasonable in terms of overhead.
If the latter, could stdbuf somehow be used to alter the stdout behavior of the sub-shell running the program, especially if it's a program that can't be rewritten?
Thanks!
I've tested, and found the issue can be resolved with either of these approaches:
Add fflush at the end of the C program.
Run the C program without fflush using (in perl) `stdbuf -oL myprog` (I had incorrectly referenced system() before, sorry).
Some commenters refer to the fact that C should flush the buffer on exit, but CERT vulnerability FI023.C is specifically about unflushed data in stdout using C. When capturing stdout using perl tickmarks, I'm left wondering if that's a sort of redirection where this situation can occur.

change terminal directory using c language

i am trying to change the directory of my linux terminal using c language. the thing is i am using threads. i have tried to use chdir() but its not working, n yes chdir() is also a process function.
Actually the thing i am trying to implement is, i am trying to make a multi threaded program which compiles Linux kernel, and that is not possible without specifying directories( i have tried to do it without specifying directories but failed :) )
can anybody help me out with this issue?
Thanks in advance :)
You can't! No process can change the working directory of another. A process can only change its own WD. When you launch an external command such like your C program, then a new process is launched.
The cd command of your shell is an internal one.
It isn't very clear what you are trying to achieve. It appears that somehow you wish to emulate the behaviour of a script (or the make tool) using a C program, that is to say having the C program you wrote launch new processes (using system or perhaps a combination of fork and exec*). If that is the case, then what you actually want is to modify the environment variables of these processes for them to find the files they need. A forked process will inherit the environment of its parent, so all you need is to use getenv, putenv, setenv and unsetenv to retrieve and set the environment variables you want to add, update or remove; or you may use the specific execve, execvpe which let you specify exactly what environment should be available to the new program.
references:
fork system call
exec family of system calls
unix environment

Write a dispatch program in c to run other c programs in Linux

I would like to write a small program that will run other programs. I'm not just trying to get their output as stdio for the current process, but rather want to simply use the program to use as a dispatch program.
I don't want to compile them together, but rather keep all the different programs separate.
I'm assuming that using a shell script would be the normal way of doing this, but I specifically want to know how it would be done in C on Linux.
You could do something like fork and use execve.
I'm not entirely understanding the problem though. Do you need the dispatcher to be able to read the output of the dispatched program?
You can use the system() API to call these other programs. What system() does is actually forks a shell and runs the program in that shell.
You can specify arguments to these external programs and even check their return status.
"man system" is your friend

Resources