question on files

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sreenadh494
    New Member
    • Aug 2007
    • 13

    question on files

    one process open file for writing, one more process also open same file for writing in this situvation file is courupted, how can we lock that file when process open that one from another one
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Originally posted by sreenadh494
    one process open file for writing, one more process also open same file for writing in this situvation file is courupted, how can we lock that file when process open that one from another one
    You could always create an "in use" variable to let other processes know that it is currently being written to.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by sreenadh494
      one process open file for writing, one more process also open same file for writing in this situvation file is courupted, how can we lock that file when process open that one from another one
      File creation is an atomic operation; that's why you see .lck files all over the place.
      Before you want to write to a file "foo", attempt to create a file "foo.lck". If that
      succeeds you can wite to "foo". When you're ready writing, remove file "foo.lck"
      again. If the creation failed, something else is writing to file "foo" so you should
      wait a bit or try again or whatever.

      kind regards,

      Jos

      Comment

      • vinot85
        New Member
        • Aug 2007
        • 53

        #4
        Hi freind,
        You can use fcntl() to lock and unlock the files between the process.
        It is available in <fcntl.h> header.

        Comment

        Working...