IO进程----线程

什么是线程

概念

线程是一个轻量级的进程,为了提高系统的性能引入线程。

线程和进程是参与统一的调度。

在同一个进程中可以创建的多个线程, 共享进程资源。

(Linux里同样用task_struct来描述一个线程)

进程和线程的区别

相同点:

都为系统提供了并发执行的能力

不同点:

调度和资源:线程是系统调度的最小单位;进程是资源分配的最小单位。

地址空间方面:同一个进程创建的多个线程共享该进程的资源;进程的地址空间相互独立。

通信方面:线程通信相对简单,只需要通过全局变量可以实现,但是需要考虑临界资源访问的问题;进程通信比较复杂,需要借助进程间的通信机制(借助3g-4g内核空间)

安全性方面:线程安全性差一些,当进程结束时会导致所有线程退出;进程相对安全。

面试题:程序什么时候该使用线程?什么时候用进程?(深圳棱镜空间智能科技有限公司)(北京明朝万达)

1. 对资源的管理和保护要求高,不限制开销和效率时,使用多进程。

2. 要求效率高、速度快的高并发环境时,需要频繁创建、销毁或切换时,资源的保护管理要求不是很高时,使用多线程。

线程资源

共享的资源:可执行的指令、静态数据、进程中打开的文件描述符、信号处理函数、当前工作目录、用户ID、用户组ID

私有的资源:线程ID (TID)、PC(程序计数器)和相关寄存器、堆栈(局部变量, 返回地址)、错误号 (errno)、信号掩码和优先级、执行状态和属性

函数接口

创建线程:pthread_create

int pthread_create(pthread_t *thread, const pthread_attr_t *attr, 
                                void *(*start_routine) (void *), void *arg);
功能:创建线程
参数:      thread:线程标识
            attr:线程属性, NULL:代表设置默认属性
            start_routine:函数名:代表线程函数(自己写的)
            arg:用来给前面函数传参
返回值:成功:0
      失败:错误码
      
编译的时候需要加 -pthread 链接动态库

函数指针格式: 数据类型 (* 指针名)(参数列表);

#include <stdio.h>
#include <stdlib.h>
int test(int (*p)(int, int), int a, int b) //p=fun, a=3, b=4
{
    return p(a,b); //fun(3,4);
}
int fun(int n, int m)  //n=3, m=4
{
    return n * m;  //3*4=12
}

int main(int argc, char const *argv[])
{
      printf("%d\n", test(fun, 3, 4));  //12
      return 0;
}

例子:

#include <stdio.h>
#include <pthread.h>

void *handler_thread(void *arg)
{
    printf("in handler_thread\n");
    while (1)
        ; //不让线程退出
    return NULL;
}

int main(int argc, char const *argv[])
{
    pthread_t tid;
    if (pthread_create(&tid, NULL, handler_thread, NULL) != 0)  //创建线程
    {
        perror("phtread err");
        return -1;
    }
    printf("in main\n");
    while(1);  //让主线程不要结束
    return 0;
}

退出线程:pthread_exit

void  pthread_exit(void *value_ptr) 
功能:用于退出线程的执行
参数:value_ptr:线程退出时返回的值
#include <stdio.h>
#include <pthread.h>

void *handler_thread(void *arg)
{
    printf("in handler_thread\n");
    pthread_exit(NULL);   //退出当前线程
    while (1)
        ; //不让线程退出
    return NULL;
}

int main(int argc, char const *argv[])
{
    pthread_t tid;
    if (pthread_create(&tid, NULL, handler_thread, NULL) != 0) //创建线程
    {
        perror("phtread err");
        return -1;
    }
    printf("in main\n");
    while (1)
        ; //让主线程不要结束
    return 0;
}

回收线程资源

int  pthread_join(pthread_t thread,  void **value_ptr) 
功能:用于等待一个指定的线程结束,阻塞函数
参数:thread:创建的线程对象,线程ID
     value_ptr:指针*value_ptr 用于指向线程返回的参数, 一般为NULL
返回值:成功 : 0
       失败:errno

int pthread_detach(pthread_t thread);
功能:让线程结束时自动回收线程资源,让线程和主线程分离,非阻塞函数
参数:thread:线程ID
非阻塞式的,例如主线程分离(detach)了线程T2,
那么主线程不会阻塞在pthread_detach(),pthread_detach()会直接返回,
线程T2终止后会被操作系统自动回收资源
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

void *handler_thread(void *arg)
{
    printf("in handler_thread\n");
    sleep(2);
    pthread_exit(NULL); //退出当前线程
    while (1)
        ; //不让线程退出
    return NULL;
}

int main(int argc, char const *argv[])
{
    pthread_t tid;
    if (pthread_create(&tid, NULL, handler_thread, NULL) != 0) //创建线程
    {
        perror("phtread err");
        return -1;
    }

    // pthread_join(tid, NULL);   //阻塞等待指定的线程结束然后给其回收资源
    pthread_detach(tid);     //不阻塞,让指定线程结束时自动回收资源
    printf("in main\n");
    while (1)
        ; //让主线程不要结束
    return 0;
}
练习:通过父子进程完成对文件的拷贝(cp)

1. 通过父子进程完成对文件的拷贝(cp),父进程从文件开始到文件的一半开始拷贝,子进程从文件的一半到文件末尾。要求:文件IO cp src dest

(1) 文件长度获取:lseek

(2) 子进程定位到文件一半:lseek

(3) 父进程怎么准确读到文件一半的位置?

(4) fork之前打开文件,父子进程读写时,位置指针是同一个

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(int argc, char const *argv[])
{
    int fd1, fd2;
    pid_t pid;
    char buf[32] = "";
    ssize_t n;
    if (argc != 3)
    {
        printf("err: %s <srcfile> <destfile>\n", argv[0]);
        return -1;
    }

    fd1 = open(argv[1], O_RDONLY);
    if (fd1 < 0)
    {
        perror("fd1 err");
        return -1;
    }
    fd2 = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0777);
    if (fd2 < 0)
    {
        perror("fd2 err");
        return -1;
    }

    //获取源文件长度的一半
    off_t len = lseek(fd1, 0, 2) / 2;

    //创建子进程
    pid = fork();
    if (pid < 0)
    {
        perror("fork err");
        return -1;
    }
    else if (pid == 0) //拷贝后半段
    {
        //定位到一半的位置
        lseek(fd1, len, 0);
        lseek(fd2, len, 0);

        //读源文件,写入目标文件
        while ((n = read(fd1, buf, 32)) > 0)
        {
            write(fd2, buf, n);
            sleep(1);
        }
    }
    else //拷贝前半段
    {
        wait(NULL); //等子进程读写完父进程再拷贝
        //定位到文件开头
        lseek(fd1, 0, 0);
        lseek(fd2, 0, 0);

        //读源文件,写入目标文件
        while (len > 0)
        {
            if (len > 32)
                n = read(fd1, buf, 32);
            else
                n = read(fd1, buf, len);
            write(fd2, buf, n);
            len -= n;   //len保存的是剩余要读的字符个数
            sleep(1);
        }
    }
    close(fd1);
    close(fd2);

    return 0;
}
练习:输入输出,quit结束

通过线程实现数据的交互,主线程循环从终端输入,线程函数将数据循环输出,当输入quit结束程序。

1) 全局变量进行通信

2) 加上标志位(flag),实现主线程输入一次,线程函数打印一次, int flag = 0;

#include <stdio.h>
#include <pthread.h>
#include <string.h>
char s[32];
int flag = 0; //为了进行线程间通讯,保证主线程先输入然后从线程再输出

void *handler_thread(void *arg)
{
    while (1)
    {
        if (flag == 1) //主线程输入完将flag置1从线程再输出
        {
            if (strcmp(s, "quit") == 0)
                break;
            printf("%s\n", s);
            flag--;
        }
    }
    return NULL;
}

int main(int argc, char const *argv[])
{
    pthread_t tid;
    if (pthread_create(&tid, NULL, handler_thread, NULL) != 0)
    {
        perror("err");
        return -1;
    }

    while (1)
    {
        //scanf前也可以不加if判断,利用阻塞时间让从线程输出
        // if (flag == 0)  //从线程输出完将flag置0主线程再输入
        // {
            scanf("%s", s);
            flag++;

            if (strcmp(s, "quit") == 0)
                break;
        // }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值