writing to file with multiple forked process

This is a discussion on writing to file with multiple forked process within the C Programming forums, part of the General Programming Boards category; I am learning programming in C,
I have multiple forked process running, now I want them to read/write data from/to ...

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)

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.

"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

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.

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

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.

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...

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.