# newbie for loop question



## bluetick (Apr 7, 2010)

Is there a proper way to pause a for loop until the encoding of the file is completed?


----------



## ckester (Apr 7, 2010)

Your question needs to provide more context.   For instance, is this a shell script, a C program?  Python? Perl?  What? 

What do you mean by "encoding of the file" and how is that being done?  Are you launching some program that runs in the background or calling a function that executes asynchronously?

You might want to see if there are synchronous alternatives.  Or you can launch the program as a foreground process.  That will suspend execution of your for loop until control returns from the synchronous function or the child process terminates.

If the work has to be done in an asynchronous function or background process, look at the *wait(2)* family of functions.  There's a wait utility built into the shell, and most scripting languages have something similar.


----------



## jotawski (Apr 8, 2010)

hi

i'm going to do quite the same thing.  my case is to do some calculation and displaying the results afterwards and waiting for user to quit seeing pictures and the resume another calculation.

may be that fork and wait(2) as suggested by ckester be the solution.


----------



## expl (Apr 8, 2010)

I do not see why do you want one process in "wait" state and a second running calculations, thats just wasting hardware when you can do that just with one process. Unless you are running some short of GUI refresher.


----------



## ckester (Apr 8, 2010)

expl said:
			
		

> I do not see why do you want one process in "wait" state and a second running calculations, thats just wasting hardware when you can do that just with one process. Unless you are running some short of GUI refresher.



Possible design motivations:
- separation of concerns, good encapsulation
- re-use of existing code or programs
- etc.

I agree, it seems like a strange idea to make an asynchronous call and then wait for it to finish.  But there are a lot of shell scripts and programs around that do exactly that: fork a bunch of child processes and then wait for them to terminate.

We really need to know more about what these guys are trying to accomplish before we can say whether it's the most appropriate design for them to use.


----------



## jotawski (Apr 8, 2010)

hi

thanks for comments.
for i myself, it is an assignment that force me to do so.  they simply want me to write programs so i have to do.

for other case, i do not know exactly.

apoloziged me for disturbing the forum anyway.


----------



## expl (Apr 8, 2010)

ckester said:
			
		

> I agree, it seems like a strange idea to make an asynchronous call and then wait for it to finish.  But there are a lot of shell scripts and programs around that do exactly that: fork a bunch of child processes and then wait for them to terminate.



Thats because shell scripts aren't real binaries and every action needs forking or the exec will overwrite it's interpreter.


----------



## SR_Ind (Apr 10, 2010)

jotawski and bluetick,

Usual technique to deal with this situation is as follows:

1. Setup a signal handler, to handle either USR1 or USR2 signals. These are user defined signals, commonly used for custom events defined by user applications

2. Write a thread function. This function should contain the code for file encoding algorithm. You'll have to raise USR1/USR2 before the code exits this function.

3. Whenever your are designing the thread function, make allowance for passage of any arbitrary parameter as pointer. You'll have to typecast the incoming parameter inside the thread function.

4. From the main thread (usually the application itself) i.e. the thread that is going to start the encoding process, launch the child thread. Pass whatever parameter you require.

5. You are done. Your application will simply execute next line onwards without waiting.

6. As the encoding finishes your signal handler function will be called automatically.

Hope it helps.


----------



## jotawski (Apr 14, 2010)

ckester said:
			
		

> Possible design motivations:
> - separation of concerns, good encapsulation
> - re-use of existing code or programs
> - etc.
> ...



i simply do calculations and display graphp with gnuplot so that i need to fork and exec.. gnuplot afterward.

i have a limited time to learn using gtk+tutorial in doing so in one step.  many thanks indeed for all comments and for your times.


----------

