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

RE: Forking with Threads



O'Reilly "Pthreads programming" can help you.
The chap.5 explains this concern.
Threads and Process management.

See you.

Fernando RUIZ CASAS
home: correo@xxxxxxx.com
work: fernando.ruiz@xxxxxxx.es



> -----Mensaje original-----
> De: owner-dev-etrax@xxxxxxx.com]En">mailto:owner-dev-etrax@xxxxxxx.com]En nombre
> de Greg Cannon
> Enviado el: jueves, 13 de septiembre de 2001 17:35
> Para: dev-etrax@xxxxxxx.com
> Asunto: 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);
> }
>