下如何实现进程间互斥锁

 我来答
百度网友ded4135
高粉答主

2018-09-21 · 醉心答题,欢迎关注
知道大有可为答主
回答量:2.7万
采纳率:87%
帮助的人:1.2亿
展开全部
/* process_svr.c */#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <pthread.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/shm.h>#include <sys/time.h>#include <signal.h>#include <errno.h>#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);}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式