Reemplace fork () con pthread_create ()

Can someone show me how to replace this simple code to use pthread_create instead of fork()? Is it possible?in particular, I've some problems with the struct *ex passed into main(). how have I to change it?

int k=0;
pthread_mutex_t mutex= PTHREAD_MUTEX_INITIALIZER;

struct primi{
    int s;
    int temp;};
struct example{
    int c;
    struct primi primissimi;};

//handler atfork()
void prepare(void){ 
    pthread_mutex_lock(&mutex);}
void parent(void){
    pthread_mutex_unlock(&mutex);}
void child(void){
    pthread_mutex_unlock(&mutex); }


void *funzione_thread(void* args){
   pthread_mutex_lock(&mutex);
   struct example *exthread = args;
   struct example locale = *exthread;
   locale.primissimi.s++;
   pthread_mutex_unlock(&mutex);
   //do something
   pthread_exit(NULL);
  }

int ffork(struct example *extmp){
   pthread_t id[5];
   int i;
   while(k<3){
      k++;
      pthread_create(&id[k],NULL,funzione_thread,extmp);
      }
   for(i=1;i<=3;i++){
      pthread_join( id[i] ,NULL );
      printf("waited thread %d\n",i);
      }
   printf("threads completed\n");
   return 1;
   }

 int main(int argc, char** argv){   
   struct example *ex = malloc(sizeof(*ex));
   int pid,tmp,err;
   if ((err = pthread_atfork(prepare, parent, child)) != 0){
       printf("can't install fork handlers");
       exit(-1);}
   pid=fork();
   if(pid==0){
      ex->c=1;
      ex->primissimi.s=1;
      if((tmp=ffork(ex))!=1){
          printf("error ffork\n");
          exit(0);
          }
          else{printf("ok ffork\n");
          pthread_exit (NULL);
          }
      }

else{
    sleep(10);
    }
return 1;
 }

preguntado el 01 de febrero de 12 a las 04:02

@BillyONeal not really. I'm working on a program and I need to change it to get a feature.so I simplified the situation -

the line "struct example locale = *exthread;" creates a copy of the data you then modify instead of a pointer to the existing data, and thus the modifications will not show up in the other threads. Is this the problem you said you were having? If not, please explain what the problem is, as you are being incredibly vague. -

@tbert no, the problem is in the struct of the main. I need to pass it to a pthread_create(xxx,NULL,ffork,here that struct) -

Which you're doing; I truly fail to see what problem you're having. Specifically: a) what is happening that you don't want to happen, or b) what is no happening that debiera ser ¿sucediendo? -

2 Respuestas

fork() creates an new process,
pthread_create() spawns a new thread, both the functions do drastically different things. A Process and a Thread are different.
Are you sure what you are trying to achieve?

Respondido 01 Feb 12, 08:02

@rschirin: The problem is that your question obstinately says "I want to change this from fork a pthread_create, without changing the behavior of the program". But that is not possible -- the functions do completely different things and have completely different applications. Switching is going to have some behavior change on your program. You have to tell us what behavior you want in order for us to help you. Put another way, what is the expected behavior? - Billy ONeal

@BillyONeal I want to create a thread and pass it to function ffork(), then it continues its normal behaviour, creating other 3 threads into while. - rschirin

I think the basic rule of thumb is that fork() spans a process, who does not share any data with the parent program, while pthread_create() spans a thread (on GNU/Linux, still a process), that shares data with the parent program.

Consulte nuestra página http://www.makelinux.net/alp/032 para más detalles.

So, it should be possible to replace all fork() calls with pthread_create() calls, since the latter is more flexible.

Bear in mind that the structure of your program deberán be altered if you do that change. Mainly because fork()-spanned process will execute the next instruction following the fork() system call. While, for pthread_create(), you must explicitly encapsulate the code of the new process into a function.

So, first step should be to encapsulate code following the fork() command into a stand-alone function that you later pass to pthread_create().

Respondido 14 Abr '12, 14:04

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.