{"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\/it\/blog\/administrirovanie\/znakomimsya-s-semaforami-v-linux","title":{"rendered":"Introduzione ai semafori in Linux","gt_translate_keys":[{"key":"rendered","format":"text"}]},"content":{"rendered":"<p><b><i>La traduzione dell'articolo \u00e8 stata preparata in vista dell'inizio del corso <noindex><a rel=\"nofollow\" href=\"https:\/\/otus.pw\/kRX0\/\">\u00abAmministratore Linux.Basic\u00bb<\/a><\/noindex>.<\/p>\n<p><img decoding=\"async\" alt=\"Introduzione ai semafori 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>Il semaforo \u00e8 un meccanismo che consente ai processi e ai thread concorrenti di lavorare con risorse condivise e aiuta a risolvere vari problemi di sincronizzazione come le condizioni di gara, i deadlock (bloccaggi reciproci) e i comportamenti anomali dei thread.<\/p>\n<p>Per risolvere questi problemi, nel kernel sono presenti strumenti come mutex, semafori, segnali e barriere.<\/p>\n<p>Esistono tre tipi di semafori:<\/p>\n<ol>\n<li>Semafori binari (binary semaphore)<\/li>\n<li>Semafori contatori (counting semaphore)<\/li>\n<li>Array di semafori (semaphore set)<\/li>\n<\/ol>\n<p><noindex><a rel=\"nofollow\" name=\"habracut\"><\/a><\/noindex><\/p>\n<h3>Monitoraggio dello stato IPC<\/h3>\n<p>\nUtilizzando i comandi indicati di seguito, \u00e8 possibile ottenere informazioni sullo stato attuale degli strumenti di comunicazione tra processi (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>Array di semafori attivi<\/h3>\n<p>\nVisualizza informazioni sugli array di semafori attivi.<\/p>\n<pre><code class=\"bash\"># ipcs -s\n------ Semaphore Arrays --------\nkey semid owner perms nsems<\/code><\/pre>\n<p><\/p>\n<h3>Segmenti di memoria condivisa<\/h3>\n<p>\nVisualizza informazioni sui segmenti attivi di memoria condivisa.<\/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>Limiti<\/h3>\n<p>\nTeam <code>ipcs -l<\/code> mostra i limiti della memoria condivisa, dei semafori e dei messaggi.<\/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>Memoria condivisa<\/h3>\n<p>\nIl comando sottostante visualizza la memoria condivisa.<\/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>Creatori delle risorse<\/h3>\n<p>\nIl comando mostra l'utente e il gruppo del proprietario e del creatore della risorsa.<\/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>Utilizzo degli strumenti IPC<\/h3>\n<p>\nNell'esempio riportato di seguito, il parametro <code>-u<\/code> mostra un riepilogo sull'utilizzo di tutti gli strumenti 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>\nQuando i servizi vengono arrestati, i semafori e i segmenti di memoria condivisa devono essere rimossi. Se non vengono rimossi, \u00e8 possibile farlo utilizzando il comando ipcrm, passando l'identificatore dell'oggetto IPC.<\/p>\n<pre><code class=\"bash\"># ipcs -a\n# ipcrm -s &lt; sem id&gt;<\/code><\/pre>\n<p>\n\u00c8 anche possibile modificare i limiti dei semafori, utilizzando <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=\"Introduzione ai semafori in Linux\" src=\"\/wp-content\/uploads\/2020\/10\/e9f6606dbef02ed0dd83662ed9b40015.jpg\" style=\"display:block;margin: 0 auto;\" \/><\/a><\/noindex><br \/>\n<br \/>Fonte: <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\/it\/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=\"it_IT\" \/>\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\/it\/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\udd47Introduzione ai semafori in Linux | ProHoster","description":"La traduzione dell'articolo \u00e8 stata preparata in vista dell'inizio del corso \u00abAmministratore Linux.Basic\u00bb.","canonical_url":"https:\/\/prohoster.info\/it\/blog\/administrirovanie\/znakomimsya-s-semaforami-v-linux","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":null,"og:locale":"it_IT","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\/it\/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\/it\/wp-json\/wp\/v2\/posts\/96138","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prohoster.info\/it\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prohoster.info\/it\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prohoster.info\/it\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/prohoster.info\/it\/wp-json\/wp\/v2\/comments?post=96138"}],"version-history":[{"count":0,"href":"https:\/\/prohoster.info\/it\/wp-json\/wp\/v2\/posts\/96138\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/prohoster.info\/it\/wp-json\/wp\/v2\/media\/96139"}],"wp:attachment":[{"href":"https:\/\/prohoster.info\/it\/wp-json\/wp\/v2\/media?parent=96138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prohoster.info\/it\/wp-json\/wp\/v2\/categories?post=96138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prohoster.info\/it\/wp-json\/wp\/v2\/tags?post=96138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}