ken18
March 13th, 2014, 02:29 AM
Trying to learn about pthread and am getting a segmentation fault using the following program.
Any idea why the segmentation fault?
#include <stdio.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <pthread.h>
#include <unistd.h>
#include <math.h>
void * thread(void *arg) {
int i;
double tmp;
for (i=0; i<100000; i++) {
tmp = cos( (double) i);
tmp *= tmp;
}
return 0;
}
int main(void) {
int i;
int n = 40000; // result is segmentation fault. n = 30000 is ok...
// note: cat /proc/sys/kernel/threads-max returns 90245
// compile string: gcc pthread7.c -Wall -lm -lpthread
pthread_t tid[n];
for (i=0; i<n; i++ ) { pthread_create( &tid[i], NULL, thread, NULL ); }
for (i=0; i<n; i++ ) { pthread_join( tid[i], NULL ); }
return 0;
}
Any idea why the segmentation fault?
#include <stdio.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <pthread.h>
#include <unistd.h>
#include <math.h>
void * thread(void *arg) {
int i;
double tmp;
for (i=0; i<100000; i++) {
tmp = cos( (double) i);
tmp *= tmp;
}
return 0;
}
int main(void) {
int i;
int n = 40000; // result is segmentation fault. n = 30000 is ok...
// note: cat /proc/sys/kernel/threads-max returns 90245
// compile string: gcc pthread7.c -Wall -lm -lpthread
pthread_t tid[n];
for (i=0; i<n; i++ ) { pthread_create( &tid[i], NULL, thread, NULL ); }
for (i=0; i<n; i++ ) { pthread_join( tid[i], NULL ); }
return 0;
}