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

发布于:2025-08-17 ⋅ 阅读:(15) ⋅ 点赞:(0)

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,不回收状态直接传NULL
        printf("线程退出状态:%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;
}


网站公告

今日签到

点亮在社区的每一天
去签到