The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
ssh with shared mount point devjxh SUN Solaris 4 11-30-2007 04:54 AM
Please point the error skyineyes Shell Programming and Scripting 1 07-05-2007 05:26 AM
how to get rid of decimal point? cin2000 Shell Programming and Scripting 2 12-27-2005 08:26 AM
mount point colesy UNIX for Dummies Questions & Answers 1 01-06-2004 03:03 AM
Fibre connection Point to Point SUN kie UNIX for Advanced & Expert Users 6 05-20-2003 08:20 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-07-2006
Registered User
 

Join Date: Mar 2006
Posts: 18
I have no clue what's the point - fcntl() ?

hi, I need to know how to lock a file. I used the following code, but after executing the program the file 'write.txt' remined empty, and I have no idea why.Maybe I'm not using the corresponding syntax for blocking a file. But I deleted then the blocking part and the problem persisted.
see to code:
......
f=open("read.txt",O_RDONLY);
fcntl(f,F_SETLK);
read(f,buffer,sizeof(buffer));
g=open("write.txt",O_WRONLY);
write(g,buffer,sizeof(buffer));
close(g);
fcntl(f,F_UNLCK);
close(f);
.......
then I tried to replace open with fopen (so I use FILE *g of course, and fprintf()) but still nothing was in that file(write.txt).So I have no clue. If you got sometime pls say somethin'. I need to do this as soon as I can. You are welcomed to say somethin' smart
10x

Last edited by atticus; 06-07-2006 at 02:40 PM.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 06-07-2006
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,861
I think you mean lock a file.

Try creating a file with stuff in it to start with
Code:
echo "now my file has stuff in it" > write.txt
fcntl(filedescriptor, F_SETLK, struct *flock ) three (3) arguments are required to make fcntl work for locking.

Note - a lot of unix kernels do not support mandatory locking. This means that anybody who breaks the rules (like your code is doing) will still be able to stomp on somebody else's file. Regardless of fcntl. In other words, locking depends on each C module playing by the rules.
Reply With Quote
  #3 (permalink)  
Old 06-08-2006
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,861
Read the C example at the end of this page:

http://www.opengroup.org/onlinepubs/...ons/fcntl.html
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:25 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0