Linux 进程 vs 线程
2017-04-09 08:55
204 查看
进程:
资源管理的最小单位,独占内存空间线程:
调度的最小单位,在进程中创建,共享内存空间。函数调用
步骤 | 线程 | 进程 |
---|---|---|
创建 | pthread_create | fork |
退出 | pthread_exit/pthread_cancel | exit/__exit |
等待 | pthread_join | wait |
#include <stdio.h> #include <pthread.h> //并发:多线程 void *thread(void *st){ while(1){ printf("%d\n",st); sleep(1); } pthread_exit(NULL); } int main(int argc,char **argv) { pthread_t tid[11]; int i; for(i = 1; i <= 10; i++){ pthread_create(tid+i,NULL,thread,(void*)i); } sleep(5); pthread_cancel(tid[1]); pthread_cancel(tid[3]); pthread_cancel(tid[5]); pthread_cancel(tid[7]); pthread_cancel(tid[9]); pthread_join(tid[2],NULL); }
运行
root@ubuntu:/home/Desktop/pthread# gcc pthread_create.c -lpthread root@ubuntu:/home/Desktop/pthread# ./a.out 7 6 8 5 9 4 10 3 2 1
#include <stdio.h> #include <unistd.h> int main(int argc,char **argv) { pid_t pid; pid = fork(); // if(pid < 0){ perror("fork:"); return 0; //exit //_exit } if(pid > 0){ //父进程 printf("%d:HELLO WORLD!\n",getpid()); wait(NULL);//清理子进程(僵尸进程) sleep(5); } else if(pid == 0){//子进程 printf("%d:hello world!\n",getpid()); return 0; } // return 0; }
运行
root@ubuntu:/home/Desktop/fork# gcc fork.c root@ubuntu:/home/Desktop/fork# ./a.out 6326:HELLO WORLD! 6327:hello world! root@ubuntu:/home/Desktop/fork#
相关文章推荐
- linux 进程 VS 线程
- linux 线程 进程经典文章
- Linux 进程和线程查看以及管理
- linux下线程调用sleep,进程挂起
- linux进程介绍之fork vs vfork(转)
- Linux 进程管理--------------------线程
- 复习进程和线程 process vs thread
- 将Win32 C/C++应用程序迁移到Linux-进程、线程和共享内存
- linux 线程 进程经典文章
- linux进程之fork vs vfork
- Linux 线程实现机制分析--线程 进程经典文章
- Linux 进程和线程查看以及管理
- linux线程,进程经典文章
- linux线程,进程经典文章
- 将 Win32 C/C++ 应用程序迁移到 POWER 上的 Linux,第 1 部分: 进程、线程和共享内存服务 (转)
- 将 Win32 C/C++ 应用程序迁移到 POWER 上的 Linux,第 1 部分: 进程、线程和共享内存服务 (转载)
- linux线程,进程经典文章
- linux进程之fork vs vfork
- linux 线程 进程经典文章
- 从内核实现看Linux进程与线程