Solved

Lock file in Unix

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MFC Dialog 9 54
ASCII Non-Printable characters/codes and their HTML equivalents 6 159
sum13 challenge 24 148
Use of condition with 'serial' in ansible 2 101
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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…
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

734 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