本节重点!!!
正文开始!

CPU看到的所有的task_struct都是一个进程
CPU看到的所有的task_struct都是一个执行流(线程)


进程是调度的基本单位!
线程内核对应的数据结构



#include
#include
#include
#include
using namespace std;
void *callback1(void* args)
{
string name=(char*)args;
while(true)
{
cout<<name<<": "<<::getpid()<<endl;
sleep(1);
}
}
void *callback2(void* args)
{
string name=(char*)args;
while(true)
{
cout<<name<<": "<<::getpid()<<endl;
sleep(1);
}
}
int main()
{
pthread_t tid1;
pthread_t tid2;
pthread_create(&tid1,nullptr,callback1,(char*)"thread 1");
pthread_create(&tid2,nullptr,callback2,(char*)"thread 2");
while(true)
{
cout<<"我是主线程..."<<::getpid()<<endl;
sleep(1);
}
pthread_join(tid1,nullptr);
pthread_join(tid2,nullptr);
return 0;
}
g++ mythread.cc -o mythread -pthread -std=c++11

ps axj|head -1 && ps ajx | grep mythread

查看的时候只能看到mythread进程只有一个!
查看轻量级进程
ps -aL

#include
#include
#include
#include
using namespace std;
int main()
{
thread t([](){
while(true)
{
cout<<"线程运行起来啦!"<<endl;
sleep(1);
}
});
t.join();

我们发现直接编译就报错了,说的是未定义的phread_create!
接下来链接线程库
g++ mythread.cc -o mythread -pthread -std=c++11

我们发现编译就可以通过了!

所以我们可以得出结论C++的底层一定是封装了Linux的库!

Page的内核数据结构

以上的转化工作都是由硬件(MMU)[内存管理单元]完成的!
虚拟地址到物理地址方面的转化采用的是软(页表)硬件(MMU)结构结合的方式!!!
(本小节完!)