当前位置: 首页 > news >正文

【学习嵌入式day-25-线程】

exec函数族

exec函数族

        利用进程空间执行另一份代码

#include "../head.h"int main(void)
{char *parg[5] = {"./hello","how","are","you",NULL,};printf("execl-up\n");//execl("./hello", "./hello", "how", "are", "you", NULL);execv(parg[0], parg);printf("execl-error\n");printf("execl-down\n");return 0;}

主函数传参

主函数形式

#include "../head.h"int main(int argc, const char **argv)
{int i = 0;printf("hello world\n");printf("==========\n");for(i = 0; i < argc; i++){printf("%s\n", argv[i]);}//for(i = 0; argv[i] != NULL; i++){printf("%s\n", argv[i]);}return 0;
}

system

#include "../head.h"void mysystem(void)
{pid_t pid;pid = fork();if(-1 == pid){perror("fail to fork");return;}if(0 == pid){execlp("ls", "ls", "-l", NULL);}wait(NULL); //父子进程同步return;
}int main(void)
{printf("systen-up\n");mysystem();printf("system-down\n");return 0;
}

线程

概念

  • 线程是一个轻量级的进程
    • 线程本质就是一个进程
    • 线程和进程不完全一致,轻量指的是内存空间,进程空间和线程空间管理方法不同

进程和线程区别

  • 线程本质是进程,线程是任务创建、调度、回收的过程
  • 进程空间:文本段、数据段、系统数据段共同构成
  • 线程空间:
    • 线程必须位于进程内部,没有进程,线程无法独立存在
    • 一个进程中的所有线程共享文本段+数据段+堆区,独享栈区
    • 线程独享的栈区默认8M
    • 一个进程中的多个线程切换调度任务时,资源开销比较小

  • 区别总结:

    • 线程是CPU任务调度的最小单元,和进程一样都是独立执行的任务
    • 进程是操作系统资源分配的最小单元,线程无法独立存在,不是一个独立的空间,只是任务的独立。

多进程和多线程的优缺点

多线程和多进程对比

线程的调度

  • 与进程调度保持一致
  • 宏观并行,微观串行

线程的消亡

  • 线程结束需要回收线程空间,否则产生僵尸线程

线程的函数接口

函数接口

pthread_create(创建线程)

pthread_self(获得线程的ID号)

pthread_exit(结束线程任务)

pthread_join(回收线程空间)

#include "../head.h"void *thread1(void *arg)
{printf("线程1(TID:%#lx)开始执行\n", pthread_self());pthread_exit("线程1退出");return NULL;
}
void *thread2(void *arg)
{printf("线程2(TID:%#lx)开始执行\n", pthread_self());pthread_exit("线程2退出");return NULL;
}
void *thread3(void *arg)
{printf("线程3(TID:%#lx)开始执行\n", pthread_self());pthread_exit("线程3退出");return NULL;
}int main(void)
{pthread_t tid[3];int i = 0;void *pret = NULL;void *(*p[3])(void *) = {thread1, thread2, thread3};    //函数指针数组for(i = 0; i < 3; i++){pthread_create(&tid[i], NULL, p[i], NULL);}for(i = 0; i < 3; i++){pthread_join(tid[i], &pret);    //回收状态传&pret,不回收状态直接传NULLprintf("线程退出状态:%s\n", (char *)pret);}/*int ret = 0;pthread_t tid1;pthread_t tid2;pthread_t tid3;*//*ret = pthread_create(&tid1, NULL, thread1, NULL);if(ret != 0){perror("fail to pthread_create");return -1;}printf("线程1(TID:%#lx)创建成功\n", tid1);ret = pthread_create(&tid2, NULL, thread2, NULL);if(ret != 0){perror("fail to pthread_create\n");return -1;}printf("线程2(TID:%#lx)创建成功\n", tid2);ret = pthread_create(&tid3, NULL, thread3, NULL);if(ret != 0){perror("fail to pthread_create\n");return -1;}printf("线程3(TID:%#lx)创建成功\n", tid3);pthread_join(tid1, NULL);pthread_join(tid2, NULL);pthread_join(tid3, NULL);*/return 0;
}

http://www.xdnf.cn/news/1301779.html

相关文章:

  • 扣子(Coze),开源了!Dify 天塌了
  • 无人机智能跟踪模块设计与运行分析
  • Mac Mysql 卸载
  • 【Docker】openEuler 使用docker-compose部署gitlab-ce
  • C++设计模式:类间关系
  • 企业级时序数据库选型指南:从传统架构向智能时序数据管理的转型之路
  • Flinksql bug: Heartbeat of TaskManager with id container_XXX timed out.
  • gitee_流水线搭配 Dockerfile 部署vue项目
  • MetaFox官方版:轻松转换视频,畅享MKV格式的便捷与高效
  • 【Linux基础知识系列】第九十六篇 - 使用history命令管理命令历史
  • std::set_symmetric_difference
  • 4. 图像识别模型与训练策略
  • 解锁AI大模型:Prompt工程全面解析
  • Spring MVC ModelAndView 详解
  • Linux网络基础(一)
  • 【计算机视觉与深度学习实战】01基于直方图优化的图像去雾技术
  • Python入门第3课:Python中的条件判断与循环语句
  • 电商架构测试体系:ZKmall开源商城筑牢高并发场景下的系统防线
  • Dijkstra与Floyd求最短路算法简介
  • 【JAVA高级】实现word转pdf 实现,源码概述。深坑总结
  • Vue3 学习教程,从入门到精通,Axios 在 Vue 3 中的使用指南(37)
  • 在Ubuntu 22.04上安装远程桌面服务
  • 关于C++的#include的超超超详细讲解
  • 为什么 /deep/ 现在不推荐使用?
  • 稳定且高效:GSPO如何革新大型语言模型的强化学习训练?
  • Webpack详解
  • 思考:高速场景的行星轮混动效率如何理解
  • 解决Electron透明窗口点击不影响其他应用
  • 启动electron桌面项目控制台输出中文时乱码解决
  • 下载及交叉编译zlib库,记录