X-Recipient: archive-cygwin@delorie.com
X-Spam-Check-By: sourceware.org
Message-ID: <003301c92110$336cee20$0200a8c0@whiterabc2znlh>
From: "Hirokazu Yamamoto" <ocean-city@m2.ccsnet.ne.jp>
To: <cygwin@cygwin.com>
Subject: crash if fork(2) from another thread
Date: Sun, 28 Sep 2008 11:16:21 +0900
MIME-Version: 1.0
Content-Type: text/plain; 	charset="iso-2022-jp"
Content-Transfer-Encoding: 7bit
X-Mailer: Microsoft Outlook Express 6.00.2800.1933
X-IsSubscribed: yes
Mailing-List: contact cygwin-help@cygwin.com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe@cygwin.com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin@cygwin.com>
List-Help: <mailto:cygwin-help@cygwin.com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner@cygwin.com
Mail-Followup-To: cygwin@cygwin.com
Delivered-To: mailing list cygwin@cygwin.com

# I've post mail, but it didn't show up in
http://www.nabble.com/Cygwin-f12165.html.
# Maybe it was not good to attach a file. So try again...

I'm not familiar with pthread & fork, but I think following code should not
crash. Is this expected behavior?

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

void *thread_func(void* args)
{
    int ret;
    pid_t pid;
    pthread_t thread;

    puts("thread_func");

    pid = fork();

    assert(pid != (pid_t)-1);

    if (pid != 0) /* parent process */
    {
        int status;

        printf("parent process (child pid = %d)\n", pid);

        waitpid(pid, &status, 0);

        puts("parent process end");
    }
    else /* child process */
    {
        puts("child process"); /* crash here */
    }

    return args;
}

int main()
{
    int ret;
    pthread_t thread;

    ret = pthread_create(&thread, NULL, thread_func, NULL);
    assert(ret == 0);

    ret = pthread_join(thread, NULL);
    assert(ret == 0);
}

// built with "gcc main.c -o main.exe"

Thank you.


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

