/∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗ FILE: hellodumpfix.c

∗ DESCRIPTION:

∗ This is just one way to fix the ”hello world” Pthreads program that dumps

∗ core. Things to note:

∗ − attr variable and its scoping

∗ − use of the pthread attr setstacksize routine

∗ − initialization of the attr variable with pthread attr init

∗ − passing the attr variable to pthread create ∗

∗ LAST REVISED: 02/11/2002 Blaise Barney ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗/

#include <pthread.h>
#include <stdio.h>

#define NTHREADS 8 #define ARRAY SIZE 16000

pthread attr t attr;

void ∗Hello(void ∗threadid) {

double A[ARRAY SIZE];

int i;

size t mystacksize;

sleep(3);

for (i=0; i<ARRAY SIZE; i++)

{

A[i] = i ∗ 1.0; } printf("%d: Hello World! %f\n", threadid, A[ARRAY SIZE−1]); pthread attr getstacksize (&attr, &mystacksize); printf("%d: Thread stack size = %d bytes \n", threadid, mystacksize); pthread exit(NULL);

}

int main(int argc, char ∗argv[]) {

pthread t threads[NTHREADS];

size t stacksize;

int rc, t;

pthread attr init(&attr);

stacksize = 500000;

pthread attr setstacksize (&attr, stacksize);

pthread attr getstacksize (&attr, &stacksize);

   printf("Thread stack size = %d bytes (hint, hint)\n",stacksize);

for(t=0;t<NTHREADS;t++){ rc = pthread create(&threads[t], &attr, Hello, (void ∗)t); if (rc){

      printf("ERROR; return code from pthread create() is %d\n", rc);

exit(−1); }

}

printf("Created %d threads.\n", t);

pthread exit(NULL); }