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

(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.

Related

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

how to buffer and delay printf() output?

I wrote a C program and in the program there are many printf() which output log information to stdout. Now I want to use multiple processes to run the program simultaneously with different arguments. And I want to redirect the output from stdout to a log file using >.
But multiple processes are running at the same time, their log information output overlap with each other, which can be confusing for future analysis.
one solution is: considering that different processes will exit at different time,modify the C program, so each log information is temporarily written into a temporal file. When the C program is about to exit. Read from the temporal file and write the content to stdout, this requires a lot of modification.
My idea is: I hope in the C program, all the printf() output can be buffered, the outputs put into stdout/redirection only when the process exits.
is it possible or not?
thanks!
This is not really possible, unless you are sure that the output is reasonably bounded (e.g. the total output is less than a few megabytes), otherwise use a logging mechanism which send to some central logger (like syslog).
On Linux and most Posix systems, the simplest way to do logging would be to use syslog(3) which is designed for logging (and is able to deal with different processes). I think this is the preferable approach.
With GNU libc, you could consider using open_memstream(3) -to write to memory, and here you need to be sure the total output is bounded- and use atexit(3) to have the memory stream written at the exit of the program into some file; you probably want to use some locking mechanism like flock(2) etc...
As commented by J.Holetzeck the simplest way is to redirect output into different files (perhaps using freopen(3), or simply in the invoking shell), and later merge these files.
I'm guessing you use Linux, or some Posix system. For Windows, I have no idea.

IPC in Fortran?

I am trying to do IPC between a Fortran and C program. So far I have found good documentation for IPC in Linux using C but can't find any help with Fortran.
Is this possible to do IPC between a Fortrans and then a Fortran and C program?
Thanks
B
You can from Fortran 2003 onwards.
In terms of Inter Process Communication, you use objects like pipes. A pipe is basically a read,write,(or both) object and typically what happens is that programs create specially named pipes, and then each process will get the appropriate handle and then send and receive data to it, as if it was like a network connection.
EXECUTE_COMMAND_LINE runs a shell command, synchronously or asynchronously.
You could try the code found on this page:
http://www.jcameron.com/vms/fortran.htm
The programs are:
MAILBOX_A.FOR
MAILBOX_B.FOR

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

How can I capture another process's output using C?

How can I capture another process's output using pure C? Can you provide sample code?
EDIT: let's assume Linux. I would be interested in "pretty portable" code. All I want to do is to execute a command, capture it's output and process it in some way.
There are several options, but it does somewhat depend on your platform. That said popen should work in most places, e.g.
#include <stdio.h>
FILE *stream;
stream = popen("acommand", "r");
/* use fread, fgets, etc. on stream */
pclose(stream);
Note that this has a very specific use, it creates the process by running the command acommand and attaches its standard out in a such as way as to make it accessible from your program through the stream FILE*.
If you need to connect to an existing process, or need to do richer operations, you may need to look into other facilities. Unix has various mechanisms for hooking up a processes stdout etc.
Under windows you can use the CreateProcess API to create a new process and hook up its standard output handle to what you want. Windows also supports popen.
There's no plain C way to do this that I know of though, so it's always going somewhat dependent on platform specific APis.
Based on your edits popen seems ideal, it is "pretty portable", I don't think there's a unix like OS without it, indeed it is part of the Single Unix Specification, and POSIX, and it lets you do exactly what you want, execute a process, grab its output and process it.
If you can use system pipes, simply pipe the other process's output to your C program, and in your C program, just read the standard input.
otherprocess | your_c_program
Which OS are you using? On *nix type OS if you are process is outputting to STDOUT or STDERR you can obviously use pipes

Resources