% % This file was automatically produced at Feb 27 2003, 22:51:56 by % c2latex -c hellodumpfix.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: hellodumpfix.c\mbox{}\\ $\ast$ DESCRIPTION:\mbox{}\\ $\ast$ This is just one way to fix the "hello world" Pthreads program that dumps \mbox{}\\ $\ast$ core. Things to note:\mbox{}\\ $\ast$ $-$ attr variable and its scoping\mbox{}\\ $\ast$ $-$ use of the pthread\_attr\_setstacksize routine\mbox{}\\ $\ast$ $-$ initialization of the attr variable with pthread\_attr\_init\mbox{}\\ $\ast$ $-$ passing the attr variable to pthread\_create\mbox{}\\ $\ast$ \mbox{}\\ $\ast$ LAST REVISED: 02/11/2002 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{}\\ {\tt \#define} NTHREADS 8\mbox{}\\ {\tt \#define} ARRAY\_SIZE 16000\mbox{}\\ \mbox{}\\ pthread\_attr\_t attr;\mbox{}\\ \mbox{}\\ {\bf void} $\ast$Hello({\bf void} $\ast$threadid)\mbox{}\\ \{\mbox{}\\ \hspace*{3\indentation}{\bf double} A[ARRAY\_SIZE];\mbox{}\\ \hspace*{3\indentation}{\bf int} i;\mbox{}\\ \hspace*{3\indentation}size\_t mystacksize;\mbox{}\\ \hspace*{3\indentation}sleep(3);\mbox{}\\ \hspace*{3\indentation}{\bf for} (i=0; i$<$ARRAY\_SIZE; i++)\mbox{}\\ \hspace*{3\indentation}\{\mbox{}\\ \hspace*{6\indentation}A[i] = i $\ast$ 1.0;\mbox{}\\ \hspace*{3\indentation}\}\mbox{}\\ \hspace*{3\indentation}printf({\tt"\%d: Hello World! \%f$\backslash$n"}, threadid, A[ARRAY\_SIZE$-$1]);\mbox{}\\ \hspace*{3\indentation}pthread\_attr\_getstacksize (\&attr, \&mystacksize);\mbox{}\\ \hspace*{3\indentation}printf({\tt"\%d: Thread stack size $=$ \%d bytes $\backslash$n"}, threadid, mystacksize);\mbox{}\\ \hspace*{3\indentation}pthread\_exit(NULL);\mbox{}\\ \}\mbox{}\\ \mbox{}\\ {\bf int} main({\bf int} argc, {\bf char} $\ast$argv[])\mbox{}\\ \{\mbox{}\\ \hspace*{3\indentation}pthread\_t threads[NTHREADS];\mbox{}\\ \hspace*{3\indentation}size\_t stacksize;\mbox{}\\ \hspace*{3\indentation}{\bf int} rc, t;\mbox{}\\ \hspace*{3\indentation}pthread\_attr\_init(\&attr);\mbox{}\\ \hspace*{3\indentation}stacksize = 500000;\mbox{}\\ \hspace*{3\indentation}pthread\_attr\_setstacksize (\&attr, stacksize);\mbox{}\\ \hspace*{3\indentation}pthread\_attr\_getstacksize (\&attr, \&stacksize);\mbox{}\\ \hspace*{3\indentation}printf({\tt"Thread stack size $=$ \%d bytes (hint, hint)$\backslash$n"},stacksize);\mbox{}\\ \hspace*{3\indentation}{\bf for}(t=0;t$<$NTHREADS;t++)\{\mbox{}\\ \hspace*{6\indentation}rc = pthread\_create(\&threads[t], \&attr, Hello, ({\bf void} $\ast$)t);\mbox{}\\ \hspace*{6\indentation}{\bf if} (rc)\{\mbox{}\\ \hspace*{9\indentation}printf({\tt"ERROR; return code from pthread\_create() is \%d$\backslash$n"}, rc);\mbox{}\\ \hspace*{9\indentation}exit($-$1);\mbox{}\\ \hspace*{6\indentation}\}\mbox{}\\ \hspace*{3\indentation}\}\mbox{}\\ \hspace*{3\indentation}printf({\tt"Created \%d threads.$\backslash$n"}, t);\mbox{}\\ \hspace*{3\indentation}pthread\_exit(NULL);\mbox{}\\ \}\mbox{}\\ \end{flushleft} \end{document}