线程详解

线程详解

1.进程和线程

在Linux的学习过程中,进程和线程的区分以及各自的优缺点,困扰了很大一部分的人,同样,笔者作为广大吃瓜群众的一员,也被这个问题所困扰。
所以呢,今天特别写了这篇博客来记忆一下。

首先呢,我们从定义上了解一下进程和线程:
进程是一个具有一定独立功能的程序的一次运行活动,同时也是资源分配的最小单元。进程是程序执行时的一个实例,即它是程序已经执行到某种程度的数据结构的汇集。从内核的观点看,进程的目的就是担当分配系统资源(CPU时间、内存等)的基本单位。
线程是进程的一个执行流,是CPU调度和分派的基本单位,它是比进程更小的能独立运行的基本单位。线程自己基本上不拥有系统资源,只拥有一点在运行中必不可少的资源(如程序计数器,一组寄存器和栈),但是它可与同属一个进程的其他的线程共享进程所拥有的全部资源。

其次,它们有什么联系呢?
从上面的定义我们不难理解,线程比进程小,一个进程可以由多个线程组成,即同一个进程中的多个线程之间可以并发执行,且一个线程可以创建和撤销另一个线程。相对进程而言,线程是一个更加接近于执行体的概念,它可以与同进程中的其他线程共享数据,但拥有自己的栈空间,拥有独立的执行序列。

然后,它们的区别什么?
进程和线程的主要差别在于它们是不同的操作系统资源管理方式。进程有独立的地址空间,一个进程崩溃后,在保护模式下不会对其它进程产生影响,而线程只是一个进程中的不同执行路径。线程有自己的堆栈和局部变量,但线程之间没有单独的地址空间,一个线程死掉就等于整个进程死掉,所以多进程的程序要比多线程的程序健壮,但在进程切换时,耗费资源较大,效率要差一些。但对于一些要求同时进行并且又要共享某些变量的并发操作,只能用线程,不能用进程。
1) 简而言之,一个程序至少有一个进程,一个进程至少有一个线程.
2) 线程的划分尺度小于进程,使得多线程程序的并发性高。
3) 进程有独立的地址空间,线程没有单独的地址空间(同一进程内的线程共享进程的地址空间)。
4) 线程在执行过程中与进程还是有区别的。每个独立的线程有一个程序运行的入口、顺序执行序列和程序的出口。但是线程不能够独立执行,必须依存在应用程序中,由应用程序提供多个线程执行控制。
5) 从逻辑角度来看,多线程的意义在于一个应用程序中,有多个执行部分可以同时执行。但操作系统并没有将多个线程看做多个独立的应用,来实现进程的调度和管理以及资源分配。这就是进程和线程的重要区别。

最后,我们可以总结一下它们的优缺点:
1)和进程相比,线程是一种非常“节俭”的多任务操作方式.在Linux系统下,启动一个新的进程必须分配给它独立的地址空间,建立众多的数据表来维护它的代码段、堆栈段和数据段,这是一种”昂贵”的多任务工作方式运行于一个进程中的多个线程,它们之间使用相同的地址空间,而且线程间彼此切换所需的时间也远远小于进程间切换所需要的时间.据统计,一个进程的开销大约是一个线程开销的30倍左右。但是,虽然线程执行开销小,但不利于资源的管理和保护;而进程正相反。同时,线程适合于在SMP机器上运行,而进程则可以跨机器迁移。
2)多个线程共享内存,从而极大地提高了程序的运行效率。即,线程间方便的通信机制。对不同进程来说,它们具有独立的数据空间,要进行数据的传递只能通过进程间通信的方式进行,这种方式不仅费时,而且很不方便。线程则不然,由于同一进程下的线程之间共享数据空间,所以一个线程的数据可以直接为其它线程所用,这不仅快捷,而且方便。
3)使多CPU系统更加有效.操作系统会保证当线程数不大于CPU数目时,不同的线程运行于不同的CPU上。
4)改善程序结构.一个既长又复杂的进程可以考虑分为多个线程,成为几个独立或半独立的运行部分,这样的程序会利于理解和修改。

2.线程接口

2.1线程的创建

#include <pthread.h>
int pthread_create(pthread_t * tidp,const pthread_attr_t*attr,void*(*start_rtn)(void),void*arg)

tidp:线程id
attr: 线程属性(通常为空)
start_rtn:线程要执行的函数
arg:start_rtn的参数

注意:因为pthread的库不是linux系统的库,所以在进行编译的时候要加上 -lpthread


2.2线程的退出

如果进程中任何一个线程中调用exit或_exit,那么整个进程都会终止。线程的正常退出方式有:
(1) 线程从启动例程中返回
(2) 线程可以被另一个进程终止
(3) 线程自己调用pthread_exit函数

#include <pthread.h>
void pthread_exit(void * rval_ptr)

功能:终止调用线程
Rval_ptr:线程退出返回值的指针

2.3线程的等待

#include <pthread.h>

int pthread_join(pthread_t tid,void **rval_ptr)

功能:阻塞调用线程,直到指定的线程终止。
Tid :等待退出的线程id
Rval_ptr:线程退出的返回值的指针

2.4线程的同步

进行多线程编程,因为无法知道哪个线程会在哪个时候对共享资源进行操作,因此让如何保护共享资源变得复杂,通过下面这些技术的使用,可以解决线程之间对资源的竞争:
1 互斥量Mutex
2 信号灯Semaphore
3 条件变量Conditions

举个例子    :
Item * p =queue_list;
Queue_list=queue_list->next;
process_job(p);
free(p);
当线程1处理完Item *p=queue_list后,系统停止线程1的运行,改而运行线程2。线程2照样取出头节点,然后进行处理,最后释放了该节点。过了段时间,线程1重新得到运行。而这个时候,p所指向的节点已经被线程2释放掉,而线程1对此毫无知晓。他会接着运行process_job(p)。而这将导致无法预料的后果!

对于这种情况,系统给我们提供了互斥 量.线程在取出头节点前必须要等待互斥量,如果此时有其他线程已经获得该互斥量,那么该线程将会阻塞在这里.只有等到其他线程释放掉该互斥量后,该线程才有可能得到该互斥量。互斥量从本质上说就是一把锁, 提供对共享资源的保护访问。

在Linux中, 互斥量使用类型pthread_mutex_t表示.在使用前, 要对它进行初始化:

对于静态分配的互斥量, 可以把它设置为默认的mutex对象PTHREAD_MUTEX_INITIALIZER
对于动态分配的互斥量, 在申请内存(malloc)之后, 通过pthread_mutex_init进行初始化, 并且在释放内存(free)前需要调用pthread_mutex_destroy

#include <pthread.h>
int pthread_mutex_init(pthread_mutex_t 、*mutex,const pthread_mutexattr_t *attr)
int pthread_mutex_destroy(pthread_mutex_t *mutex)

对共享资源的访问, 要使用互斥量进行加锁, 如果互斥量已经上了锁, 调用线程会阻塞, 直到互斥量被解锁。
int pthread_mutex_lock(pthread_mutex_t *mutex)
int pthread_mutex_trylock(pthread_mutex_t *mutex)

返回值: 成功则返回0, 出错则返回错误编号。
trylock是非阻塞调用模式, 如果互斥量没被锁住, trylock函数将对互斥量加锁, 并获得对共享资源的访问权限; 如果互斥量被锁住了,trylock函数将不会阻塞等待而直接返回EBUSY, 表示共享资源处于忙状态

在操作完成后,必须给互斥量解锁,也就是前面所说的释放。这样其他等待该锁的线程才有机会获得该锁,否则其他线程将会永远阻塞。
int pthread_mutex_unlock(pthread_mutex_t *mutex)

这里又涉及到了一个非常容易混淆的概念:互斥量和信号量。
Mutex是一把钥匙,一个人拿了就可进入一个房间,出来的时候把钥匙交给队列的第一个。
Semaphore是一件可以容纳N人的房间,如果人不满就可以进去,如果人满了,就要等待有人出来。对于N=1的情况,称为binary semaphore。

Binary semaphore与Mutex的差异:
1. mutex要由获得锁的线程来释放(谁获得,谁释放)。而semaphore可以由其它线程释放
2. 初始状态可能不一样:mutex的初始值是1 ,semaphore的初始值可能是0(或者为1)

3.线程实例

为了更深入的展现进程和线程的不同,以及线程接口的应用,笔者分别用进程和线程写了两个进程间通信的代码。

上代码:
进程间通信:
文件1:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>

#define MSGKEY 1234
#define BUF_TYPE1 1
#define BUF_TYPE2 2

struct msgbuf
{
    long mtype;
    char mtext[100];
};

int main()
{
    int msgid, ret;
    pid_t pid;
    struct msgbuf buf;
    msgid = msgget(MSGKEY, IPC_CREAT | IPC_EXCL);
    if(-1 == msgid)
    {
        perror("msgget");
        exit(1);
    }

    pid = fork();
    if(-1 == pid)
    {
        perror("fork");
        exit(1);
    }
    else if(0 == pid)
    {
        while(1)
        {
            memset(&buf, 0, sizeof(buf));
            scanf("%s", buf.mtext);
            buf.mtype = BUF_TYPE1;

            ret = msgsnd(msgid, &buf, sizeof(buf.mtext), 0);
            if(-1 == ret)
            {
                perror("msgsnd");
                exit(1);
            }

            if(!strncmp(buf.mtext, "end", 3))
            {
                buf.mtype = BUF_TYPE2;
                ret = msgsnd(msgid, &buf, sizeof(buf.mtext), 0);
                if(-1 == ret)
                {
                    perror("msgsnd");
                    exit(1);
                }
                break;
            }
        }
    }
    else
    {   
        while(1)
        {
            memset(&buf, 0, sizeof(buf));

            ret = msgrcv(msgid, &buf, sizeof(buf.mtext), BUF_TYPE2, 0);
            if(-1 ==ret)
            {
                perror("msgrcv");
                exit(1);
            }
            if(!strncmp(buf.mtext, "end", 3))
            {
                kill(pid, SIGKILL);
                break;
            }
            printf("Receive From Message : %s\n", buf.mtext);
        }
        waitpid(pid, NULL, 0);
    }
    msgctl(msgid, IPC_RMID, NULL);

    return 0;
}
文件2:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>

#define MSGKEY 1234
#define BUF_TYPE1 1
#define BUF_TYPE2 2

struct msgbuf
{
    long mtype;
    char mtext[100];
};

int main()
{
    int msgid, ret;
    struct msgbuf buf ;
    pid_t pid;

    msgid = msgget(MSGKEY, 0);
    if(-1 == msgid)
    {
        perror("msgget");
        exit(1);
    }

    pid = fork();
    if(-1 == pid)
    {
        perror("fork");
        exit(1);
    }
    else if(0 == pid)
    {
        while(1)
        {
            memset(&buf, 0, sizeof(buf));
            scanf("%s", buf.mtext);
            buf.mtype = BUF_TYPE2;

            ret = msgsnd(msgid, &buf, sizeof(buf.mtext), 0);
            if(-1 == ret)
            {
                perror("msgrcv");
                exit(1);
            }
            if(!strncmp(buf.mtext, "end", 3))
            {
                buf.mtype = BUF_TYPE1;
                ret = msgsnd(msgid, &buf, sizeof(buf.mtext), 0);
                if(-1 == ret)
                {
                    perror("msgsand");
                    exit(1);
                }
                break;
            }
        }
    }
    else
    {
        while(1)
        {
            memset(&buf, 0, sizeof(buf));

            ret = msgrcv(msgid, &buf, sizeof(buf.mtext), BUF_TYPE1, 0);
            if(-1 == ret)
            {
                perror("msgrcv");
                exit(1);
            }
            if(!strncmp(buf.mtext, "end", 3))
            {
                kill(pid, SIGKILL);
                break;
            }
            printf("Receive From Mssage : %s\n", buf.mtext);
        }
        waitpid(pid, NULL, 0);
    }

    return 0;
}
/*文件1,创建队列通信,2个文件都是子进程发父进程收*/

这里写图片描述
这里写图片描述

改线程:
文件3:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>

#define MSGKEY 1234
#define BUF_TYPE1 1
#define BUF_TYPE2 2
pthread_t tid[2] = {0};

struct msgbuf
{
    long mtype;
    char mtext[100];
};

void *my_receive(void *arg)
{
    struct msgbuf buf;
    int ret;
    while(1)
    {
        memset(&buf, 0, sizeof(buf));

        ret = msgrcv(*(int *)arg, &buf, sizeof(buf.mtext), BUF_TYPE2, 0);
        if(-1 ==ret)
        {
            perror("msgrcv");
            exit(1);
        }
        if(!strncmp(buf.mtext, "end", 3))
        {
            pthread_cancel(tid[1]);
            break;
        }
        printf("Receive From Message : %s\n", buf.mtext);
    }
}

void *my_send(void *arg)
{
    struct msgbuf buf;
    int ret, oldtype;

    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype);
    while(1)
    {
        memset(&buf, 0, sizeof(buf));
        scanf("%s", buf.mtext);
        buf.mtype = BUF_TYPE1;
        ret = msgsnd(*(int *)arg, &buf, sizeof(buf.mtext), 0);
        if(-1 == ret)
        {
            perror("msgsnd");
            exit(1);
        }

        if(!strncmp(buf.mtext, "end", 3))
        {
            buf.mtype = BUF_TYPE2;
            ret = msgsnd(*(int *)arg, &buf, sizeof(buf.mtext), 0);
            if(-1 == ret)
            {
                perror("msgsnd");
                exit(1);
            }
            break;
        }
    }
}

int main()
{
    int msgid, ret;
    struct msgbuf buf;
    msgid = msgget(MSGKEY, IPC_CREAT | IPC_EXCL);
    if(-1 == msgid)
    {
        perror("msgget");
        exit(1);
    }

    ret = pthread_create(&tid[0], NULL, my_receive, (void *)&msgid);
    if(0 != ret)
    {
        perror("pthread_create1");
        exit(1);
    }
    ret = pthread_create(&tid[1], NULL, my_send, (void *)&msgid);
    if(0 != ret)
    {
        perror("pthread_create2");
        exit(1);
    }   

    pthread_join(tid[0], NULL);
    pthread_join(tid[1], NULL);
    msgctl(msgid, IPC_RMID, NULL);

    return 0;
}
文件4:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>

#define MSGKEY 1234
#define BUF_TYPE1 2
#define BUF_TYPE2 1
pthread_t tid[2] = {0};

struct msgbuf
{
    long mtype;
    char mtext[100];
};

void *my_receive(void *arg)
{
    struct msgbuf buf;
    int ret;
    while(1)
    {
        memset(&buf, 0, sizeof(buf));

        ret = msgrcv(*(int *)arg, &buf, sizeof(buf.mtext), BUF_TYPE2, 0);
        if(-1 ==ret)
        {
            perror("msgrcv");
            exit(1);
        }
        if(!strncmp(buf.mtext, "end", 3))
        {
            pthread_cancel(tid[1]);
            break;
        }
        printf("Receive From Message : %s\n", buf.mtext);
    }
}

void *my_send(void *arg)
{
    struct msgbuf buf;
    int ret, oldtype;

    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype);
    while(1)
    {
        memset(&buf, 0, sizeof(buf));
        scanf("%s", buf.mtext);
        buf.mtype = BUF_TYPE1;
        ret = msgsnd(*(int *)arg, &buf, sizeof(buf.mtext), 0);
        if(-1 == ret)
        {
            perror("msgsnd");
            exit(1);
        }

        if(!strncmp(buf.mtext, "end", 3))
        {
            buf.mtype = BUF_TYPE2;
            ret = msgsnd(*(int *)arg, &buf, sizeof(buf.mtext), 0);
            if(-1 == ret)
            {
                perror("msgsnd");
                exit(1);
            }
            break;
        }
    }
}

int main()
{
    int msgid, ret;
    struct msgbuf buf;
    msgid = msgget(MSGKEY, 0);
    if(-1 == msgid)
    {
        perror("msgget");
        exit(1);
    }

    ret = pthread_create(&tid[0], NULL, my_receive, (void *)&msgid);
    if(0 != ret)
    {
        perror("pthread_create1");
        exit(1);
    }
    ret = pthread_create(&tid[1], NULL, my_send, (void *)&msgid);
    if(0 != ret)
    {
        perror("pthread_create2");
        exit(1);
    }

    pthread_join(tid[0], NULL);
    pthread_join(tid[1], NULL);

    return 0;
}
/*注意文件4,我的宏定义队列序号1,2换了位置*/

这里写图片描述
这里写图片描述

总的来说用到的知识主要是之前博客里的进程间通信,对于少量通信而言,进程线程还是没什么区别的
但是如果涉及到大量数据的话,线程会更占优势。
举个例子,QQ相信大家都有用过,一个很简单的问题,是不是没和一个人聊天,如果都起一个进程的话,那么假使你同时和100个人聊天,那是不是像当于起了400 个进程,那系统基本已经死机完蛋啦,所以我们打开任务管理器才只看到了一个进程。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值