linux下显示所有线程信息
来源:互联网 发布:gbdt python实现 编辑:程序博客网 时间:2024/05/21 09:27
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *print_message_function( void *ptr );
main()
{
pthread_t thread1, thread2;
char *message1 = "Thread 1";
char *message2 = "Thread 2";
int iret1, iret2;
/* Create independent threads each of which will execute function */
iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);
/* Wait till threads are complete before main continues. Unless we */
/* wait we run the risk of executing an exit which will terminate */
/* the process and all threads before the threads have completed. */
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
printf("Thread 1 returns: %d\n",iret1);
printf("Thread 2 returns: %d\n",iret2);
exit(0);
}
void *print_message_function( void *ptr )
{
char *message;
int i;
for(i = 0; i < 100000; i++)
{
message = (char *) ptr;
usleep(1000);
printf("%s \t\t %d\n", message, i);
}
}
编译和运行程序
2073 gcc -lpthread muti-thread.c
2074 ./a.out
在另外一个putty终端上运行
root@9131-TestMAC-Compile:/home/tjiang/code/join# ps -eLF | grep a.out
root 6162 5743 6162 0 3 4567 532 0 14:46 pts/4 00:00:00 ./a.outroot 6162 5743 6163 0 3 4567 532 1 14:46 pts/4 00:00:00 ./a.out
root 6162 5743 6164 0 3 4567 532 1 14:46 pts/4 00:00:00 ./a.out
root 6172 6103 6172 0 1 836 844 1 14:47 pts/0 00:00:00 grep --color=auto a.out
其实在Linux中,新建的线程并不是在原先的进程中,而是系统通过一个系统调用clone()。该系统copy了一个和原先进程完全一样的进程,并在这个进程中执行线程函数。不过这个copy过程和fork不一样。 copy后的进程和原先的进程共享了所有的变量,运行环境。这样,原先进程中的变量变动在copy后的进程中便能体现出来。
- linux下显示所有线程信息
- linux显示线程信息
- Linux 下查看线程信息
- LINUX下获取所有进程信息
- Linux下获取所有可用网卡信息
- Linux下获取所有可用网卡信息
- Linux下获取所有可用网卡信息
- linux下查看所有用户及所有用户组的信息
- linux下显示所有符号的命令
- toad 显示某一用户下的所有表结构信息
- Linux下grep显示多行信息
- linux 下 madplay显示时间信息
- Linux下grep显示多行信息
- linux下面显示所有正在运行的线程
- linux下面显示所有正在运行的线程
- 打印所有线程BT信息
- Linux系统下所有用户的操作信息
- MYSQL 显示所有列的所有信息
- Session存储在数据中
- 一个布线工程师谈PCB设计的经验
- HOJ 1401 Gigantic Sums 趣题
- OUTPUT新增记录入库示例C#+存储过程
- 迭代器for写法
- linux下显示所有线程信息
- ExtJS.form中msgTarget
- codebloks 下如何链接-lpthread
- Android Fragment的使用
- oracle函数和存储过程有什么区别
- poj1160 动态规划
- C++语言中变量的储存类别
- POWERPCB常用技巧
- 设置UIButton字体的大小