In the case where pid == -1, you don't unlock the write lock and it appears that the error case doesn't handle that either.