freopen stderr into file but getting it printed anyway into console - c

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.

Related

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

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.

After 2 seconds run SIGALRM alarm command in C

I write a C program and my code copies a file from a directory to another. If the process takes more than 2 seconds, every second it has to print "Process is continue". How to delay alarm command 2 seconds?

Batch script for sleep/hibernate

I would like to write a script in batch that forces the computer to enter
sleep (s3) and/or hibernate(s4), for a certain amount of time. I couldn't find answer for this question nowhere.
Example:
Computer enters sleep state.
30 seconds pass.
Computer returns and continues the script.
I managed to use an external program, but after a few cycles of the procedure
the computer for some reason enters a sleep state for 4,294,966,391 seconds
and only continues the script when turned on manually.

batch script to kill process if still running after a certain time

I need to create a batch script that launches a program, and waits for up to 1 minute for the program to finish, and then do something else. The exact steps are:
Launch program A
Wait for program A to finish OR kill it if not finished within 1 minute.
Launch program B
To emphasise, in step 2, I do not want to wait for the full 1 minute if program A is finished within say, 5 seconds.

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.

Resources