Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In addition of the sequence given in <a href="https://stackoverflow.com/a/17304744/841108">David Schwartz answer</a> you could perhaps use advisory locks with e.g. <a href="http://man7.org/linux/man-pages/man2/flock.2.html" rel="nofollow noreferrer">flock(2)</a> syscall (or maybe <a href="http://man7.org/linux/man-pages/man3/lockf.3.html" rel="nofollow noreferrer">lockf(3)</a> i.e. <a href="http://man7.org/linux/man-pages/man2/fcntl.2.html" rel="nofollow noreferrer">fcntl(2)</a> with <code>F_SETLK</code> ....)</p> <p>That would mean to add, just after</p> <pre><code> FILE * fil = fopen("/etc/routing_table.tmp", "wb"); </code></pre> <p>the lines</p> <pre><code> if (!fil) { perror("/etc/routing_table.tmp"); exit(EXIT_FAILURE); }; if (flock(fileno(fil), LOCK_EX)) { perror("flock LOCK_EX"); exit(EXIT_FAILURE); }; </code></pre> <p>and at the end, you would</p> <pre><code> if (fflush(fil)) /* flush the file before unlocking it!!*/ { perror("fflush"); exit(EXIT_FAILURE); }; if (flock(fileno(fil), LOCK_UN)) { perror("flock LOCK_UN"); exit(EXIT_FAILURE); }; if (fclose (fil)) { perror("fclose"); exit(EXIT_FAILURE); };; if (rename("/etc/routing_table.tmp", "/etc/routing_table")) { perror("rename"); exit(EXIT_FAILURE); }; </code></pre> <p>Using such advisory locking would ensure that even if two processes of your program are running, only one would write the file.</p> <p>But it is overkill probably.</p> <p>BTW, you seems to write binary data in <code>/etc/</code>. I believe it is against the habits or the conventions (see <a href="http://www.tldp.org/LDP/Linux-Filesystem-Hierarchy/html/Linux-Filesystem-Hierarchy.html" rel="nofollow noreferrer">Linux Filesystem Hierarchy</a>, or <a href="http://www.linuxfoundation.org/collaborate/workgroups/lsb" rel="nofollow noreferrer">Linux Standard Base</a>). I expect files under <code>/etc</code> to be textual. Perhaps you want your file under <code>/var/lib</code> ?</p> <p>See also <a href="http://advancedlinuxprogramming.com/" rel="nofollow noreferrer">Advanced Linux Programming</a> book online.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload