Basic Linux Multi-Process & Multi-Thread Programming
来源:互联网 发布:韩国网络电视apk 编辑:程序博客网 时间:2024/04/28 18:45
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
- Basic Linux Multi-Process & Multi-Thread Programming
- Java Multi-thread Programming
- difference between Multi process and multi thread
- Posix multi-thread programming 笔记
- Multi-Programming-5 Thread Pools
- Python multi-process VS multi-thread (多核CPU利用率)
- android multi process and single thread principle
- Multi-Thread Programming Resources for Game Engine
- Micro Focus COBOL multi-thread programming
- c# multi thread programming ,c# 多线程编程
- multi-thread
- Multi-Thread
- multi thread
- Analysis of Process Multi-Thread Manangement Based on a simpilified Linux Kernel--myKernel
- Multi-process Architecture
- Multi-process Resource Loading
- GDB debug multi-process
- chromium Multi-process Architecture
- MongoDB的主从部署
- /dev/fb0入门练习
- js弹窗
- C# x64程序部署 System.BadImageFormatException异常
- 产生随机概率的c方法
- Basic Linux Multi-Process & Multi-Thread Programming
- 1
- Android: m, mm以及mmm编译命令以及make snod的使用
- 在windows环境下写linux代码
- UVa 10522 - Height to Area
- 如何查看安卓系统dhcp协议栈的源代码
- Android APK反编译得到Java源代码和资源文件
- 查询一个JOB是否存在
- js 节点用法