Thread: writing to file with multiple forked process

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    3

    writing to file with multiple forked process

    I am learning programming in C,
    I have multiple forked process running, now I want them to read/write data from/to one file.
    My question is how can I do it safely? I am not sure if the one process is writing data to the file the second process can read it while first is busy(so I get wrong data)

    Thanx in advance

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    There are a few ways of doing it, some worse than others. What you decide will probably depend upon what you're trying to do. Are you just trying to communicate between the processes? If so, files are probably not what you want to use.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You could create some sort of lock: if the lock is in place, a thread would wait a few milliseconds and then see if the lock was still there.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by hiawatha View Post
    I am learning programming in C,
    I have multiple forked process running, now I want them to read/write data from/to one file.
    My question is how can I do it safely? I am not sure if the one process is writing data to the file the second process can read it while first is busy(so I get wrong data)

    Thanx in advance
    Typical solution is to use file locking. How you do this is platform dependent. There are different levels of file locking. The most basic methods allow you to lock the file so that nobody else is able to open it until it is unlocked. But since you intend multiple processes to have this file open at the same time, that isn't going to work.

    Finer-grained file locking is usually available, which lets you lock only certain portions of a file. Lacking that, you could implement your own locking system between the processes using some kind of interprocess synchronization, like UNIX IPC primitives.

    Getting this kind of code right is incredibly difficult. And it is very hard to prove definitively that there are no errors which might occur only one in a million times.

    It would help if you specified more exactly what you are trying to do.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    3
    Thanx all for your replays,
    @ brewbuck
    You are right. Its (with my level of knowledge) difficult to get it work properly.
    I googled with your comment and expand my vocabulary with such words as mutex and semaphores. So now I am sweating trying to understand and implement it. Its not working yet but is very interesting.

    QUESTION:
    Is it possible to declare a global structur with forked processes running? If yes how?
    With global int/float/char etc it works fine..but.
    I want to read file ones (for all forks) and after be able to read and maybe append some data dynamically. At this point every process creating its own empty structure and its not exactly what I want...... thanx in advance

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by hiawatha View Post
    QUESTION:
    Is it possible to declare a global structur with forked processes running? If yes how?
    In UNIX, there are two usual ways of sharing in-memory data between processes. You can use UNIX shared memory, or you can use a shared memory map.

    For UNIX shared memory, one process makes a special system call to create a shared memory region and associate a key with it. Other processes can then bind this region into their address space by using this key. Google the "shmget" and "shmctl" functions.

    The other approach is to create an actual file somewhere in the filesystem. The communicating processes then use mmap() to map this file into their address spaces. Changes to the pages of this mapped region will be reflected between all the processes. In this case, the filename itself acts like the "key."

    Of course, simply sharing the data is not enough. You still have the synchronization problem where the activities of two or more processes corrupt each other. So you must perform some kind of locking. This is where semaphores come into the picture. However, for certain kinds of data access patterns, a simpler method might be used. Again, it all depends what you're trying to do.

    EDIT: Also, you're not right in thinking that "simple" global variables get shared between the processes. They don't. When you call fork(), the entire address space is duplicated. In the child process, the values of all global variables are the same as they were in the parent, the instant it called fork(). But CHANGES to those variables do NOT reflect between the processes.

    It might be easier to just use threads instead. The memory space is shared automatically between threads, so the only thing you have to worry about is synchronization.
    Last edited by brewbuck; 04-18-2007 at 05:58 PM.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    3
    at this point I am just trying to learn basics of C,
    so I started with manipulating strings, then send them over the internet. Then I forked it and now try to understand IPC next step I will try to stream sound, but as I see it wont happen today )))
    and yes indeed its better done with threads...
    Last edited by hiawatha; 04-18-2007 at 06:17 PM.

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by hiawatha View Post
    at this point I am just trying to learn basics of C,
    so I started with manipulating strings, then send them over the internet. Then I forked it and now try to understand IPC next step I will try to stream sound, but as I see it wont happen today )))
    and yes indeed its better done with threads...
    Not today, but maybe next week! :-) The interfaces are intimidating but the concepts are not hard. You say you've figured out sockets, at least at a basic level. So you understand the basic gist of, "I grasp the concept but the interface is weird." The same goes for UNIX IPC.

    Suggestion: once you figure out how to use a particular API, package it up into your own SIMPLER interface while things are still fresh in your mind. Now you can use the simplified interface in other projects, referring back to the complicated stuff only when necessary.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM