I have no clue what's the point - fcntl() ?


 
Thread Tools Search this Thread
Top Forums Programming I have no clue what's the point - fcntl() ?
# 1  
Old 06-07-2006
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 Smilie
10x

Last edited by atticus; 06-07-2006 at 06:40 PM..
# 2  
Old 06-07-2006
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.
# 3  
Old 06-08-2006
Read the C example at the end of this page:

http://www.opengroup.org/onlinepubs/...ons/fcntl.html
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Linux

Block with fcntl

Good evening, friends I'm learning with a book: Programming Linux by Kurt Wall (Prentice Hall) The code below could run in two windows (./lockit /tmp/foo in both for example). There is not problem with the read block (first byte) but when one, apply the write block while in the other is... (1 Reply)
Discussion started by: joe_cosmo
1 Replies

2. Shell Programming and Scripting

How to perform a hexdump using dd from start point to end point?

hi, I would like to ask or is it possible to dump a hex using dd from starting point to end point just like the "xxd -s 512 -l 512 <bin file>" I know the redirect hexdump -C but i can't figure it out the combination options of dd. Hope someone can share their knowledge.. Thanks in... (3 Replies)
Discussion started by: jao_madn
3 Replies

3. Shell Programming and Scripting

File Locking with fcntl on Darwin Mac OSX

Hello I have a Perl script that works on non-darwin Mac OS X environments and I think I have narrowed down the issue to a file locking problem. In other linux environments, the flock struct is defined differently. I have adjusted this via the reference for Mac OS X fcntl(2) man page. The... (4 Replies)
Discussion started by: flagman5
4 Replies

4. Programming

fcntl on socket

I have very simple client - server setup, where client connects, exchanges some messages with the server and then closes socket and exits. Everythink works OK, except when I am trying to change socket to be non-blocking AND the shell happens to be plain old Bourne. It runs no problem under ksh or... (3 Replies)
Discussion started by: migurus
3 Replies

5. Programming

fcntl works in linux but not in mac os x

Hi, Unless I am missing some serious differences in Mac and linux in terms of C programming, I dont know why this would happen. Please take a look at the following piece of code fragment: bool add_input_to_db(Cons *new_data) { // Set the attributes of the lock struct flock fl =... (3 Replies)
Discussion started by: newhere
3 Replies

6. Solaris

Filesystem filling up and no clue as to why!

df shows that the filesystem is filling up and the usage is 94%. However when I actually traverse to the directory I du shows only about 10% of the space occupied! Below is the output of df and du: >>>df -kh /cbmdata/00 470M 393M 29M 94% /cbmdata/00 >>>/cbmdata/00>... (3 Replies)
Discussion started by: zombiezparadize
3 Replies

7. UNIX for Advanced & Expert Users

Fibre connection Point to Point SUN

Anyone know of a guide or instructions for Solaris I got to configure a SBUS HBA to talk to a tape robot. I have done this on a switch but not point to point. just going HBA >>>>> TAPE Fibre simple two nodes Kie (6 Replies)
Discussion started by: kie
6 Replies
Login or Register to Ask a Question