That's not safe. PIDs are recycled.
The only correct solution I can think of is to place the lock in shared memory, use the futex(2) API, and use set_robust_list (https://linux.die.net/man/2/set_robust_list) to ensure the kernel will unlock a futex if a process dies.
You may (should?) be able to initialize a pthread-based PTHREAD_PROCESS_SHARED + PTHREAD_MUTEX_ROBUST mutex in a shared memory region and have everything just work.