Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 30 Apr 1998 11:29:31 -0700
From:      Peter Haight <psh1@cornell.edu>
To:        freebsd-questions@FreeBSD.ORG
Subject:   Threads and system()
Message-ID:  <199804301829.LAA03082@wartch.rih.org>

next in thread | raw e-mail | index | archive | help

I might have posted this already, but I didn't get any replies. I'm not on
the list, so I don't know whether my earlier post showed up. By the way, is
there anyway to check that? I tried searching for my post, but it wasn't
there. How soon do the articles become searchable in the archive?

The problem I'm having is that I have this threaded program in which I want
to launch an external editor using the system() call. This works fine until
the system() call returns. When the call returns, that thread finishes its
stack and then exits. When that thread exits, my whole program exits. The
rest of my program should be in a permanent while() loop. What's going on
here. Is this correct behavior? Am I doing something wrong?

Here's an example program that illustrates this:

#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>

void signal_received(int arg)
{
    printf("signal %d\n", arg);
}

void* run_system(void* arg)
{
    system("cat /home/peterh/.cshrc");
    printf("Hello!\n");
}

int main()
{
    pthread_t thread;
    pthread_attr_t thread_attr;
    int result;

    while (1)
    {
	result = pthread_attr_init(&thread_attr);
	printf("pthread_attr_init returned %d\n", result);
	result = pthread_create(&thread, &thread_attr, &run_system, 0);
	printf("pthread_create returned %d\n", result);
	sleep(5);
    }

    return 0;
}


Here's how I compile it:

gcc thread.c -lc_r

The program totally exits after printing out my .cshrc and 'Hello!'. If I
take out the system() call, it will run indefinitely.



To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199804301829.LAA03082>