{"id":11920,"date":"2021-08-05T14:33:17","date_gmt":"2021-08-05T14:33:17","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=11920"},"modified":"2024-02-16T10:34:30","modified_gmt":"2024-02-16T10:34:30","slug":"inter-process-synchronization-linux","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=11920","title":{"rendered":"Inter process synchronization (Linux)"},"content":{"rendered":"<p>Synchronizes the access on critical sections between processes.<\/p>\n<hr>\n<p><strong>Prototypic lock file<\/strong> (with symlink)<\/p>\n<pre class=\"brush: cpp; gutter: false; title: lockfile.hpp; notranslate\" title=\"lockfile.hpp\">\r\n#include &lt;string&gt;\r\n\r\nclass LockFile\r\n{\r\npublic:\r\n  LockFile(std::string&amp; fpath);\r\n  ~LockFile();\r\n \r\nprivate:\r\n  void lock();\r\n  void unlock();\r\n \r\n  std::string m_fpath;\r\n};\r\n<\/pre>\n<pre class=\"brush: cpp; gutter: false; title: lockfile.cpp; notranslate\" title=\"lockfile.cpp\">\r\n#include &quot;lockfile.hpp&quot;\r\n#include &lt;unistd.h&gt;\r\n \r\nLockFile::LockFile(std::string&amp; fpath) : m_fpath(fpath)\r\n{\r\n  lock();\r\n}\r\n \r\nLockFile::~LockFile()\r\n{\r\n  unlock();\r\n}\r\n \r\nvoid LockFile::lock()\r\n{\r\n  std::string pid = std::to_string(getpid());\r\n  while (symlink(pid.c_str(), m_fpath.c_str()))\r\n  {\r\n    usleep(500000);\r\n  }\r\n}\r\n \r\nvoid LockFile::unlock()\r\n{\r\n  unlink(m_fpath.c_str());\r\n}\r\n<\/pre>\n<pre class=\"brush: cpp; gutter: false; title: main.cpp; notranslate\" title=\"main.cpp\">\r\n#include &quot;lockfile.hpp&quot;\r\n#include &lt;string&gt;\r\n \r\nint main()\r\n{\r\n  std::string fpath(&quot;\/tmp\/lock_file&quot;);\r\n  LockFile lockfile(fpath);\r\n  \/\/ do synchronized stuff\r\n  return 0;\r\n}\r\n<\/pre>\n<hr>\n<p><strong>Alternative<\/strong><\/p>\n<p>Boost has an interprocess namespace which provides for example a mutex in shared memory.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Synchronizes the access on critical sections between processes. Prototypic lock file (with symlink) Alternative Boost has an interprocess namespace which<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false},"categories":[65],"tags":[],"_links":{"self":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/11920"}],"collection":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=11920"}],"version-history":[{"count":1,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/11920\/revisions"}],"predecessor-version":[{"id":16792,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/11920\/revisions\/16792"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11920"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11920"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11920"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}