线程的同步、互斥机制
2016-11-09 21:53
531 查看
整理线程同步的API函数及其作用
线程同步的方式和机制
临界区、互斥量、事件、信号量四种方式
临界区(Critical Section)、互斥量(Mutex)、信号量(Semaphore)、事件(Event)的区别
1、临界区:通过对多线程的串行化来访问公共资源或一段代码,速度快,适合控制数据访问。在任意时刻只允许一个线程对共享资源进行访问,如果有多个线程试图访问公共资源,那么在有一个线程进入后,其他试图访问公共资源的线程将被挂起,并一直等到进入临界区的线程离开,临界区在被释放后,其他线程才可以抢占。
2、互斥量:采用互斥对象机制。 只有拥有互斥对象的线程才有访问公共资源的权限,因为互斥对象只有一个,所以能保证公共资源不会同时被多个线程访问。互斥不仅能实现同一应用程序的公共资源安全共享,还能实现不同应用程序的公共资源安全共享
3、信号量:它允许多个线程在同一时刻访问同一资源,但是需要限制在同一时刻访问此资源的最大线程数目
4、事 件: 通过通知操作的方式来保持线程的同步,还可以方便实现对多个线程的优先级比较的操作。
API函数见上一节。
注意点:能够运用同步机制实现生产者和消费者例程
producer.c
/*producer.c*/
#include"shm_com.h"
#include"sem_com.h"
#include<signal.h>
int ignore_signal(void)
{ /*忽略一些信号,以免非法突出程序*/
signal(SIGINT,SIG_IGN);
signal(SIGSTOP,SIG_IGN);
signal(SIGQUIT,SIG_IGN);
return 0;
}
int main()
{
char *shared_memory;
struct shm_buff *shm_buff_inst;
char buffer[BUFSIZ];
int shmid,semid;
/*定义信号量,用于实现访问共享内存的进程间的互斥*/
ignore_signal(); /*用于防止程序非正常退出*/
semid=semget(ftok(".",'a'),1,0666|IPC_CREAT);/*创建一个信号量*/
init_sem(semid,1); /*初始值为1*/
/*创建共享内存*/
shmid=shmget(ftok(".",'c'),sizeof(struct shm_buff),0666|IPC_CREAT);
if(shmid==-1)
{
perror("shmget failed");
del_sem(semid);
exit(1);
}
/*将共享内存地址映射到当前进程地址空间*/
shared_memory=shmat(shmid,0,0);
if(shared_memory==(void*)-1)
{
perror("shmat");
del_sem(semid);
exit(1);
}
printf("Memory attached at %p\n",shared_memory);
/*获取共享内存的映射地址*/
shm_buff_inst=(struct shm_buff *)shared_memory;
do
{
sem_p(semid);
printf("Emter some text to the shared memory(enter 'quit' to exit):");
/*向共享内存写入数据*/
if(fgets(shm_buff_inst->buffer,SHM_BUFF_SZ,stdin)==NULL)
{
perror("fgets");
sem_v(semid);
break;
}
shm_buff_inst->pid=getpid();
sem_v(semid);
}while(strncmp(shm_buff_inst->buffer,"quit",4)!=0);
/*删除信号量*/
del_sem(semid);
/*删除共享内存到当前进程地址空间中的映射*/
if(shmdt(shared_memory)==-1)
{
perror("shmdt");
exit(1);
}
exit(0);
}
customer.c
/* customer.c */
#include"shm_com.h"
#include"sem_com.h"
int main()
{
char *shared_memory=NULL;
struct shm_buff *shm_buff_inst;
int shmid,semid;
/*获得信号量*/
semid=semget(ftok(".",'a'),1,0666);
if(semid==-1)
{
perror("Producer isn't exist");
exit(1);
}
/*获得共享内存*/
shmid=shmget(ftok(".",'c'),sizeof(struct shm_buff),0666|IPC_CREAT);
if(shmid==-1)
{
perror("shmget");
exit(1);
}
/*将共享内存地址映射到当前进程地址空间*/
shared_memory=shmat(shmid,0,0);
if(shared_memory==(void *)-1)
{
perror("shmat");
exit(1);
}
printf("Memory attached at %p\n",shared_memory);
/*获得共享内存的映射地址*/
shm_buff_inst=(struct shm_buff *)shared_memory;
do
{
sem_p(semid);
printf("Shared memory was written by process %d : %s",shm_buff_inst->pid,shm_buff_inst->buffer);
if(strncmp(shm_buff_inst->buffer,"quit",4)==0)
{
perror("strncmp");
exit(1);
}
shm_buff_inst->pid=0;
memset(shm_buff_inst->buffer,0,SHM_BUFF_SZ);
sem_v(semid);
}while(1);
/*删除共享内存到当前进程地址空间中的映射*/
if(shmdt(shared_memory)==-1)
{
perror("shmdt");
exit(1);
}
/*删除共享内存*/
if(shmctl(shmid,IPC_RMID,NULL)==-1)
{
perror("shmctl");
exit(1);
}
exit(0);
}
sem_com.c
/*sem_com.h*/
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#include<stdio.h>
#include<stdlib.h>
union semun
{
int val;
struct semid_ds *buf;
unsigned short *array;
};
/*信号量初始化(赋值)函数*/
int init_sem(int sem_id,int init_value);
/*从系统中删除信号量的函数*/
int del_sem(int sem_id);
/*p操作函数*/
int sem_p(int sem_id);
/*V操作函数*/
int sem_v(int sem_id);
shm_com.h
/*shm_com.h*/
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#define SHM_BUFF_SZ 2048
struct shm_buff
{
int pid;
char buffer[SHM_BUFF_SZ];
};
运行结果:
线程同步的方式和机制
临界区、互斥量、事件、信号量四种方式
临界区(Critical Section)、互斥量(Mutex)、信号量(Semaphore)、事件(Event)的区别
1、临界区:通过对多线程的串行化来访问公共资源或一段代码,速度快,适合控制数据访问。在任意时刻只允许一个线程对共享资源进行访问,如果有多个线程试图访问公共资源,那么在有一个线程进入后,其他试图访问公共资源的线程将被挂起,并一直等到进入临界区的线程离开,临界区在被释放后,其他线程才可以抢占。
2、互斥量:采用互斥对象机制。 只有拥有互斥对象的线程才有访问公共资源的权限,因为互斥对象只有一个,所以能保证公共资源不会同时被多个线程访问。互斥不仅能实现同一应用程序的公共资源安全共享,还能实现不同应用程序的公共资源安全共享
3、信号量:它允许多个线程在同一时刻访问同一资源,但是需要限制在同一时刻访问此资源的最大线程数目
4、事 件: 通过通知操作的方式来保持线程的同步,还可以方便实现对多个线程的优先级比较的操作。
API函数见上一节。
注意点:能够运用同步机制实现生产者和消费者例程
producer.c
/*producer.c*/
#include"shm_com.h"
#include"sem_com.h"
#include<signal.h>
int ignore_signal(void)
{ /*忽略一些信号,以免非法突出程序*/
signal(SIGINT,SIG_IGN);
signal(SIGSTOP,SIG_IGN);
signal(SIGQUIT,SIG_IGN);
return 0;
}
int main()
{
char *shared_memory;
struct shm_buff *shm_buff_inst;
char buffer[BUFSIZ];
int shmid,semid;
/*定义信号量,用于实现访问共享内存的进程间的互斥*/
ignore_signal(); /*用于防止程序非正常退出*/
semid=semget(ftok(".",'a'),1,0666|IPC_CREAT);/*创建一个信号量*/
init_sem(semid,1); /*初始值为1*/
/*创建共享内存*/
shmid=shmget(ftok(".",'c'),sizeof(struct shm_buff),0666|IPC_CREAT);
if(shmid==-1)
{
perror("shmget failed");
del_sem(semid);
exit(1);
}
/*将共享内存地址映射到当前进程地址空间*/
shared_memory=shmat(shmid,0,0);
if(shared_memory==(void*)-1)
{
perror("shmat");
del_sem(semid);
exit(1);
}
printf("Memory attached at %p\n",shared_memory);
/*获取共享内存的映射地址*/
shm_buff_inst=(struct shm_buff *)shared_memory;
do
{
sem_p(semid);
printf("Emter some text to the shared memory(enter 'quit' to exit):");
/*向共享内存写入数据*/
if(fgets(shm_buff_inst->buffer,SHM_BUFF_SZ,stdin)==NULL)
{
perror("fgets");
sem_v(semid);
break;
}
shm_buff_inst->pid=getpid();
sem_v(semid);
}while(strncmp(shm_buff_inst->buffer,"quit",4)!=0);
/*删除信号量*/
del_sem(semid);
/*删除共享内存到当前进程地址空间中的映射*/
if(shmdt(shared_memory)==-1)
{
perror("shmdt");
exit(1);
}
exit(0);
}
customer.c
/* customer.c */
#include"shm_com.h"
#include"sem_com.h"
int main()
{
char *shared_memory=NULL;
struct shm_buff *shm_buff_inst;
int shmid,semid;
/*获得信号量*/
semid=semget(ftok(".",'a'),1,0666);
if(semid==-1)
{
perror("Producer isn't exist");
exit(1);
}
/*获得共享内存*/
shmid=shmget(ftok(".",'c'),sizeof(struct shm_buff),0666|IPC_CREAT);
if(shmid==-1)
{
perror("shmget");
exit(1);
}
/*将共享内存地址映射到当前进程地址空间*/
shared_memory=shmat(shmid,0,0);
if(shared_memory==(void *)-1)
{
perror("shmat");
exit(1);
}
printf("Memory attached at %p\n",shared_memory);
/*获得共享内存的映射地址*/
shm_buff_inst=(struct shm_buff *)shared_memory;
do
{
sem_p(semid);
printf("Shared memory was written by process %d : %s",shm_buff_inst->pid,shm_buff_inst->buffer);
if(strncmp(shm_buff_inst->buffer,"quit",4)==0)
{
perror("strncmp");
exit(1);
}
shm_buff_inst->pid=0;
memset(shm_buff_inst->buffer,0,SHM_BUFF_SZ);
sem_v(semid);
}while(1);
/*删除共享内存到当前进程地址空间中的映射*/
if(shmdt(shared_memory)==-1)
{
perror("shmdt");
exit(1);
}
/*删除共享内存*/
if(shmctl(shmid,IPC_RMID,NULL)==-1)
{
perror("shmctl");
exit(1);
}
exit(0);
}
sem_com.c
/*sem_com.c*/ #include"sem_com.h" /*信号量初始化(赋值)函数*/ int init_sem(int sem_id,int init_value) { union semun sem_union; sem_union.val=init_value; /*init_value为初始值*/ if(semctl(sem_id,0,SETVAL,sem_union)==-1) { perror("Initialize semaphore"); return -1; } return 0; } /*从系统中删除信号量的函数*/ int del_sem(int sem_id) { union semun sem_union; if(semctl(sem_id,0,IPC_RMID,sem_union)==-1) { perror("Delete semaphore"); return -1; } } /*p操作函数*/ int sem_p(int sem_id) { struct sembuf sem_b; sem_b.sem_num=0; /*单个信号量的编号应该为0*/ sem_b.sem_op=-1; /*取值为-1,表示p操作*/ sem_b.sem_flg=SEM_UNDO; /*系统自动释放将会在系统中残留的信号量*/ if(semop(sem_id,&sem_b,1)==-1) { perror("P operationn"); return -1; } return 0; } /*V操作函数*/ int sem_v(int sem_id) { struct sembuf sem_b; sem_b.sem_num=0; /*单个信号量的编号应该为0*/ sem_b.sem_op=1; /*取值为+1,表示v操作*/ sem_b.sem_flg=SEM_UNDO; /*系统自动释放将会在系统中残留的信号量*/ if(semop(sem_id,&sem_b,1)==-1) { perror("V operationn"); return -1; } return 0; }sem_com.h
/*sem_com.h*/
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#include<stdio.h>
#include<stdlib.h>
union semun
{
int val;
struct semid_ds *buf;
unsigned short *array;
};
/*信号量初始化(赋值)函数*/
int init_sem(int sem_id,int init_value);
/*从系统中删除信号量的函数*/
int del_sem(int sem_id);
/*p操作函数*/
int sem_p(int sem_id);
/*V操作函数*/
int sem_v(int sem_id);
shm_com.h
/*shm_com.h*/
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#define SHM_BUFF_SZ 2048
struct shm_buff
{
int pid;
char buffer[SHM_BUFF_SZ];
};
运行结果:
相关文章推荐
- 线程间同步机制02_互斥锁
- <linux线程>同步互斥机制之【互斥锁】
- java中的线程机制和线程的同步与互斥.
- 浅析线程的同步与互斥机制
- <linux线程>同步互斥机制之【条件变量】
- 线程间机制(同步和互斥)
- 线程同步机制之互斥锁
- Java实例说明 100个线程同时向一个银行账户中存入1元钱,在没有使用同步机制和使用同步机制情况下的执行情况
- Linux多线程编程(三)---线程之间的同步与互斥进阶实验
- 多线程篇(二)——线程之间的同步与互斥,关键段的使用
- 【操作系统】进程(线程)间同步互斥问题——熟睡的理发师问题
- 为了在线程间进行可靠的通信,也为了互斥访问,同步是必要的。
- Linux线程浅析[线程的同步和互斥之线程死锁,线程与信号的关系]
- Day19—线程的基本操作、线程的同步机制
- 专业课程设计之客户与服务器程序的同步与通信机制的设计(三)数据共享和线程
- 线程的同步与互斥:条件变量&信号量
- 3、线程的同步互斥技术和通信
- 线程同步的方式和机制
- 线程的三种同步机制
- 心得3--由售票系统中的抢票机制解说同步线程及死锁案例分析