you can either start a new process or you can start a new thread other wise execution will not continue until after the system call returns. Creating a new thread uses much less resources than creating a new process.
fork creates a new process here is a simple example.
***************************************
int forks=100;
for(i=0;i<mat_dat[3];i++)
{
if(forks>0)
{
pipe(&ps[i].pp[0]);
pid=fork();
forks--;
if(pid==0)
{
sprintf(fdstr,"%d",ps[i].pp[1]);
sprintf(col,"%d",i);
execl("./vecmult","vecmult", f1_name.c_str(), f2_name.c_str(), col, fdstr, NULL);
}
}
else
{
printf("created fork bomb");
return;
}
}
Here is a simple example using pthreads.
antelope, giraffe, cheetah, lion are are functions that take a void * parameter and return void *.
****************************************************
list_t<pthread_t*> thr_list;/*used to keep track of threads*/
pthread_t* p_thrp;
p_thrp=new pthread_t;/*create a new thread*/
thr_list.insert(p_thrp);/*add new thread to list*/
...
...
/*switch the animal type and then create correct thread type*/
switch(a_type){
case 'A': rc=pthread_create(p_thrp,NULL,antelope,(void*)_num);
break;
case 'G': rc=pthread_create(p_thrp,NULL,giraffe,(void*)a_num);
break;
case 'C': rc=pthread_create(p_thrp,NULL,cheetah,(void*)a_num);
break;
case 'L': rc=pthread_create(p_thrp,NULL,lion,(void*)a_num);
break;
default: break;
} /*end switch*/
if(rc){
printf("pthread_create returned with an error\n");
exit(-1);
}
...
...
pthread_exit(NULL); /*edit*this makes the parent wait for all children before exiting.*/
Last edited by haobaba1; 02-28-2004 at 09:44 PM.
|