1、进程和线程的区别

  • 进程的目的就是担当分配系统资源(CPU时间、内存等)的基本单位。线程是进程的一个执行流,是CPU调度和分派的基本单位,它是比进程更小的能独立运行的基本单位。一个进程由几个线程组成,线程与同属一个进程的其他的线程共享进程所拥有的全部资源。

  • 地址空间:进程有独立的地址空间,包括文本区域(text region)、数据区域(data region)和堆栈(stack region);一个进程崩溃后,在保护模式下不会对其它进程产生影响;线程只是一个进程中的不同执行路径,线程有自己的堆栈和局部变量(在运行中必不可少的资源),但线程之间没有单独的地址空间,一个线程死掉就等于整个进程死掉。同一进程内的线程共享进程的地址空间。

  • 通信:进程间通信IPC,线程间可以直接读写进程数据段(如全局变量)来进行通信——需要进程同步和互斥手段的辅助,以保证数据的一致性。

  • 调度和切换:线程上下文切换比进程上下文切换要快得多。

  • 在多线程OS中,进程不是一个可执行的实体。


  • 地址空间:进程内的一个执行单元;进程至少有一个线程;它们共享进程的地址空间;而进程有自己独立的地址空间;


  • 资源拥有:进程是资源分配和拥有的单位,同一个进程内的线程共享进程的资源

  • 线程是处理器调度的基本单位,但进程不是.

  • 二者均可并发执行.

2、使用线程原因

    在Linux系统下,启动一个新的进程必须分配给它独立的地址空间,建立众多的数据表来维护它的代码段、堆栈段和数据段,这是一种"昂贵"的多任务工作方式。而运行于一个进程中的多个线程,它们彼此之间使用相同的地址空间,共享大部分数据,启动一个线程所花费的空间远远小于启动一个进程所花费的空间,而且,线程间彼此切换所需的时间也远远小于进程间切换所需要的时间。

    线程间方便的通信机制。对不同进程来说,它们具有独立的数据空间,要进行数据的传递只能通过通信的方式进行,这种方式不仅费时,而且很不方便。线程则不然,由于同一进程下的线程之间共享数据空间,所以一个线程的数据可以直接为其它线程所用,这不仅快捷,而且方便。

3、线程操作的函数

#include <pthread.h> 
int pthread_create(pthread_t *tid, const pthread_attr_t *attr, void *(*func) (void *), 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,否则返回Exxx(为正数)。

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

  • const pthread_attr_t *attr:指定创建线程的属性,如线程优先级、初始栈大小、是否为守护进程等。可以使用NULL来使用默认值,通常情况下我们都是使用默认值。

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

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

pthread_join:用于等待某个线程退出,成功返回0,否则返回Exxx(为正数)。

  • 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、线程间互斥

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

int pthread_mutex_lock(pthread_mutex_t * mptr); 
int pthread_mutex_unlock(pthread_mutex_t * mptr);

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

5、线程间同步

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

#include <pthread.h> 
int pthread_cond_wait(pthread_cond_t *cptr, pthread_mutex_t *mptr); 
int pthread_cond_signal(pthread_cond_t *cptr); 
//Both return: 0 if OK, positive Exxx value on error

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

/*
 是否熟悉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);
}