Basic Linux Multi-Process & Multi-Thread Programming

來源:互聯網
上載者:User

Today I have to make my algorithm running in parallel in order to make it faster. At first I used following way to implement multi-process:

unsigned int proc_num = 5;pid_t* pids=new pid_t[proc_num];double incr=(double)N/(double)proc_num;/* Start children. */for (unsigned int i = 0; i < proc_num; ++i) {if ((pids[i] = fork()) < 0) {    perror("fork");    abort(); } else if (pids[i] == 0) {    // DoWorkInChild();    exit(0);  }}/* Wait for children to exit. */int status;pid_t pid;while (proc_num > 0) {  pid = wait(&status);if(status != 0)  printf("Child Process with PID %ld aborted with status 0x%x.\n", (long)pid, status);  --proc_num;  // TODO(pts): Remove pid from the pids array.}

Above way worked well, however, there's no way to change the "shared" variables in child processes. Because each child process has an independent copy of all variables.

In order to change the same array in parallel, I implemented multi threads.

struct thread_info {    /* Used as argument to thread_start() */pthread_t thread_id;        /* ID returned by pthread_create() */intstart;       /* Application-defined thread # */int     end;      /* From command-line argument */int*gpdarr;/* Array to store GPD number */long int G;/* genome length */unsigned int L;/* read length */double l1;// GPD parameter lambda1double l2;// GPD parameter lambda2double M;// maximum of GPD density};void printPt(pthread_t pt) {  unsigned char *ptc = (unsigned char*)(void*)(&pt);  printf("0x");  for (size_t i=0; i<sizeof(pt); i++) {    printf("%02x", (unsigned)(ptc[i]));  }}void* gen_gpd_num(void* arg){struct thread_info *tinfo = (struct thread_info *) arg;// do something herepthread_exit(0);}int main(){        unsigned int tnum = 20;double incr=(double)N/(double)tnum;thread_info* tinfo=new thread_info[tnum];int err;void* res;/* Start children. */for(unsigned int idx=0;idx<tnum;++idx) {tinfo[idx].start=incr*idx;tinfo[idx].end=incr*(idx+1);tinfo[idx].gpdarr=gpdnum;tinfo[idx].G=G;tinfo[idx].L=l;tinfo[idx].l1=l1;tinfo[idx].l2=l2;tinfo[idx].M=M;err = pthread_create(&tinfo[idx].thread_id, NULL, &gen_gpd_num, &tinfo[idx]);if(err!=0)printf("can't create thread :[%s]", strerror(err));elsepthread_join(tinfo[idx].thread_id, &res);}        delete [] tinfo;}

The potential problem of using multi-threading in Linux is -- it is hard to figure out if there really are many threads are running simultaneously. 

When compiling multi-threading program using GCC, pthread library must be specified:

g++ -o foo -Wall foo.cc -L/usr/lib -lpthread

References:

How to Create Threads in Linux (With a C Example Program) http://www.kernel.org/doc/man-pages/online/pages/man3/pthread_create.3.html


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.