C - running program accept input - c

This is a very beginner-level question in C.
Don't know where to start looking/searching.
So, if I have a program continuously running in C, what is the best way to accept input through the command line into the program?
EX, mysql is already running, but you can process a command call
mysql SELECT * FROM *
Do I need a different program to write to file/stdin?enter code here
Clarification:
So, mysql seems to be able to take in commands while it is already running... is that possible in C?
Goal:
I have some hooks into open gl es, and I want to run a continuous draw loop in the background, while having the ability to call commands such as
glhookprogram make "object1" model "triangle" program "default"
glhookprogram attr "object1" position "1.0, 1.0, 0.0" scale "2.0" rotation "45, 0, 0"
this way, I can have a node server run hw-accelerated animations in javascript on the rpi.

Looks like this is what you need (and I'm sorry - I won't be going into too much details as there are plenty of sources on the Web about that):
A "server" - that would be your background process that stays running in memory and can accept and process commands (requests)
A "client" - a (short-running?) process that can accept commands from user (GUI, command-line. Network? Other process?) and send requests to your "server"
This is not a trivial task for a beginner. I would suggest googling for "server-client" and for "inter-process communications" first and go from there.
The range of options to "accept input" into your server includes (but is not limited to) the following:
(Windows) messages
Shared memory and a command queue (producer-consumer)
Shared file (just listing it here for completeness, I'd advise against this particular one for your case)
Named pipes
Sockets (thanks for reminding me of those in the comments, can't believe I missed that!)

Related

How to stop a running program in C?

I have a problem.I've written a program in C for armbian.
I am using RTKLIB software for GPS data conversion from ubx to RTCM3.
I get some data from serial port and start str2str(rtklib software).
It creates this command to run
str2str -in tcpsvr://:2101#ubx -out serial://ttyS2:115200#rtcm3
and call system function to run this command. It is successful, but when I send a new command, I want it to stop the str2str software.
I've tried the exit(0) and it stops my software. I don't want to stop my software. I want to stop str2str and create a new command and run it again.
How can I do it? I am not good with the linux environment.
Thanks
I suggest you find out how to search for the str2str process you want to kill, and get the PID. A stackoverflow search will reveal this and then use the PID to kill the process. Unless RTKLIB has a process to do this directly.

Make a file availabe on all nodes

I'm writing a MPI application that takes a filename as an argument and tries to read from the file using regular C functions. I run this application on several nodes of a cluster by using qsub, which in turn uses mpiexec.
The application runs just fine on a local node where the file is. For this I just call mpiexec directly:
mpiexec -n 4 ~/my_app ~/input_file.txt
But when I submit it with qsub to be run on other nodes of the cluster, the file reading part fails. The application errors at fopen call -- it can't open the file (likely because it's not present).
The question is, how do I make the file available to all nodes? I have looked over qsub manpage and couldn't fine anything relevant.
I guess Vanilla Gorilla doesn't need an answer any more? However, let's consider the case of a pathological system with no parallel file system and a file system available only at one node. There is a way in ROMIO (a very common MPI-IO implementation) to achieve your goal:
how can i transfer file from one proccess to all other with mpi?

Linux: Capture output of an already running process ( in pure C! )

My situation is the following: I've got a lot of small gizmos ( pretty close to routers, not exactly but anyway that's irrelevant) ; they are running a bare-bones MIPS-based Linux distro.
To control them, one can telnet there ( thru serial port ) and issue commands to an interactive bash-like shell which then writes back some output. The shell's input and output are both attached to /dev/ttyAS0.
Now, I'd like to automate all of this, i.e. write a program that will run inside the gizmo, be a small server listening on some port, and which would pass on any command to the said shell, capture shell's output and relay it back to whoever contacted to server.
I:
1) can install (small, <500KB) programs inside the gizmo
2) can't modify the OS, startup scripts, the shell, anything
3) have root access
4) know how to write a SOAP server
5) know how to get a SOAP message, translate it to a command and inject it into /dev/ttyAS0
6) DONT KNOW how to capture the shell's reply
7) know how to, having shell's reply, translate it back to a SOAP message and reply to the original inquirer.
So basically, the problem is 6) : how to, having injected a string to /dev/ttyAS0 and thus having made the shell execute it, capture the shell's output ?
I am aware of
http://etbe.coker.com.au/2008/02/27/redirecting-output-from-a-running-process/
i.e. I know that I could change the shell's stdout if I had GDB ( or strace ) running inside the box, but I can't install it there - it's too big and anyway this approach seems too much like a hack.
So, summarizing:
How root can capture stdout of an already running process, IN PURE C, without gdb or strace, with no access to the way the process is started?
Or - almost equivalently - how to capture what's being written to a terminal, IN PURE C ?
You might want to take a look at reptyr. It will probably need some adaptation to work for your system though
Have you tried driving the serial port with a kermit script? I would probably forgo trying to insert a more clever proxy on the device and just try and drive the existing interface.
If you really want to get it on the device, you may be able to look at the source to something like screen or kermit to get a sense of how they interact with ttys.

dtach - how to check if attached or unattached

I am creating a small C program which will find first unattached dtach session and attach to it. However dtach does not provide a way to check unattached/attached status. Is it possible to get this information at all? (For example by directly reading sockets created by dtach?)
use lsof to check how many dtach processes opened the socket file.
if process number > 1, then the socket is attached.

ftp client controlled by pipe in C

I am trying to control ftp client from C program (OS X). I did fork and execve - process is started ok. The problem is with pipes - I can send command to ftp client process and get feedback from it just fine (If i send "help\n" i get back help output) but what I never get in pipe is "ftp> " prompt. Any ideas?
Ivan
Your ftp client is probably behaving differently if stdin/stdout is a terminal or something else (lots of program do, for a start the C library does buffering in a different way...) If you want to control that, search information about pseudo-terminals, that's a little too technical to be explained here. (And looks first at programs like expect, it's possible you won't have to write yours).
A program can examine stdin to find out whether it's a terminal or a pipe. In your case, the FTP program probably does that (for example to know whether it can use escape sequences to render progress bars or offer command line editing).
If you really need the prompt, you have to look into PTYs (pseudo terminals) which emulate a console.
wild guess: isn't the "ftp>" prompt written to STDERR ?

Resources