{"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\/pl\/blog\/administrirovanie\/znakomimsya-s-semaforami-v-linux","title":{"rendered":"Poznajemy semafory w Linux","gt_translate_keys":[{"key":"rendered","format":"text"}]},"content":{"rendered":"<p><b><i>Przek\u0142ad artyku\u0142u przygotowany przed rozpocz\u0119ciem kursu <noindex><a rel=\"nofollow\" href=\"https:\/\/otus.pw\/kRX0\/\">\u00abAdministrator Linux.Basic\u00bb<\/a><\/noindex>.<\/p>\n<p><img decoding=\"async\" alt=\"Poznajemy semafory w Linux\" src=\"\/wp-content\/uploads\/2020\/10\/93604a8804db099834e873de072cbfc9.jpg\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<br \/>\n<\/i><\/b><\/p>\n<p>Semafor to mechanizm, kt\u00f3ry pozwala konkuruj\u0105cym procesom i w\u0105tkom na prac\u0119 z wsp\u00f3lnymi zasobami i pomaga w rozwi\u0105zywaniu r\u00f3\u017cnych problem\u00f3w synchronizacji, takich jak wy\u015bcigi, blokady wzajemne (deadlock) i niew\u0142a\u015bciwe zachowanie w\u0105tk\u00f3w.<\/p>\n<p>Aby rozwi\u0105za\u0107 te problemy, w j\u0105drze dost\u0119pne s\u0105 takie narz\u0119dzia jak mutexy, semafory, sygna\u0142y i bariery.<\/p>\n<p>Istniej\u0105 trzy rodzaje semafor\u00f3w:<\/p>\n<ol>\n<li>Semafory binarne (binary semaphore)<\/li>\n<li>Semafory zliczaj\u0105ce (counting semaphore)<\/li>\n<li>Zbiory semafor\u00f3w (semaphore set)<\/li>\n<\/ol>\n<p><noindex><a rel=\"nofollow\" name=\"habracut\"><\/a><\/noindex><\/p>\n<h3>Wy\u015bwietlanie stanu IPC<\/h3>\n<p>\nZa pomoc\u0105 poni\u017cszych polece\u0144 mo\u017cna uzyska\u0107 informacje o bie\u017c\u0105cym stanie \u015brodk\u00f3w interprocesowej komunikacji (inter-process communication, IPC).<\/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>Aktywne zbiory semafor\u00f3w<\/h3>\n<p>\nWy\u015bwietlanie informacji o aktywnych zbiorach semafor\u00f3w.<\/p>\n<pre><code class=\"bash\"># ipcs -s\n------ Semaphore Arrays --------\nkey semid owner perms nsems<\/code><\/pre>\n<p><\/p>\n<h3>Segmenty pami\u0119ci dzielonej<\/h3>\n<p>\nWy\u015bwietlanie informacji o aktywnych segmentach pami\u0119ci dzielonej.<\/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>Limity<\/h3>\n<p>\nZesp\u00f3\u0142 <code>ipcs -l<\/code> wy\u015bwietla limity pami\u0119ci dzielonej, semafor\u00f3w i komunikat\u00f3w.<\/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>Pami\u0119\u0107 dzielona<\/h3>\n<p>\nPoni\u017csze polecenie wy\u015bwietla pami\u0119\u0107 dzielon\u0105.<\/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>Tw\u00f3rcy zasob\u00f3w<\/h3>\n<p>\nPolecenie wy\u015bwietla u\u017cytkownika i grup\u0119 w\u0142a\u015bciciela i tw\u00f3rcy zasobu.<\/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>U\u017cycie \u015brodk\u00f3w IPC<\/h3>\n<p>\nW przyk\u0142adzie poni\u017cej parametr <code>-u<\/code> wy\u015bwietla podsumowanie wykorzystania wszystkich \u015brodk\u00f3w IPC.<\/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>\nPodczas zatrzymywania us\u0142ug semafory i segmenty pami\u0119ci dzielonej powinny r\u00f3wnie\u017c by\u0107 usuwane. Je\u015bli nie s\u0105 usuwane, mo\u017cna to zrobi\u0107 za pomoc\u0105 polecenia ipcrm, przekazuj\u0105c identyfikator obiektu IPC.<\/p>\n<pre><code class=\"bash\"># ipcs -a\n# ipcrm -s &lt; sem id&gt;<\/code><\/pre>\n<p>\nMo\u017cna r\u00f3wnie\u017c zmieni\u0107 limity semafor\u00f3w, u\u017cywaj\u0105c <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=\"Poznajemy semafory w Linux\" src=\"\/wp-content\/uploads\/2020\/10\/e9f6606dbef02ed0dd83662ed9b40015.jpg\" style=\"display:block;margin: 0 auto;\" \/><\/a><\/noindex><br \/>\n<br \/>\u0179r\u00f3d\u0142o: <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\/pl\/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=\"pl_PL\" \/>\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\/pl\/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\udd47Poznajemy semafory w Linuxie | ProHoster","description":"T\u0142umaczenie artyku\u0142u przygotowane w zwi\u0105zku z rozpocz\u0119ciem kursu \u00abAdministrator Linux.Basic\u00bb.","canonical_url":"https:\/\/prohoster.info\/pl\/blog\/administrirovanie\/znakomimsya-s-semaforami-v-linux","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":null,"og:locale":"pl_PL","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\/pl\/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\/pl\/wp-json\/wp\/v2\/posts\/96138","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prohoster.info\/pl\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prohoster.info\/pl\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prohoster.info\/pl\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/prohoster.info\/pl\/wp-json\/wp\/v2\/comments?post=96138"}],"version-history":[{"count":0,"href":"https:\/\/prohoster.info\/pl\/wp-json\/wp\/v2\/posts\/96138\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/prohoster.info\/pl\/wp-json\/wp\/v2\/media\/96139"}],"wp:attachment":[{"href":"https:\/\/prohoster.info\/pl\/wp-json\/wp\/v2\/media?parent=96138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prohoster.info\/pl\/wp-json\/wp\/v2\/categories?post=96138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prohoster.info\/pl\/wp-json\/wp\/v2\/tags?post=96138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}