Jump to content

fork more than one process

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
vibhu

vibhu

    Newbie

  • Members
  • Pip
  • 2 posts
How do I fork more than one process in unix. There are lot of examples how to fork one child process from the main process. But there are no examples as to how to fork more than one process. Can you please just give me an example of forking two processes. Thank you.

#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,712 posts
Use <pthread.h>.

I copied and pasted this from another forum.
#include <stdio.h>
#include <pthread.h> 
 
// The follwing is a GCCE toolchain workaround needed when compiling with GCCE
// and using main() entry point
#ifdef __GCCE__
#include <staticlibinit_gcce.h>
#endif
 
void *TestFunction(void*)
{
    printf("Test function....\n");
    return NULL;
}
 
int CreateThread()
{
   pthread_t thread_hdl;
   if(pthread_create(&thread_hdl,NULL,TestFunction, NULL) != 0)
   {
     printf("Thread creation failed \n");
     return -1;
   }
}
int main(void)
{
	CreateThread();
	printf("Hello Open C!\n");
	return 0;
}


#3
vibhu

vibhu

    Newbie

  • Members
  • Pip
  • 2 posts
That did not help. There is a constraint that I should use fork(). any other answers.

#4
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,712 posts
Have your child process fork then. Just pass it a counter indicating how many times it should fork.

PARENT (fork 3 times) ->child(2)->grandchild(1)->greatgrandchild(0)