ConEmuC: how to avoid: Root process was alive less than 10 sec - conemu

How can I avoid this message? I would like that if the process ends... then the console should close.
D:\Projects.sc\start cmdn
ConEmuC: Root process was alive less than 10 sec, ExitCode=0.
Press Enter or Esc to close console...

Finally I found the answer:
Settings->Integration->Default term->Confirm Close
See attached screenshot.

Related

Creating my own shell. Handling Ctrl-Z and then sending SIGCONT closes the process instead of continue it

I am creating my own shell in C language. So far I implemented many features but the thing I am having problems with is CTRL-Z handling(SIGTSTP). Let me specify the problem over successful attempts:
When I execute a program in my shell (like gedit), and then press Ctrl-Z it executes kill(p_id, SIGTSTP) and stops that process. The shell also adds the process id in background_processes array so we can reach it in further. Then if I type "fg" in my shell, it brings the process to the foreground and executes kill(p_id, SIGCONT) so we can continue to use the program. Also the shell waits for the process to complete by executing waitpid function. We close the program by clicking X button or pressing Ctrl-C. Exact same thing in Linux shell. SUCCESFULL!!!
If I execute a program in my shell (like gedit) in background by specifying & (ampersand), it automatically starts this process in backgrounds by not waiting the process. But it adds the process id in background_processes array so we can reach it in further. Then when I type "fg" in my shell, it brings the process to the foreground. It actually waits for the process to complete by executing waitpid function. Also it doesn't matter if have more than one process in background, they will be bring to the foreground one by one. We close the programs by clicking X button or pressing Ctrl-C. Exact same thing in Linux shell. SUCCESFULL!!!
Lets execute a process in the foreground and then send it to the background by Ctrl-Z, and execute a process in background. We have 2 processes in the background. If I type "fg" it brings the first background process to the foreground and waits it. If I press X button (close button) which closes the program the shell brings the second process to the foreground and waits for it. Going very well right, thats what we want. So this scenario also worked very well.
The problem scenario is the same as the previous scenario in creating processes. When I type "fg" it brings the first background process to the foreground and waits it. But then if I press Ctrl-C it closes both processes!!!!!! It should only closed the first process and should have wait for the second process!!!
I searched everywhere, tried everything but couldn't figure it out. But the problem seems like with line 525. When I send SIGCONT signal it closes the process. But if comment that line it doesn't close but also I can't use the process since it is stopped!!!
I have the code in my GitHub repo here : https://github.com/EmreKumas/Myshell
Thanks for reading...
It seems like the problem is caused because of process groups. I did only create different process groups for background jobs but since you cannot change the process group of a child after it executed exec command, you better do it at the beginning before exec call. Now, the problem is solved thanks to "#that other guy" and "#John Bollinger".

freopen stderr into file but getting it printed anyway into console

This is the schema of my code (for simplicity error control removed)
main:
freopen("error.txt","w",stderr);//redirecting stderr to error.txt
FILE *fp=popen("./process", "w");//lunching a process
pthread_t readerThread;
/*reading output from process lunched in a thread otherwise it could block*/
pthread_create(&readerThread, NULL,IsKhacToolKit::threadRead, solver);
solver->generateDimacFile(fp);
pclose(fp);
pthread_exit(0);
The object solver use
fprintf(stderr,"Somes debugs Messages" --------%f seconds |%f seconds\n", (double)(start-tmp)/CLOCKS_PER_SEC, (double)start/CLOCKS_PER_SEC);
everywhere to keep trace of what happen.
My probleme is that when I lunch the executable I can see the debug messages and I dont understand why because before doing anything I redirect stderr to error.txt. What am I missing here?
What I am trying to do is lunching a process, giving him output to the threat, and then I will need to read its outPut. But apparently I don't understand how it works because here I already can't understand why debug messages are printed into the console.
Also I fclose(stderr) after comuting the output of process in the thread function.
//edit
Example:
_______#voisin:~/espaces/travail/calculabilite/version2$ ./dimac_generator_is_k_HAC 10K2 5
Initializing adjList --------0.003381 seconds |0.003381 seconds
Initialiszing translater --------0.000125 seconds |0.003506 seconds
Allocating solutionParser --------0.000012 seconds |0.003518 seconds
s UNSATISFIABLE
^C
_______#voisin:~/espaces/travail/calculabilite/version2$ cat error.txt
Preparing buffer to receive dimacFile --------0.000627 seconds |0.004145 seconds
Tranlating contraint 1 --------0.000451 seconds |0.004596 seconds
Tranlating contraint 2 --------0.000045 seconds |0.004641 seconds
Tranlating contraint 3 --------0.000010 seconds |0.004651 seconds
Tranlating contraint 4 --------0.000037 seconds |0.004688 seconds
Sending dimacFile to glucose --------0.000029 seconds |0.004717 seconds
_______#voisin:~/espaces/travail/calculabilite/version2$
The whole thing is not finished yet so it block. I need to Ctrl+C but you can see that before Ctrl+C debugs messages have been printed. Adding the fflush after printing allowed error.txt to not be empty anymore.

Prevent command prompt from closing after the code run

I have Microsoft Visual Studio and after I write a code and debug it the command prompt closes directly after the code run. Then I used
getc(stdin)
getch()
But all these also closes it after a click, Is there anyway to unable it from closing untill I click X?
There is an option to keep the output window open after run is finished, though it's pretty similar to what you did(adds a "Press any key to continue . . .")
If you want just to hold the window just add at the end
Sleep(100000000);
It will hold the window for this many mili seconds.
If it's still not enough do
while(1)
{
Sleep(100000000);
}
It will hold the window indefinitely

Is there a way to stop and resume a C program

I am doing a project in C and in that I need to process a lot of data . Is there a way to stop the program automatically after 30 minutes and then resume whenever I run the program again?
There is no automatic way to do this. If you're on a Unix system though, you can press Ctrl+Z while your program is running to send it a STOP signal. It will wait until you continue again by sending it a CONT signal (the easiest way is with the fg shell command). A stopped process is still resident in memory, but is not running.
There are more sophisticated ways to take a "snapshot" of a running program and save it to disk. Later, you can load the snapshot and continue execution of the program. For example, see CryoPID.
You could automate it. The Crtl-Z and fg are, as already mentioned, nothing more than signals. In Unixes you can send them with the kill command. So kill -19 $PID will stop your process (when $PID is its PID) and kill -18 will continue it.
You just have to write a wrapper script, that looks like:
#!/bin/sh
PIDFILE=wherever/myprogram.pid
if [ -e $PIDFILE ]; then
kill -18 `cat $PIDFILE`
else
myprogram &
echo $! > $PIDFILE
fi
sleep 30
kill -19 `cat $PIDFILE`
(very short, untested, you have to take care to remove the pidfile when you kill the process). First time you run it, it will start the execution (for 30 seconds), the later times it will just revoke your program. You could improve it e.g by checking in the beginning if there is a process of your program in the process list, and if not remove the pidfile in case it is already there. So it would even behave correct after reboot or termination of your process.
Short answer: Without putting in some effort to have your program 'hibernate itself', no.
If you're running on UNIX then I'm sure you can set up a cron job to suspend / resume the program as and when needed. Under Windows you'll be afforded no such luxury.

How to send Ctrl-C control character or terminal hangup message to child process?

I have a child process which runs in a pseudo terminal. The parent process does not run as root, but the child process does, through su or sudo. Because of this it is not possible to send a signal to the child process to force it to exit. I want to force it to exit by one of these means:
emulating a Ctrl-C.
emulating a terminal hangup.
How do I do either of these? I already have a pty master fd, and I've tried something like this:
write(master, &termios.c_cc[VINTR], 1)
but it doesn't do anything.
It seems to me that if you truly have a pty (unless you mean something else by pseudo terminal), that all you have to do is send a Control-C to that FD. As evidence of this, I submit the following code in Python (but fairly close to the C required to do it):
import pty, os, sys, time
pid, fd = pty.fork()
if pid == 0:
os.execv('/bin/sh', ['/bin/sh', '-c',
'while true; do date; sleep 1; done'])
sys.exit(0)
time.sleep(3)
os.write(fd, '^C')
print 'results:', os.read(fd, 1024)
This forks a process under a pty, which runs an infinite loop printing the
date. Then the parent waits 3 seconds and sends a control-C.
This results in the following output:
guin:/tmp$ time python /tmp/foo
results: Fri Feb 5 08:28:09 MST 2010
Fri Feb 5 08:28:10 MST 2010
Fri Feb 5 08:28:11 MST 2010
python /tmp/foo 0.02s user 0.01s system 1% cpu 3.042 total
guin:/tmp$
It ran just over 3 seconds, printed out the date 3 times, and exited.
I eventually went with the following solution:
After forking, instead of exec'ing sudo immediately, I exec() a helper child process instead, which in turn forks and execs sudo and calls waitpid on it. So the process hierarchy looks like this:
original process <---- runs as user
|
+-- helper process <---- runs as user, session leader,
| has own pty, in pty's foreground process group
|
+--- sudo <---- runs as root
By killing the helper process, the pty does not have a foreground process anymore. This will cause the OS to send SIGHUP to the entire foreground process group, regardless of the user, so sudo is SIGHUP'ed too.
I think you need to use ioctl to insert the interrupt character instead of write. Unfortunately the mechanism for this does not seem to be portable. For linux it looks this might work:
ioctl(master, TIOCSTI, &termios.c_cc[VINTR]);
There is two ways to achieve this:
From the child process, trap the SIGCHLD signal and handle it, you could _exit(0) to end the child process
There's a program called ptree. You could cheat this by doing it this way...in pseudocode:
obtain the parent's pid.
using _popen("ptree %d", parent_pid)
for each entry of child process
system ("kill -1 %d", child_process_pid)
There the two that comes to mind...sorry if its not of further help to you,
Hope this helps,
Best regards,
Tom.
Closing the master should signal a hangup to the controlling process group of the slave.
The first thing I'd check is if you need to make it the controlling terminal on the slave side. It turns out this is more complex than I remember, with ptys possibly not becoming controlling by default. That link is for Linux, other systems should do one or the other depending on their SysV vs. BSD-ness, but it looks like the TIOCSCTTY is a good bet to try.
Secondly, I'd check if you're setting ISIG in your termios; if not, VINTR and VQUIT won't work.
Of course, if the other end is catching SIGINT and SIGQUIT, you will have other issues.

Resources