% % This file was automatically produced at Feb 27 2003, 22:51:56 by % c2latex -c minim1.c % \documentclass[11pt,a4paper]{article} \setlength{\textwidth}{15cm} \setlength{\textheight}{22.5cm} \setlength{\hoffset}{-2cm} \setlength{\voffset}{-2cm} \begin{document} \expandafter\ifx\csname indentation\endcsname\relax% \newlength{\indentation}\fi \setlength{\indentation}{0.5em} \begin{flushleft} {$/\ast$\it{}$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$\mbox{}\\ $\ast$ FILE: minim1.c\mbox{}\\ $\ast$ DESCRIPTION:\mbox{}\\ $\ast$ This is a very minimal program using Pthreads. The main function \mbox{}\\ $\ast$ creates 4 threads. Each thread executes a function which prints out the \mbox{}\\ $\ast$ thread "number".\mbox{}\\ $\ast$\mbox{}\\ $\ast$ SOURCE: Vijay Sonnad, IBM\mbox{}\\ $\ast$ LAST REVISED: 9/20/98 Blaise Barney\mbox{}\\ $\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast/$}\mbox{}\\ \mbox{}\\ {\tt \#include} $<${\tt{}pthread.h}$>$ \mbox{}\\ {\tt \#include} $<${\tt{}stdio.h}$>$ \mbox{}\\ \mbox{}\\ {\tt \#define} NUMTHDS 4\mbox{}\\ {\bf void} $\ast$thrdfun ({\bf void} $\ast$arg);\mbox{}\\ \mbox{}\\ {\bf int} main({\bf int} argc, {\bf char} $\ast$argv[])\mbox{}\\ \{ \mbox{}\\ \hspace*{3\indentation}{$/\ast$\it{}\mbox{}\\ \hspace*{3\indentation}We define an array of thread identifiers which are later used as \mbox{}\\ \hspace*{3\indentation}in the call to to create threads.\mbox{}\\ \hspace*{3\indentation}$\ast/$}\mbox{}\\ \hspace*{3\indentation}pthread\_t thrdid[NUMTHDS];\mbox{}\\ \hspace*{3\indentation}{\bf int} i;\mbox{}\\ \mbox{}\\ \hspace*{3\indentation}{$/\ast$\it{} \mbox{}\\ \hspace*{3\indentation}Create threads using default values for the attributes,\mbox{}\\ \hspace*{3\indentation}(specified by setting NULL as the argument in the call).\mbox{}\\ \hspace*{3\indentation}Note that in this version of AIX (which uses Draft7 of\mbox{}\\ \hspace*{3\indentation}POSIX.1c) the default is "detached"; thus any attempt to \mbox{}\\ \hspace*{3\indentation}join with any of these threads will result in an error. \mbox{}\\ \hspace*{3\indentation}$\ast/$}\mbox{}\\ \mbox{}\\ \hspace*{3\indentation}{\bf for} (i=0; i$<$NUMTHDS; i++)\mbox{}\\ \hspace*{8\indentation}\{\mbox{}\\ \hspace*{16\indentation}pthread\_create( \&thrdid[i], NULL, thrdfun, ({\bf void} $\ast$)i);\mbox{}\\ \hspace*{8\indentation}\}\mbox{}\\ \hspace*{3\indentation}{$/\ast$\it{}\mbox{}\\ \hspace*{3\indentation}It is necessary to use pthread\_exit at the end of the \mbox{}\\ \hspace*{3\indentation}main program $-$ otherwise, when it exits, all running \mbox{}\\ \hspace*{3\indentation}threads will be killed \mbox{}\\ \hspace*{3\indentation}$\ast/$}\mbox{}\\ \hspace*{3\indentation}pthread\_exit(NULL); \mbox{}\\ \}\mbox{}\\ \mbox{}\\ \hspace*{3\indentation}{$/\ast$\it{}\mbox{}\\ \hspace*{3\indentation}This function is executed when the thread is created.\mbox{}\\ \hspace*{3\indentation}Because it was created in a detached state, it will \mbox{}\\ \hspace*{3\indentation}be recycled upon termination, i.e. all data associated \mbox{}\\ \hspace*{3\indentation}with the thread is released. \mbox{}\\ \hspace*{3\indentation}$\ast/$}\mbox{}\\ {\bf void} $\ast$thrdfun ({\bf void} $\ast$arg)\mbox{}\\ \{\mbox{}\\ \hspace*{3\indentation}printf ({\tt"My thread number $=$ \%d $\backslash$n"}, ({\bf int})arg);\mbox{}\\ \hspace*{3\indentation}pthread\_exit(({\bf void} $\ast$) arg);\mbox{}\\ \}\mbox{}\\ \end{flushleft} \end{document}