Don't do cooperative locking -- it's not atomic. Suppose you had two processes each contending for a single file. The first file might touch the lock file and then immediately give up control to the second program (hey, it's a race condition and it really happens!). The second program is unable to open the file, even though the first is not using it, because the first has marked the lock file -- or it could try it anyway, grabbing the lock out from underneath it. The behavior is undefined.
flock is probably your best bet, as it uses the CPU's specific SINGLE operation for obtaining/releasing a lock.