{"id":96138,"date":"2020-10-08T19:42:26","date_gmt":"2020-10-08T17:42:26","guid":{"rendered":"https:\/\/prohoster.info\/blog\/administrirovanie\/znakomimsya-s-semaforami-v-linux"},"modified":"2020-10-08T19:42:26","modified_gmt":"2020-10-08T17:42:26","slug":"znakomimsya-s-semaforami-v-linux","status":"publish","type":"post","link":"https:\/\/prohoster.info\/en\/blog\/administrirovanie\/znakomimsya-s-semaforami-v-linux","title":{"rendered":"Introduction to Semaphores in Linux","gt_translate_keys":[{"key":"rendered","format":"text"}]},"content":{"rendered":"<p><b><i>This article's translation was prepared in anticipation of the course launch <noindex><a rel=\"nofollow\" href=\"https:\/\/otus.pw\/kRX0\/\">\u2018Administrator Linux.Basic\u2019<\/a><\/noindex>.<\/p>\n<p><img decoding=\"async\" alt=\"Introduction to Semaphores in Linux\" src=\"\/wp-content\/uploads\/2020\/10\/93604a8804db099834e873de072cbfc9.jpg\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<br \/>\n<\/i><\/b><\/p>\n<p>A semaphore is a mechanism that allows competing processes and threads to work with shared resources and helps solve various synchronization problems such as race conditions, deadlocks, and improper thread behavior.<\/p>\n<p>To address these problems, the kernel includes tools such as mutexes, semaphores, signals, and barriers.<\/p>\n<p>There are three types of semaphores:<\/p>\n<ol>\n<li>Binary semaphores<\/li>\n<li>Counting semaphores<\/li>\n<li>Semaphore sets<\/li>\n<\/ol>\n<p><noindex><a rel=\"nofollow\" name=\"habracut\"><\/a><\/noindex><\/p>\n<h3>Viewing IPC Status<\/h3>\n<p>\nUsing the commands below, you can obtain information about the current state of inter-process communication (IPC) tools.<\/p>\n<pre><code class=\"bash\"># ipcs\n------ Shared Memory Segments --------\nkey shmid owner perms bytes nattch status\n0x00000000 65536 root 600 393216 2 dest\n0x00000000 98305 root 600 393216 2 dest\n0x00000000 131074 root 600 393216 2 dest\n0x00000000 163843 root 600 393216 2 dest\n0x00000000 196612 root 600 393216 2 dest\n0x00000000 229381 root 600 393216 2 dest\n0x00000000 262150 root 600 393216 2 dest\n0x00000000 294919 root 600 393216 2 dest\n0x00000000 327688 root 600 393216 2 dest\n------ Semaphore Arrays --------\n\nkey semid owner perms nsems\n\n------ Message Queues --------\nkey msqid owner perms used-bytes messages<\/code><\/pre>\n<p><\/p>\n<h3>Active semaphore arrays<\/h3>\n<p>\nDisplays information about active semaphore arrays.<\/p>\n<pre><code class=\"bash\"># ipcs -s\n------ Semaphore Arrays --------\nkey semid owner perms nsems<\/code><\/pre>\n<p><\/p>\n<h3>Shared memory segments<\/h3>\n<p>\nView information about active shared memory segments.<\/p>\n<pre><code class=\"bash\"># ipcs -m\n------ Shared Memory Segments --------\nkey shmid owner perms bytes nattch status\n0x00000000 65536 root 600 393216 2 dest\n0x00000000 98305 root 600 393216 2 dest<\/code><\/pre>\n<p><\/p>\n<h3>Limits<\/h3>\n<p>\nThe command <code>ipcs -l<\/code> displays the limits on shared memory, semaphores, and messages.<\/p>\n<pre><code class=\"bash\"># ipcs -l\n------ Shared Memory Limits --------\nmax number of segments = 4096\nmax seg size (kbytes) = 4194303\nmax total shared memory (kbytes) = 1073741824\nmin seg size (bytes) = 1\n\n------ Semaphore Limits --------\nmax number of arrays = 128\nmax semaphores per array = 250\nmax semaphores system wide = 32000\nmax ops per semop call = 32\nsemaphore max value = 32767\n\n------ Messages: Limits --------\nmax queues system wide = 16\nmax size of message (bytes) = 65536\ndefault max size of queue (bytes) = 65536<\/code><\/pre>\n<p><\/p>\n<h3>Shared memory<\/h3>\n<p>\nThe command below displays shared memory.<\/p>\n<pre><code class=\"bash\"># ipcs -m\n------ Shared Memory Segments --------\nkey shmid owner perms bytes nattch status\n0x00000000 65536 root 600 393216 2 dest\n0x00000000 98305 root 600 393216 2 dest\n0x00000000 131074 root 600 393216 2 dest\n0x00000000 163843 root 600 393216 2 dest\n0x00000000 196612 root 600 393216 2 dest\n0x00000000 229381 root 600 393216 2 dest\n0x00000000 262150 root 600 393216 2 dest\n0x00000000 294919 root 600 393216 2 dest\n0x00000000 327688 root 600 393216 2 dest<\/code><\/pre>\n<p><\/p>\n<h3>Resource creators<\/h3>\n<p>\nThe command displays the user and group of the owner and creator of the resource.<\/p>\n<pre><code class=\"bash\"># ipcs -m -c\n\n------ Shared Memory Segment Creators\/Owners --------\nshmid perms cuid cgid uid gid\n65536 600 root root root root\n98305 600 root root root root\n131074 600 root root root root\n163843 600 root root root root\n196612 600 root root root root\n229381 600 root root root root\n262150 600 root root root root\n294919 600 root root root root\n327688 600 root root root root<\/code><\/pre>\n<p><\/p>\n<h3>Using IPC Tools<\/h3>\n<p>\nIn the example provided below, the parameter <code>-u<\/code> displays a summary of the utilization of all IPC tools.<\/p>\n<pre><code class=\"bash\"># ipcs -u\n\n------ Shared Memory Status --------\nsegments allocated 9\npages allocated 864\npages resident 477\npages swapped 0\nSwap performance: 0 attempts 0 successes\n\n------ Semaphore Status --------\nused arrays = 0\nallocated semaphores = 0\n\n------ Messages: Status --------\nallocated queues = 0\nused headers = 0\nused space = 0 bytes<\/code><\/pre>\n<p>\nWhen stopping services, semaphores and shared memory segments should also be removed. If they are not removed, this can be done using the ipcrm command by passing the IPC object ID.<\/p>\n<pre><code class=\"bash\"># ipcs -a\n# ipcrm -s &lt; sem id&gt;<\/code><\/pre>\n<p>\nLimits for semaphores can also be changed using <code>sysctl<\/code>.<\/p>\n<pre><code class=\"bash\"># \/sbin\/sysctl -w kernel.sem=250\n<\/code><\/pre>\n<p>\n<noindex><a rel=\"nofollow\" href=\"https:\/\/otus.pw\/kRX0\/\"><img decoding=\"async\" alt=\"Introduction to Semaphores in Linux\" src=\"\/wp-content\/uploads\/2020\/10\/e9f6606dbef02ed0dd83662ed9b40015.jpg\" style=\"display:block;margin: 0 auto;\" \/><\/a><\/noindex><br \/>\n<br \/>Source: <a content=\"nofollow\" rel=\"nofollow\" href=\"https:\/\/habr.com\/ru\/company\/otus\/blog\/522418\/\">habr.com<\/a> <\/p>","protected":false,"gt_translate_keys":[{"key":"rendered","format":"html"}]},"excerpt":{"rendered":"<p>\u041f\u0435\u0440\u0435\u0432\u043e\u0434 \u0441\u0442\u0430\u0442\u044c\u0438 \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043b\u0435\u043d \u0432 \u043f\u0440\u0435\u0434\u0434\u0432\u0435\u0440\u0438\u0438 \u0441\u0442\u0430\u0440\u0442\u0430 \u043a\u0443\u0440\u0441\u0430 \u00abAdministrator Linux.Basic\u00bb. \u0421\u0435\u043c\u0430\u0444\u043e\u0440 \u2013 \u044d\u0442\u043e \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043a\u043e\u043d\u043a\u0443\u0440\u0438\u0440\u0443\u044e\u0449\u0438\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430\u043c \u0438 \u043f\u043e\u0442\u043e\u043a\u0430\u043c \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u043e\u0431\u0449\u0438\u043c\u0438 \u0440\u0435\u0441\u0443\u0440\u0441\u0430\u043c\u0438 \u0438 \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 \u0432 \u0440\u0435\u0448\u0435\u043d\u0438\u0438 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0442\u0430\u043a\u0438\u0445 \u043a\u0430\u043a \u0433\u043e\u043d\u043a\u0438, \u0434\u0435\u0434\u043b\u043e\u043a\u0438 (\u0432\u0437\u0430\u0438\u043c\u043d\u044b\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438) \u0438 \u043d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0435 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u043f\u043e\u0442\u043e\u043a\u043e\u0432. \u0414\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u044d\u0442\u0438\u0445 \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u0432 \u044f\u0434\u0440\u0435 \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0442\u0430\u043a\u0438\u0435 \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430 \u043a\u0430\u043a \u043c\u044c\u044e\u0442\u0435\u043a\u0441\u044b, \u0441\u0435\u043c\u0430\u0444\u043e\u0440\u044b, \u0441\u0438\u0433\u043d\u0430\u043b\u044b \u0438 \u0431\u0430\u0440\u044c\u0435\u0440\u044b. [&hellip;]<\/p>\n","protected":false,"gt_translate_keys":[{"key":"rendered","format":"html"}]},"author":1,"featured_media":96139,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[688],"tags":[],"class_list":["post-96138","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-administrirovanie"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.1.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"\u041f\u0435\u0440\u0435\u0432\u043e\u0434 \u0441\u0442\u0430\u0442\u044c\u0438 \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043b\u0435\u043d \u0432 \u043f\u0440\u0435\u0434\u0434\u0432\u0435\u0440\u0438\u0438 \u0441\u0442\u0430\u0440\u0442\u0430 \u043a\u0443\u0440\u0441\u0430 \u00abAdministrator Linux.Basic\u00bb.\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Yuri Gagarin\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/prohoster.info\/en\/blog\/administrirovanie\/znakomimsya-s-semaforami-v-linux\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.1.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"ProHoster | \u041a\u0443\u043f\u0438\u0442\u044c \u043d\u0430\u0434\u0435\u0436\u043d\u044b\u0439 \u0445\u043e\u0441\u0442\u0438\u043d\u0433 \u0434\u043b\u044f \u0441\u0430\u0439\u0442\u043e\u0432 \u0441 \u0437\u0430\u0449\u0438\u0442\u043e\u0439 \u043e\u0442 DDoS, VPS VDS \u0441\u0435\u0440\u0432\u0435\u0440\u044b\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"\ud83e\udd47\u0417\u043d\u0430\u043a\u043e\u043c\u0438\u043c\u0441\u044f \u0441 \u0441\u0435\u043c\u0430\u0444\u043e\u0440\u0430\u043c\u0438 \u0432 Linux | ProHoster\" \/>\n\t\t<meta property=\"og:description\" content=\"\u041f\u0435\u0440\u0435\u0432\u043e\u0434 \u0441\u0442\u0430\u0442\u044c\u0438 \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043b\u0435\u043d \u0432 \u043f\u0440\u0435\u0434\u0434\u0432\u0435\u0440\u0438\u0438 \u0441\u0442\u0430\u0440\u0442\u0430 \u043a\u0443\u0440\u0441\u0430 \u00abAdministrator Linux.Basic\u00bb.\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/prohoster.info\/en\/blog\/administrirovanie\/znakomimsya-s-semaforami-v-linux\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg\" \/>\n\t\t<meta property=\"og:image:width\" content=\"350\" \/>\n\t\t<meta property=\"og:image:height\" content=\"350\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2020-10-08T17:42:26+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2020-10-08T17:42:26+00:00\" \/>\n\t\t<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prohoster\" \/>\n\t\t<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/prohoster\" \/>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"\ud83e\udd47Introduction to Semaphores in Linux | ProHoster","description":"The article translation has been prepared in anticipation of the launch of the \u2018Administrator Linux.Basic\u2019 course.","canonical_url":"https:\/\/prohoster.info\/en\/blog\/administrirovanie\/znakomimsya-s-semaforami-v-linux","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":null,"og:locale":"en_US","og:site_name":"ProHoster | \u041a\u0443\u043f\u0438\u0442\u044c \u043d\u0430\u0434\u0435\u0436\u043d\u044b\u0439 \u0445\u043e\u0441\u0442\u0438\u043d\u0433 \u0434\u043b\u044f \u0441\u0430\u0439\u0442\u043e\u0432 \u0441 \u0437\u0430\u0449\u0438\u0442\u043e\u0439 \u043e\u0442 DDoS, VPS VDS \u0441\u0435\u0440\u0432\u0435\u0440\u044b","og:type":"article","og:title":"\ud83e\udd47\u0417\u043d\u0430\u043a\u043e\u043c\u0438\u043c\u0441\u044f \u0441 \u0441\u0435\u043c\u0430\u0444\u043e\u0440\u0430\u043c\u0438 \u0432 Linux | ProHoster","og:description":"\u041f\u0435\u0440\u0435\u0432\u043e\u0434 \u0441\u0442\u0430\u0442\u044c\u0438 \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043b\u0435\u043d \u0432 \u043f\u0440\u0435\u0434\u0434\u0432\u0435\u0440\u0438\u0438 \u0441\u0442\u0430\u0440\u0442\u0430 \u043a\u0443\u0440\u0441\u0430 \u00abAdministrator Linux.Basic\u00bb.","og:url":"https:\/\/prohoster.info\/en\/blog\/administrirovanie\/znakomimsya-s-semaforami-v-linux","og:image":"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg","og:image:secure_url":"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg","og:image:width":350,"og:image:height":350,"article:published_time":"2020-10-08T17:42:26+00:00","article:modified_time":"2020-10-08T17:42:26+00:00","article:publisher":"https:\/\/www.facebook.com\/prohoster","article:author":"https:\/\/www.facebook.com\/prohoster"},"aioseo_meta_data":{"post_id":"96138","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"seo_analyzer_scan_date":null,"breadcrumb_settings":null,"limit_modified_date":false,"reviewed_by":null,"ai":null,"created":"2021-02-28 10:51:01","updated":"2022-09-29 07:52:31","focus_keyword":null,"additional_keywords":null,"truseo_locale":null},"gt_translate_keys":[{"key":"link","format":"url"}],"_links":{"self":[{"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/posts\/96138","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/comments?post=96138"}],"version-history":[{"count":0,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/posts\/96138\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/media\/96139"}],"wp:attachment":[{"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/media?parent=96138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/categories?post=96138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/tags?post=96138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}