嵌入式学习第二十一天!(线程)

线程:

  1. 基本概念:

      线程:线程是一个轻量级的进程,位于进程空间内部,一个进程中可以创建多个线程

  2. 线程创建:

      线程独占栈空间,文本段、数据段和堆区与进程共享

  3. 线程调度:

      与进程调度是一样的:宏观并行,微观串行

  4. 线程消亡:

      与进程消亡是一样的

  5. 进程和线程的区别:

      进程是操作系统资源分配的最小单元

      线程是CPU任务调度的最小单元

  6. 多进程和多线程的优缺点:

      1. 效率:多线程 优于 多进程

         原因:多线程只需在同一进程空间内切换;多进程需要在不同的空间中切换

      2. 通信:多线程 优于 多进程

         原因:线程共享全局变量,可以通过全局变量实现数据通信;进程空间是独立的,没有共享空间,通信实现比较复杂

      3. 通信实现:多进程 优于 多线程

         原因:线程共享空间操作时会引发资源竞争;进程没有共享空间,不存在资源竞争的问题

      4. 安全:多进程 优于 多线程

         原因:一个进程异常不会影响其余进程空间;一个线程异常结束会导致进程异常结束,进程异常结束,该进程内所有线程任务均无法向下执行

  7. 线程相关的函数接口:

      创建:fork(进程)      pthread_create(线程)

      退出:exit(进程)      pthread_exit(线程)

      回收:wait(进程)      pthread_join(线程)

      1. pthread_create:

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                          void *(*start_routine) (void *), void *arg);

         功能:在该进程中创建一个新的线程

         参数:

            thread:存放线程ID空间首地址
            attr:线程属性空间首地址
            start_routine:线程要执行的函数的入口
            arg:给线程函数的参数

          返回值:

             成功返回0 
             失败返回错误码

      2.pthread_self:

pthread_t pthread_self(void);

         功能:获得调用该函数线程的ID

      练习:

            1. 创建三个线程任务,并打印线程tid

#include "head.h"

void *thread1(void *arg)
{
	printf("start to thread1:%#x\n", (unsigned int)pthread_self());
	return NULL;
}	
void *thread2(void *arg)
{
	printf("start to thread2:%#x\n", (unsigned int)pthread_self());
	return NULL;
}	
void *thread3(void *arg)
{
	printf("start to thread3:%#x\n", (unsigned int)pthread_self());
	return NULL;
}	

int main(void)
{
	pthread_t tid[3];
	int ret = 0;
	int i = 0;
	void *(*p[3])(void *) = {thread1, thread2, thread3};

	for(i = 0; i < 3; i++)
	{
		ret = pthread_create(&tid[i], NULL, p[i], NULL);
		if(ret != 0)
		{
			perror("fail to pthread_create");
			return -1;
		}
	}

	while(1)
	{

	}

	return 0;
}

      3. pthread_exit:

void pthread_exit(void *retval);

         功能:让调用该函数的线程任务结束

         参数:

            retval:线程结束的值

      4. pthread_join:

int pthread_join(pthread_t thread, void **retval);

         功能:回收线程空间

         参数:

            thread:线程的ID号
            retval:存放线程结束状态空间的首地址

         返回值:

            成功返回0 
            失败返回错误码

  作业:

        1. 创建4个线程任务,任务一循环间隔1s打印"采集线程正在执行"
                 任务二循环间隔2s打印"存储线程正在执行"
                 任务三循环间隔5s打印"告警线程正在执行"
                 任务四循环间隔10s打印"日志线程正在执行"

#include "head.h"

void *thread1(void *parg)
{
	while(1)
	{
		sleep(1);
		printf("采集线程正在执行...\n");
	}
	pthread_exit(NULL);
}
void *thread2(void *parg)
{
	while(1)
	{
	sleep(2);
	printf("存储线程正在执行...\n");
	}
	pthread_exit(NULL);
}
void *thread3(void *parg)
{
	while(1)
	{
		sleep(5);
		printf("告警线程正在执行...\n");
	}
	pthread_exit(NULL);
}
void *thread4(void *parg)
{
	while(1)	
	{
		sleep(10);
		printf("日志线程正在执行...\n");
	}
	pthread_exit(NULL);
}
int main(void)
{
	int i = 0;
	pthread_t pid[4];
	void *(*p[4])(void *) = {thread1, thread2, thread3, thread4};

	for(i = 0; i < 4; i++)
	{
		pthread_create(&pid[i], NULL, p[i], NULL);
	}
	for(int i = 0; i < 4; i++)
	{
		pthread_join(pid[i], NULL);
	}

	return 0;
}

        2. 1031 查验身份证 - PAT (Basic Level) Practice (中文) (pintia.cn)

#include <stdio.h>
#include <string.h>

struct people
{
	char number[19];
};

int IntputPeople(struct people *ppnum, int maxlen)
{
	int i = 0;
	int n = 0;
	
	scanf("%d", &n);
	if(n > maxlen)
	{
		perror("over the limit");
		return -1;
	}

	for(i = 0; i < n; i++)
	{
		scanf("%s", ppnum[i].number);
	}	

	return n;

}

int CheckNumber(char *ppnum)
{
	int i = 0;
		
	while(ppnum[i] != '\0' && i < 17)
	{
		if(ppnum[i] == 'X')
		{
			return 0;
		}
		i++;
	}
	return 1;
}

int main(void)
{	
	int i = 0;
	int j = 0;
	int n = 0;
	int sum = 0;
	int ret = 0;
	int checkbit = 0;

	struct people pnum[100];
	int weight[18] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
	char M[12] = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};

	n = IntputPeople(pnum, 100);

	for(i = 0; i < n; i++)
	{
		if(CheckNumber(pnum[i].number))
		{
			sum = 0;
			for(j = 0; j < 17; j++)
			{
				sum += weight[j] * (pnum[i].number[j] - '0');
			}
			checkbit = sum % 11;
			if(pnum[i].number[17] == M[checkbit])
			{
				ret++;
			}
			else
			{
				printf("%s\n", pnum[i].number);
			}
		}
		else
		{
			printf("%s\n", pnum[i].number);
		}
	}
	if(ret == n)
	{
		printf("All passed\n");
	}

	return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值