Solved

Lock file in Unix

Posted on 1998-08-17
5
294 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
Comment Utility
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
Comment Utility

0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
/* 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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

12 Experts available now in Live!

Get 1:1 Help Now