消息队列笔记1

消息队列:传送有格式的消息流
多进程网状交叉通信,实现大规模通信
使用内核中的链表(实现机制)
Posix的消息队列和系统V的消息队列区别
消息队列是随内核持续的

线程:
什么是线程
线程是进程的一个实体,它是程序运行的最小单位。
为什么要学习线程
1,线程是进程的一个实体,它是程序运行的最小单位,它比进程消耗更小的资源。
2,能共享地址空间(进程)(堆栈:程序栈)

线程由哪些组成
1,指令指针(指向当前被执行的命令)
2,一个栈(函数栈)
3,寄存器的集合(状态寄存器:一部分正在运行中的处理器的状态)
4,一个私有的数据区。

线程的特点
线程切换的开销很低
线程的通信机制简单

进程与线程的区别总结

7进程更注重网络通信,线程更关注资源保护。
1.进程是资源分配的最小单位,线程是资源调度的最小单位
2.每个进程有独立的地址空间,多个线程共享进程的地址空间,线程间的切换比进程间的切换开销小
3.线程的调度必须通过频繁的加锁来保持线程的同步,影响线程的并发性能
4.进程比线程更加健康,多进程之间相互独立,一个进程的异常对其他进程无影响,而一个线程的崩溃可能影响其他线程或整个程序
5.线程之间的通信方便(小数据量的),同一进程下,线程共享全局变量还有静态变量等数据,而进程之间的通信需要以通信的方式进行(IPC),不过如何处理好同步与互斥,是编写多线程程序的难点
6.多线程的代码结构比多进程的代码结构简单易读

线程并不是操纵系统中内核所提供的而是由线程库来提供libpthread.a/.so
线程有os支持

创建线程
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);

pthread_t *thread指向线程的指针
const pthread_attr_t *attr: 创建线程时的属性:NULL无属性创建
void (start routine) (void):线程运行的实体函数指针
void *arg:线程的参数

注意:线程是进程的一个实体,一旦进程运行结束,线程就会被回收

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>

struct demo
{
    int id;
    char name[1024];
};


void *test(void *arg)
{
    while(1){
    struct demo *ptr=(struct demo *)arg;
    printf("main thread id: %d\nmain_thread_name:%s\n",ptr->id,ptr->name);
    sleep(1);
    }
}

int main()
{
    struct demo demo1 ={2021,"zhangsan"};
    pthread_t id;
    int ret = pthread_create(&id,NULL, test ,(void *)&demo1);
    if(0!=ret)
    {
	perror("pthread_create error");
	exit(0);
    }
    while(1)
    {
    printf("main pthread\n");
    sleep(1);
    }
    pause();
    return 0;
}

线程等待
Int pthread_join(pthread_t_thread,void **retval);
需要等待的线程 返回状态

Eg两个线程读写

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
#include<string.h>
#define MAX_SIZE 1024

char buffer[MAX_SIZE];

void *thread1(void *arg)
{      
    int num=*((int *)arg);
    printf("num = %d\n",num);
    while(1)
    {
    scanf("input:%s\n",buffer);
    sleep(1);
    }
}

void *thread2(void *arg)
{      
    char *ptr=(char *)arg;
    printf("%s\n",ptr);
    while(1)
    {
    printf("buffer=%s\n",buffer);
    sleep(2);
    }
}

int main()
{
    pthread_t id;
    int num=5;
    char *ptr = "hello world";
    if(0 != pthread_create(&id,NULL, thread1 ,(void *)(&num)))
    {
	perror("pthread_create error");
	exit(1);
    }

    pthread_t id1;
    if(0 != pthread_create(&id1, NULL, thread2 ,(void *)ptr))
    {
	perror("pthread_create error");
	exit(1);
    }

    pause();
    return 0;
}

创建两个线程,两个次线程,分别的向同一个文件中写“hello”“world\n”和“hhhhhwwwww\n”

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
#include<string.h>
#include<sys/types.h>
#include<fcntl.h>
#include<sys/stat.h>

#define MAX_SIZE 1024
#define FIFO "./test.txt"

char buffer[MAX_SIZE];

void *thread1(void *arg)
{      
    int fd = *((int *)(arg));
   while(1)
   {
     write(fd,"hello\n",6);
     write(fd,"hello world!\n",13);
   }
}

void *thread2(void *arg)
{     
    Int  fd= *((int  *)(arg));
    while(1)
    {
       write(fd,"hhhhh\n",6);
       write(fd,"ww ww ww\n",7);
    }
}

int main()
{
    int num=5;
    char *ptr="hello";
    int fd = open(FIFO,O_RDWR|O_CREAT|O_APPEND,0644);
    pthread_t id;
    if(fd<0)
    {
	perror("open file error");
	exit(1);
    }

    if(0 != pthread_create(&id,NULL, thread1 ,(void *)(&fd)))
    {
	perror("pthread_create error");
	exit(1);
    }

    pthread_t id1;
    if(0 != pthread_create(&id1, NULL, thread2 ,(void *)(&fd)))
    {
	perror("pthread_create error");
	exit(1);
    }
    pause();
    return 0;
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

躺尸研究员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值