After 2 seconds run SIGALRM alarm command in C - 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?

Related

How to implement running commands in the background by supporting ‘&’ and wait (for UNIX Shell)?

So my understanding is that if you terminate a command with ‘&,’ it runs the process in the background. If two commands were separated by ‘&,’ they would run simultaneously.
e.g. command1 & command2 //runs simultaneously
To implement this, I would have the shell fork a child process and have the child execute the 1st command I. The background. The parent would not wait and would execute the second command in parallel.
Is this the right approach? And how can I apply this to support wait()?
This is using bash on a generic unix:
BigMac:tmp steve$ sleep 10 & sleep 13 & sleep 20 &
[1] 69168
[2] 69169
[3] 69170
BigMac:tmp steve$ wait %1; wait 69169; kill %3
[1] Done sleep 10
[2]- Done sleep 13
[3]+ Terminated: 15 sleep 20
The first line launches three separate sleeps. The shell assigns them job numbers [1], [2], [3]; whereas the system assigns them process id's 69178,69169,69170 resp. The commands wait and kill can take either identifier, as shown in the second line.
The second line waits for the first job % indicates to the shell that the following number is in its naming scheme, then waits for the second process the lack of % indicates a process identifier, then kills the third job.
There is a lot more to this than such a simple example can convey; particularly the notion of job is quite a bit more involved than this example portrays.
The UNIX Programming Environment ISBN despite being nearly 40 years old, provides a great fundamental understanding of how and why UNIX works the way it does. There are more modern, detailed, and system specific works as well.

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.

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