1.进程间通信(IPC):管道(把数据写在内存中),信号量(为了进行同步,两个进程之间信息的传递,非数据传送),共享内存(配合信号量使用,控制同步),消息队列(内存中空间存放,接受时候不区分信号类型)
进程:一个正在运行的程序,资源分配的基本单位
线程:进程内部的一条执行路径(序列),调度的基本单位
多线程:多线程程序稳定性并不是非常好,
#include#include #include #include #include void* thread_fun(void* arg) { int i=0; for(;i<5;i++) { printf("main runn"); } } int main() { pthread_t id; int res=pthread_create(&id,NULL,thread_fun,NULL); if(rea!=0) { printf("create thread errn"); exit(0); } int i=0; for(;i<5;i++) { printf("main runn"); } exit(0); }
执行结果:
运行时候要调用lpthread库
退出线程:pthread_exit();
退出进程:exit();
(3)并行和并发
并发是指一个处理器同时处理多个任务,并行是指多个处理器或者是多核处理器同时处理多个不同的任务
线程需要多个处理器,实现:操作系统
线程分类:用户级线程(创建开销小,由线程库管理,可以创建很多,无法使用多处理的资源),内核级线程(由内核创建管理,开销比用户级线程大,可以利用多处理器资源),组合线程
Linux系统上使用的是内核级线程



