created lab report pdf

This commit is contained in:
Ziad Arafat committed 2021-09-27 14:05:37 -06:00
1 parent c4809ade6a
commit ac4b97cd53
3 files changed
+547

No files matched your search

+412
View File
@@ -0,0 +1,412 @@
<!DOCTYPE html><html><head>
<title>README</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="file:////home/doge/.vscode/extensions/shd101wyy.markdown-preview-enhanced-0.6.0/node_modules/@shd101wyy/mume/dependencies/katex/katex.min.css">
<style>
/**
* prism.js Github theme based on GitHub's theme.
* @author Sam Clarke
*/
code[class*="language-"],
pre[class*="language-"] {
color: #333;
background: none;
font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;
text-align: left;
white-space: pre;
word-spacing: normal;
word-break: normal;
word-wrap: normal;
line-height: 1.4;
-moz-tab-size: 8;
-o-tab-size: 8;
tab-size: 8;
-webkit-hyphens: none;
-moz-hyphens: none;
-ms-hyphens: none;
hyphens: none;
}
/* Code blocks */
pre[class*="language-"] {
padding: .8em;
overflow: auto;
/* border: 1px solid #ddd; */
border-radius: 3px;
/* background: #fff; */
background: #f5f5f5;
}
/* Inline code */
:not(pre) > code[class*="language-"] {
padding: .1em;
border-radius: .3em;
white-space: normal;
background: #f5f5f5;
}
.token.comment,
.token.blockquote {
color: #969896;
}
.token.cdata {
color: #183691;
}
.token.doctype,
.token.punctuation,
.token.variable,
.token.macro.property {
color: #333;
}
.token.operator,
.token.important,
.token.keyword,
.token.rule,
.token.builtin {
color: #a71d5d;
}
.token.string,
.token.url,
.token.regex,
.token.attr-value {
color: #183691;
}
.token.property,
.token.number,
.token.boolean,
.token.entity,
.token.atrule,
.token.constant,
.token.symbol,
.token.command,
.token.code {
color: #0086b3;
}
.token.tag,
.token.selector,
.token.prolog {
color: #63a35c;
}
.token.function,
.token.namespace,
.token.pseudo-element,
.token.class,
.token.class-name,
.token.pseudo-class,
.token.id,
.token.url-reference .token.variable,
.token.attr-name {
color: #795da3;
}
.token.entity {
cursor: help;
}
.token.title,
.token.title .token.punctuation {
font-weight: bold;
color: #1d3e81;
}
.token.list {
color: #ed6a43;
}
.token.inserted {
background-color: #eaffea;
color: #55a532;
}
.token.deleted {
background-color: #ffecec;
color: #bd2c00;
}
.token.bold {
font-weight: bold;
}
.token.italic {
font-style: italic;
}
/* JSON */
.language-json .token.property {
color: #183691;
}
.language-markup .token.tag .token.punctuation {
color: #333;
}
/* CSS */
code.language-css,
.language-css .token.function {
color: #0086b3;
}
/* YAML */
.language-yaml .token.atrule {
color: #63a35c;
}
code.language-yaml {
color: #183691;
}
/* Ruby */
.language-ruby .token.function {
color: #333;
}
/* Markdown */
.language-markdown .token.url {
color: #795da3;
}
/* Makefile */
.language-makefile .token.symbol {
color: #795da3;
}
.language-makefile .token.variable {
color: #183691;
}
.language-makefile .token.builtin {
color: #0086b3;
}
/* Bash */
.language-bash .token.keyword {
color: #0086b3;
}
/* highlight */
pre[data-line] {
position: relative;
padding: 1em 0 1em 3em;
}
pre[data-line] .line-highlight-wrapper {
position: absolute;
top: 0;
left: 0;
background-color: transparent;
display: block;
width: 100%;
}
pre[data-line] .line-highlight {
position: absolute;
left: 0;
right: 0;
padding: inherit 0;
margin-top: 1em;
background: hsla(24, 20%, 50%,.08);
background: linear-gradient(to right, hsla(24, 20%, 50%,.1) 70%, hsla(24, 20%, 50%,0));
pointer-events: none;
line-height: inherit;
white-space: pre;
}
pre[data-line] .line-highlight:before,
pre[data-line] .line-highlight[data-end]:after {
content: attr(data-start);
position: absolute;
top: .4em;
left: .6em;
min-width: 1em;
padding: 0 .5em;
background-color: hsla(24, 20%, 50%,.4);
color: hsl(24, 20%, 95%);
font: bold 65%/1.5 sans-serif;
text-align: center;
vertical-align: .3em;
border-radius: 999px;
text-shadow: none;
box-shadow: 0 1px white;
}
pre[data-line] .line-highlight[data-end]:after {
content: attr(data-end);
top: auto;
bottom: .4em;
}html body{font-family:"Helvetica Neue",Helvetica,"Segoe UI",Arial,freesans,sans-serif;font-size:16px;line-height:1.6;color:#333;background-color:#fff;overflow:initial;box-sizing:border-box;word-wrap:break-word}html body>:first-child{margin-top:0}html body h1,html body h2,html body h3,html body h4,html body h5,html body h6{line-height:1.2;margin-top:1em;margin-bottom:16px;color:#000}html body h1{font-size:2.25em;font-weight:300;padding-bottom:.3em}html body h2{font-size:1.75em;font-weight:400;padding-bottom:.3em}html body h3{font-size:1.5em;font-weight:500}html body h4{font-size:1.25em;font-weight:600}html body h5{font-size:1.1em;font-weight:600}html body h6{font-size:1em;font-weight:600}html body h1,html body h2,html body h3,html body h4,html body h5{font-weight:600}html body h5{font-size:1em}html body h6{color:#5c5c5c}html body strong{color:#000}html body del{color:#5c5c5c}html body a:not([href]){color:inherit;text-decoration:none}html body a{color:#08c;text-decoration:none}html body a:hover{color:#00a3f5;text-decoration:none}html body img{max-width:100%}html body>p{margin-top:0;margin-bottom:16px;word-wrap:break-word}html body>ul,html body>ol{margin-bottom:16px}html body ul,html body ol{padding-left:2em}html body ul.no-list,html body ol.no-list{padding:0;list-style-type:none}html body ul ul,html body ul ol,html body ol ol,html body ol ul{margin-top:0;margin-bottom:0}html body li{margin-bottom:0}html body li.task-list-item{list-style:none}html body li>p{margin-top:0;margin-bottom:0}html body .task-list-item-checkbox{margin:0 .2em .25em -1.8em;vertical-align:middle}html body .task-list-item-checkbox:hover{cursor:pointer}html body blockquote{margin:16px 0;font-size:inherit;padding:0 15px;color:#5c5c5c;background-color:#f0f0f0;border-left:4px solid #d6d6d6}html body blockquote>:first-child{margin-top:0}html body blockquote>:last-child{margin-bottom:0}html body hr{height:4px;margin:32px 0;background-color:#d6d6d6;border:0 none}html body table{margin:10px 0 15px 0;border-collapse:collapse;border-spacing:0;display:block;width:100%;overflow:auto;word-break:normal;word-break:keep-all}html body table th{font-weight:bold;color:#000}html body table td,html body table th{border:1px solid #d6d6d6;padding:6px 13px}html body dl{padding:0}html body dl dt{padding:0;margin-top:16px;font-size:1em;font-style:italic;font-weight:bold}html body dl dd{padding:0 16px;margin-bottom:16px}html body code{font-family:Menlo,Monaco,Consolas,'Courier New',monospace;font-size:.85em !important;color:#000;background-color:#f0f0f0;border-radius:3px;padding:.2em 0}html body code::before,html body code::after{letter-spacing:-0.2em;content:"\00a0"}html body pre>code{padding:0;margin:0;font-size:.85em !important;word-break:normal;white-space:pre;background:transparent;border:0}html body .highlight{margin-bottom:16px}html body .highlight pre,html body pre{padding:1em;overflow:auto;font-size:.85em !important;line-height:1.45;border:#d6d6d6;border-radius:3px}html body .highlight pre{margin-bottom:0;word-break:normal}html body pre code,html body pre tt{display:inline;max-width:initial;padding:0;margin:0;overflow:initial;line-height:inherit;word-wrap:normal;background-color:transparent;border:0}html body pre code:before,html body pre tt:before,html body pre code:after,html body pre tt:after{content:normal}html body p,html body blockquote,html body ul,html body ol,html body dl,html body pre{margin-top:0;margin-bottom:16px}html body kbd{color:#000;border:1px solid #d6d6d6;border-bottom:2px solid #c7c7c7;padding:2px 4px;background-color:#f0f0f0;border-radius:3px}@media print{html body{background-color:#fff}html body h1,html body h2,html body h3,html body h4,html body h5,html body h6{color:#000;page-break-after:avoid}html body blockquote{color:#5c5c5c}html body pre{page-break-inside:avoid}html body table{display:table}html body img{display:block;max-width:100%;max-height:100%}html body pre,html body code{word-wrap:break-word;white-space:pre}}.markdown-preview{width:100%;height:100%;box-sizing:border-box}.markdown-preview .pagebreak,.markdown-preview .newpage{page-break-before:always}.markdown-preview pre.line-numbers{position:relative;padding-left:3.8em;counter-reset:linenumber}.markdown-preview pre.line-numbers>code{position:relative}.markdown-preview pre.line-numbers .line-numbers-rows{position:absolute;pointer-events:none;top:1em;font-size:100%;left:0;width:3em;letter-spacing:-1px;border-right:1px solid #999;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none}.markdown-preview pre.line-numbers .line-numbers-rows>span{pointer-events:none;display:block;counter-increment:linenumber}.markdown-preview pre.line-numbers .line-numbers-rows>span:before{content:counter(linenumber);color:#999;display:block;padding-right:.8em;text-align:right}.markdown-preview .mathjax-exps .MathJax_Display{text-align:center !important}.markdown-preview:not([for="preview"]) .code-chunk .btn-group{display:none}.markdown-preview:not([for="preview"]Line truncated
/* Please visit the URL below for more information: */
/* https://shd101wyy.github.io/markdown-preview-enhanced/#/customize-css */
</style>
</head>
<body for="html-export">
<div class="mume markdown-preview ">
<h1 class="mume-header" id="program-15">Program 1.5</h1>
<p>This is a program to see if forked processes will respect a shared memory semaphore.</p>
<pre data-role="codeBlock" data-info="c" class="language-c"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/types.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/ipc.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/shm.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;stdio.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;stdlib.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;unistd.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/wait.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;semaphore.h&gt;</span></span>
<span class="token comment">// Ziad Arafat</span>
<span class="token comment">// Sep 25 2021</span>
<span class="token comment">/**
* Changelog:
* Sep 25 2021
* Used my old OS class code to create and manage shared memory
* Added another part of my old OS class code to create an IPC semaphore
*
* Sep 26 2021
* BUGFIX: Semaphore was not being respected by both processes because the
* sem_init() call was not enabling shared memory semaphore by setting the
* second parameter to 1.
*
* Sep 27 2021
* Cleaned up and documented code
*
*/</span>
<span class="token comment">/**
* @brief This program will demonstrate if a forked process will respect a
* shared memory semaphore created in the parent code. We will achieve this by
* forcing the child to enter a critical segment protected by a mutex first and
* then forcing the parent to wait 10 seconds on the child. Print statements
* will help us know the order in which code was executed. If we succeed then
* the parent will never enter the critical section until the child has exited
* it. Otherwise the mutex is not being respected.
*/</span>
<span class="token comment">/**
* Results:
* This is the output from running the code
* We can know that the semaphore behaves correctly because the parent
* does not enter the mutex until after the child has left.
*
* parent making child and waiting 2 seconds
* child in mutex
* child leaving mutex
* child has left the mutex
* parent in mutex
* parent is out of mutex
*/</span>
<span class="token comment">// create key for shared memory</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">SHMKEY</span> <span class="token expression"><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token class-name">key_t</span><span class="token punctuation">)</span><span class="token number">1497</span><span class="token punctuation">)</span></span></span>
<span class="token comment">// define struct to store value in</span>
<span class="token comment">// This might not be necessary but this is how I did it in OS class.</span>
<span class="token keyword">typedef</span> <span class="token keyword">struct</span> <span class="token punctuation">{</span>
<span class="token class-name">sem_t</span> mut<span class="token punctuation">;</span>
<span class="token punctuation">}</span> shared_mem<span class="token punctuation">;</span>
<span class="token comment">// initialize memory</span>
<span class="token keyword">static</span> shared_mem <span class="token operator">*</span>mutex<span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">int</span> shmid<span class="token punctuation">;</span> <span class="token comment">// The shared memory id</span>
<span class="token keyword">int</span> pid1<span class="token punctuation">;</span> <span class="token comment">// The pid returned by the forked process</span>
<span class="token keyword">char</span> <span class="token operator">*</span>shmadd_ptr<span class="token punctuation">;</span> <span class="token comment">// The address of the shared memory</span>
shmadd_ptr <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span><span class="token punctuation">)</span><span class="token number">0</span><span class="token punctuation">;</span>
<span class="token comment">// If shmget doesn&apos;t return a valid memory address then barf</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>shmid <span class="token operator">=</span> <span class="token function">shmget</span><span class="token punctuation">(</span>SHMKEY<span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span><span class="token punctuation">,</span> IPC_CREAT <span class="token operator">|</span> <span class="token number">0666</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">&lt;</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">&quot;uh oh, shmget failed&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">exit</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">// check if the shared mem attached correctly.</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>mutex <span class="token operator">=</span> <span class="token punctuation">(</span>shared_mem <span class="token operator">*</span><span class="token punctuation">)</span><span class="token function">shmat</span><span class="token punctuation">(</span>shmid<span class="token punctuation">,</span> shmadd_ptr<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">==</span>
<span class="token punctuation">(</span>shared_mem <span class="token operator">*</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">&quot;shmat failed&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">exit</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token function">sem_init</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>mutex<span class="token operator">-&gt;</span>mut<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// initialize the semaphore to 1</span>
<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;parent making child and waiting 2 seconds\n&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>pid1 <span class="token operator">=</span> <span class="token function">fork</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// parent process</span>
<span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Wait 2 seconds so the child will enter first</span>
<span class="token function">sem_wait</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>mutex<span class="token operator">-&gt;</span>mut<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// try to enter critical segment.</span>
<span class="token comment">// This must not run before child leaves the mutex</span>
<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;parent in mutex\n&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">sem_post</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>mutex<span class="token operator">-&gt;</span>mut<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;parent is out of mutex\n&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">waitpid</span><span class="token punctuation">(</span>pid1<span class="token punctuation">,</span> <span class="token constant">NULL</span><span class="token punctuation">,</span> WEXITED<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// wait for child to close</span>
<span class="token function">sem_destroy</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>mutex<span class="token operator">-&gt;</span>mut<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// clean up the mutex.</span>
<span class="token comment">// if we can&apos;t delete the memory, barf</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">shmdt</span><span class="token punctuation">(</span>mutex<span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// clean up the shared memory</span>
<span class="token comment">// barf</span>
<span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">&quot;Cant delete the shared memory&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">exit</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token function">exit</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// exit parent</span>
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> <span class="token comment">// child process</span>
<span class="token function">sem_wait</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>mutex<span class="token operator">-&gt;</span>mut<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// enter the critical segment</span>
<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;child in mutex\n&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// sleep for 10 seconds so the parent has to wait.</span>
<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;child leaving mutex\n&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">sem_post</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>mutex<span class="token operator">-&gt;</span>mut<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;child has left the mutex\n&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">exit</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// exit child</span>
<span class="token punctuation">}</span>
<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</pre>
</div>
</body></html>
+135
View File
@@ -1,3 +1,138 @@
# Program 1.5
This is a program to see if forked processes will respect a shared memory semaphore.
```c
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <semaphore.h>
// Ziad Arafat
// Sep 25 2021
/**
* Changelog:
* Sep 25 2021
* Used my old OS class code to create and manage shared memory
* Added another part of my old OS class code to create an IPC semaphore
*
* Sep 26 2021
* BUGFIX: Semaphore was not being respected by both processes because the
* sem_init() call was not enabling shared memory semaphore by setting the
* second parameter to 1.
*
* Sep 27 2021
* Cleaned up and documented code
*
*/
/**
* @brief This program will demonstrate if a forked process will respect a
* shared memory semaphore created in the parent code. We will achieve this by
* forcing the child to enter a critical segment protected by a mutex first and
* then forcing the parent to wait 10 seconds on the child. Print statements
* will help us know the order in which code was executed. If we succeed then
* the parent will never enter the critical section until the child has exited
* it. Otherwise the mutex is not being respected.
*/
/**
* Results:
* This is the output from running the code
* We can know that the semaphore behaves correctly because the parent
* does not enter the mutex until after the child has left.
*
* parent making child and waiting 2 seconds
* child in mutex
* child leaving mutex
* child has left the mutex
* parent in mutex
* parent is out of mutex
*/
// create key for shared memory
#define SHMKEY ((key_t)1497)
// define struct to store value in
// This might not be necessary but this is how I did it in OS class.
typedef struct {
sem_t mut;
} shared_mem;
// initialize memory
static shared_mem *mutex;
int main()
{
int shmid; // The shared memory id
int pid1; // The pid returned by the forked process
char *shmadd_ptr; // The address of the shared memory
shmadd_ptr = (char *)0;
// If shmget doesn't return a valid memory address then barf
if ((shmid = shmget(SHMKEY, sizeof(int), IPC_CREAT | 0666)) < 0) {
perror("uh oh, shmget failed");
exit(1);
}
// check if the shared mem attached correctly.
if ((mutex = (shared_mem *)shmat(shmid, shmadd_ptr, 0)) ==
(shared_mem *)-1) {
perror("shmat failed");
exit(1);
}
sem_init(&mutex->mut, 1, 1); // initialize the semaphore to 1
printf("parent making child and waiting 2 seconds\n");
if ((pid1 = fork()) == 0) { // parent process
sleep(2); // Wait 2 seconds so the child will enter first
sem_wait(&mutex->mut); // try to enter critical segment.
// This must not run before child leaves the mutex
printf("parent in mutex\n");
sem_post(&mutex->mut);
printf("parent is out of mutex\n");
waitpid(pid1, NULL, WEXITED); // wait for child to close
sem_destroy(&mutex->mut); // clean up the mutex.
// if we can't delete the memory, barf
if (shmdt(mutex) == -1) { // clean up the shared memory
// barf
perror("Cant delete the shared memory");
exit(-1);
}
exit(0); // exit parent
} else { // child process
sem_wait(&mutex->mut); // enter the critical segment
printf("child in mutex\n");
sleep(10); // sleep for 10 seconds so the parent has to wait.
printf("child leaving mutex\n");
sem_post(&mutex->mut);
printf("child has left the mutex\n");
exit(0); // exit child
}
return 0;
}
```
Binary file not shown.