IPC in Fortran? - c

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

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.

executing slow process with popen()

I'm creating a simple network scanning function using nmap and C. I want to use popen() to execute nmap, but nmap takes close to 30 seconds to complete because I'm scanning a wide range of IPs.
So, is there a way to check when the command has finished executing? I don't want my C code to hang at the system call, instead I would like to just check a flag or something in a loop that will know when popen/nmap has finished so other parts of my C program don't come to an halt. Is this possible??
Thanks!
I can think of 2 direct ways to do it
You could fork() directly and then establish a way to communicate the two processes, this would be very difficult because IPC is not an easy matter.
You could create a new thread with pthread_create() and call popen() there, it would be a lot easier and you could share data between threads by using an appropriate locking mechanism to prevent race conditions.
You can either use multi processing with fork (hardmode)
Or you can use multithreading using pthread (easymode)
Either one allows you to do 2 things at once. Multiprocessing is hard because you must worry about innerproccess communications (pipes) and the 2 tasks you're trying to do can not share memory.
Multithreading is a much more easy because all you need is to include the libraries (-lpthread) and then specify what function is on the seperate thread.

Can we pass variables from one c program to another c program?

So I want to pass a variable from one c program to another c program.
For example:
main()
{
char str[]="Hello,there!";
system("program2.exe");
}
And I want to use str[] in program2.exe. Is there a way to pass a variable to another program?
I used files to write data from first program and read data from second program but I want to know is there any other way to do this?
Is it good to use files for passing data from program to another?
You can't literally pass a variable between two processes because each process on a system will generally have it's own memory space - each variable belongs to a process and therefore can't be accessed from another process (or so I believe). But you can pass data between processes using pipes.
Pipes are buffers that are implemented by the OS and are a much more effective method of sharing data between processes than files (yes, you can use files for inter-process communication). This is because files must be written to a disk before being accessed which makes them slow for inter-process communication. You'd also have to implement some kind of method for ensuring the two processes don't corrupt a file when reading and writing to it.
Also, pipes can be used to ensure continuous communication between two processes, making them useful in many situations. When using half duplex pipes (linked above), you can have a pipe for each process to establish a communication channel between them (i.e. a one way communication channel for each).
you can:
1) pass arguments to a program.
2) use sockets to communicate between processes.

is there another way than system() to execute a binary

I develop a C code on Linux and I would like to execute a binary say /usr/sbin/binary_program -p xxx, Is there another way than system() call to execute a binary?
Yes, and in general, system should never be used, for at least these reasons:
It suffers from all the dangers of shell quoting issues, so anything but a hard-coded command line is potentially dangerous.
It is not thread-safe.
It interferes with signal handling in the calling program.
It provides no way to get output from the executed program except for the exit status, unless the command explicitly saves output to a file.
For executing external programs, you should use posix_spawn, or fork followed by one of the exec-family functions. However, if possible you should avoid dependency on external programs/commands, especially when it would be easier and less error-prone to do the work directly in your program. For example I've seen ridiculous usages like system("sleep 1"); instead of sleep(1);.
Yes, you can use the exec* family of functions.
http://pubs.opengroup.org/onlinepubs/9699919799/functions/execv.html
If needed to simulate the behavior of system you can fork and then call an exec function.
The POSIX page of system says:
The system() function shall behave as if a child process were created using fork(), and the child process invoked the sh utility using execl() as follows:
execl(< shell path>, "sh", "-c", command, (char *)0);
It is important to realize that you can have several programs running simultaneously, and communicating thru pipes (or others Inter Process Communication). This is mostly possible thru a mixture of syscalls.
I strongly suggest reading Advanced Linux Programming, or some other good books explaining a lot more (than we can do in a few minutes) about various syscalls(2) involved, notably fork(2), pipe(2), dup2(2), execve(2), waitpid(2) and several others (perhaps poll(2) for multiplexing, e.g. to avoid deadlocks in circular pipes). The system(3) function is built above these syscalls (and /bin/sh)
That Advanced Linux Programming book has an entire chapter devoted to processes.
I also suggest to understand how a Unix command shell works. Either by studying the source code of some simple free shell (like sash) or at least by strace-ing it.
Practically speaking, popen(3) is more useful then system(3). You can get the output of the companion command.
Some libraries (Poco, Qt, Glib/GTK) also have powerful process management functions.
A new process is created with fork which is tricky to understand. A new program is started in the same process with execve.
All processes are created by fork (or perhaps vfork) except some few started magically by the kernel (/sbin/init, /sbin/modprobe, ...)

How to communicate between Ruby process and C process?

There is a C process, I need to call one function which will modify one C variables by Ruby program, I want to know how to do this, thanks!
Why don't you use sockets?
You could even use threads or forks to avoid locking.
If you are on linux you should check out D-Bus, it allows communication through sockets too, but with some extra goodies ;)

Resources