How to "hibernate" a process?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to "hibernate" a process?
# 1  
Old 02-22-2011
How to "hibernate" a process?

Hello everyone!

Here's my problem (I'm a newbie):
I use a program/calculator which launches several parallel processes at each iteration. Then it expects to receive a response file from each of the processes before continuing to the next iteration:

Launch_program ---> starts 30 processes ---> Expects 30 results_files -|
^-------<---------<-------<------------v

The tricky part is that the processes do not returning directly any results file: instead they create a flag file which triggers the computing of all the 30 operations at once and returns 30 results files.

The only solution I've found to run the program is to make each process check for the existence of the results file before exiting with a while(1) loop:
(in C):

Code:
/* Wait until a results file is created*/
 while (1)
 {
  if( access( "result_fileXXX", F_OK ) != -1 )
   {      
  break;
   } 
 }

However, this obviously consumes a lot of resources... Is there any way to launch a process, make it "hibernate" and then wake it up with a signal??
In this case the signal would be the creation of the results file, but I do not want the process to be checking its existing continuously.

Any ideas? Is this even possible in unix systems?

Thanks in advance for your input!
# 2  
Old 02-22-2011
There's lots of ways to do this in UNIX.

The simplest way to fix your loop is to add a usleep(100000); at the bottom of your while(1) loop: This will put the process to sleep for at least 100 milliseconds per loop, preventing it from checking for the file more than 10 times per second. The process literally goes to sleep, consuming no CPU time until the delay is up. This may also mean a lag of up to a tenth of a second between a new file being created and the new file being used.

Other, more direct methods, may depend on what your system actually is.

One concern: Is this actually a flag file, i.e. this file is created only after all the data is finished? Or is it one of the data files, which you might catch in the middle of being written to instead of after it's done?
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 02-22-2011
I'm using Linux, if I type uname -a I get this (plus motherboard stuff):

Linux myhostname 2.6.20-1.2948.fc6

Given that the time needed to calculate the results file can vary from less than one second to many hours, isn't there any other solution to really make the process hibernate until a wake up call is received? If I use usleep() this will no doubt represent a great improvement, but does not really solve the problem, at least as I expected Smilie.
# 4  
Old 02-22-2011
I suspect you may need to redesign your application for this but, the best and proper way to wait for child processes to finish is simply wait() -- or its related call waitpid(): waitpid(child_pid, &stat, 0); Call this for each child you've created. Your process will sleep until all your children return. You can use WEXITSTATUS(stat) after each call to see what return code they gave.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 02-22-2011
Thanks, that's what I was looking for... even if I don't know if I'll be able to use it in my specific case, since no child processes are directly launched (I use a flag file to initiate the calculations).
The main problem is that I cannot exit the process before the results files are created...

Indeed, I think my application will need somme major redesign!
# 6  
Old 02-22-2011
You've made things much harder on yourself by using files instead of IPC, but all isn't lost. Linux can watch for filesystem changes with the inotify system.

This will make your code unportable though, since inotify is Linux-only, and only relatively modern Linux at that... wait() and waitpid() on the other hand should be available on any sane UNIX and UNIX-like...

You could also use sockets instead of files. Open a socket to your daemon program, write the data you want written, read the data you want read, and close the connection. read() will sleep whenever it's waiting for the other end to send. TCP sockets and UNIX domain sockets should both be doable and quite portable.
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 02-23-2011
Thank you very much for your input! It was really helpful!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. AIX

Apache 2.4 directory cannot display "Last modified" "Size" "Description"

Hi 2 all, i have had AIX 7.2 :/# /usr/IBMAHS/bin/apachectl -v Server version: Apache/2.4.12 (Unix) Server built: May 25 2015 04:58:27 :/#:/# /usr/IBMAHS/bin/apachectl -M Loaded Modules: core_module (static) so_module (static) http_module (static) mpm_worker_module (static) ... (3 Replies)
Discussion started by: penchev
3 Replies

2. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

7. Red Hat

"service" , "process" and " daemon" ?

Friends , Anybody plz tell me what is the basic difference between "service" , "process" and " daemon" ? Waiting for kind reply .. .. (1 Reply)
Discussion started by: shipon_97
1 Replies

8. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

9. UNIX for Dummies Questions & Answers

Process seen in "ps aux" but not "top"

Hi, I have a process that can be seen after "ps aux" command. However when I do "top" command. This process cannot be seen. How can this happen? Is there anything wrong with my code? (3 Replies)
Discussion started by: monkfan
3 Replies
Login or Register to Ask a Question