Solved

PROCESS ID and LOCKING a file...In C??

Posted on 1998-05-22
4
158 Views
Last Modified: 2013-12-25
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!!
0
Comment
Question by:ljaques
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 5

Expert Comment

by:julio011597
ID: 1828848
There isn't an *ANSI-C* way to do the job. That is, you need OS dependent code.
To make your code portable, try to use compiler conditionals.

HTH, julio
0
 
LVL 2

Expert Comment

by:WileyKat
ID: 1828849
I agree with julio. You need OS-dependant code. What OS are you going to run the code on?
0
 
LVL 8

Accepted Solution

by:
jhurst earned 50 total points
ID: 1828850
They are right that there is no specific call in ANSI-C however there is a sort of kludge way of getting something uique, actually a PID.  WHat you do is use fork() which as you will find in the manual makes a second copy of the running process and returns the pid to one cope and 0 to the other.  Just have the copy that gets a pid back use this number.  It is a real pid so it gives you what you want.

The lock is more of a problem.  The way that I have always handled this is to make a lock file with the pid when I start and have it removed by the atexit().  Then I just check that there are no other ones there when I start.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1828851
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.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question