1、进程与线程

 

 

 

进程是程序执行时的一个实例,即它是程序已经执行到何种程度的数据结构的汇集。从内核的观点看,进程的目的就是担当分配系统资源(CPU时间、内存等)的基本单位。

线程是进程的一个执行流,是CPU调度和分派的基本单位,它是比进程更小的能独立运行的基本单位。一个进程由几个线程组成(拥有很多相对独立的执行流的用户程序共享应用程序的大部分数据结构),线程与同属一个进程的其他的线程共享进程所拥有的全部资源。

进程资源分配的最小单位,线程程序执行的最小单位

进程有自己独立的地址空间,一个进程崩溃之后,在保护模式下不会对其它进程产生影响,而线程只是一个进程中的不同的执行路径。县城有自己的堆栈和局部变量,单线程没有单独的地址空间,一个线程死掉就等于整个进程死掉,所以多进程的程序要比多线程的程序健壮,但在进程切换时,耗费资源巨大,效率要差些。但对一些要求同时进行并且有要共享某些变量的并发操作,只能用线程,不能用进程。

 

2、使用线程的理由

进程有自己独立的地址空间,线程没有单独的地址空间(统一进程内的线程共享地址空间)。

使用多线程的理由之一就是:是一种非常节俭的多任务操作模式。我们知道,在Linux系统下,启动一个新的进程必须分配给它独立的地址空间,建立众多的数据表来维护它的代码段、堆栈段和数据段,这是一种"昂贵"的多任务工作方式。而运行一个进程中的多线程,他们彼此之间使用相同的地址空间,共享大部分数据,启动一个线程所花费的空间远远小于启动一个进程所花费的空间,而且,线程切换时间也远远小于启动一个进程所花费的时间。总的来说,一个进程的开销大约是一个线程开销的30倍。

使用多线程的理由之二:县城建方便通信,由于不同的进程有自己独立的地址空间,要进行数据的传递只能通过通信的方式进行,这种方式,不仅费时,而且很不方便。线程则不然,由于统一进程下的线程共享数据空间,所以一个线程的数据可以直接为其它线程使用,这不仅快捷而且方便,有的变量不能同时被两个线程访问,有的子程序中声明为static的数据更有可能给多线程带来灾难性的打击,这些正是编写多线程程序时最需要注意的地方。

 

除了以上所说的优点外,不和进程比较,多线程程序作为一种多任务、并发的工作方式,当然有以下的优点:

  • 提高应用程序响应。这对图形界面的程序尤其有意义,当一个操作耗时很长时,整个系统都会等待这个操作,此时程序不会响应键盘、鼠标、菜单的操作,而使用多线程技术,将耗时长的操作(time consuming)置于一个新的线程,可以避免这种尴尬的情况。

  • 使多CPU系统更加有效。操作系统会保证当线程数不大于CPU数目时,不同的线程运行于不同的CPU上。

  • 改善程序结构。一个既长又复杂的进程可以考虑分为多个线程,成为几个独立或半独立的运行部分,这样的程序会利于理解和修改

3、有关线程操作的函数:

1
2
3
4
5
6
7
8
9
10
11
#inlcude<pthread.h>
 
int pthread_create(pthreead_t tid, const pthread_attr_t * attr,(void *)*(*func),void * arg);
 
int pthread_join(pthread_t tid, void ** status);
 
pthread_t pthread_self(void );
 
int pthread_detach(pthread_t tid);
 
void pthread_exit(void * status);

 

pthread_create()用于创建一个线程,成功返回0 ,否则返回EXEE(正数)

    pthread_t tid : 线程id 的类型为pthread_t ,通常为无符号整型,当调用pthread_create()成功时,通过* tid 指针返回。

    const pthread_attr_t *attr :制定创建线程属性,如线程优先级,初始占的大小,是否守护进程等。使用NULL为默认值,通产情况下,我们都是使用默认值。

    (void*)(*func)(void *):函数指针func 指定当新的线程创建之后,将执行的函数。

    void* arg:线程将执行的函数的参数,如果想传递多个参数,请将他们封装在一个结构体中。

pthread_join()用于等待某个线程的退出,成功返回0 ,失败返回EXEE(为正数)

    pthread_t tid:指定要等待的线程id

    void **status:如果不为NULL,那么线程的返回值存储在status指向的内存空间中,(这就是为什么status是二级指针的原因,这种参数也称为”值-结果“参数)

pthread_self:用于返回当前线程的id

pthread-detach:用于是指定线程变为分离状态,就像进程脱离终端而变为二后台进程类似。成功返回0,失败返回EXXX(正数)。变为分离状态的线程,如果线程推出,她的所有资源将全部释放。而如果不是分离状态,线程必须保留他的线程id,推出状态直到其它线程对他调用了pthread-join.

进程也是类似,这也是当我们打开进程管理器的时候,发现很多僵死进程的原因!也是为什么一定有个僵死这个进程的状态。

 

pthread_exit()用于终止线程,可以指定返回值,以便其它线程通过pthread-join函数获取该线程的返回值。

    void * status:指针线程终止的返回值。

4、线程之间的互斥:

互斥锁:使用互斥锁(互斥)可以使线程按顺序执行。通常,互斥锁通过确保一次只有一个线程执行代码的临界段来同步多个线程。互斥锁还可以保护单线程代码。

互斥锁的相关操作函数如下:

 

1
2
3
4
5
#inlcude<pthread.h>
 
int pthread_mutex_lock(pthread_mutex_t *mpr);
 
int pthread_mutex_unlock(pthread_mutex_t *mpr);

在对临界资源进行操作时需要使用pthread_mutex_lock()先枷锁,操作完之后,pthread_mutex_unlock()再解锁。而且在这之前需要声明一个pthread_mutex_t 类型的变量,用作前面的两个函数的参数。

 

5、线程之间的同步:

条件变量:使用条件变量可以以原子方式阻塞进程,直到某个特定条件为真为止。条件变量始终与互斥锁一起使用。对条件的测试是在互斥锁的保护下进行的。

如果条件为假,线程通常会基于条件变量阻塞,并以原子方式释放等待条件变化的互斥锁。

如果另一个线程更改了条件,该线程可能会向相关条件变量发出信号,从而使一个或多个等待的线程执行以下操作:

    唤醒

    再次获取互斥锁

     重新评估条件

在以下情况下,条件变量可用于再进程之间同步线程:

    线程是可以写入的内存中分配的

    内存由协作进程共享

使用条件变量可以以原子方式阻塞线程,知道某个特定条件为真为止。

条件变量的相关函数如下:

#include<stdio.h>

#include<pthread.h>

 

int pthread_cond_wait(pthread_cond_t *cpr,pthread_mutex_t *mpr );

int pthread_cond_signal(pthread_cond_t *cptr);

 

pthread_cond_wait()用于等待某个特定的条件为真,pthread_cond_signal用于通知阻塞的县城某个特定的条件为真了。在调用这两个函数之前需要声明一个pthread_cond_t 类型的变量,用于这连个函数的参数。

为什么条件变量始终与互斥锁一起使用,对条件的测试是在互斥锁的保护下进行的?因为某个条件通常是在多个线程之间共享的某个变量,互斥所允许这个变量可以在不同的线程中设置和检测。

通常,pthread_cond_wati只是唤醒等待某个条件变量的一个线程。如果需要唤醒所有等待某个条件变量的线程,需要调用:

int pthread_cond_broadcast(pthread_cond_t *ptr);

默认情况下面,阻塞的线程会一直等待,直到某个条件为真,如果想设置最大的阻塞时间可以调用:

int pthread_cond_t timewati(pthread_cond_t *cptr,pthread_mutex_t *mpr ,const struct timespec * abstime);

如果时间到了,条件变量还没有为真,任然返回,返回值为ETIME。

通过上面介绍,我们可以轻松写出线面代码了:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
是否熟悉POSIX多线程编程技术?如熟悉,编写程序完成如下功能:
  1)有一int型全局变量g_Flag初始值为0
  2)在主线称中起动线程1,打印“this is thread1”,并将g_Flag设置为1
  3)在主线称中启动线程2,打印“this is thread2”,并将g_Flag设置为2
  4)线程序1需要在线程2退出后才能退出
  5)主线程在检测到g_Flag从1变为2,或者从2变为1的时候退出
   */
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<errno.h>
#include<unistd.h>
 
typedef void* (*fun)(void*);
 
int g_Flag=0;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
 
void* thread1(void*);
void* thread2(void*);
 
/*
 *  when program is started, a single thread is created, called the initial thread or main thread.
 *  Additional threads are created by pthread_create.
 *  So we just need to create two thread in main().
 */
 
int main(int argc, char** argv)
{
    printf("enter main\n");
    pthread_t tid1, tid2;
    int rc1=0, rc2=0;
    rc2 = pthread_create(&tid2, NULL, thread2, NULL);
    if(rc2 != 0)
        printf("%s: %d\n",__func__, strerror(rc2));
 
    rc1 = pthread_create(&tid1, NULL, thread1, &tid2);
    if(rc1 != 0)
        printf("%s: %d\n",__func__, strerror(rc1));
 
    pthread_cond_wait(&cond, &mutex);
    printf("leave main\n");
    exit(0);   
}
/*
 * thread1() will be execute by thread1, after pthread_create()
 * it will set g_Flag = 1;
 */
void* thread1(void* arg)
{
    printf("enter thread1\n");
    printf("this is thread1, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());
    pthread_mutex_lock(&mutex);
    if(g_Flag == 2)
        pthread_cond_signal(&cond);
    g_Flag = 1;
    printf("this is thread1, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());
    pthread_mutex_unlock(&mutex);
    pthread_join(*(pthread_t*)arg, NULL);
    printf("leave thread1\n");
    pthread_exit(0);
}
 
/*
 * thread2() will be execute by thread2, after pthread_create()
 * it will set g_Flag = 2;
 */
void* thread2(void* arg)
{
    printf("enter thread2\n");
    printf("this is thread2, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());
    pthread_mutex_lock(&mutex);
    if(g_Flag == 1)
        pthread_cond_signal(&cond);
    g_Flag = 2;
    printf("this is thread2, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());
    pthread_mutex_unlock(&mutex);
    printf("leave thread2\n");
    pthread_exit(0);
}