• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1562
  • Last Modified:

do i need to use flock in c program?

i am writing the socket program in c (to update the text file)
do i need to use flock to lock the text file(like perl) to prevent the file to be updated by more than one client concurrently?
0
cymancalvin
Asked:
cymancalvin
  • 5
  • 4
  • 2
  • +2
1 Solution
 
Kent OlsenData Warehouse Architect / DBACommented:

No.  flock() is a JAVA function.  C has no direct equivalent.


Kent
0
 
cymancalvinAuthor Commented:
so, if i don't lock the file. will it casue problem (if more than one client update the file concurrently)?
0
 
Kent OlsenData Warehouse Architect / DBACommented:

C doesn't have a mechanism for locking files.  If your server is multitasking, so that several tasks could try to update the file at one time, you'll have to build your own locking.

0
Firewall Management 201 with Professor Wool

In this whiteboard video, Professor Wool highlights the challenges, benefits and trade-offs of utilizing zero-touch automation for security policy change management. Watch and Learn!

 
cymancalvinAuthor Commented:
that mean i can't use the build-in function such as flock to lock the file ?
0
 
Kent OlsenData Warehouse Architect / DBACommented:

Right.  It doesn't exist in C.

0
 
PaulCaswellCommented:
There are some Microsoft C extensions available in VC under windows that have been around for a while. The central function is _sopen which is like the low-level open but with extras.

Here's a copy of some sample code from MSDN that describes it:

/* LOCKING.C: This program opens a file with sharing. It locks
 * some bytes before reading them, then unlocks them. Note that the
 * program works correctly only if the file exists.
 */

#include <io.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/locking.h>
#include <share.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>

void main( void )
{
   int  fh, numread;
   char buffer[40];

   /* Quit if can't open file or system doesn't
    * support sharing.
    */
   fh = _sopen( "locking.c", _O_RDWR, _SH_DENYNO,
                 _S_IREAD | _S_IWRITE );
   if( fh == -1 )
      exit( 1 );

   /* Lock some bytes and read them. Then unlock. */
   if( _locking( fh, LK_NBLCK, 30L ) != -1 )
   {
      printf( "No one can change these bytes while I'm reading them\n" );
      numread = _read( fh, buffer, 30 );
      printf( "%d bytes read: %.30s\n", numread, buffer );
      lseek( fh, 0L, SEEK_SET );
     _locking( fh, LK_UNLCK, 30L );
      printf( "Now I'm done. Do what you will with them\n" );
   }
   else
      perror( "Locking failed\n" );

   _close( fh );
}


It may be worth a loog if you are running under Windoze.

Paul
0
 
brettmjohnsonCommented:
In spite of what Kent says, there is lockf(), flock(), and file locking using ioctl():

NAME
     lockf - record locking on files

LIBRARY
     Standard C Library (libc, -lc)

SYNOPSIS
     #include <unistd.h>

     int
     lockf(int filedes, int function, off_t size);

DESCRIPTION
     The lockf() function allows sections of a file to be locked with advi-
     sory-mode locks.  Calls to lockf() from other processes which attempt to
     lock the locked file section will either return an error value or block
     until the section becomes unlocked.  All the locks for a process are
     removed when the process terminates.

.....


NAME
     flock - apply or remove an advisory lock on an open file

SYNOPSIS
     #include <sys/file.h>
     #define   LOCK_SH   1    /* shared lock */
     #define   LOCK_EX   2    /* exclusive lock */
     #define   LOCK_NB   4    /* don't block when locking */
     #define   LOCK_UN   8    /* unlock */

     int
     flock(int fd, int operation);

DESCRIPTION
     Flock() applies or removes an advisory lock on the file associated with
     the file descriptor fd.  A lock is applied by specifying an operation
     parameter that is one of LOCK_SH or LOCK_EX with the optional addition of
     LOCK_NB.  To unlock an existing lock operation should be LOCK_UN.

.....
0
 
Kent OlsenData Warehouse Architect / DBACommented:

Hi Brett,

 lockf() and flock(), aren't ANSI and don't have Windows equivalents.

But they'll certainly work on a unix environment.  Since the poster referenced JAVA functions, I assumed that he's in a Window's environment.


Kent
0
 
brettmjohnsonCommented:
0
 
Kent OlsenData Warehouse Architect / DBACommented:

Don't know where my head it tonight.  Maybe I should just take the rest of the night off and catch up on my

zzzzzz..........
0
 
cymancalvinAuthor Commented:
flock(int fd, int operation);
fd is an int type but the file is FILE type, so how can i use this? can i have some example?
0
 
B1-66ERCommented:
> fd is an int type but the file is FILE type, so how can i use this?

you can use function:
int fileno(FILE *) ;
which examines the argument stream and returns its integer descriptor.

cymancalvin in what OS you want to lock files ??
0
 
cymancalvinAuthor Commented:
i am doing this in Linux (Fedora)

So is that i can use the flock() function to lock the file same as i write cgi in perl?
or it is no need to lock the file in c programming ?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

How do you know if your security is working?

Protecting your business doesn’t have to mean sifting through endless alerts and notifications. With WatchGuard Total Security Suite, you can feel confident that your business is secure, meaning you can get back to the things that have been sitting on your to-do list.

  • 5
  • 4
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now