threads/76690: fork hang in child for (-lc_r & -lthr)

Леонтьев Сергей Ефимович lse at
Wed Jan 26 04:20:25 PST 2005

The following reply was made to PR threads/76690; it has been noted by GNATS.

From: =?koi8-r?B?7MXPztTYxdcg88XSx8XKIOXGyc3P18ne?= <lse at>
To: <freebsd-gnats-submit at>,
	=?koi8-r?B?7MXPztTYxdcg88XSx8XKIOXGyc3P18ne?= <lse at>
Subject: Re: threads/76690: fork hang in child for (-lc_r & -lthr)
Date: Wed, 26 Jan 2005 15:18:38 +0300

 Sorry, program in section "How-To-Repeat" hang only on "-lc_r" library.
 Hang for library -lthr cause by malloc()/free() in child, not fork() =
 "Correct" version "How-To-Repeat" program:
 # This is a shell archive.  Save it in a file, remove anything before
 # this line, and then unpack it by entering "sh file".  Note, it may
 # create directories; files and directories will be owned by you and
 # have default permissions.
 # This archive contains:
 #       fork-malloc-free.c
 echo x - fork-malloc-free.c
 sed 's/^X//' >fork-malloc-free.c << 'END-of-fork-malloc-free.c'
 X#include <errno.h>
 X#include <pthread.h>
 X#include <signal.h>
 X#include <stdlib.h>
 X#include <stdio.h>
 X#include <sys/wait.h>
 X#include <unistd.h>
 Xconst int INFL =3D 1000000000;
 Xconst int PTHR =3D 4;
 Xconst int NATR =3D 2;
 X#ifndef SEMI_OK                // if SEMI_OK not defined - >95% hang on =
 my syst
 Xconst int FRKL =3D 100;
 Xconst int CHLD =3D 100;
 X#else                  // if SEMI_OK defined - 50% hang on my system
 Xconst int FRKL =3D 3;
 Xconst int CHLD =3D 3;
 Xvoid *malloc_free(void *pvcnt)
 X    volatile sig_atomic_t *pscnt =3D (volatile sig_atomic_t *)pvcnt;
 X    int n =3D *pscnt;
 X    int i;
 X    void *m;
 X    for(i =3D 0; i < n; i++){
 X        m =3D malloc(10);
 X       *pscnt =3D i;
 X        free(m);
 X    }
 X    return NULL;
 Xint main (void)
 X    pthread_attr_t attrs[NATR];
 X    pthread_t thread_id;
 X    volatile sig_atomic_t cnt[PTHR];
 X    int i;
 X    int cntr;
 X    pid_t pid, savedpid;
 X    int pstat;
 X    pthread_attr_init(&attrs[0]);
 X    pthread_attr_setdetachstate(&attrs[0], PTHREAD_CREATE_DETACHED);
 X    pthread_attr_setscope(&attrs[0], PTHREAD_SCOPE_PROCESS);
 X    pthread_attr_init(&attrs[1]);
 X    pthread_attr_setdetachstate(&attrs[1], PTHREAD_CREATE_DETACHED);
 X    pthread_attr_setscope(&attrs[1], PTHREAD_SCOPE_SYSTEM);
 X    for(i =3D 0; i < PTHR; i++) {
 X        cnt[i] =3D INFL;
 X        if(pthread_create(&thread_id, &attrs[i%NATR],
 X                       &malloc_free, (void *)&cnt[i])){
 X            perror("pthread_create:");
 X            return 1;
 X        }
 X    }
 X    fprintf(stderr, "Threads created.\n");
 X    for (i =3D 0; i < FRKL; i++) {
 X        fprintf(stderr, "forking\n");
 X        switch(pid =3D fork()) {
 X        case -1:                        /* error */
 X           perror("fork fail:");
 X           return 2;
 X        case 0:                         /* child */
 X           // -lc_r:
 X           // Child don't anything to work - simple exit
 X           // When child hang, this hang in fork code
 X           //
 X           // -lthr:
 X           // Child hang by malloc()/free();
 X            cntr =3D CHLD;
 X            malloc_free(&cntr);
 X            _exit(0);
 X        default:                        /* parent */
 X            savedpid =3D pid;
 X            do{
 X                pid =3D waitpid(savedpid, &pstat, 0);
 X            }while(pid =3D=3D -1 && errno =3D=3D EINTR);
 X            break;
 X        }
 X    }
 X    fprintf(stderr, "Threads OK:");
 X    for(i =3D 0; i < PTHR; i++){
 X        fprintf(stderr, " %d", (int)cnt[i]);
 X    }
 X    fprintf(stderr, "\n");
 X    _exit(0);
 X    return 0;
 Sorry for my bests English.
 Serguei E. Leontiev w:+7(095)289-4367 USSR, Moscow, 127018, Obraztsova =
 Crypto-Pro=9A=9A=9A=9A=9A=9A=9A=9A=9A w:+7(095)933-1168
 <> m:+7(916)686-1081 SMS: <>
 =9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A =
 p:+7(095)231-3838 for abonent +7(916)686-1081
 =9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A =
 h:+7(095)318-1146 USSR, Moscow, 113303, Kakhovka 6-40
 =9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A=9A =
 w:+7(095)939-2382 USSR, Moscow, Universitetskij 13

More information about the freebsd-threads mailing list