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

嵌入式自学第二十九天(5.27)

(1)非阻塞式,代码继续走,能回收就回收,不能回收继续运行程序,但需要判断子程序返回信号,缺点是cpu占有率高。
进程概念比线程大一些,但是都能并发
thread线程:优点:比多进程节省资源,可以共享变量。
新线程创建就会新建栈。
线程是轻量级进程,一般是一个进程中的多个任务。
进程是系统的最小资源分配单位,线程是系统最小执行单位。

线程共享进程资源,除了栈区。进程空间独立
并发程度:线程并发度高一些,线程切换容易,只需要栈区切换。
线程稳定性稍差,
进程可以申请到硬件资源,线程只能用进程的资源。
资源竞争问题

栈区,pc寄存器切换

线程效率高30%,只需要栈区创建回收

三方库:pthread.h   :posix协议:便于移植的
编译代码加载库:-lpthread  library

动态库(共享库)文件命名:lib***.so

线程稳定性差一些。有一个线程栈区崩溃,整个进程都会结束

资源够用,选线程

线程调试更麻烦

线程资源共享ipc,栈区资源不共享,必须独立出来, 

线程的设计框架  posix
创建多线程-》线程空间操作-》线程资源回收

线程是平级的,创建新线程时默认有一个主线程,新创建的是次线程。

(2)创建:int pthread_create(*新线程线程号,属性(NULL默认8m),函数名,*参数)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

 

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <pthread.h>

#include <unistd.h>

void *th1 (void *arg)

{

while(1)

{

printf("发送视频\n");

sleep(1);

}

}

void *th2 (void *arg)

{

while(1)

{

printf("接收控制\n");

sleep(1);

}

}

int main(int argc, char **argv)

{

pthread_t tid1,tid2;

pthread_create(&tid1,NULL,th1,NULL);

pthread_create(&tid2,NULL,th2,NULL);

while(1)sleep(1);

//system("pause");

return 0;

}

编译时:gcc main.c -lpthread

sterror(号码)返回错误信息
errno返回错误号,
perror返回错误信息

l都加上

主线程退出,进程就结束了

ps aux中SL+  这个l表示线程

ps -eLf显示线程

没有0号进程,最早只有1
LWP轻量级进程

主线程号跟进程号一样

伪终端pts/1表示跟哪个终端关联

tty真正终端

tid线程好:用户层表示长一些,linux系统层采用短编号,他们有对应关系。

(3)

pthread_self()获取tid号

pthread_exit(void *retval);子线程自行退出

int pthread_cancel(pthread_t thread)主线程删除子线程。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

 

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <pthread.h>

#include <unistd.h>

void *th1 (void *arg)

{

while(1)

{

printf("发送视频 th1, tid:%ld\n",pthread_self());

sleep(1);

}

}

void *th2 (void *arg)

{

while(1)

{

printf("接收控制th2, tid:%ld\n",pthread_self());

sleep(1);

}

}

int main(int argc, char **argv)

{

pthread_t tid1,tid2;

pthread_create(&tid1,NULL,th1,NULL);

pthread_create(&tid2,NULL,th2,NULL);

while(1)

{

printf("main th, tid:%ld\n",pthread_self());

sleep(1);

}

//system("pause");

return 0;

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

 

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <pthread.h>

#include <unistd.h>

int main(int argc, char **argv)

{

int i = 0 ;

for(i=0;i<400;i++)

{

printf("%d %s\n",i,strerror(i));

}

//system("pause");

return 0;

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

 

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <pthread.h>

#include <unistd.h>

void* th(void*arg)

{

int i =3;

while(i--)

{

printf("th,tid:%ld\n",pthread_self());

sleep(1);

}

pthread_exit(NULL);

//return NULL;

}

int main(int argc, char **argv)

{

pthread_t tid;

pthread_create(&tid,NULL,th,NULL);

while(1)sleep(1);

system("pause");

return 0;

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

 

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <pthread.h>

#include <unistd.h>

void* th(void*arg)

{

int i =3;

while(i)

{

printf("th,tid:%ld\n",pthread_self());

sleep(1);

}

pthread_exit(NULL);

//return NULL;

}

int main(int argc, char **argv)

{

pthread_t tid;

pthread_create(&tid,NULL,th,NULL);

int i = 0 ;

while(1)

{

printf("main th, tid %ld\n",pthread_self());

sleep(1);

i++;

if(3 == i )

{

pthread_cancel(tid);

}

}

//system("pause");

return 0;

}

(5)

线程回收:栈区资源回收:int pthread_join(pthread_t thread,void **retval)
有阻塞,等子进程走完回收它的栈区,接住返回值,

pcb里有tcb线程控制块

地址:栈区变量,全局变量,静态变量。堆区变量

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

 

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <pthread.h>

void * th(void* arg)

{

int i = 5;

while(i--)

{

printf("th tid:%ld\n",pthread_self());

sleep(1);

}

return NULL;

}

int main(int argc, char **argv)

{

pthread_t tid;

pthread_create(&tid,NULL,th,NULL);

//while(1)sleep(1);

pthread_join(tid,NULL);

system("pause");

return 0;

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

 

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <pthread.h>

void * th(void* arg)

{

//static

//char str[]="我要消亡了";

char* p = malloc(20);

strcpy(p,"我要消亡了");

sleep(3);

return p;

}

int main(int argc, char **argv)

{

pthread_t tid;

pthread_create(&tid,NULL,th,NULL);

//while(1)sleep(1);

void* ret;

pthread_join(tid,&ret);

printf("ret %s\n",(char*)ret);// void* ->char*

free(ret);

system("pause");

return 0;

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

 

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <pthread.h>

int a = 0 ;

void* th(void* arg)

{

sleep(1);

a+=10;

return NULL;

}

int main(int argc, char **argv)

{

pthread_t tid;

printf("a is %d\n",a);

pthread_create(&tid,NULL,th,NULL);

pthread_join(tid,NULL);

printf("a is %d\n",a);

system("pause");

return 0;

}

(6)设置分离属性,与主线程分离,目的线程消亡,自动回收空间,不能调用join回收。
int pthread_detach(tid)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

 

#include <pthread.h>

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

void* th(void* arg)

{

pthread_detach(pthread_self()); //栈区系统自动回收

return NULL;

}

int main(int argc, char** argv)

{

int i = 0;

pthread_t tid;

for (i = 0; i < 50000; i++)

{

int ret = pthread_create(&tid, NULL, th, NULL);

if (ret != 0)

{

break;

}

printf("%d\n", i);

}

system("pause");

return 0;

}

(7)注册清理函数:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

 

#include <pthread.h>

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <string.h>

void clean(void *arg)

{

printf("this clean %s\n", (char*)arg);

free(arg);

}

void* th(void* arg)

{

strcpy((char* )arg,"hello world\n");

printf("th,strcpy over\n");

return NULL;

}

int main(int argc, char **argv)

{

pthread_t tid;

char* p = malloc(50);

pthread_cleanup_push(clean,p);

pthread_create(&tid,NULL,th,p);

pthread_join(tid,NULL);

printf("before pop\n");

pthread_cleanup_pop(1);

system("pause");

return 0;

}

(8)多元素传递

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

 

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <pthread.h>

typedef struct

{

char name[50];

int age;

char addr[50];

}PER;

void* th(void* arg)

{

PER* per = (PER*)arg;

printf("th:%s %d %s\n",per->name,per->age,per->addr);

strcat(per->name,"1");

return per;

}

int main(int argc, char **argv)

{

PER per;

bzero(&per,sizeof(per));

printf("pls name:");

fgets(per.name,sizeof(per.name),stdin);//zhagnsan\n

per.name[strlen(per.name)-1]='\0';

char buf[5]={0};

printf("pls age:");

fgets(buf,sizeof(buf),stdin);

per.age = atoi(buf);//12345

printf("pls addr:");

fgets(per.addr,sizeof(per.addr),stdin);

per.addr[strlen(per.addr)-1]='\0';

pthread_t tid;

pthread_create(&tid,NULL,th,&per);// int * -->void*

void* ret=NULL;

pthread_join(tid,&ret);

printf("join ret :%s %d %s\n", ((PER*)ret)->name, ((PER*)ret)->age,((PER*)ret)->addr);

system("pause");

return 0;

}

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

相关文章:

  • 北京大学 | DeepSeek内部研讨资料:AI工具深度测评与选型指南,319页
  • 系统编程day05
  • 基于 STM32 的智慧农业温室控制系统设计与实现
  • 学习python day9
  • DeviceNET转EtherCAT协议转换网关解读
  • Qwen3内置提示词模板解读
  • 数据库大学实验一
  • 投影机三色光源和单色光源实拍对比:一场视觉体验的终极较量
  • 知识图谱系列(4):查询与推理技术
  • 第四十七篇-Tesla P40+Qwen3-30B-A3B部署与测试
  • 什么是PLM软件?离散制造业和流程制造业的主流PLM介绍、国产PLM应用案例
  • 5月27日星期二今日早报简报微语报早读
  • RuoYi前后端分离框架集成Jasypt实现配置信息加密
  • Kubernetes简介及常用命令
  • 高效大电流缓启动电路设计
  • Manus,AGI 要来临了吗?
  • 电子电路:欧姆定律和电流量子化有关系吗?
  • 深入剖析机器学习之波士顿房价案例
  • 易境通海外仓系统:如何提高仓库尾程派送环节效率?
  • 「Python教案」循环语句的使用
  • 离子风机如何保障汽车电子智造组装车间良品率
  • C语言数据存储
  • 操作系统——第四章(文件共享、保护、层级结构、系统布局..)
  • Docker+MobaXterm+x11实现容器UI界面转发本地
  • Python map()函数详解:批量数据处理的瑞士军刀
  • STM32 Keil工程搭建 (手动搭建)流程 2025年5月27日07:42:09
  • STM32之IIC(重点)和OLED屏
  • Spring Boot整合JWT实现认证与授权
  • screen开启和删除session会话
  • JSONP跨域原理全解析