Solved

Lock file in Unix

Posted on 1998-08-17
5
300 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
[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
  • 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

696 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