Technical article 100848

The qsort(S) routine hangs when sorting a large array on SCO OpenServer 5.

OpenServer 5.0.0OpenServer 5.0.2

Written · Last revised

Problem

I am using the routine qsort(S) with a large (over 250000 elements of type struct) array. The program hangs at the qsort(S) call although the same code worked fine in earlier releases.

Cause

There is a problem with the qsort(S) library routine in the SCO OpenServer Development System. This can be seen in the following sample:

/* Compile two versions of this to see the problem. * "cc -o qtest qtest.c" should hang at the qsort call * "cc -o qtest.short qtest.c -D SHORT" should execute normally */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct {
    char        name[31];
    int         key;
    char        action;
    char        row[19];
} STRUCTS;
static STRUCTS    *struct_ptr;
#define TABLES  10
#define ACTIONS 23
main( int argc, char *argv[] )
{
#if SHORT
#define STRUCT_ARRAY_SIZE   250
#else
#define STRUCT_ARRAY_SIZE   250000
#endif
static STRUCTS    structs[STRUCT_ARRAY_SIZE];
STRUCTS   *first,*second;
int     compare_structs();
static char     *tables[TABLES] = {
        "G_TABLE",                      "CL_TABLE",
        "B_TABLE",                      "G_TABLE",
        "O_TABLE",                      "CR_TABLE",
        "R_TABLE",                      "CODE",
        "CA_TABLE",                     "TEMP_TABLE" };
static  char    actions[] = {
        'I','I','U','D','I','D','U','I','I','U',
        'I','D','U','I','U','D','D',' ','D','I',
        ' ','I','U' };
static  char    *row = {"00000333.0000.0001"};
int     ii = 0,jj = 0,kk=0;
/* Initialize the array */
for( struct_ptr=structs; struct_ptr-structs < STRUCT_ARRAY_SIZE; struct_ptr++ ) {
        strcpy( struct_ptr->name,tables[(ii++)%TABLES] );
        struct_ptr->key = (kk++)%1234;
        struct_ptr->action    = actions[(jj++)%TABLES];
        strcpy( struct_ptr->row, row );
}
printf("Print out first 20 structs\n");
for( struct_ptr=structs; struct_ptr-structs < 20; struct_ptr++ )
        printf( "%30s  %4d  %c  %s\n",
                struct_ptr->name,
                struct_ptr->key,
                struct_ptr->action,
                struct_ptr->row );

printf("\nCalling qsort: "); qsort( (void *)structs, STRUCT_ARRAY_SIZE, (size_t)sizeof(structs[0]), compare_structs ); printf("/nReturning from qsort\n");

printf("The first 20 sorted structs are:\n");
for( struct_ptr=structs; struct_ptr-structs < 20; struct_ptr++ )
        printf( "%30s  %4d  %c  %s\n",
                struct_ptr->name,
                struct_ptr->key,
                struct_ptr->action,
                struct_ptr->row );
}
/*-----------------------------------------------------------------*/
int     compare_structs(STRUCTS *first, STRUCTS *second)
{
int     order;
    if( !( order = strcmp(first->name, second->name) ) ) {
        if( first->action == second->action )
            order = 0;
        else if( first->action < second->action )
            order = -1;
        else
            order = 1;
    }
    return(order);
}
/*************************************************************************/

Solution

This has been reported to SCO Engineering. As a workaround, the qsort(S) object file from either the SCO UNIX System V/386 Release 3.2 Version 4.2 or SCO Open Desktop Release 3.0 Development System can be used as follows:

1. Extract the qsort.o file from /usr/lib/libc.a:

# ar -x /lib/libc.a qsort.o

2. Compile your program using this object file instead of the object file in the OpenServer libc.a:

# cc <filename.c> qsort.o ...

From the Xinuos technical article database, record 100848. Reproduced with permission. The original is at www.sco.com/ta/100848, which is reachable but blocked from search engines. Xinuos marks this article against a retired product line. That refers to the product’s lifecycle, not to whether the advice still works — plenty of it still applies to running systems.

Next step

Need this fixed rather than explained?

Tell us the release, what the system runs and what it is doing. We will reply by email with what we would do next.

  • You deal with the people who do the work
  • No obligation, and no upgrade required before we will help
  • We tell you plainly if something is not worth doing

Send an enquiry

We reply by email. If a server is down, say so in the first line. You can also write to help@scoexpert.com.