Example #1
0
void *qsort_2 (void *threadarg) {
  //busy thread count mutex
  static pthread_mutex_t mutex_numBusyThreads = PTHREAD_MUTEX_INITIALIZER;  
  //pivot index after partitioning
  int pivot;
  //thread arguments
  struct recur_pthread_qsort_args *args = threadarg, leftargs, rightargs;
  memcpy (&leftargs, args, sizeof(leftargs));
  memcpy (&rightargs, args, sizeof(rightargs));
  //child thread ids
  pthread_t threadid[2];
  //child return stati
  int status;
  //temp vals
  int i;

  if (args->right - args->left < SWITCH_THRESH) {
    insertsort(&args->data[args->left], args->right - args->left + 1);
  } else if (args->left < args->right) {
    pivot = partition (args->data, args->left, args->right);
#ifdef DEBUG
    printf("pivot is at index %d\n", pivot);
#endif
    leftargs.right = pivot;
    rightargs.left = pivot + 1;
    pthread_mutex_lock(&mutex_numBusyThreads);
    if (*(args->numBusyThreads) >= args->maxThreads) {
      pthread_mutex_unlock(&mutex_numBusyThreads);
      //quicksort without spawning
      qsort_2(&leftargs);
      qsort_2(&rightargs);
    } else {
      //increment busy thread count
      *(args->numBusyThreads) += 2;
#ifdef DEBUG      
      printf("+2 busy thread count: %d\n", *(args->numBusyThreads));
#endif
      pthread_mutex_unlock(&mutex_numBusyThreads);
      //spawn new threads
      if (pthread_create(&threadid[0], NULL, qsort_2, &leftargs)) {
	perror("pthread_create (left side)");
	exit(-1);
      } 
      if (pthread_create(&threadid[1], NULL, qsort_2, &rightargs)) {
	perror("pthread_create (right side)");
	exit(-1);
      }
      for (i = 0; i < 2; i++) {
	pthread_join (threadid[i], (void **)&status);
	pthread_mutex_lock(&mutex_numBusyThreads);
	(*args->numBusyThreads)--;
#ifdef DEBUG
	printf("-1 busy thread count %d\n", *(args->numBusyThreads));
#endif
	pthread_mutex_unlock(&mutex_numBusyThreads);
      }
    }
  }
  return 0;
}
Example #2
0
void qsort_1 (int *arr, int size) {
  stackT stack;
  stackElementT element, e;
  int k;
  StackInit (&stack, size);
  element.left = 0;
  element.right = size - 1;
  StackPush(&stack, element);
  while (!StackIsEmpty (&stack)) {
    element = StackPop (&stack);
    while (element.left < element.right) {
      if (element.right - element.left < SWITCH_THRESH) {
	insertsort(&arr[element.left], element.right - element.left + 1);
	element.left = element.right;
      } else {
	k = partition(arr, element.left, element.right);
	e.left = element.left;
	e.right = k;
	StackPush(&stack, e);
	element.left = k+1;
      }
    }
  }
  StackDestroy (&stack);
  return;
}
void branchbound(int (*map)[NODESIZE]) {
    struct Node *currentnode=malloc(sizeof(struct Node));
    currentnode->id=STARTNODE,currentnode->height=0,currentnode->prev=NULL,currentnode->cost=0;
    push(currentnode);
    struct Node *node;
    struct Node *neibors[NODESIZE];
    int neiborsize,i;
    while(!empty()) {
        currentnode=top();
        if(currentnode->height==NODESIZE-1 && currentnode->cost+map[currentnode->id][STARTNODE]<min)
            display_outcome(currentnode,currentnode->cost+map[currentnode->id][STARTNODE]);
        pop();
        if(currentnode->cost > min) {
            printf("drop one.\n");
            continue;
        }
        neiborsize=0;
        for(i=0; i<NODESIZE; i++)
            if(i!=currentnode->id && !inme(currentnode,i)) {
                node=malloc(sizeof(struct Node));
                node->id=i,node->height=currentnode->height+1,node->prev=currentnode;
                node->cost=currentnode->cost+map[currentnode->id][i];
                neibors[neiborsize++]=node;
            }
        insertsort(neibors,neiborsize);
        for(i=0; i<neiborsize; i++)
            push(neibors[i]);
    }
}
Example #4
0
void *qsort_4_thread (void *threadarg) {
  //global busy thread count mutex and cv
  static pthread_mutex_t mutex_busy = PTHREAD_MUTEX_INITIALIZER;
  static pthread_cond_t cv_work = PTHREAD_COND_INITIALIZER;
  //deference thread argument struct
  struct qsort_4_thread_args *args = threadarg;
  //working vars
  int left, right, idle = 1, pivot;
  stackElementT element;

  while (1) {
    while (idle) {
      //try to get work
      pthread_mutex_lock (&mutex_busy);
      if (!StackIsEmpty (args->work)) {
	element = StackPop (args->work);
	(*args->busy)++;
	pthread_mutex_unlock (&mutex_busy);
	left = element.left;
	right = element.right;
	idle = 0;
	continue;
      }
      //if there's no work and nobody is busy signal and quit
      if (*args->busy == 0) {
	pthread_cond_broadcast (&cv_work);
	pthread_mutex_unlock (&mutex_busy);
	return (void *) 0;
      }
      //wait for signal of more work
      pthread_cond_wait (&cv_work, &mutex_busy);
      pthread_mutex_unlock (&mutex_busy);
    }
    while (!idle) {
      //if nothing to do, set idle and decrement busy
      if (left >= right) {
	idle = 1;
	pthread_mutex_lock (&mutex_busy);
	(*args->busy)--;
	pthread_mutex_unlock (&mutex_busy);
	continue;
      }
      //if problem small enough, insert sort
      if (right - left < SWITCH_THRESH && left < right) {
	insertsort (&args->data[left], right - left + 1);
	left = right;
      } else {
	//else partition, push and signal
	pivot = partition (args->data, left, right);
	element.left = pivot + 1;
	element.right = right;
	right = pivot;
	pthread_mutex_lock (&mutex_busy);
	StackPush (args->work, element);
	pthread_cond_broadcast (&cv_work);
	pthread_mutex_unlock (&mutex_busy);
      }
    }
  }
}
Example #5
0
int main()
{
    int a[7]={9,4,5,6,7,2,3};
    insertsort(a,7);
    bubblesort(a,7);
    selectsort(a,7);
}
Example #6
0
int main( int argc, char *argv[] ) {

  int A[100000] ;

  int i ;

  int n ;

  FILE* fin ;

  fin = fopen( argv[1], "r" ) ;

  n = atoi( argv[2] ) ;

  for ( i = 0 ; i < n ; i++ ) fscanf( fin, "%d", &A[i] ) ;

  if( insertsort( A, n ) == ERROR ) printf( "\n Trouble sorting.\n" ) ;

  printf( "\n\nThe first five numbers are" ) ;

  for ( i = 0 ; i < 5 ; i++ ) printf( " %d", A[i] ) ;

  printf( ".\n\n" ) ;

  printf( "The last five numbers are" ) ;

  for ( i = n-5 ; i < n ; i++ ) printf( " %d", A[i] ) ;

  printf( ".\n\n" ) ;

  fclose( fin ) ;

  return 0 ;

}
Example #7
0
int main()
{
	FILE *f;
	int *a, i = 0;
	time_t t;

	f = fopen("data", "r");
	if (!f)
		PERROR_RET("Unable to open file data for reading");


	a = (int *) calloc(sizeof(int), MAX_NUM);

	while (!feof(f)) {
		fscanf(f, "%d", &a[i]);
		i++;
	}

	start_timestamp();
	insertsort(a, i - 1);
	t = stop_timestamp();
	printf("Sorting complete in %lu microseconds\n", t);
	printarray(a, i - 1);

	free(a);
	fclose(f);

	return 0;
}
Example #8
0
void testinsertsort()
{
	struct node* node = NULL;
	node = buildlistattailbyref(10);
	printlist(node, "before sorting the list");
	insertsort(&node);
	printlist(node, "after sorting the list ");
}
int main(int argc,char** argv)
{
	int i=11;
	int a[11]={12,2,16,30,8,28,4,10,20,6,18};

	insertsort(a,11);
/*	for(i=0;i<11;i++)
		printf("%d ",a[i]);
	printf("\n");
*/
	return 0;
}
Example #10
0
void recur_qsort(int *arr, int i, int j)
{
  int k;

  if (j-i < SWITCH_THRESH){
    insertsort(&arr[i], j-i+1);
  } else if (i<j) {
    k = partition(arr, i, j);
    recur_qsort(arr, i, k);
    recur_qsort(arr, k+1, j);
  }
}
Example #11
0
int _tmain(int argc, _TCHAR* argv[])
{
	int a[N];
	for (int i = 0; i < N; i++){
		scanf_s("%d", &a[i]);
	}
	insertsort(a, N);
	for (int i = 0; i < N; i++){
		printf("%d\n", a[i]);
	}
	return 0;
}
Example #12
0
void main(int argc, char *argv[])
{
    FILE *fp_infile = fopen(argv[1], "r"), *fp_outfile = fopen(argv[2], "w");
    int digits[MAXLENGTH];
    
    getdigits(digits, fp_infile);
    readdigits(digits);
    insertsort(digits);
    readdigits(digits);
    savedigits(digits, fp_outfile);
    
}
Example #13
0
void testremoveduplicates()
{
	struct node* node = NULL;
	node = buildlistinsortedorder(5);
	printlist(node, "node");
	struct node* temp = buildlistinsortedorder(5);
	printlist(temp, "temp");
	append(&node, &temp);
	printlist(node, "after appending");
	insertsort(&node);
	printlist(node, "after sorting");
	removeduplicates(node);
}
STATUS insertarIdConjunto(CONJUNTO *c, const ID *e)

{

        if(estaId(c,e)==TRUE){
                return ERR;
        }
        if(cardinalidadConjunto(c)==c->max){/*Si no hubiera más memoria, reserva más*/
                if(masmemoria(c)==ERR) return ERR;
		c->elems[cardinalidadConjunto(c)]=*e;
       		c->card++;
		insertsort(c);/*Después de la inserción reordena el array*/
		return OK;
        }
	else{
      	  	 c->elems[cardinalidadConjunto(c)]=*e;
       		 c->card++;
		 insertsort(c);
        return OK;
	}
        
}
Example #15
0
void
start_insertsort(void)
{
	int n;
	fputs("INSERT SORT ALGORITHM \n",stdout);
	fputs("ENTER A LENGTH OF THE ARRAY = ",stdout);
	scanf("%d",&n);
	int data[n];

	read_data(data,n);
	insertsort(data,n, swap);
	echo_data(data,n);
}
void main()
{
    int i;
    int dlta[max];
    SqList l,a,b,c,d,e,f;
    CreateSq(&l);
    a=b=c=d=e=f=l;
    BubbleSort(&a);
    SelectSort(&b);
    QuickSort(&c);
    ShellSort(&d,dlta);
    Heapsort(&e);
    insertsort(&f);
}
Example #17
0
int main()
{
    int *a;
    int i;

    a = malloc(NUM * sizeof(int));
    assert(a != NULL);

    for (i = 0; i < NUM; i++)
        a[i] = NUM - i;
    insertsort(a, NUM);

    return 0;
}
Example #18
0
int main() {
    int arr[] = { 15, 25, 52, 49, 79, 20 };
    for (int i = 0; i < 6; i++) {
        printf("%d   ", arr[i]);
    }
    printf("\n");
    //shellsort(arr, 6);
    insertsort(arr, 6);
    //selectsort(arr, 6);
    //bubblesort(arr, 6);

    for (int i = 0; i < 6; i++) {
        printf("%d   ", arr[i]);
    }
    printf("\n");
}
Example #19
0
void main()
{cout<<"charufa3.cpp运行结果:\n";
 sqlist a;int i,n=MAXI;
 for(i=1;i<n;i++)
  {a[i].key=random(101+i)%80;
   a[i].data=random(98+i)%100;}
 cout<<"排序前数组:\n";
 for(int i=1;i<n;i++)
  cout<<setw(4)<<a[i].key;
 cout<<endl;
 cout<<"数组排序过程演示:\n";
 insertsort(a,n);
 cout<<"排序后数组:\n";
 for(int i=1;i<n;i++)
  cout<<setw(4)<<a[i].key;
 cout<<endl;cin.get();}
Example #20
0
static void
sort3_1 (int *array, int lo, int hi)
{
  int mid;

  if (hi - lo < THRESHOLD)
    {
      insertsort (array, lo, hi);
      return;
    }

  mid = partition (array, lo, hi);
  #pragma omp task
    sort3_1 (array, lo, mid - 1);
  sort3_1 (array, mid, hi);
}
Example #21
0
int main()
{    
    // struct _arr_random arrobject;
//	 Initarr(100,0);
//	 printf_s("%d\n",returnmax());
//	 printf_s("%d\n",returnmin());
//	 printf_s("%d\n ",countnum());
//	 printfall();
//	 printf_allnum();
	int a[]={1,22,4,56,78};
	insertsort(a,5);
	printf_all(a,5);
	 getchar();

	return 0;
}
int main(void)
{
	int ar_size;
	scanf("%d", &ar_size);
	int i;
	int ar1[ar_size];
	int ar2[ar_size];
	for(i = 0; i < ar_size; i++) { 
		scanf("%d", &ar1[i]);
		ar2[i] = ar1[i];
	}
	
	quicksort(ar_size, ar1);
	insertsort(ar_size, ar2);
	printf("%d\n", insertsort_swap_count - quicksort_swap_count);
	printf("%d\n", sizeof(ar1));
}
Example #23
0
static void ssort2(string a[], int n, int depth)
{   int d, r, partval;
    string *pa, *pb, *pc, *pd, *pl, *pm, *pn, t;
    if (n < 10) {
        insertsort(a, n, depth);
        return;
    }
    pl = a;
    pm = a + (n/2);
    pn = a + (n-1);
    if (n > 30) { /* On big arrays, pseudomedian of 9 */
        d = (n/8);
        pl = med3(pl, pl+d, pl+2*d);
        pm = med3(pm-d, pm, pm+d);
        pn = med3(pn-2*d, pn-d, pn);
    }
    pm = med3(pl, pm, pn);
    swap2(a, pm);
    partval = ptr2char(a);
    pa = pb = a + 1;
    pc = pd = a + n-1;
    for (;;) {
        while (pb <= pc && (r = ptr2char(pb)-partval) <= 0) {
            if (r == 0) { swap2(pa, pb); pa++; }
            pb++;
        }
        while (pb <= pc && (r = ptr2char(pc)-partval) >= 0) {
            if (r == 0) { swap2(pc, pd); pd--; }
            pc--;
        }
        if (pb > pc) break;
        swap2(pb, pc);
        pb++;
        pc--;
    }
    pn = a + n;
    r = min(pa-a, pb-pa);    vecswap2(a,  pb-r, r);
    r = min(pd-pc, pn-pd-1); vecswap2(pb, pn-r, r);
    if ((r = pb-pa) > 1)
        ssort2(a, r, depth);
    if (ptr2char(a + r) != 0)
        ssort2(a + r, pa-a + pn-pd-1, depth+1);
    if ((r = pd-pc) > 1)
        ssort2(a + n-r, r, depth);
}
Example #24
0
int main( int argc, char *argv[] ) {

    int *A, i = 0, count = 0, temp ;

    FILE *fin, *fun ;

    fun = fopen( argv[ 1 ] , "r" ) ;
    fin = fopen( argv[ 1 ] , "r" ) ;

    while( ( fscanf( fin , "%d", &temp ) ) != EOF ) {

        count++ ;

    }

    fclose( fin ) ;

    A = ( int * ) malloc( sizeof( int ) * count ) ;

    while( ( fscanf( fun, "%d", &A[ i ] ) ) != EOF ) i++ ;

    insertsort( A, count ) ;

    printf( "\nThe first 5 numbers are." ) ;

    for( i = 0 ; i < 5 ; i++ ) {

        printf( " %d", A[ i ] ) ;

    }

    printf( "\nThe last 5 numbers are." ) ;

    for( i = ( count - 5 ) ; i <= count  ; i++ ) {

        printf( " %d", A[ i ]  ) ;

    }

    fclose( fun ) ;

    return  0 ;

}
Example #25
0
int main(void) {
    int n;
    scanf("%d", &n);
    int i;
    int arr[n];
    for (i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }
    for (i = 0; i < n; i++) {
         printf("%d ", arr[i]);
    }
    printf("\n");
    insertsort(arr, n-1);
    for (i = 0; i < n; i++) {
         printf("%d ", arr[i]);
    }
    printf("\n");
    return 0;
}
Example #26
0
int main(void)
{
   int ret = 0;
   int i = 0, v[LEN] = {0};
   printf("\nThis program sorts %d elements random generated using insert sort\nThe elements are:\n\t", LEN);

   for (i = 0; i< LEN; i++)
   {
      v[i] = rand() % 500 + 1;
      printf(" %d",v[i]);
   }

   insertsort(v, LEN);

   printf("\nThe sorted elements are:\n\t");
   for (i = 0; i< LEN; i++)
      printf(" %d",v[i]);
   printf("\n");

   return ret;
}
STATUS extraerIdConjunto(CONJUNTO *c, const ID e){
	
	int indice=0;
	ID temporal;

	if(c==NULL) return ERR;
	/*No se llama a estaId para no hacer dos búsquedas binarias similares*/

	else if(esConjuntoVacio(c)==TRUE) return ERR;	

	else{

		indice=indiceIdConjunto(c,&e);/*Obtengo su índice, lo intercambio con el de la última posición, resto en 1 la cardinalidad y ordeno con insertsort*/
		if(indice==-1) return ERR;
		temporal=c->elems[indice];
		c->elems[indice]=c->elems[cardinalidadConjunto(c)-1];
		c->elems[cardinalidadConjunto(c)-1]=temporal;
		c->card-=1;
		insertsort(c);
		return OK;
	}
		
}
Example #28
0
static void
sort2_1 (int *array, int lo, int hi, int num_threads, int *busy)
{
  int mid;

  if (hi - lo < THRESHOLD)
    {
      insertsort (array, lo, hi);
      return;
    }

  mid = partition (array, lo, hi);

  if (*busy >= num_threads)
    {
      sort2_1 (array, lo, mid - 1, num_threads, busy);
      sort2_1 (array, mid, hi, num_threads, busy);
      return;
    }

  #pragma omp atomic
    *busy += 1;

  #pragma omp parallel num_threads (2) \
		       firstprivate (array, lo, hi, mid, num_threads, busy)
  {
    if (omp_get_thread_num () == 0)
      sort2_1 (array, lo, mid - 1, num_threads, busy);
    else
      {
	sort2_1 (array, mid, hi, num_threads, busy);
	#pragma omp atomic
	  *busy -= 1;
      }
  }
}
Example #29
0
iter_t
benchmp_interval(void* _state)
{
	char		c;
	iter_t		iterations;
	double		result;
	fd_set		fds;
	struct timeval	timeout;
	benchmp_child_state* state = (benchmp_child_state*)_state;

	iterations = (state->state == timing_interval ? state->iterations : state->iterations_batch);

	if (state->need_warmup) {
		/* remove spurious compilation warning */
		result = state->enough;
	} else {
		result = stop(0,0);
		if (state->cleanup) {
			if (benchmp_sigchld_handler == SIG_DFL)
				signal(SIGCHLD, SIG_DFL);
			(*state->cleanup)(iterations, state->cookie);
		}
		save_n(state->iterations);
		result -= t_overhead() + get_n() * l_overhead();
		settime(result >= 0. ? (uint64)result : 0.);
	}

	/* if the parent died, then give up */
	if (getppid() == 1 && state->cleanup) {
		if (benchmp_sigchld_handler == SIG_DFL)
			signal(SIGCHLD, SIG_DFL);
		(*state->cleanup)(0, state->cookie);
		exit(0);
	}

	timeout.tv_sec = 0;
	timeout.tv_usec = 0;
	FD_ZERO(&fds);

	switch (state->state) {
	case warmup:
		iterations = state->iterations_batch;
		FD_SET(state->start_signal, &fds);
		select(state->start_signal+1, &fds, NULL,
		       NULL, &timeout);
		if (FD_ISSET(state->start_signal, &fds)) {
			state->state = timing_interval;
			read(state->start_signal, &c, sizeof(char));
			iterations = state->iterations;
		}
		if (state->need_warmup) {
			state->need_warmup = 0;
			/* send 'ready' */
			write(state->response, &c, sizeof(char));
		}
		break;
	case timing_interval:
		iterations = state->iterations;
		if (state->parallel > 1 || result > 0.95 * state->enough) {
			insertsort(gettime(), get_n(), get_results());
			state->i++;
			/* we completed all the experiments, return results */
			if (state->i >= state->repetitions) {
				state->state = cooldown;
			}
		}
		if (state->parallel == 1 
		    && (result < 0.99 * state->enough || result > 1.2 * state->enough)) {
			if (result > 150.) {
				double tmp = iterations / result;
				tmp *= 1.1 * state->enough;
				iterations = (iter_t)(tmp + 1);
			} else {
				iterations <<= 3;
				if (iterations > 1<<27
				    || (result < 0. && iterations > 1<<20)) {
					state->state = cooldown;
				}
			}
		}
		state->iterations = iterations;
		if (state->state == cooldown) {
			/* send 'done' */
			write(state->response, (void*)&c, sizeof(char));
			iterations = state->iterations_batch;
		}
		break;
	case cooldown:
		iterations = state->iterations_batch;
		FD_SET(state->result_signal, &fds);
		select(state->result_signal+1, &fds, NULL, NULL, &timeout);
		if (FD_ISSET(state->result_signal, &fds)) {
			/* 
			 * At this point all children have stopped their
			 * measurement loops, so we can block waiting for
			 * the parent to tell us to send our results back.
			 * From this point on, we will do no more "work".
			 */
			read(state->result_signal, (void*)&c, sizeof(char));
			write(state->response, (void*)get_results(), state->r_size);
			if (state->cleanup) {
				if (benchmp_sigchld_handler == SIG_DFL)
					signal(SIGCHLD, SIG_DFL);
				(*state->cleanup)(0, state->cookie);
			}

			/* Now wait for signal to exit */
			read(state->exit_signal, (void*)&c, sizeof(char));
			exit(0);
		}
	};
	if (state->initialize) {
		(*state->initialize)(iterations, state->cookie);
	}
	start(0);
	return (iterations);
}
Example #30
0
void
benchmp_parent(	int response, 
		int start_signal, 
		int result_signal, 
		int exit_signal,
		pid_t* pids,
		int parallel, 
	        iter_t iterations,
		int warmup,
		int repetitions,
		int enough
		)
{
	int		i, j;
	int		bytes_read;
	result_t*	results = NULL;
	result_t*	merged_results = NULL;
	char*		signals = NULL;
	unsigned char*	buf;
	fd_set		fds_read, fds_error;
	struct timeval	timeout;

	if (benchmp_sigchld_received || benchmp_sigterm_received) {
#ifdef _DEBUG
		fprintf(stderr, "benchmp_parent: entering, benchmp_sigchld_received=%d\n", benchmp_sigchld_received);
#endif
		goto error_exit;
	}

	results = (result_t*)malloc(sizeof_result(repetitions));
	merged_results = (result_t*)malloc(sizeof_result(parallel * repetitions));
	signals = (char*)malloc(parallel * sizeof(char));
	if (!results || !merged_results || !signals) return;

	/* Collect 'ready' signals */
	for (i = 0; i < parallel * sizeof(char); i += bytes_read) {
		bytes_read = 0;
		FD_ZERO(&fds_read);
		FD_ZERO(&fds_error);
		FD_SET(response, &fds_read);
		FD_SET(response, &fds_error);

		timeout.tv_sec = 1;
		timeout.tv_usec = 0;
		select(response+1, &fds_read, NULL, &fds_error, &timeout);
		if (benchmp_sigchld_received 
		    || benchmp_sigterm_received
		    || FD_ISSET(response, &fds_error)) 
		{
#ifdef _DEBUG
			fprintf(stderr, "benchmp_parent: ready, benchmp_sigchld_received=%d\n", benchmp_sigchld_received);
#endif
			goto error_exit;
		}
		if (!FD_ISSET(response, &fds_read)) {
			continue;
		}

		bytes_read = read(response, signals, parallel * sizeof(char) - i);
		if (bytes_read < 0) {
#ifdef _DEBUG
			fprintf(stderr, "benchmp_parent: ready, bytes_read=%d, %s\n", bytes_read, strerror(errno));
#endif
			goto error_exit;
		}
	}

	/* let the children run for warmup microseconds */
	if (warmup > 0) {
		struct timeval delay;
		delay.tv_sec = warmup / 1000000;
		delay.tv_usec = warmup % 1000000;

		select(0, NULL, NULL, NULL, &delay);
	}

	/* send 'start' signal */
	write(start_signal, signals, parallel * sizeof(char));

	/* Collect 'done' signals */
	for (i = 0; i < parallel * sizeof(char); i += bytes_read) {
		bytes_read = 0;
		FD_ZERO(&fds_read);
		FD_ZERO(&fds_error);
		FD_SET(response, &fds_read);
		FD_SET(response, &fds_error);

		timeout.tv_sec = 1;
		timeout.tv_usec = 0;
		select(response+1, &fds_read, NULL, &fds_error, &timeout);
		if (benchmp_sigchld_received 
		    || benchmp_sigterm_received
		    || FD_ISSET(response, &fds_error)) 
		{
#ifdef _DEBUG
			fprintf(stderr, "benchmp_parent: done, benchmp_child_died=%d\n", benchmp_sigchld_received);
#endif
			goto error_exit;
		}
		if (!FD_ISSET(response, &fds_read)) {
			continue;
		}

		bytes_read = read(response, signals, parallel * sizeof(char) - i);
		if (bytes_read < 0) {
#ifdef _DEBUG
			fprintf(stderr, "benchmp_parent: done, bytes_read=%d, %s\n", bytes_read, strerror(errno));
#endif
			goto error_exit;
		}
	}

	/* collect results */
	insertinit(merged_results);
	for (i = 0; i < parallel; ++i) {
		int n = sizeof_result(repetitions);
		buf = (unsigned char*)results;

		FD_ZERO(&fds_read);
		FD_ZERO(&fds_error);

		/* tell one child to report its results */
		write(result_signal, buf, sizeof(char));

		for (; n > 0; n -= bytes_read, buf += bytes_read) {
			bytes_read = 0;
			FD_SET(response, &fds_read);
			FD_SET(response, &fds_error);

			timeout.tv_sec = 1;
			timeout.tv_usec = 0;
			select(response+1, &fds_read, NULL, &fds_error, &timeout);
			if (benchmp_sigchld_received 
			    || benchmp_sigterm_received
			    || FD_ISSET(response, &fds_error)) 
			{
#ifdef _DEBUG
				fprintf(stderr, "benchmp_parent: results, benchmp_sigchld_received=%d\n", benchmp_sigchld_received);
#endif
				goto error_exit;
			}
			if (!FD_ISSET(response, &fds_read)) {
				continue;
			}

			bytes_read = read(response, buf, n);
			if (bytes_read < 0) {
#ifdef _DEBUG
				fprintf(stderr, "benchmp_parent: results, bytes_read=%d, %s\n", bytes_read, strerror(errno));
#endif
				goto error_exit;
			}
		}
		for (j = 0; j < results->N; ++j) {
			insertsort(results->v[j].u, 
				   results->v[j].n, merged_results);
		}
	}

	/* we allow children to die now, without it causing an error */
	signal(SIGCHLD, SIG_DFL);
	
	/* send 'exit' signals */
	write(exit_signal, results, parallel * sizeof(char));

	/* Compute median time; iterations is constant! */
	set_results(merged_results);

	goto cleanup_exit;
error_exit:
#ifdef _DEBUG
	fprintf(stderr, "benchmp_parent: error_exit!\n");
#endif
	signal(SIGCHLD, SIG_DFL);
	for (i = 0; i < parallel; ++i) {
		kill(pids[i], SIGTERM);
		waitpid(pids[i], NULL, 0);
	}
	free(merged_results);
cleanup_exit:
	close(response);
	close(start_signal);
	close(result_signal);
	close(exit_signal);

	if (results) free(results);
	if (signals) free(signals);
}