简体   繁体   English

用pthread_create()替换fork()

[英]Replace fork() with pthread_create()

Can someone show me how to replace this simple code to use pthread_create instead of fork()? 有人可以告诉我如何使用pthread_create代替fork()替换此简单代码吗? Is it possible?in particular, I've some problems with the struct *ex passed into main(). 可能吗?特别是,传递给main()的结构* ex遇到了一些问题。 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;
 }

fork() creates an new process, fork()创建一个新进程,
pthread_create() spawns a new thread, both the functions do drastically different things. pthread_create()产生一个新线程,这两个函数的作用截然不同。 A Process and a Thread are different. 进程和线程是不同的。
Are you sure what you are trying to achieve? 您确定要达到的目标吗?

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. 我认为基本的经验法则是fork()跨越一个进程,该进程不与父程序共享任何数据,而pthread_create()跨越一个线程(在GNU / Linux上仍然是一个进程),该线程与父程序。

See http://www.makelinux.net/alp/032 for details. 有关详细信息,请参见http://www.makelinux.net/alp/032

So, it should be possible to replace all fork() calls with pthread_create() calls, since the latter is more flexible. 因此,应该可以将所有fork()调用替换为pthread_create()调用,因为后者更加灵活。

Bear in mind that the structure of your program must be altered if you do that change. 请记住,如果进行更改,则必须更改程序的结构。 Mainly because fork()-spanned process will execute the next instruction following the fork() system call. 主要是因为跨fork()的进程将在fork()系统调用之后执行下一条指令。 While, for pthread_create(), you must explicitly encapsulate the code of the new process into a function. 同时,对于pthread_create(),必须将新进程的代码显式封装到一个函数中。

So, first step should be to encapsulate code following the fork() command into a stand-alone function that you later pass to pthread_create(). 因此,第一步应该是将fork()命令之后的代码封装到一个独立的函数中,然后将其传递给pthread_create()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM