Solved

Lock file in Unix

Posted on 1998-08-17
5
295 Views
Last Modified: 2013-12-26
Hi.
How do I lock a file in Unix(SunOs5.6)so other sessions
of a program cant manipulate with the locked file?
I have tried lockf,flock but doesn't get it to work.
Im using C on Win95 and I have no manual for Unix.
(except "man" on Telnet)
I open my file with fopen.
I have tried this

FILE *f;
f = fopen("lock","r");
lockf((int)f,2,1);
fclose(f);
When I run it I got segmentation fault
Help me.
/Pelle Sweden
0
Comment
Question by:PerBoman
  • 3
5 Comments
 
LVL 7

Accepted Solution

by:
faster earned 100 total points
ID: 1292898
To do record locking you can use fcntl().

Set the first parameter to F_RDLCK, F_WRLCK, or F_UNLCK.  It is quite complex, you'd better do a "man fcntl" to check the detail out by yourself.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1292899

0
 
LVL 84

Expert Comment

by:ozo
ID: 1292900
/* lockf takes an int file descriptor, not a FILE*stream
perhaps you meant */
int f = open("lock",O_RDONLY);
locked = lockf(f,F_TLOCK,1);
.
close(f);
0
 

Author Comment

by:PerBoman
ID: 1292901
Yes ozo,that was my problem (FILE* vs int)
I didn't know about the "open" command.
Thanks.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1292902
or

FILE *f;
f = fopen("lock","r");
lockf(fileno(f),2,1);

Since 2 is a F_TLOCK, be sure to check the return value.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl Awk Need Help 3 109
no14 challenge 14 62
noX challenge 17 91
Message not shown 5 27
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

929 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now