在嵌入式Linux中创建四个进程,2个实时进程,2个非实时进程。每个进程中创建两个线程,在线程中调用系统服务来测试任务可中断睡眠、不可中断睡眠、暂停3中状态的切换,用一个 进程来测试进程退出过程。
任务介绍
在嵌入式Linux中创建四个进程,2个实时进程,2个非实时进程。每个进程中创建两个线程,在线程中调用系统服务来测试任务可中断睡眠、不可中断睡眠、暂停3中状态的切换,用一个 进程来测试进程退出过程。
任务要求: 1.说明进程创建与线程创建过程; 2.分别说明你在不同进程状态切换中使用的系统调用是哪种; 3.通过串口输出进程状态切换的打印信息,并截图插入作业报告中。
这篇文档已经过时了,建议转到我主页中的另一篇文章《嵌入式系统原理与应用——基于Linux和ARM笔记整理》,里面有该题更详细的讲解
导入所有库函数
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>
定义进程,创建进程,打印进程的pid,打印进程的父进程
pid_t pid1,pid2,pid3,pid4;
pid1=fork();
printf("pid1:%d ",getpid());
printf("the return value:%d\n",pid1);
pid2=fork();
printf("pid2:%d ",getpid());
printf("the return value:%d\n",pid2);
pid3=fork();
printf("pid3:%d ",getpid());
printf("the return value:%d\n",pid3);
pid4=fork();
printf("pid4:%d ",getpid());
printf("the return value:%d\n",pid4);
修改进程的创建过程,实现创建两个实时进程,两个非实时进程
声明所有线程函数
//declare call thread function
void *fun1(void *);
void *fun2(void *);
void *fun3(void *);
void *fun4(void *);
void *fun5(void *);
void *fun6(void *);
void *fun7(void *);
void *fun8(void *);
每个进程中创建两个线程,即在某一句fork()后面进行输入
pid1=fork();
pthread_t tid1,tid2;
int ret = pthread_create(&tid1,NULL,fun1,NULL);
if(ret != 0){
perror("pthread_create");
return -1;
}
//sleep(1);
ret = pthread_create(&tid2,NULL,fun2,(void *)(long)a);
if(ret != 0){
perror("pthread_create");
return -1;
}
定义所有线程函数,先赋予每个线程打印自己tid号的功能
void *fun1(void *arg)
{
printf("pthread_tid1 = %lu\n",(unsigned long)pthread_self());
}
void *fun2(void *arg)
{
printf("pthread_tid2 = %lu\n",(unsigned long)pthread_self());
}
void *fun3(void *arg)
{
printf("pthread_tid3 = %lu\n",(unsigned long)pthread_self());
}
void *fun4(void *arg)
{
printf("pthread_tid4 = %lu\n",(unsigned long)pthread_self());
}
void *fun5(void *arg)
{
printf("pthread_tid5 = %lu\n",(unsigned long)pthread_self());
}
void *fun6(void *arg)
{
printf("pthread_tid6 = %lu\n",(unsigned long)pthread_self());
}
void *fun7(void *arg)
{
printf("pthread_tid7 = %lu\n",(unsigned long)pthread_self());
}
void *fun8(void *arg)
{
printf("pthread_tid8 = %lu\n",(unsigned long)pthread_self());
}
整合上半段任务的代码
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>
//declare call thread function
void *fun1(void *);
void *fun2(void *);
void *fun3(void *);
void *fun4(void *);
void *fun5(void *);
void *fun6(void *);
void *fun7(void *);
void *fun8(void *);
int main(){
pid_t pid1,pid2,pid3,pid4;
int ret;
pid1=fork();
printf("pid1:%d ",getpid());
printf("the return value:%d\n",pid1);
pthread_t tid1,tid2;
ret = pthread_create(&tid1,NULL,fun1,NULL);
ret = pthread_create(&tid2,NULL,fun2,NULL);
pid2=fork();
printf("pid2:%d ",getpid());
printf("the return value:%d\n",pid2);
pthread_t tid3,tid4;
ret = pthread_create(&tid3,NULL,fun3,NULL);
ret = pthread_create(&tid4,NULL,fun4,NULL);
pid3=fork();
printf("pid3:%d ",getpid());
printf("the return value:%d\n",pid3);
pthread_t tid5,tid6;
ret = pthread_create(&tid5,NULL,fun5,NULL);
ret = pthread_create(&tid6,NULL,fun6,NULL);
pid4=fork();
printf("pid4:%d ",getpid());
printf("the return value:%d\n",pid4);
pthread_t tid7,tid8;
ret = pthread_create(&tid7,NULL,fun7,NULL);
ret = pthread_create(&tid8,NULL,fun8,NULL);
while(1){}
return 0;
}
void *fun1(void *arg)
{
printf("pthread_tid1 = %lu\n",(unsigned long)pthread_self());
pthread_exit(NULL);
}
void *fun2(void *arg)
{
printf("pthread_tid2 = %lu\n",(unsigned long)pthread_self());
pthread_exit(NULL);
}
void *fun3(void *arg)
{
printf("pthread_tid3 = %lu\n",(unsigned long)pthread_self());
pthread_exit(NULL);
}
void *fun4(void *arg)
{
printf("pthread_tid4 = %lu\n",(unsigned long)pthread_self());
pthread_exit(NULL);
}
void *fun5(void *arg)
{
printf("pthread_tid5 = %lu\n",(unsigned long)pthread_self());
pthread_exit(NULL);
}
void *fun6(void *arg)
{
printf("pthread_tid6 = %lu\n",(unsigned long)pthread_self());
pthread_exit(NULL);
}
void *fun7(void *arg)
{
printf("pthread_tid7 = %lu\n",(unsigned long)pthread_self());
pthread_exit(NULL);
}
void *fun8(void *arg)
{
printf("pthread_tid8 = %lu\n",(unsigned long)pthread_self());
pthread_exit(NULL);s
}
在终端的运行结果

修改函数内部,实现在线程中调用系统服务来测试任务可中断睡眠、不可中断睡眠、暂停3中状态的切换
-
"任务可中断睡眠"指进程进入睡眠状态,可以被外部中断唤醒。在 Linux 系统中,可以使用
sleep或usleep系统调用来实现可中断睡眠。例如,sleep(5)表示进程睡眠 5 秒钟。如果在睡眠期间接收到信号,进程会被唤醒。 -
"任务不可中断睡眠"指进程进入睡眠状态,无法被外部中断唤醒。在 Linux 系统中,可以使用
pause系统调用来实现不可中断睡眠。进程会一直睡眠,直到接收到一个信号。通常,用于等待信号的时候会使用pause函数。 -
"任务暂停"指进程的执行被暂停,直到接收到某个特定的信号。在 Linux 系统中,可以使用
pause或sigsuspend系统调用来实现任务暂停。调用这些函数后,进程将一直等待,直到接收到一个指定的信号才会继续执行。
void *fun1(void *arg)
{
printf("pthread_tid1 = %lu\n",(unsigned long)pthread_self());
while(1)
{
// 任务可中断睡眠
sleep(1); //休眠一秒
printf("fun1: Task is awake\n");
}
}
void *fun2(void *arg)
{
printf("pthread_tid2 = %lu\n",(unsigned long)pthread_self());
while(1)
{
// 任务不可中断睡眠
pause();
printf("fun2: Task is awake\n");
}
}
休眠1秒时,tid1比tid2先创建

休眠5秒时,tid2比tid1先创建

用一个进程来测试进程退出过程。首先用命令ps -ef找到要杀死的线程,然后再kill。可以在进程中看到“Terminated”,说明成功退出


openvela 操作系统专为 AIoT 领域量身定制,以轻量化、标准兼容、安全性和高度可扩展性为核心特点。openvela 以其卓越的技术优势,已成为众多物联网设备和 AI 硬件的技术首选,涵盖了智能手表、运动手环、智能音箱、耳机、智能家居设备以及机器人等多个领域。
更多推荐


所有评论(0)