How to communicate between Ruby process and C process? - c

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 ;)

Related

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.

C: How to make threads communicate with each other?

Interesting that this seems to be a basic question, and yet I couldn't find any example of it for the C language (in SO, I found only for Python, C# and C++).
The point is: as a Qt programmer, when I need to make some data to be transmitted between different threads, I start a signal-slot connection between then and use the emit signal mechanism to do the work.
But now I'm working in a C application for Embedded Linux where I need to do a similar work, but I don't have Qt's mechanism available. The question is: how can I make two or more threads communicate with each other in C in a manner similar to that of Qt with signals and slots?
I know that one of the ways to share data is with global variables with changes protected by mutexes. But even then I would probably be unable to do the system in a asynchronous way: I would have to have a loop that would constantly check if the variable has changed or not. But what if I want to execute a specific method of a thread just after another one finished some work (so, in an asynchronous way)? Then it seems such way fails.
Note: although I'm using Embedded Linux and, therefore, mentioning some options that would take POSIX functions and other "Linux-related ways" would be helpful, it would still be better for the community if more time is given to solutions that are not based strictly to one specific platform (if that is possible).
Read a good tutorial on pthreads. You want to know more about condition variables to be used with mutexes.
Condition variables and mutexes should probably be enough for your needs.
You could also use most traditional inter-process communication mechanisms between threads, e.g. a pipe(7) (probably with poll(2)...). So read Advanced Linux Programming and study syscalls(2) and pthreads(7)
Avoid using signal(7)-s between threads and be aware of signal-safety(7). See however signalfd(2), eventfd(2), userfaultfd(2) (you might cleverly handle SIGSEGV with it) and take inspiration from the approach suggested by Calling Qt functions from Unix signal handler.
Observe a running multi-threaded Linux process with strace(1), ltrace(1), gdb(1). You'll understand that several pthreads(7) primitives are using futex(7).
Both GNU glibc and musl-libc are open source and implement the pthreads specification (and Glib, GTK, Qt or POCO are built above them). I invite you to study their source code.
One way is to use message passing between threads via asynchronous queues. This way you can avoid using shared data between threads and only the queues need to be thread-safe.
Asynchronous queues can be implemented using different synchronisation primitives:
Pipes or sockets.
Queues protected with a mutex and a condition variable.
Non-blocking or lock-free queues.
Thread which you want to notify of an event like "data available" can register a callback function which can be trigerred by the notifier thread. You can use a function pointer for this.
Ex: Thread 2 registers a callback function for one or more events. Thread 1 on occurrence of the condition or event calls the registered function.
producer and consumer threads should capture each other's tid. producer on producing can send:
pthread_kill(consumerID, SIGUSR1);
consumer is setup with the signal handler for SIGUSR1, and can retrieve the produced result from the common std::queue saved by pthread_setspecific().
producer and consumer can continue their tasks without being locked by semaphore or cond var/mutex.

C Communication between process

My university professor has asked me to develop a project in C for Unix machines.
I should do a soccer championship emulator, in which there is a parent, and there are some child(every match between two teams).
The parent must create the matches, and the matches must tell the end result to the parent.
I think the best thing to do is to use fork() syscall and unnamed pipes.
What do you think about?
Thanks
Your suggestion above is valid. That approach would work. It might be easier to use a chunk of shared memory and mutex instead, but it's ultimately your call. I've included a working example that uses pthread_mutex calls and mmap in the references below that should get you up and running. Good luck!
References
C procs, fork(), and mutexes, Accessed 2014-04-29, <https://stackoverflow.com/questions/19172541/procs-fork-and-mutexes>

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

How does pthread work?

I am experienced at multithreaded programming in Java and C#, and am starting to learn how to do it in C on Linux. I "grew up" in the programming sense on Linux, so I understand it's memory philophy, process handling, etc. at a high level.
My question is not how to do threading. I would like to know how pthread actually does it. Does it fork a process and handle your interprocess communication for you somehow? Or does it just manage the address space? I want nitty-gritty details :) Googling has only produced "how to do it" questions, not "how it works".
The details are probably too complex to really get into (without posting a link to the glibc source code), but I can give you better things to look up:
Pthread uses sys_clone() to create new threads, which the kernel sees as a new task that happens to share many data structures with other threads.
To do synchronization, pthread relies heavily on futexes in the kernel.
On Linux, both fork() and ptrheads use the same syscall clone(), which creates a new process. The difference between them is simply the parameters they send to clone(), when creating a new thread, it simply makes both processes use the same memory mappings.
Remember, in Linux (and other modern Unixes), memory mappings, stacks, processor state, PIDs, and others are orthogonal features of a process; so you can create a new process with just a new stack and process state (sharing everything else), and call it a thread.
Here is the source of pthread.c. This may help you answer your question.

Resources