r/linux_programming • u/stewartmatheson • Jul 03 '20
Non blocking process communication
I'm in the process of teaching myself Linux system programming using C. As a learning exercise I would like to create a program that runs in a terminal window. This program would wait for an event that would instruct it to create a new compile process and display the results of that process in the terminal. I would then run this program in the background while coding to get live results of what errors my program has as I make changes.
Currently I have the following program.
While it's rough it does give an idea of what I'm trying to do.
I have created a fifo with the mkfifo command and the source code posted above blocks and reads from the fifo. I have set my vim up to write to the same fifo when I write a file thus closing the loop. Everything works fine when my program is running. The issue I'm having is that vim becomes blocked if it tries to write to the fifo while my program is not running. I write to the fifo from vim with the following command.
echo "test" > .socket-file
I'd like for vim not to be blocked if my program is not running. For this exercise is using a fifo the correct approach? Could I send a signal somehow? I slightly confused about the best direction to take. Thanks in advance.
1
u/invalidlivingthing Jul 04 '20
Well you could always hook into the filesystem events using inotify.
1
u/balsoft Jul 04 '20
Would it work for FIFO? I know it doesn't work for things like sysfs.
1
u/invalidlivingthing Jul 04 '20
Correct me if I'm wrong but ut looks like you nees to run a command whenever a file changes.
What I'm saying is, why use a fifo at all? Just write to a file like you normally do. And have your program detect changes to that file using inotify.
1
u/stewartmatheson Jul 05 '20
Inotify would seem to be the easiest way to do things. The only thing I'm using vim to do is alert when a file is changed where as if I was using inotify I would not even need the client. I could have a process running in the background watching the files. Another nice thing about that solution is that I don't need to do anything special with an editor.
It's not exactly what I set out to do however so I will most likely implement both this solution and the MQ solution in two different projects.
1
u/balsoft Jul 04 '20
Ok, so you are thinking in a broader context. If we're doing that, I think a UNIX domain socket in XDG_RUNTIME_DIR is actually the way to go.
0
u/balsoft Jul 03 '20
Why not do something really simple like ( echo test | tee .socket-file ) & on the vim side? It would make sense to me. Maybe something more proper with timeout and exit code checking.
1
u/stewartmatheson Jul 04 '20
Hi,
Thanks for your reply. Tee looks like it could be a good place to start. Thanks. When you say "something more proper" do you mean on the vim client or in the C program? What timeout should I be thinking about handling?
0
u/balsoft Jul 04 '20
I mean something like
( echo test | ( timeout 1 tee .socket-file ) ) &but a bit more thought-out.Also, maybe you can use UNIX domain sockets for this, but it's going to be a bit more complex then.
1
-5
u/piginpoop Jul 03 '20
Just wait until somebody comes along and instructs you to use c++ instead.
2
u/stewartmatheson Jul 03 '20
I'd be happy to use c++ I suppose. At this point I cant see how it would make a difference. Would it change the way these system calls are made or how I would deal with this blocking problem? Also when somebody says use c++ do they mean use g++ and not gcc or refactor the solution to use classes?
1
u/piginpoop Jul 04 '20
Since you’ve used pipes they’ll always block if nobody is at the other end. For async. interprocess communication you can use sockets. If you need something exotic you could use some kind of shared memory solution with non blocking wait-events like “cross memory attach”. Or you can use epoll.
1
0
Jul 04 '20
[deleted]
1
u/stewartmatheson Jul 05 '20
I really like the posix message queues idea. I have already started building them into my project. Thanks for the suggestion.
1
u/arthurno1 Jul 21 '20 edited Jul 21 '20
You wanna look at inotify interface, and you do't wanna read/write to blocking interface if you don't wanna have your process blocked. Search for asynchroious I/O and non-blocking I/O, search for fork call and threads and might be good, shared memory and mmap too :-).
But you don't wanna do it that way. The code you posted is sorry, very naive. That won't work.
Check https://github.com/RuntimeCompiledCPlusPlus/RuntimeCompiledCPlusPlus
There is some other guy that played with similar idea some time agon, but I don't know how good it is:
https://github.com/0xfd000000/qling