Linux软件编程(Day6)

1.waitpid 
    pid_t waitpid(pid_t pid, int *wstatus, int options);
    功能:
        回收指定的子进程空间
    参数:
        pid:要回收的子进程的pid
        wstatus:回收状态的指针
        options:回收选项
            0 阻塞回收
            WNOHANG:非阻塞回收
    返回值:
        成功:返回回收的子进程的pid
        失败:返回-1

2.exec函数族:
    extern char **environ;
    int execl(const char *path, const char *arg, .../* (char  *) NULL */);
    int execlp(const char *file, const char *arg, .../* (char  *) NULL */);
    int execle(const char *path, const char *arg, .../*, (char *) NULL, char * const envp[] */);
    int execv(const char *path, char *const argv[]);
    int execvp(const char *file, char *const argv[]);
    int execvpe(const char *file, char *const argv[], char *const envp[]);
    功能:
        利用当前的进程空间执行另外一份代码

    l:参数以列表形式传递
    v:参数以指针数组形式传递
    p:在系统目录下查找文件 
    e:传递环境变量

    getenv
    char *getenv(const char *name);
    功能:
        根据环境变量的名字获得环境变量对应的值

    int setenv(const char *name, const char *value, int overwrite);
    功能:
        设置环境变量的值

3.system函数原型:
    int system(const char *command);
    功能:
        在代码中执行一条shell命令

1.线程基本概念:
    线程是一个轻量级的进程
    1.线程的创建
        1.线程必须位于进程空间内部
        2.线程独享栈区,剩余的文本段、数据段、堆区与进程共享

    2.线程的调度
        和进程调度完全相同
        宏观并行,微观串行

    3.线程的消亡
        线程代码执行结束,空间不回收会成为僵尸线程,需要回收线程空间

2.进程和线程的区别:
    1.进程是操作系统资源分配的最小单元 
    2.线程是CPU任务调度的最小单元 

3.多进程和多线程的优缺点:
    1.执行效率:
        多线程 > 多进程 
        多进程需要在不同的进程空间内部切换调度任务
        多线程只需要在同一进程空间内部切换调度任务

    2.安全性: 
        多进程 > 多线程 
        多进程一个进程任务异常结束不会影响其余任务 
        多线程一个线程任务异常结束可能导致进程异常结束,会导致进程中其余线程也随进程一起结束

    3.通信效率: 
        多线程 > 多进程 
        多线程全局变量共享,通信直接使用全局变量即可 
        多进程没有共享空间,通信需要使用其余的进程间通信方式完(管道、套接字、信号等)成
    
    4.编程复杂性: 
        多进程 > 多线程 
        多线程全局变量共享,通信简单但需要考虑资源竞争问题,需要引入互斥锁防止资源竞争
        多进程不用考虑资源竞争问题

    同一软件下的多任务考虑用多线程
    不同软件下的多任务考虑用多进程
    进程和线程实际效率差不多

4.线程相关的函数接口:
    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:线程属性 (默认属性 NULL)
        start_routine:线程任务函数
        arg:对线程函数的传参
      返回值:
        成功返回0 
        失败返回错误码

函数指针为 void * threadfun(void *arg)

多个线程任务初始化可以指向同一个线程,比如QQ的使用。

#include "../head.h"

void *threadfun(void *arg)
{
    printf("线程(TID:%#lx)开始执行\n", (unsigned long)pthread_self());

    return NULL;
}

int main(void)
{
    pthread_t tid1;
    pthread_t tid2;
    pthread_t tid3;

    pthread_create(&tid1, NULL, threadfun, NULL);
    pthread_create(&tid2, NULL, threadfun, NULL);
    pthread_create(&tid3, NULL, threadfun, NULL);

    printf("线程(TID:%#lx  TID:%#lx  TID:%#lx)创建成功\n", (unsigned long)tid1, (unsigned long)tid2, (unsigned long)tid3);

    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);
    pthread_join(tid3, NULL);

    return 0;
}

链接 gcc filename.c -lpthread 

 2.pthread_self 
      pthread_t pthread_self(void);
      功能:
        获得当前线程的ID号
      参数:
        缺省
      返回值:
        成功返回线程的ID号 

线程号为 unsigned long型,输出表示为(unsigned long)tid

.pthread_exit 
      void pthread_exit(void *retval);
      功能:
        退出线程任务
      参数:
        retval:线程结束的状态
      返回值:
        缺省

4.pthread_join 
      int pthread_join(pthread_t thread, void **retval);
      功能:
        回收线程任务
      参数:
        thread:要回收的线程ID号
        retval:存放线程结束状态的值的空间首地址
      返回值:
        成功返回0 
        失败返回错误码 

注意:v
        pthread_join具有阻塞功能,线程不结束,会阻塞等到直到线程结束回收线程空间
        pthread_join具有同步功能 

作业1:
    1.创建3个线程,线程负责打印线程 线程(自己的TID)开始执行
      主进程打印三个线程的TID  
        线程(线程TID)创建成功

实现方法一  最简单的方法实现

#include "../head.h"

void *thread1fun(void *arg)
{
    printf("线程1(TID:%#lx)开始执行\n", (unsigned long)pthread_self());

    return NULL;
}

void *thread2fun(void *arg)
{
    printf("线程2(TID:%#lx)开始执行\n", (unsigned long)pthread_self());
    
    return NULL;
}

void *thread3fun(void *arg)
{
    printf("线程3(TID:%#lx)开始执行\n", (unsigned long)pthread_self());

    return NULL;
}

int main(void)
{
    pthread_t tid1;
    pthread_t tid2;
    pthread_t tid3;

    pthread_create(&tid1, NULL, thread1fun, NULL);
    pthread_create(&tid2, NULL, thread2fun, NULL);
    pthread_create(&tid3, NULL, thread3fun, NULL);

    printf("线程(TID:%#lx   TID:%#lx   TID:%#lx)创建成功\n", (unsigned long)tid1, (unsigned long)tid2, (unsigned long)tid3);

    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);
    pthread_join(tid3, NULL);

    return 0;
}

实现方法2 更改为一个线程 

#include "../head.h"

void *threadfun(void *arg)
{
    printf("线程(TID:%#lx)开始执行\n", (unsigned long)pthread_self());

    return NULL;
}

int main(void)
{
    pthread_t tid1;
    pthread_t tid2;
    pthread_t tid3;

    pthread_create(&tid1, NULL, threadfun, NULL);
    pthread_create(&tid2, NULL, threadfun, NULL);
    pthread_create(&tid3, NULL, threadfun, NULL);

    printf("线程(TID:%#lx  TID:%#lx  TID:%#lx)创建成功\n", (unsigned long)tid1, (unsigned long)tid2, (unsigned long)tid3);

    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);
    pthread_join(tid3, NULL);

    return 0;
}

实现方法3 利用循环实现

#include "../head.h"

void *threadfun(void *arg)
{
    int cnt = (int)arg;

    printf("线程%d (TID:%#lx)开始执行\n", cnt, (unsigned long)pthread_self());

    return NULL;
}

int main(void)
{
    pthread_t tid[3];
    int i = 0;

    for (i = 0; i < 3; i++)
    {
        pthread_create(&tid[i], NULL, threadfun, (void *)i);
    }
    
    printf("线程(TID:%#lx  TID:%#lx  TID:%#lx)创建成功\n", (unsigned long)tid[0], (unsigned long)tid[1], (unsigned long)tid[2]);

    for (i = 0; i < 3; i++)
    {
        pthread_join(tid[i], NULL);
    }   

    return 0;
}

实现方法4 利用函数指针数组实现

#include "../head.h"

void *thread1fun(void *arg)
{
    printf("线程1(TID:%#lx)开始执行\n", (unsigned long)pthread_self());

    return NULL;
}

void *thread2fun(void *arg)
{
    printf("线程2(TID:%#lx)开始执行\n", (unsigned long)pthread_self());
    
    return NULL;
}

void *thread3fun(void *arg)
{
    printf("线程3(TID:%#lx)开始执行\n", (unsigned long)pthread_self());

    return NULL;
}

int main(void)
{
    pthread_t tid[3];
    int i = 0;
    void *(*pfun[3])(void *) = {thread1fun, thread2fun, thread3fun};
    pthread_attr_t attr;

    pthread_attr_init(&attr);

    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    for (i = 0; i < 3; i++)
    {
        pthread_create(&tid[i], &attr, pfun[i], NULL);
    }
    
    printf("线程(TID:%#lx   TID:%#lx   TID:%#lx)创建成功\n", (unsigned long)tid[0], (unsigned long)tid[1], (unsigned long)tid[2]);

    while (1)
    {
        
    }
#if 0
    for (i = 0; i < 3; i++)
    {
        pthread_join(tid[i], NULL);
    }
#endif

    pthread_attr_destroy(&attr);

    return 0;
}

 2.使用fork+exec实现minishell 



#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
#include <time.h>
#include <pthread.h>



void ShowCommand(void)
{
    char tmpbuff[4096] = {0};
    char *ptmp = NULL;

    // /
    // /home/linux/Desktop/dirname
    getcwd(tmpbuff, sizeof(tmpbuff));
    ptmp = tmpbuff + strlen(tmpbuff);
    while (*ptmp != '/')
    {
        ptmp--;
    }

    if (strcmp(tmpbuff, "/") != 0)
    {
        ptmp++;
    }
    printf("[linux@ubuntu:%s]", ptmp);

    return;
}

int GetCommand(char *pcommand, int size)
{
    //ls -l a.txt\n\0
    fgets(pcommand, size, stdin);
    pcommand[strlen(pcommand)-1] = '\0';

    return 0;
}

int SplitCommand(char *pcommand, char **pparg, int maxlen)
{
    int cnt = 0;

    pparg[cnt] = strtok(pcommand, " ");
    if (NULL == pparg[cnt])
    {
        return cnt;
    }
    cnt++;

    while (cnt < maxlen)
    {
        pparg[cnt] = strtok(NULL, " ");
        if (NULL == pparg[cnt])
        {
            break;
        }
        cnt++;
    }
    
    return cnt;
}

int ExecCommand(char **pparg, int cnt)
{
    pid_t pid;

    if (0 == strcmp(pparg[0], "cd"))
    {
        chdir(pparg[1]);
    }
    else if (0 == strcmp(pparg[0], "exit"))
    {
        exit(0);
    }
    else 
    {
        pid = fork();
        if (-1 == pid)
        {
            return -1;
        }
        if (0 == pid)
        {
            execvp(pparg[0], pparg);
        }
        else if (pid > 0)
        {
            wait(NULL);
        }
    }  

    return 0;
}

int main(void)
{
    char command[4096] = {0};
    char *parg[10] = {NULL};
    int cnt = 0;

    while (1)
    {
        ShowCommand();
        GetCommand(command, 4096);
        cnt = SplitCommand(command, parg, 10);
        ExecCommand(parg, cnt);
    } 

    return 0;
}

  • 12
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值