Run multiple executables all at once - c

I have a C Sockets application, different executables of which must run at same time all at once, preferably in different terminals. How do I do it?
For example, there are four exes, ./one, ./two, ./three, ./four.
I want them to be run in different linux terminals without slightest of time difference. How can I do it?

There will always be at least a "slightest of time difference".
Just have your exe's agree on a time to proceed and just sleep until that time before doing whatever it is that they need to do.

You will never run several process at the same time. If you need just to start them nearly at the same time for testing purposes, then, probably, easiest way is to start terminal emulator to run your each of your executable. If you have xorg installed, then you can open xterm and run there something like this:
xterm -e "./app1; /bin/bash" & xterm -e "app2; /bin/bash" & ...
where the /bin/bash is called to prevent closing new xterms after your application will be finished.
I'm not bash expert so this works on my machine and I can't be sure about exactly this string will work with older or newer bash or sh pr with another terminal emulator. But you always can write something similar.

To run simultaneously you can do something like this:
"declare -a cmd=(./first ./second ./third); for i in ${cmd[#]}; do exec $i wait & done"
This can help you to automate your task. Keep your commands on array, exec them. It'll run all the processes on background and will start trying to run them simultaneously.

Related

How to enable logging in xterm

Is it possible to turn on logging feature by default in xterm?
Lets say for example, I have example program in c that give an output in xterm everytime i ran the program from default bash terminal in linux. And I want to save the output that shows in xterm into a file everytime the programs is run.
I'm using centos7_x86_64 fyi
Thanks.
In Windowmaker:
Hold down ctrl + left click in xterm window (on the terminal text), click on "log to file".
A cool thing to do is when you're coding, log the terminal, and then when you cat the Xterm log, you see coding in fast forward. If you wanted you could video it.
Note that there are also other menus in xterm, accessible using Ctrl+Left Click, Ctrl+Middle Click, Ctrl+Right Click.
Log file will be in the directory that you launched Xterm from, and will be in the format: Xterm.log.<hostname>.<date>.<time>.log.
This is a very good question, there's no reason to mark down a question like this.
It can be done in two ways:
Using script:
xterm -e script mylogfile -c "someCommand -i input_file -o output_file -f someArg"
Redirect to a file:
xterm -e 'someCommand --arguments 2>&1> /path/to/mylogfile'
I do essentially this with "terminal-window", mrxvt, "hcm" and "pypty".
terminal-window wraps mrxvt, just filling in some commandline options. mrxvt is a lightweight, multitabbed, nonunicode terminal emulator not dissimilar to xterm.
hcm is a GUI that makes it easy to run a shell (or other command) on a remote host. It can also start an mrxvt with remote ssh's without requiring the GUI if you prefer (using hcm-term).
pypty is a /usr/bin/script reimplementation that is written in Python. It is not significantly different from /usr/bin/script, except it gives a "dated files mode", that allows you to have one file per pseudo terminal per day. So if you leave a shell logged in overnight, you get one file per day - this tends to make it easier to find what you're looking for.
All this combines to give you pseudo terminal logging with great ease. Commands are run on remote hosts, but logged locally. Just start a "fancy terminal-window" (or use hcm-term), and everything you see on the screen plus control characters (but not nonechoed passwords) will be logged under ~/.hcm/logs/<year>/<month>/<day>/* .
Also, if you hit the shell button in the lower right of mrxvt, you get another ssh session into the same remote host, which is also logged locally (to a different file under ~/.hcm/logs/...). When I started making use of that feature, I had no idea how much I would grow to like it.
You can obtain them from http://stromberg.dnsalias.org/~strombrg/hcm/ There's a video there that shows how easy it is to set up and get started with.
BTW, fancy terminal-window sets up $PS0 or "trap DEBUG" to give you command start times and finish times. It's great for post mortems. It does this without replacing any of the usual bash startup files.
I wrote terminal-window, hcm and pypty, but I can't take credit for mrxvt. :)
HTH

Launching program w/posix_openpt() call from script

I have a program that relies on a psuedo terminal that uses
term = posix_openpt()
grantpt(term)
unlockpt(term)
open(term)
to open a psuedo terminal. I'll call this program psuedoTerminal.bin
I want to open psuedoTerminal.bin from a bash script, in the following fashion (or similar)
#!/bin/bash
/bin/sh -c psuedoTerminal.bin &
The problem is when my program arrives to the posix_openpt() call, the behaviour is erratic, sometimes CPU consumption arrives to 100%, but it never works. I believe ssh, telnet, etc suffer from the same problems on opening psuedo terminals from inside scripts.
How can I run this program from inside a script? Thanks for your help.

(Linux, C) Two threads, two separate text windows in the screen, how to do it?

I have two threads. The main thread and two others created with *pthread_create*, for instance thread_1 and thread_2.
The main thread writes the output in the terminal that i used to run the program. Then, i want to create two new windows, in which thread_1 and thread_2 will write some text.
i tried to use the system call "system()" and run "/bin/bash" but there is no new terminal appearing after that.
now, i am thinking to use some graphic library (g2, SDL...).
i searched the web for some hours and cant find a good solution. All i just need is to output text in independent windows, i don't want any kind of drawings, only text lines.
i am using Linux mint and C language.
Do you need the program to open the other two windows?
If not:
Have each thread write to a separate file.
Manually open two more windows.
Use the bash command 'tail -f' in each of the new windows to display the output of each file.
The closest non-gui solution that comes to mind is ncurses
Thanks for the advices.
I achieved a solution for my problem with the Jay's hint.
It's very simple. i put the program writing the data to a file. (closest to the fifo idea)
then, inside the program i run this:
system("mate-terminal -e --command='tail -f filename.txt'");
and voilá! It creates a new terminal window and runs the command tail -f.
Thanks for your answers, they helped me thinking in that solution. I tried the ncurses but had problems in installing the package and then compiling with the -lncurses flag. I will try ncurses someday, but now the problem is solved.
bye

Is it possible to run a program from terminal and have it continue to run after you close the terminal?

I have written a program which I run after connecting to the box over SSH. It has some user interaction such as selecting options after being prompted, and usually I wait for the processes it carries out to finish before logging out which closes the terminal and ends the program. But now the process is quite lengthy and I don't want to wait whilst being logged in, so how could I implement a workaround for this in C please?
You can run a program in the background by following the command with "&"
wget -m www.google.com &
Or, you could use the "screen" program, that allows you to attach-deattach sessions
screen wget -m www.google.com
(PRESS CTRL+D)
screen -r (TO RE ATTACH)
http://linux.die.net/man/1/screen
The process is sent the HUP signal when the shell exits. All you have to do is install a signal handler that ignores SIGHUP.
Or just run the program using nohup.
The traditional way to do this is using the nohup(1) command:
nohup mycmd < /dev/null >& output.log &
Of course if you don't care about the output you can send it to /dev/null too, or you could take input from a file if you wanted.
Doing it this way will protect your process from a SIGHUP that would normally cause it to exit. You'll also want to redirect stdin/stdout/stderr like above, as you'll be ending your ssh session.
Syntax shown above is for bash.
you can use screen command. here is a tutorial. note you might need to install it to your systems.
There are many options :-) TIMTOWTDI… However, for your purposes, you might look into running a command-line utility such as dtach or GNU screen.
If you actually want to implement something in C, you could re-invent that wheel, but from your description of the problem, I doubt it should be necessary…
The actual C code to background a process is trivial:
//do interactive stuff...
if(fork())
exit(0);
//cool, I've been daemonized.
If you know the code will never wind up on a non-linux-or-BSD machine, you could even use daemon()
//interactive...
daemon(0, 0);
//background...

create process independent of bash

I have written a program which calculates the amount of battery level available in my laptop. I have also defined a threshold value in the program. Whenever the battery level falls below threshold i would like to call another process. I have used system("./invoke.o") where invoke.o is the program that i have to run. I am running a script which runs the battery level checker program for every 5 seconds. Everything is working fine but when i close the bash shell the automatic invocation of invoke.o is not happening. How should i make the invoke.o to be invoked irrespective of whether bash is closed or not??. I am using UBUNTU LINUX
Try running it as: nohup ./myscript.sh, where the nohup command allows you to close the shell without terminating the process.
You could run your script as a cron job. This lets cron set up standard input and output for you, reschedule the job, and it will send you email if it fails.
The alternative is to run a script in the background with all input and output, including standard error output, redirected.
While you could make a proper daemon out of your program that kind of effort is probably not necessary.
man nohup
man upstart
man 2 setsid (more complex, leads to longer trail of breadcrumbs on daemon launching).

Resources