不同进程之间的线程互斥锁的使用

/* process_svr.c */

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define NONE "\033[m"
#define RED "\033[0;32;31m"
#define LIGHT_RED "\033[1;31m"
#define GREEN "\033[0;32;32m"
#define LIGHT_GREEN "\033[1;32m"
#define BLUE "\033[0;32;34m"
#define LIGHT_BLUE "\033[1;34m"
#define DARY_GRAY "\033[1;30m"
#define CYAN "\033[0;36m"
#define LIGHT_CYAN "\033[1;36m"
#define PURPLE "\033[0;35m"
#define LIGHT_PURPLE "\033[1;35m"
#define BROWN "\033[0;33m"
#define YELLOW "\033[1;33m"
#define LIGHT_GRAY "\033[0;37m"
#define WHITE "\033[1;37m"


void alrm_func(int sn);
void int_func(int sn);

pthread_mutex_t *p_mutex_shared = NULL;
int shmid = -1;

int main(int argc, char *argv[])
{
	// 创建一个共享内存段
	key_t key_id = ftok(".", 1);
	shmid = shmget(key_id, sizeof(pthread_mutex_t), 
			IPC_CREAT | IPC_EXCL | 0644);
	if (shmid < 0)
	{
		perror(RED "shmget() create failed" NONE);
		return -1;
	}
	printf("shmget() create success, shmid is %d.\n", shmid);

	// 挂接到共享内存
	p_mutex_shared = shmat(shmid, NULL, 0);
	if (p_mutex_shared == (void *)-1)
	{
		perror(RED "shmat() failed" NONE);
		// 删除共享内存,这里实际只是标记为删除,真正的删除动作在所有挂接的进程都脱接的状态下进行。
		// 同时不允许有新进程挂接到该共享内存上。
		shmctl(shmid, IPC_RMID, 0);
		return -2;
	}
	printf("shmat() success.\n");

	// 初始化共享内存段,存放互斥锁,该锁用于不同进程之间的线程互斥。
	pthread_mutexattr_t mutextattr;
	pthread_mutexattr_init(&mutextattr);
	// 设置互斥锁在进程之间共享
	pthread_mutexattr_setpshared(&mutextattr, PTHREAD_PROCESS_SHARED);
	pthread_mutex_init(p_mutex_shared, &mutextattr);

	signal(SIGALRM, alrm_func);
	signal(SIGINT, int_func);

	struct itimerval itimer = { {0, 500000}, {0, 5000} };
	setitimer(ITIMER_REAL, &itimer, NULL);

	while (1)
		sleep(1);

	return 0;
}

void alrm_func(int sn)
{
	if (p_mutex_shared == NULL)
	{
		printf("p_mutex_shared is NULL.\n");
		return ;
	}

	time_t tm = time(NULL);
	printf("%s\t", ctime(&tm));
	if (pthread_mutex_trylock(p_mutex_shared) == 0)
	{
		printf(GREEN "pthread_mutex_trylock success.\n" NONE);
		pthread_mutex_unlock(p_mutex_shared);
	}
	else
	{
		printf(BLUE "pthread_mutex_trylock failed.\n" NONE);
	}
	printf("################################################\n");
}

void int_func(int sn)
{
	if (p_mutex_shared != NULL)
	{
		// 脱接
		if (shmdt(p_mutex_shared) == -1)
			perror(RED "shmdt() failed" NONE);
		else
			printf("shmdt() success.\n");
		p_mutex_shared = NULL;
	}
	if (shmid != -1)
	{
		// 删除共享内存
		if (shmctl(shmid, IPC_RMID, NULL) == -1)
			perror(RED "shmctl() IPC_RMID is failed" NONE);
		else
			printf("shmctl() IPC_RMID is success.\n");
		shmid = -1;
	}

	exit(0);
}

/* process_clt.c */

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define NONE "\033[m"
#define RED "\033[0;32;31m"
#define LIGHT_RED "\033[1;31m"
#define GREEN "\033[0;32;32m"
#define LIGHT_GREEN "\033[1;32m"
#define BLUE "\033[0;32;34m"
#define LIGHT_BLUE "\033[1;34m"
#define DARY_GRAY "\033[1;30m"
#define CYAN "\033[0;36m"
#define LIGHT_CYAN "\033[1;36m"
#define PURPLE "\033[0;35m"
#define LIGHT_PURPLE "\033[1;35m"
#define BROWN "\033[0;33m"
#define YELLOW "\033[1;33m"
#define LIGHT_GRAY "\033[0;37m"
#define WHITE "\033[1;37m"

typedef int bool;
#define true (1)
#define false (0)

void alrm_func(int sn);
void int_func(int sn);

pthread_mutex_t *p_mutex_shared = NULL;
int shmid = -1;
bool is_lock = false;

int main(int argc, char *argv[])
{
	signal(SIGALRM, alrm_func);
	signal(SIGINT, int_func);

	key_t key_id = ftok(".", 1);
	shmid = shmget(key_id, 0, 0);
	if (shmid < 0)
	{
		perror(RED "shmget() failed" NONE);
		return -1;
	}

	p_mutex_shared = shmat(shmid, NULL, 0);
	if (p_mutex_shared == NULL)
	{
		perror(RED "shmat() failed" NONE);
		return -2;
	}

	struct itimerval timer = { {3, 0}, {1, 0} };
	setitimer(ITIMER_REAL, &timer, NULL);

	while (1)
		pause();

	return 0;
}

void alrm_func(int sn)
{
	if (p_mutex_shared == NULL)
	{
		printf("p_mutex_shared is NULL.\n");
		return ;
	}

	time_t tm = time(NULL);
	printf("%s\t", ctime(&tm));

	int ret = 0;
	if (is_lock)
	{
		ret = pthread_mutex_unlock(p_mutex_shared);
		is_lock = false;
		printf(BLUE "pthread_mutex_unlock(). return is %d\n" NONE, ret);
	}
	else
	{
		ret = pthread_mutex_lock(p_mutex_shared);
		is_lock = true;
		printf(GREEN "pthread_mutex_lock(). return is %d\n" NONE, ret);
	}

	printf("#####################################################\n");
}

void int_func(int sn)
{
	pthread_mutex_t *mutex = NULL;
	if (p_mutex_shared != NULL)
	{
		mutex = p_mutex_shared;
		p_mutex_shared = NULL;
	}

	// 保证退出时解锁,不然死锁
	if (is_lock)
	{
		pthread_mutex_unlock(mutex);
		is_lock = false;
		printf("pthread_mutex_unlock. exit.\n");
	}

	shmdt(mutex), mutex = NULL;	// 脱接

	exit(0);
}



 
  


你可能感兴趣的:(linux,多线程)