[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Forking with Threads



I am having difficulty getting a fork call to work within a thread.
The code compiles and works fine on development host system.
The code cross compiles, but fails to run on target system.

Can anyone explain this to me?

//
Greg Cannon

I am compiling with standard makefile...

$(PROGS): $(OBJS)
	$(CC) $(LDFLAGS) -pthread $^ $(LDLIBS) -o $@

Test.o: Test.c
	$(CC) -c Test.c

Test.c follows:

#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<pthread.h>

void forkSlave() {
	pid_t	childpid;
	int	rv;

	char*	args[]	=	{"/bin/ls",NULL};

	if((childpid=fork())==-1) {
		perror("bad fork");
		exit(1);
	} else if(childpid==0) {
			printf("HAPPY CHILD\n");
			exit(0);
	} else {
			printf("WAITING PARENT\n");
			waitpid(childpid,&rv,0);
			printf("HAPPY PARENT\n");
	}
}

void* Thread1(void* arg) {

	forkSlave();
	printf("HAPPY THREAD\n");
	return(NULL);
}

int main(int argc, char* argv[]) {
	pthread_t	t1;

	pthread_create(&t1,NULL, Thread1, NULL);	// a thread that forks a
process...
	pthread_join(t1, NULL);

	forkSlave();
	printf("HAPPY MAIN\n");

	return(0);
}