Not sure it is related to VxWorks or to rlogin, but anyway.
I read one should never use the exit command to quit a VxWorks rsh shell, but only the logout command. However I can't seem to find an explanation. What is the difference between those two commands ?
Thanks.
If rsh connects to the vxWorks Kernel Shell (i.e. the -> prompt) then the advise makes sense.
The kernel shell is typically a permanent task that parses whatever the user types and executes the appropriate function.
When you type a "command", the shell actually looks up the symbol (i.e. global variable or function) and if it's a function, it will call the function with whatever parameters you pass in.
logout() is presumably a function related to closing the RSH connection.
However, exit() is a standard C function which terminates the current process (or Task in vxWorks). Which means that you would just have killed the actual kernel shell task (vs. just the RSH connection). Probably not what you mean to do.
Normally the shell is set to auto-restart, but why tempt fate...
Related
I am trying to run multiple command in ubuntu using c++ code at the same time.
I used system() call to run multiple command but the problem with system() call is it invoke only one command at a time and rest commands are in waiting.
below I wrote my sample code, may this help you to get what I am trying to do.
major thing is I want to run all these command at a time not one by one. Please help me.
Thanks in advance.
main()
{
string command[3];
command[0]= "ls -l";
command[1]="ls";
command[2]="cat main.cpp";
for(int i=0;i<3;i++){
system(command[i].c_str());
}
}
You should read Advanced Linux Programming (a bit old, but freely available). You probably want (in the traditional way, like most shells do):
perhaps catch SIGCHLD (set the signal handler before fork, see signal(7) & signal-safety(7)...)
call fork(2) to create a new process. Be sure to check all three cases (failure with a negative returned pid_t, child with a 0 pid_t, parent with a positive pid_t). If you want to communicate with that process, use pipe(2) (read about pipe(7)...) before the fork.
in the child process, close some useless file descriptors, then run some exec function (or the underlying execve(2)) to run the needed program (e.g. /bin/ls)
call (in the parent, perhaps after having got a SIGCHLD) wait(2) or waitpid(2) or related functions.
This is very usual. Several chapters of Advanced Linux Programming are explaining it better.
There is no need to use threads in your case.
However, notice that the role of ls and cat could be accomplished with various system calls (listed in syscalls(2)...), notably read(2) & stat(2). You might not even need to run other processes. See also opendir(3) & readdir(3)
Perhaps (notably if you communicate with several processes thru several pipe(7)-s) you might want to have some event loop using poll(2) (or the older select(2)). Some libraries provide an event loop (notably all GUI widget libraries).
You have a few options (as always):
Use threads (C++ standard library implementation is good) to spawn multiple threads which each perform a system call then terminate. join on the thread list to wait for them all to terminate.
Use the *NIX fork command to spawn a new process, then within each child process use exec to execute the desired command (see here for an example of "getting the right string to the right child"). Parent process can use waitpid to determine when all children have finished running, in order to move on with the program.
Append "&" to each of your commands, which'll tell the shell to run each one in the background (specifically, system will start the process in the background then return, without waiting for the result). Not tried this, don't know if it'll work. You can't then wait for the call to terminate though (thanks PSkocik).
Just pointing out - if you run those 3 specific commands at the same time, you're unlikely to be able to read the output as they'll all print text to the terminal at the same time.
If you do require reading the output from within the program (though not mentioned in your question), this is relevant (although it doesn't use system).
I can restart my application with
QProcess::startDetached(QApplication::applicationFilePath());
exit(12);
But if I have QtSingleApplication new instance will not run. Is there way to trick QtSingleApplication to allow it to run new instance when isRunning() returns true maybe by some command line arguments or another aspects?
I'll be fully fine if I will add Sleep(5000); in the beginning but it doesn't look alike good design, I think I just need an additional mutex to detect if my application is gone :S what do you think?
When you want to start your app as new running instance, pass a specific argument to signal it should close any existing rather the usual opposite.
This can be handled as
//code to send to instances
if(myturn)
{
if (a.isRunning()) {
QString rep("Another instance is running, so I will ask to leave.");
bool sentok = a.sendMessage(message_gtfo,1000);
if(!sentok)
return "some error";
}
}
//code to receive message from instance
connect(&a, SIGNAL(messageReceived(const QString&)),
&this, SLOT(handlMessage(const QString &))
);
...
void instancecomhandler::handlMessage(const QString & msg)
{
if(msg == message_gtfo)
//proceed to exit
}
edit:
You still need to pass the argument to modify the behavior. Default behavior quit when another instance is running. New behavior waits till the other instance terminate.
One way to do such a thing is to use a little helper application (or script). The helper application is started detached and receives an identifier (ie PID) of the currently running program as a command line argument. It then waits until that identifier has disappeared from the process table, then restarts the application and exits.
This is a bit hacky/racy, but it works okay for me in practise. I use this in an update system, where I need to replace the running program with a new version of itself, which on Windows cannot be done while the program is running.
If you are restarting your application from inside itself, it is by definition no more a single-process application (since for a short time at least, two processes running the same application exist).
I would recommend using some shell tricks to restart it.
On Linux with a X11 desktop, you might e.g. popen(3) some at (or batch) command which would restart your application. You'll probably need to explicitly set the DISPLAY environment variable.
On Linux or other POSIX systems, a possible way would be to wrap the application in a shell script (for example mywrapper) like e.g.
#! /bin/sh
# script file mywrapper
aftersource=$(tempfile -p app)
myapp --aftersource $aftersource "$#"
source $aftersource
rm $aftersource
You'll need to improve the above script to handle failures and trap some signals... I guess you might adapt that script to Windows if so needed
Then your application would process the --aftersource argument to write some shell commands in it. If it want to restart itself it would e.g. write some command into that file to do that, etc.
Feel free to improve the script (trapping some signals and exit, looping, etc...)
So here is the situation, we have a C++ datafeed client program which we run ~30 instances of with different parameters, and there are 3 scripts written to run/stop them: start.sh stop.sh and restart.sh (which runs stop.sh and then start.sh).
When there is a high volume of data the client "falls behind" real time. We test this by comparing the system time to the most recent data entry times listed. If any of the clients falls behind more than 10 minutes or so, I want to call the restart script to start all the binaries fresh so our data is as close to real time as possible.
Normally I call a script using System(script.sh), however the restart script looks up and kills the process using kill, BUT calling System() also makes the current program execution ignore SIGQUIT and SIGINT until system() returns.
On top of this if there are two concurrent executions with the same arguments they will conflict and the program will hang (this stems from establishing database connections), so I can not start the new instance until the old one is killed and I can not kill the current one if it ignores SIGQUIT.
Is there any way around this? The current state of the binary and missing some data does not matter at all if it has reached the threshold, I also can not just have the program restart itself, since if one of the instances falls behind, we want to restart all 30 of the instances (so gaps in the data are at uniform times). Is there a clean way to call a script from within C++ which hands over control and allows the script to restart the program from scratch?
FYI we are running on CentOS 6.3
Use exec() instead of system(). It will replace your process with the new one. Note there is a significant different in how exec() is called and how it behaves: system() passes its string argument to the system shell to run. exec() actually executes an executable file, and you need to supply the arguments to the process one at a time, instead of letting the shell parse them apart for you.
Here's my two cents.
Temporary solution: Use SIGKILL.
Long-term solution: Optimize your code or the general logic of your service tree, using other system calls like exec or by rewritting it to use threads.
If you want better answers maybe you should post some code and or degeneralize the issue.
For example,if i use system() command from a c program to invoke a shell command whether it will complete the shell command and then it'll proceed with program or it'll do both concurrently
what are all the various methods available to find this information ?
You can use fork() or exec() for non-blocking, but a system() call is blocking. This means it will wait for your shell command to finish prior to resuming executing of your C program.
Note if you want it to return right away, you can issue your system command with an & behind it and your C program will run concurrently.
Sequential Example:
system("long_script.sh");
Concurrent Example:
system("long_script.sh &");
I need to execute some commands via "/bin/sh" from a daemon. Some times these commands takes too long to execute, and I need to somehow interrupt them. The daemon is written in C++, and the commands are executed with std::system(). I need the stack cleaned up so that destructors are called when the thread dies. (Catching the event in a C++ exception-handler would be perfect).
The threads are created using boost:thread. Unfortunately, neither boost::thread::interrupt() or pthread_cancel() are useful in this case.
I can imagine several ways to do this, from writing my own version of system(), to finding the child's process-id and signal() it. But there must be a simpler way?
Any command executed using the system command is executed in a new process. Unfortunately system halts the execution of the current process until the new process completes. If the sub process hangs the new process hangs as well.
The way to get round this is to use fork to create a new process and call one of the exec calls to execute the desired command. Your main process can then wait on the child process's Process Id (pid). The timeout can be achieve by generating a SIGALRM using the alarm call before the wait call.
If the sub process times out you can kill it using the kill command. Try first with SIGTERM, if that fails you can try again will SIGKILL, this will certainly kill the child process.
Some more information on fork and exec can be found here
I did not try boost::process, as it is not part of boost. I did however try ACE_Process, which showed some strange behavior (the time-outs sometimes worked and sometimes did not work). So I wrote a simple std::system replacement, that polls for the status of the running process (effectively removing the problems with process-wide signals and alarms on a multi threading process). I also use boost::this_thread::sleep(), so that boost::thread::interrupt() should work as an alternative or in addition to the time-out.
Stackoverflow.com does not work very good with my Firefox under Debian (in fact, I could not reply at all, I had to start Windows in a VM) or Opera (in my VM), so I'm unable to post the code in a readable manner. My prototype (before I moved it to the actual application) is available here: http://www.jgaa.com/files/ExternProcess.cpp
You can try to look at Boost.Process:
Where is Boost.Process?
I have been waiting for a long time for such a class.
If you are willing to use Qt, a nice portable solution is QProcess:
http://doc.trolltech.com/4.1/qprocess.html
Of course, you can also make your own system-specific solution like Let_Me_Be suggests.
Anyway you'd probably have to get rid of the system() function call and replace it by a more powerful alternative.