一、回顾
硬盘:
code:代码段 二进制格式:ELF

内存mem:
code:只读的.c代码区; data:static定义类似的全局变量; share/map:共享和映射区

二、获取线程id
pthread_t pthread_self(void);
unsigned long int;
%lu
功能:获取当前线程的线程id
参数:无
返回值:成功:返回当前线程的线程id;失败-1;syscall(SYS_get_tid);
cs
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void *th1(void *arg)
{
while(1)
{
printf("发送视频\n");
sleep(1);
}
return NULL;
}
void *th2(void *arg)
{
while (1)
{
printf("接收视频\n");
sleep(1);
}
return NULL;
}
int main(int argc, const char *argv[])
{
pthread_t tid1, tid2;
pthread_create(&tid1, NULL, th1, NULL);
pthread_create(&tid2, NULL, th2, NULL);
while (1)
{
printf("main th,pid:%d,tid:%lu\n", getpid(), pthread_self());
sleep(1);
}
return 0;
}
注意:
1.一次pthread_create执行只能创建一个线程。
2.每个进程至少有一个线程称为主线程。
3.主线程退出则所有创建的子线程都退出,主线程必须有子线程同时运行才算多线程,
程序,线程id是线程的唯一标识,是CPU维护的一组数字。
4.pstree查看系统中多线程的对应关系。
5.多个子线程可以执行同一回调函数。
6.ps-eLf查看线程相关信息LowWeigthProcess
7.ps -eLo pid,ppid,lwp,stat,comm
☆全局变量:主程序会接着线程之后修改变量