linux下多线程学习4_打印线程id和进程id
2012-03-16 17:19
549 查看
原文地址::http://hi.baidu.com/neu_stroller/blog/item/4213d8c52ddf0aa18226accb.html
例程8
程序目的:实现在新建立的线程中打印该线程的id和进程id
程序名称:pthread_id.c
#include <stdio.h>
#include <pthread.h>
#include <unistd.h> /*getpid()*/
void *create(void *arg)
{
printf("New thread .... \n");
printf("This thread's id is %u \n", (unsigned int)pthread_self());
printf("The process pid is %d \n",getpid());
return (void *)0;
}
int main(int argc,char *argv[])
{
pthread_t tid;
int error;
printf("Main thread is starting ... \n");
error = pthread_create(&tid, NULL, create, NULL);
if(error)
{
printf("thread is not created ... \n");
return -1;
}
printf("The main process's pid is %d \n",getpid());
sleep(1);
return 0;
}
编译方法:
gcc -Wall -lpthread pthread_id.c
执行结果:
Main thread is starting ...
The main process's pid is 3307
New thread ....
This thread's id is 3086347152
The process pid is 3307
例程8
程序目的:实现在新建立的线程中打印该线程的id和进程id
程序名称:pthread_id.c
#include <stdio.h>
#include <pthread.h>
#include <unistd.h> /*getpid()*/
void *create(void *arg)
{
printf("New thread .... \n");
printf("This thread's id is %u \n", (unsigned int)pthread_self());
printf("The process pid is %d \n",getpid());
return (void *)0;
}
int main(int argc,char *argv[])
{
pthread_t tid;
int error;
printf("Main thread is starting ... \n");
error = pthread_create(&tid, NULL, create, NULL);
if(error)
{
printf("thread is not created ... \n");
return -1;
}
printf("The main process's pid is %d \n",getpid());
sleep(1);
return 0;
}
编译方法:
gcc -Wall -lpthread pthread_id.c
执行结果:
Main thread is starting ...
The main process's pid is 3307
New thread ....
This thread's id is 3086347152
The process pid is 3307
相关文章推荐
- Linux进程线程学习笔记:进程创建
- Unix/linux进程及线程间同步技术总结【学习总结,请勿吐槽。。。】
- 有三个线程ID分别是ABC,用多线程编程在屏幕上循环打印ABCABC....十次
- Linux进程与线程学习随笔
- Linux-线程和进程学习
- 多线程 学习笔记<4> 进程与线程的区别联系
- Linux进程与线程学习笔记(更新)
- Linux进程线程学习笔记:进程间通信 之 管道
- Linux进程线程学习笔记:进程创建
- Linux进程线程学习笔记:进程创建
- Linux 多线程环境下 进程线程终止函数小结
- Linux下多线程编程__线程的创建pthread_create与退出pthread_exit,等待当前线程退出pthread_join,获取线程ID pthread_self
- linux多线程学习(五)——信号量线程控制
- Linux 线程与进程的学习
- linux查看某个进程的线程id(spid)
- 黑马程序员_学习日记65_713ASP.NET(委托、托管、扩展方法、泛型委托、进程_应用程序域_线程、多线程、异步委托)
- Linux-----线程、进程、多线程、内核线程
- Unix/linux进程及线程间同步技术总结【学习总结,请勿吐槽。。。】
- Java多线程与并发学习之(一):进程与线程
- 【学习总结】【多线程】 线程 & 进程 & NSThread(多线程的一套API)