ljaques
asked on
PROCESS ID and LOCKING a file...In C??
I have used Perl for some time now allowing my C skills to wither away. Perl has given me options like
getting the presently launched perl program's process id with the constant variable $$. This gives me a unique number which I can identify this launched program as (unique from the many that can be launched at one time on a multitasking OS). Perl also allows me to use a command called FLOCK which will lock a file from being written over by someone else giving exclusive rights to whoever.
Those commands are great but I need them for C. I am translating my Perl code and really need to know how to get the PROCESS ID for my launched compiled C program. I also need to LOCK a file so that the launched program can have exclusive rights to a file. And all this in ANSI-C because I want it to work on any platform (Unix/NT) that I compile it for.
Can you please help me identify these commands in ANSI-C? Or possibly give me alternative methods to use. Thank you VERY much!!
getting the presently launched perl program's process id with the constant variable $$. This gives me a unique number which I can identify this launched program as (unique from the many that can be launched at one time on a multitasking OS). Perl also allows me to use a command called FLOCK which will lock a file from being written over by someone else giving exclusive rights to whoever.
Those commands are great but I need them for C. I am translating my Perl code and really need to know how to get the PROCESS ID for my launched compiled C program. I also need to LOCK a file so that the launched program can have exclusive rights to a file. And all this in ANSI-C because I want it to work on any platform (Unix/NT) that I compile it for.
Can you please help me identify these commands in ANSI-C? Or possibly give me alternative methods to use. Thank you VERY much!!
I agree with julio. You need OS-dependant code. What OS are you going to run the code on?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
getpid is POSIX standard, and should be at least as portable as fork.
The lock file method suggested above can be tricky.
What if another process makes a lock file right after you check that there are no other ones there, and before you make yours?
I'd suggest looking at the perl flock code to see how they emulate it on various different platforms.
The lock file method suggested above can be tricky.
What if another process makes a lock file right after you check that there are no other ones there, and before you make yours?
I'd suggest looking at the perl flock code to see how they emulate it on various different platforms.
To make your code portable, try to use compiler conditionals.
HTH, julio