<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//en">

<!–Converted with LaTeX2HTML 2022 (Released January 1, 2022) –> <HTML lang="en"> <HEAD> <TITLE>Contents of Building a Context Switch in ``C'' <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8"> <META NAME="viewport" CONTENT="width=device-width, initial-scale=1.0"> <META NAME="Generator" CONTENT="LaTeX2HTML v2022">

<LINK REL="STYLESHEET" HREF="MPTHD.css">

<LINK REL="next" HREF="node11_mn.html"> <LINK REL="previous" HREF="node2_mn.html"> <LINK REL="up" HREF="MPTHD_mn.html"> <LINK REL="next" HREF="node6_mn.html"> </HEAD>

<BODY bgcolor="#ffffff" text="#000000" link="#9944EE" vlink="#0000ff" alink="#00ff00">

<H1><A ID="SECTION00030000000000000000"> Building a Context Switch in ``C'' <P> In implementing the context switch (see code in mp*.h and mp*.c files), we use the powerful programming methodology of ``clustering'' similar code and data into one modular unit with defined interfaces.<A NAME="tex2html3" HREF="footnode_mn.html#foot134" TARGET="footer"><SUP>2</SUP></A> In this case, we create a thread cluster for initializing, manipulating, and deinitializing threads of execution. The <B>interface</B> for the thread cluster is in the file <EM>mpthd.h</EM>; you should include <EM>mpthd.h</EM> in your programs when you wish to have multiple threads of execution. The <B>implementation and representation</B> for the threads is in the file <EM>mpthd.c</EM>; your programs should not know about this file, and not make any assumptions about how threads are work beyond their interface. These good programming disciplines help build clean, well-defined interfaces between program modules &ndash; a big help when your testing, debugging, or modifying your code.

<P> More generally, most <EM>*.h</EM> files are cluster interfaces (except the sections marked <EM>INTERNAL USE ONLY</EM>), and their respective <EM>*.c</EM> file (if it exists) the cluster implementation. Within the clusters, all datatypes are in upper case (eg, <I>MPTHD</I>) and, if associated with a cluster, are referenced externally by name (pointer) and internally by value (contents); on the other hand, all procedures are in lower case, and prefixed with the major datatype they operate on (eg, <!– MATH mpthd$\_switch$ –> <I>mpthd</I><IMG STYLE="height: 37.63ex; vertical-align: 135.10ex; " SRC="img2.png" ALT="$\_switch$">). To help prevent naming conflicts, all global names are prefixed with the initials of their creator. In this case, every name begins with <EM>mp</EM> for Mike Parker. There are quite a number of other programming disciplines adhered to within the code not relevant to context switching but worth looking at.

<P> Though your programs may not know how threads are implemented, I expect a few humans are curious. So if you haven't already, take a moment now and read the overview and functional specification of the thread cluster in <EM>mpthd.h</EM>, and briefly scan its implementation in <EM>mpthd.c</EM>. Also glance at the two major clusters it depends on, <EM>mpsig.h</EM> and <EM> mpsem.h</EM>. Here's a map to help guide your way (see figure).

<P>

<DIV class="CENTER"><A ID="66"></A> <TABLE> <CAPTION class="BOTTOM"><STRONG>Figure:</STRONG> Modular Dependency Diagram from mpthd Thread Cluster</CAPTION> <TR><TD><IMG STYLE="height: 314.00ex; " SRC="img3.png" ALT="

\begin{figure}\begin{verbatim}/------------------------\
\vert mpthd.*...
...------------------------/ \------------------------/\end{verbatim}
\end{figure}
"></TD></TR> </TABLE> </DIV>

<P> * * *

<P> Since we can often understand what a program does (or can do) by understanding how it represents its data, I will cover the data structure of the thread cluster first. However, I will also cover its code structure, defining the functions available to access this data and how they work.

<P> <BR><HR> <!–Table of Child-Links–> <A ID="CHILD_LINKS"><STRONG>Subsections</STRONG></A>

<UL> <LI><A NAME="tex2html47" HREF="node6_ct.html" TARGET="contents"><SMALL>The Thread Cluster - Data Structure</SMALL></A> <LI><A NAME="tex2html48" HREF="node7_mn.html" TARGET="main"><SMALL>The Thread Cluster - Code Structure</SMALL></A> <UL> <LI><A NAME="tex2html49" HREF="node8_mn.html" TARGET="main"><SMALL>Achieving Atomicity with Signals and Semaphores</SMALL></A> <LI><A NAME="tex2html50" HREF="node9_mn.html" TARGET="main"><SMALL>Switching Between Contexts: <!– MATH mpthd$\_switch$ –> <I>mpthd</I><IMG STYLE="height: 37.63ex; vertical-align: 135.10ex; " SRC="img2.png" ALT="$\_switch$"></SMALL></A> </UL> <BR> <LI><A NAME="tex2html51" HREF="node10_mn.html" TARGET="main"><SMALL>Creating and Destroying Contexts: <!– MATH mpthd$\_init$ –> <I>mpthd</I><IMG STYLE="height: 1.61ex; vertical-align: 177.62ex; " SRC="img5.png" ALT="$\_init$"> and <!– MATH mpthd$\_dinit$ –> <I>mpthd</I><IMG STYLE="height: 1.61ex; vertical-align: 177.62ex; " SRC="img6.png" ALT="$\_dinit$"></SMALL></A> </UL> <!–End of Table of Child-Links–>

<HR>

</BODY> </HTML>