NEXT 1 进程2

一、回顾

硬盘:

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

☆全局变量:主程序会接着线程之后修改变量

相关推荐
历程里程碑1 分钟前
39. 从零实现UDP服务器实战(带源码) V1版本 - Echo server
服务器·开发语言·网络·c++·网络协议·udp·php
Book思议-2 分钟前
【数据结构实战】:基于C语言单链表实现红旗渠景区年卡信息管理系统
c语言·开发语言·数据结构
Chase_______3 分钟前
【快速入手 Python 基础 | 第1章】:数据存储与运算
开发语言·python
骇客野人4 分钟前
Java springboot里注解大全和使用指南
java·开发语言·spring boot
用户8307196840826 分钟前
Spring Boot 启动报错:OpenFeign 隐性循环依赖,排查了整整一下午
java·spring boot·spring cloud
恼书:-(空寄6 分钟前
事务绑定事件监听器的使用
java
星辰_mya10 分钟前
@SpringBootApplication 与 SPI 机制的终极解密
java·spring boot·spring
add45a11 分钟前
C++与自动驾驶系统
开发语言·c++·算法
xdl259911 分钟前
【异常解决】Unable to start embedded Tomcat Nacos 启动报错
java·tomcat
坚持学习前端日记12 分钟前
python对接comfyui的过程
开发语言·网络·python