Multiple processes writing on the same file simultaneously

Hi All,
I have encountered a problem,please help me.

I have a script in which multiple processes are writing on to the same file .
How should I stop this , I mean lock mechanism can be implemented or we can write the at different files and then concatenate the files.

What would be a better approach?
If file lock is to be implemented how should I go on implementing that?

this might be of help to you