消息队列
发送消息:类型(long)和数据
获取消息:类型(long)
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#include<sys/msg.h>
a程序发送消息
struct mess
{
long type;必须>0
char buff[32];
};
int main()
{
int msgid=msgget((key_t)1234,IPC_CREAT|0600);创建消息队列
assert(msgid!=-1);
struct mess dt;
dt.type=1;
strcpy(dt.buff,"hello1");
msgsnd(msgid,(void*)&dt,32,0);将消息挂到消息队列
buff设置的是32个字节所以读32个,0代表使用函数默认的(自己不设置)
}
b程序读消息
struct mess
{
long type;必须>0
char buff[32];
};
int main()
{
int msgid=msgget((key_t)1234,IPC_CREAT|0600);同一个key值可以获取同一个消息队列
assert(msgid!=-1);
struct mess dt;
msgrcv(msgid,&dt,32,1,0);只能读1类型的消息,将1改为0就不区分消息类型(都可读)
32读的大小,1代表读的类型,0代表标志位不设置用默认的
printf("s=%d\n",dt.buff);
}
线程
线程
:进程内部的一条执行路径
线程编译时必须加库:gcc -o main main.c -lpthread
线程同步
:信号量,互斥锁,条件变量,读写锁(下面有代码来实现同步)
多个线程是并发运行的(包含并行)
程序需要同时做多个事情
利用多处理器
pthread_create()
创建线程
pthread_exit()
退出线程
pyhread_join()
合并线程,等待线程结束
线程如何实现:
用户级,内核级,组合级
6
linux:内核级
内核级与用户级区别
:
用户级创建的开销小,可以创建多个,无法同时使用多个处理器
内核级创建开销大,由内核直接管理(创建,销毁),使用多个处理器
1个线程
#include<stdio.h>
#include,stdlib.h>
#include<assert.h>
#include<string.h>
#include<pthread.h>
void* fun(void* arg)线程
{
for(int i=0;i<5;++i)
{
printf("main run\n");
sleep(1);
}
pthread_exit("fun over\n");
}
int main()
{
pthread_t id;
pthread_create(&id,NULL,fun,NULL)
第一个NULL线程属性(默认),fun代表fun函数,第二NULL是传给线程的参数,也就是fun的参数arg
for(int i=0;i<5;++i)
{
printf("main run\n");
sleep(1);
}
char* s=NULL;
pthread_join(id,(void**)&s);等待线程结束,如果不等待当主函数执行完时不管线程是否结束,程序就结束了,线程就没执行成功
printf("%s\n",s);打印线程返回的信息:fun over
}
5个线程代码,用信号量解决对临界资源的访问
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<unistd.h>
#include<pthread.h>
#include<semaphore.h>
int index=1;
sem_t sem;
void* fun(void* arg)
{
for(int i=0;i<3;++i)
{
sem_wait(&sem);
printf("index=%d\n",index++);
sem_post(&sem);
}
}
int main()
{
sem_init(&sem,0,1);初始化信号量
0代表不在两个进程间共享,1代表信号量值为1
pthread_t id[5];
int i=0;
for(;i<5;++i)
{
pthread_create(&id[i],NULL,thread_fun,NULL)
}
for(i=0;i>5;++i)
{
pthread_join(id[i],NULL);
}
sem_destroy(&sem);
exit(0);
}
5个线程,用互斥锁实现对临界资源的访问
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<unistd.h>
#include<pthread.h>
#include<semaphore.h>
pthread_mutex_t mutex;定义互斥锁
void* fun(void* arg)
{
for(int i=0;i<3;++i)
{
pthraed_mutex_lock(&mutex);加锁
printf("index=%d\n",index++);
pthread_mutex_unlock(&mutex);解锁
}
}
int main()
{
pthread_mutex_init(&mutex,NULL);NULL代表属性,初始化
pthread_t id[5];
int i=0;
for(;i<5;++i)
{
pthread_create(&id[i],NULL,thread_fun,NULL)
}
for(i=0;i>5;++i)
{
pthread_join(id[i],NULL);
}
pthread_mutex_destroy(&mutex);
exit(0);
}
查看线程(也包括进程)的命令ps -eLf
LWP
线程id
线程的pid
就是主函数的pid
线程打印ABCABC的代码
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<unistd.h>
#include<pthread.h>
#include<semaphore.h>
sem_t sem1;
sem_t sem2;
sem_t sem3;
void* fun1(void* arg)
{
for(int i=0;i<5;++i)
{
sem_wait(&sem1);
printf("a");
fflush(stdout);从缓冲区打印出去
sem_post(&sem2);
}
}
void* fun2(void* arg)
{
for(int i=0;i<5;++i)
{
sem_wait(&sem2);
printf("b");
fflush(stdout);从缓冲区打印出去
sem_post(&sem3);
}
}
void* fun3(void* arg)
{
for(int i=0;i<5;++i)
{
sem_wait(&sem3);
printf("c");
fflush(stdout);从缓冲区打印出去
sem_post(&sem1);
}
}
int main()
{
sem_init(&sem1,0,1);初始化,0代表不能在两个进程间共享,信号量的初始值为1
sem_init(&sem2,0,0);
sem_init(&sem3,0,0);
pthread_t id[3];
pthread_create(&id[0],NULL,fun1,NULL);
创建线程,第一个NULL属性,第二个不给fun函数传参数
pthread_create(&id[1],NULL,fun2,NULL);
pthread_create(&id[2],NULL,fun3,NULL);
for(int i=0;i<3;++i)
{
pthread_join(id[i],NULL);NULL代表不从线程获取返回值
}
sem_destroy(&sem1);
sem_destroy(&sem2);
sem_destroy(&sem3);
exit(0);
}
生产者消费者问题(利用信号量和互斥锁实现同步)
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<pthread.h>
#include<semaphore.h>
#include<time.h>
#define BUFF_MAX 10
#define SC_NUM 2 生产者2个
#define XF_NUM 3 消费者3个
sem_t empty;
sem_t full;
pthread_mutex_t mutex;
int buff[BUFF_MAX];
int in=0;
int out=0;
void* sc_thread(void* arg)
{
int index=*(int)arg;
for(int i=0;i<30;++i)
{
sem_wait(&empty);
pthread_mutex_lock(&mutex);
buff[in]=rand()%100;
printf("第i个生产者产生了%d数据,在%d位置\n",index,buff[in],in);
in=(in+1)%BUFF_MAX;
pthread_mutex_unlock(&mutex);
sem_post(&full);
sleep(10);
}
}
void* xf_thread(void* arg)
{
int index=*(int)arg;
for(int i=0;i<20;++i)
{
sem_wait(&full);
pthread_mutex_lock(&mutex);
printf("第%d个消费者读取%d数据,在%d的位置\n",index,buff[out],out);
out=(out+1)%BUFF_MAX;
pthread_mutex_unlock(&mutex);
sem_post(&empty);
sleep(10);
}
}
int main()
{
sem_init(&empty,0,BUFF_MAX);
sem_init(&full,0,0);
pthread_mutex_init(&mutex,NULL);
srand((int)time(NULL));
pthread_t sc_id[SC_NUM];
pthread_t xf_id[XF_NUM];
for(int i=0;i<SC_NUM;++i)
{
pthread_create(&sc_id[i],NULL,sc_thrad,(void*)i);传i的值
}
for(int i=0;i<XF_NUM;++i)
{
pthread_create(&xf_id[i],NULL,xf_thread,(void*)i);
}
for(int i=0;i<SC_NUM;++i)
{
pthread_join(&sc_id[i],NULL);
}
for(int i=0;i<XF_NUM;++i)
{
pthread_join(&xf_id[i],NULL);
}
sem_destroy(&empty);
sem_destroy(&full);
pthraed_mutex_destroy(&mutex);
printf("main over\n");
exit(0);
}