Presentation is loading. Please wait.

Presentation is loading. Please wait.

实践演练 广州创龙电子科技有限公司 01 广州创龙电子科技有限公司

Similar presentations


Presentation on theme: "实践演练 广州创龙电子科技有限公司 01 广州创龙电子科技有限公司"— Presentation transcript:

1 实践演练 广州创龙电子科技有限公司 01 广州创龙电子科技有限公司
Guangzhou Tronlong Electronic Technology Co., Ltd 实践演练 01 广州创龙电子科技有限公司

2 01 查看系统中的进程和线程 02 进程的创建 03 线程的创建和同步 02 广州创龙电子科技有限公司

3 查看系统中的进程和线程 学会使用 ps 命令 系统中当前运行的进程 $ ps -eo pid,comm,cmd 父子进程的 ID
$ ps -o pid,ppid,cmd 更多的 ps 参数 $ ps -eo pid,ppid,rtprio,ni,pcpu,cmd Linux 中的进程树 $ pstree 03 广州创龙电子科技有限公司

4 进程的创建 使用 system 调用: ret = system ( "ls -l /" ); 使用 fork:
pid_t child_pid; child_pid = fork () ; if (child_pid != 0) { printf ("this is the parent process, with id %d\n", (int)getpid()); } else { printf ("this is the child process, with id %d\n", (int)getpid()); 04 广州创龙电子科技有限公司

5 进程的创建 使用 fork 以及 exec: pid_t child_pid; child_pid = fork ();
if (child_pid != 0) { return child_pid; } else { execvp (program, arg_list); /* The execvp function returns only if an error occurs. */ abort (); 05 广州创龙电子科技有限公司

6 线程的创建和同步 线程创建 pthread_t thread_id;
pthread_create(&thread_id, NULL, &print_xs, NULL); 传递参数给线程 pthread_create(&thread1_id, NULL, &char_print, &thread1_args); 等待线程完成 pthread_create(&thread2_id, NULL, &char_print, &thread2_args); pthread_join(thread1_id, NULL); pthread_join(thread2_id, NULL); 线程返回结果 线程函数:return (void*)factorial; 主线程: pthread_join(thread, (void*)&factorial); 06 广州创龙电子科技有限公司

7 线程的创建和同步 线程同步 线程函数: pthread_mutex_lock (&mutexsum); sum += fact;
pthread_mutex_unlock (&mutexsum); 主线程: pthread_mutex_init(&mutexsum, NULL); for (i = 1; i <= n; i++) { pthread_create(&callThd[i], NULL, compute_fact, (void *)i); } for(i = 1; i <= n; i++) { pthread_join(callThd[i], NULL); pthread_mutex_destroy(&mutexsum); 07 广州创龙电子科技有限公司

8 广州创龙电子科技有限公司 谢谢 官网: 论坛: 微信公众号:广州创龙


Download ppt "实践演练 广州创龙电子科技有限公司 01 广州创龙电子科技有限公司"

Similar presentations


Ads by Google