% % This file was automatically produced at Feb 27 2003, 22:51:56 by % c2latex -c minim2.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: minim2.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". Creation of a joinable thread is demonstrated by using \mbox{}\\ $\ast$ the appropriate attributes when creating the thread.\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 in the\mbox{}\\ \hspace*{3\indentation}call to create threads.\mbox{}\\ \hspace*{3\indentation}$\ast/$}\mbox{}\\ \hspace*{3\indentation}pthread\_t thrdid[NUMTHDS];\mbox{}\\ \hspace*{3\indentation}pthread\_attr\_t attr;\mbox{}\\ \hspace*{3\indentation}{\bf int} status,i;\mbox{}\\ \mbox{}\\ \hspace*{3\indentation}{$/\ast$\it{} Create attribute of JOINABLE for use in creating threads $\ast/$}\mbox{}\\ \hspace*{3\indentation}pthread\_attr\_init(\&attr);\mbox{}\\ \hspace*{3\indentation}pthread\_attr\_setdetachstate(\&attr, PTHREAD\_CREATE\_JOINABLE);\mbox{}\\ \mbox{}\\ \hspace*{3\indentation}{$/\ast$\it{} Create threads using the attribute $\ast/$}\mbox{}\\ \hspace*{3\indentation}{\bf for} (i=0; i$<$NUMTHDS; i++)\mbox{}\\ \hspace*{8\indentation}\{\mbox{}\\ \hspace*{16\indentation}pthread\_create( \&thrdid[i], \&attr, thrdfun, ({\bf void} $\ast$)i);\mbox{}\\ \hspace*{8\indentation}\}\mbox{}\\ \mbox{}\\ \hspace*{3\indentation}pthread\_attr\_destroy(\&attr);\mbox{}\\ \mbox{}\\ \hspace*{2\indentation}\mbox{}\\ \hspace*{3\indentation}{$/\ast$\it{} Join with threads after creation $\ast/$} \mbox{}\\ \hspace*{3\indentation}{\bf for}(i=0;i$<$NUMTHDS;i++)\mbox{}\\ \hspace*{7\indentation}\{\mbox{}\\ \hspace*{9\indentation}{\bf if} (pthread\_join( thrdid[i], ({\bf void} $\ast$$\ast$)\&status)$\neq$0) \mbox{}\\ \hspace*{12\indentation}printf ({\tt" Error in join $\backslash$n"});\mbox{}\\ \hspace*{7\indentation}\}\mbox{}\\ \mbox{}\\ \hspace*{3\indentation}pthread\_exit(NULL); \mbox{}\\ \}\mbox{}\\ \mbox{}\\ {$/\ast$\it{} This function is executed when the thread is created. $\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$) 0);\mbox{}\\ \}\mbox{}\\ \end{flushleft} \end{document}