Solved

Lock file in Unix

Posted on 1998-08-17
5
297 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to create frequencies of a variable from SAS dataset? 10 125
sum13 challenge 24 107
Fibonacci challenge 11 122
how to use laptop or pad camera in vb.net windows application 2 71
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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

773 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