Your IP : 216.73.216.40


Current Path : /var/www/html/bibhas.ghoshal/lab_files/
Upload File :
Current File : /var/www/html/bibhas.ghoshal/lab_files/Assignment6.2.shtml

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <style type="text/css">

.linkBack {font-family: Arial, Helvetica, Sans-serif; color: #000000; font-size: 12pt; font-weight: bold; background-color:#FFFFFF;}
.linkBack:link {color:#003399; background-color:#FFFFFF;}
.linkBack:active {color:#000066; background-color:#FFFFFF;}
.linkBack:visited {color:#000066; background-color:#FFFFFF;}
.linkBack:hover {color:#0066cc; background-color:#FFFFFF;}

.sectionLinks {font-family: Arial, Helvetica, Sans-serif; color: #000000; font-size: 12pt; background-color:#FFFFFF;}
.sectionLinks:link {color:#003399; background-color:#FFFFFF;}
.sectionLinks:active {color:#000066; background-color:#FFFFFF;}
.sectionLinks:visited {color:#000066; background-color:#FFFFFF;}
.sectionLinks:hover {color:#0066cc; background-color:#FFFFFF;}

.content {font-family: Times New Roman, Times, Serif; color: #ff0000; font-size: 12pt}

.contentTitle {font-family: Arial, Helvetica, Sans-serif; color: #006600; font-size: 16pt; font-weight: bold;}

.contentSubTitle {font-family: Arial, Helvetica, Sans-serif; color: #003399; font-size: 13pt; font-weight: bold;}
.contentSubTitle:link {color:#003399;}
.contentSubTitle:active {color:#000066;}
.contentSubTitle:visited {color:#000066;}
.contentSubTitle:hover {color:#0066cc;}

.contentSub {font-family: Times New Roman, Times, Serif; color: #000000; font-size: 12pt; background-color:#FFFFFF;}
.contentSub:link {color:#003399; background-color:#FFFFFF;}
.contentSub:active {color:#000066; background-color:#FFFFFF;}
.contentSub:visited {color:#000066; background-color:#FFFFFF;}
.contentSub:hover {color:#0066cc; background-color:#FFFFFF;}

.sectionLine {height: 1px; background-color: #000000;}

.sigcseLocationText {padding: 5px; font-family: Arial, Helvetica, Sans-serif; color: #ffffff; font-size:medium; font-weight: bold;}

H1{text-align: center; color:#000066; background-color:#FFFFFF;}
H2{color:#000099; background-color:#FFFFFF;}
H3{color:#000099; background-color:#FFFFFF;}
H4{color:#000099; background-color:#FFFFFF;}

.header TD {padding: 3px; font-family: Arial,Helvetica,Sans-serif; color:#ffffff; font-size: small; background-color:#3f3f3f; text-decoration: none;}
.header :link {padding: 3px; font-family: Arial,Helvetica,Sans-serif; color:#ffffff; font-size: small; background-color:#3f3f3f; text-decoration: none;}
.header :active {padding: 3px; font-family: Arial,Helvetica,Sans-serif; color:#ffffff; font-size: small; background-color:#005128; text-decoration: none;}
.header :visited {padding: 3px; font-family: Arial,Helvetica,Sans-serif; color:#ffffff; font-size: small; background-color:#3f3f3f; text-decoration: none;}
.header :hover {padding: 3px; font-family: Arial,Helvetica,Sans-serif; color:#ffffff; font-size: small; background-color:#999999; text-decoration: none;}

BODY {margin-top: 0px; margin-left: 5px; margin-right: 5px; margin-bottom: 5px; background-color:#ffffff; color:#000000;}

ol.withloweralpha { list-style-type:  lower-alpha
}

ol.withupperalpha { list-style-type:  upper-alpha
}

ol.withupperroman { list-style-type:  upper-roman
}


</style>

    <title>Laboratory Exercise on Shared Memory and Process Synchronization
           in Linux</title> 
     
<p>


</p><h1>Shared Memory and Process Synchronization in Linux</h1>

<p>
<b>Summary:</b>  This laboratory exercise provides practice with shared 
memory and process synchronization in Linux.
</p>

<h3>Preparation</h3>

<ol>
<li>
<p>
Review sample programs 7-10 in <a href="http://www.cs.grinnell.edu/%7Ewalker/c/concurrency-index.html">
An Introduction to Concurrency in Unix-based [GNU] C
Through Annotated Examples</a>
</p>

</li><li>
<p>
Review the corresponding Linux-based programs (<tt>read-write-1.c</tt>
through <tt>read-write-4.c</tt>) 
</p>

</li></ol>

<h3>Experiments with Shared Memory</h3>

<ol start="3">
<li>
<p>
Copy <a href="http://www.cs.grinnell.edu/%7Ewalker/c/concurrency/read-write-1.c">
~walker/c/concurrency/read-write-1.c</a> 
to your account, compile it with <tt>gcc</tt>, and run it a few times.  
Describe the output you get, and explain briefly how it is produced.
</p>

</li><li>
<p>
As we did in class, remove the <tt>sleep</tt> statement from the child process, rerun
<tt>read-write-1.c</tt>, and explain the output produced.
</p>

</li><li>
<p>
Restore the <tt>sleep</tt> statement from the previous step, and 
remove it from the parent process.  Again, rerun
<tt>read-write-1.c</tt>, and explain the output produced.
</p>

</li><li>
<p>
Rather than rely upon <tt>sleep</tt> statements to synchronize the
two processes, consider the use of spinlocks.  In this approach,
the parent will write to shared memory when the memory location 
contains the value -1, and the child will read when the memory location
is not -1.
</p>

<ol type="a">
<li>
<p>
Initialize the shared memory location in main memory to -1 before the
<tt>fork</tt> operation.  (Why must this be done before the <tt>fork</tt>?)
</p>

</li><li>
<p>
Replace the <tt>sleep</tt> statement for the child by a spinlock that
checks that the shared memory contains a nonnegative value.  At the end of the
child's loop, the shared memory location should be reset to -1.
</p>

</li><li>
<p>
Remove the <tt>sleep</tt> statement from the parent at the end of the loop,
and insert a spinlock at the beginning of the loop that checks that
memory is -1.  When this condition occurs, the parent may write the
next nonnegative number to shared memory.
</p>
</li></ol>

</li><li>
<p>
Copy  programs <a href="http://www.cs.grinnell.edu/%7Ewalker/c/concurrency/read-write-2.c">
~walker/c/concurrency/read-write-2.c</a>
to your account.  Then, compile <tt>read-write-2.c</tt>, run them a few times, 
and review the code to be sure you understand how the programs work.
</p>

</li><li>
<p>
Explain whether <tt>read-write-2.c</tt> will work when there are 
multiple readers or when there are multiple writers.  In each case, if
the code would work, explain why.  If the code would not work, give
a timing sequence involving the several readers and/or writers showing
what might go wrong.
</p>

</li></ol>


<h3>Experiments with Semaphores</h3>

<ol start="9">
<li>
<p>
Copy <a href="http://www.cs.grinnell.edu/%7Ewalker/c/concurrency/read-write-3.c">
~walker/c/concurrency/read-write-3.c</a>,
to your account, compile it with
<tt>gcc</tt> and run it.  
</p>

</li><li>
<p>
Review <tt>read-write-3.c</tt> to be sure you understand how it works.
</p>

</li><li>
<p>
Use the idea of semaphores, as implemented in <tt>read-write-3.c</tt>,
in place of the spinlocks in Step 6 to handle the synchronization
of the reader and writer process from <tt>read-write-1.c</tt>.  Your final
program should not use either spinlocks or <tt>sleep</tt> statements.
</p>

</li><li>
<p>
With this use of semaphores, explain whether your code in step 11
will work when there are 
multiple readers or when there are multiple writers.  In each case, if
the code would work, explain why.  If the code would not work, give
a timing sequence involving the several readers and/or writers showing
what might go wrong.
</p>

</li></ol>

<h3>Experiments with Multiple Readers and Multiple Writers</h3>

<ol start="13">
<li>
<p>
Copy <a href="http://www.cs.grinnell.edu/%7Ewalker/c/concurrency/read-write-4.c">
~walker/c/concurrency/read-write-4.c</a>,
to your account, compile it with <tt>gcc</tt> and run it.  
</p>

</li><li>
<p>
This program contains a third semaphore, <tt>mutex</tt>.  Explain the
purpose of this semaphore.  Specifically, if semaphore <tt>mutex</tt> were
omitted, give a timing sequence involving the several readers and/or
writers showing what might go wrong. 
</p>

</li><li>
<p>
Program <tt>read-write-4.c</tt> prevents any reader from working at the
same time as any writer.  Assuming that the buffer contains several
locations, however, writing to one buffer location should not interfere
with reading from another.  That is, the critical section for
readers need not be considered exactly the same as the critical section for 
writers.  Remove semaphore <tt>mutex</tt> and add additional semaphores, as
needed, so that some reader could work concurrently with some writer (assuming 
the buffer contained some data but was not full -- so both reading
and writing made sense).
 
</p></li></ol>

<h3>A Simple Pipeline Program</h3>

<p>
<b>Consider the following problem:</b>  A program is to be written to print
all numbers between 1 and 1000 (inclusive) that are not (evenly) divisible 
by either 2 or 3.
</p>

<p>
This problem is to be solved using three processes (P0, P1, P2) and two
one-integer buffers (B0 and B1) as follows: 
</p>

<ol type="a">
<li>
<p>
P0 is to generate the integers from 1 to 1000, and place them in B0 one
at a time.  After placing 1000 in the buffer, P0 places the sentinel 0
in the buffer, and terminates.
</p>
</li><li>
<p>
P1 is to read successive integers from B0.  If a value is not divisible by 2,
the value is placed in B1. If the value is positive and divisible by 2,
it is ignored.  If the value is 0, 0 is placed in B1, and P1 terminates.
</p>
</li><li>
<p>
P2 is to read successive integers from B1.  If a value is not divisble
by 3, it is printed.  If the value is positive and divisible by 3, it 
is ignored.  If the value is 0, P2 terminates.
</p>

</li></ol>

<p>
 
</p>

<p>
<img src="Laboratory%20Exercise%20on%20Shared%20Memory%20and%20Process%20Synchronization%20in%20Linux_files/lab-semaphores-1.gif" alt="pipeline processing">
</p>

<ol start="16">
<li>
<p>
Write a program to implement P0, P1, and P2 as separate processes and
B0 and B1 as separate pieces of shared memory -- each the size of just 
one integer.  Use semaphores to coordinate processing.  Access to 
B0 should be independent of access to B1; for example, P0 could be 
writing into B0 while either P1 was writing into B1 or P2 was reading.
</p>

</li></ol>

<p>
</p><hr><p>



This document is available on the World Wide Web as

</p><pre>     http://www.cs.grinnell.edu/~walker/courses/213.fa04/lab-semaphores.shtml
</pre>

<hr>




</body></html>