linux 进程间通信--systemV 信号量 实例

2024-02-12 04:18

本文主要是介绍linux 进程间通信--systemV 信号量 实例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1:代码

#include <stdio.h>
#include <sys/types.h>
#include <sys/sem.h>
#include <errno.h>
#include <stdlib.h>
#include <signal.h>
#define MAX_SEMAPHORES  3
union semun
{int val;                   			/* value for SETVAL */struct semid_ds *buf;       		/* buffer for IPC_STAT, IPC_SET */unsigned short int *array;  		/* array for GETALL, SETALL */struct seminfo *__buf;      		/* buffer for IPC_INFO */
};
static int set_semvalue(int sem_id,int semIndex,int semValue)
{union semun sem_union;int value;sem_union.val = semValue;if (semctl(sem_id, semIndex, SETVAL, sem_union) == -1){return(0);}printf("set value success,");printf("init value is %d\n",get_semvalue(sem_id,semIndex));return(1);
}int get_semvalue(int sem_id,int semIndex)
{int res;if((res=semctl(sem_id, semIndex, GETVAL)) == -1){perror("semctl");exit(EXIT_FAILURE);}return res;
}static int semaphore_v(int sem_id,int semIndex)
{struct sembuf sem_b;sem_b.sem_num = semIndex;sem_b.sem_op = 1; /* V() *///sem_b.sem_flg = SEM_UNDO;sem_b.sem_flg=IPC_NOWAIT; //0if (semop(sem_id, &sem_b, 1) == -1){perror("semop");return(0);}return(1);
}static int semaphore_p(int sem_id,int semIndex)
{struct sembuf sem_b;sem_b.sem_num = semIndex;sem_b.sem_op = -1; /* V() *///sem_b.sem_flg = SEM_UNDO;sem_b.sem_flg=IPC_NOWAIT;//0if (semop(sem_id, &sem_b, 1) == -1){perror("semop");return(0);}return(1);
}
void signalHandler(int sig)
{printf("child process exit\n");exit(EXIT_SUCCESS);
}
void semphorePV()
{pid_t childPid;int i;int value;key_t key;int status;int chlid_sem_id,parent_sem_id,semIndex=0,semValue=MAX_SEMAPHORES;if((childPid=fork())==-1){perror("fork");exit(EXIT_FAILURE);}else if(childPid==0){
#if 1struct sigaction sa;sa.sa_handler = signalHandler;sa.sa_flags = 0;	/* Interrupt system calls */sigemptyset(&sa.sa_mask);sigaction(SIGINT, &sa, NULL);sigaction(SIGQUIT, &sa, NULL);
#endifif((chlid_sem_id=semget((key_t)123456,1,IPC_CREAT|0770))==-1){perror("semget");exit(EXIT_FAILURE);}if (!set_semvalue(chlid_sem_id,semIndex,semValue)){fprintf(stderr, "Failed to initialize semaphore\n");exit(EXIT_FAILURE);}value=get_semvalue(chlid_sem_id,semIndex);printf("this is child,the current value is %d\n",value);if(!semaphore_v(chlid_sem_id,semIndex)){fprintf(stderr, "Failed to v operator\n");exit(EXIT_FAILURE);}value=get_semvalue(chlid_sem_id,semIndex);printf("child V operator,the current value is %d\n",value);printf("--------------------------------------------------\n");while(1){if(!semaphore_v(chlid_sem_id,semIndex)){fprintf(stderr, "Failed to v operator\n");exit(EXIT_FAILURE);}value=get_semvalue(chlid_sem_id,semIndex);printf("child V operator,the current value is %d\n",value);sleep(2);}printf("child exit success\n");exit(EXIT_SUCCESS);}else	//parent{sleep(1);if((parent_sem_id=semget((key_t)123456,1,IPC_CREAT|0770))==-1){perror("semget");exit(EXIT_FAILURE);}value=get_semvalue(parent_sem_id,semIndex);printf("this is parent ,the current value is %d\n",value);sleep(1);if(!semaphore_p(parent_sem_id,semIndex)){fprintf(stderr, "Failed to v operator\n");exit(EXIT_FAILURE);}printf("parent P operator,the current value is %d\n",value);printf("################################################################\n");while(1){if(!semaphore_p(parent_sem_id,semIndex)){fprintf(stderr, "Failed to v operator\n");break;}else{value=get_semvalue(parent_sem_id,semIndex);printf("parent P operator,the current value is %d\n",value);}sleep(1);}kill(childPid,SIGINT);sleep(2);if(semctl(parent_sem_id,0, IPC_RMID,(struct msquid_ds*)0)==-1){perror("semctl");exit(EXIT_FAILURE);}else{printf("the parent remove the sem\n");}return 0;}
}
int main(int argc,char *argv)
{semFunTest(argc,argv);semphorePV();
}int semFunTest(int argc,char *argv[]){int i, ret, semid;unsigned short sem_write_array[MAX_SEMAPHORES];unsigned short sem_read_array[MAX_SEMAPHORES];union semun arg;if((semid = semget( IPC_PRIVATE, MAX_SEMAPHORES,IPC_CREAT | 0666 )) == -1){perror("semget");exit(EXIT_FAILURE);}/* Initialize the sem_array */for (i = 0 ; i < MAX_SEMAPHORES ; i++ ){sem_write_array[i] = (unsigned short)(i+1);}/* Update the arg union with the sem_array address */arg.array = sem_write_array;/* Set the values of the semaphore-array */if((ret = semctl( semid, 0, SETALL, arg)) == -1){perror("SETALL failed\n");}/* Update the arg union with another array for read */arg.array = sem_read_array;/* Read the values of the semaphore array */if((ret = semctl( semid, 0, GETALL, arg)) == -1){printf("GETALL failed (%d)\n", errno);}/* print the sem_read_array */for ( i = 0 ; i < MAX_SEMAPHORES ; i++ ){printf("Semaphore %d, value %d\n", i, sem_read_array[i] );}printf("************************************************\n");/* Use GETVAL in a similar manner */for ( i = 0 ; i < MAX_SEMAPHORES ; i++ ){ret = semctl( semid, i, GETVAL );printf("Semaphore %d, value %d\n", i, ret );}/* Delete the semaphore */if(semctl(semid,0, IPC_RMID,(struct msquid_ds*)0)==-1){perror("semctl");exit(EXIT_FAILURE);}else{printf("remove the sem\n");}return 0;
}


2:执行结果




代码:

这篇关于linux 进程间通信--systemV 信号量 实例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/701647

相关文章

Linux之systemV共享内存方式

《Linux之systemV共享内存方式》:本文主要介绍Linux之systemV共享内存方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、工作原理二、系统调用接口1、申请共享内存(一)key的获取(二)共享内存的申请2、将共享内存段连接到进程地址空间3、将

快速修复一个Panic的Linux内核的技巧

《快速修复一个Panic的Linux内核的技巧》Linux系统中运行了不当的mkinitcpio操作导致内核文件不能正常工作,重启的时候,内核启动中止于Panic状态,该怎么解决这个问题呢?下面我们就... 感谢China编程(www.chinasem.cn)网友 鸢一雨音 的投稿写这篇文章是有原因的。为了配置完

Python如何精准判断某个进程是否在运行

《Python如何精准判断某个进程是否在运行》这篇文章主要为大家详细介绍了Python如何精准判断某个进程是否在运行,本文为大家整理了3种方法并进行了对比,有需要的小伙伴可以跟随小编一起学习一下... 目录一、为什么需要判断进程是否存在二、方法1:用psutil库(推荐)三、方法2:用os.system调用

Linux命令之firewalld的用法

《Linux命令之firewalld的用法》:本文主要介绍Linux命令之firewalld的用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux命令之firewalld1、程序包2、启动firewalld3、配置文件4、firewalld规则定义的九大

Java程序进程起来了但是不打印日志的原因分析

《Java程序进程起来了但是不打印日志的原因分析》:本文主要介绍Java程序进程起来了但是不打印日志的原因分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java程序进程起来了但是不打印日志的原因1、日志配置问题2、日志文件权限问题3、日志文件路径问题4、程序

Linux之计划任务和调度命令at/cron详解

《Linux之计划任务和调度命令at/cron详解》:本文主要介绍Linux之计划任务和调度命令at/cron的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux计划任务和调度命令at/cron一、计划任务二、命令{at}介绍三、命令语法及功能 :at

Linux下如何使用C++获取硬件信息

《Linux下如何使用C++获取硬件信息》这篇文章主要为大家详细介绍了如何使用C++实现获取CPU,主板,磁盘,BIOS信息等硬件信息,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录方法获取CPU信息:读取"/proc/cpuinfo"文件获取磁盘信息:读取"/proc/diskstats"文

Linux内核参数配置与验证详细指南

《Linux内核参数配置与验证详细指南》在Linux系统运维和性能优化中,内核参数(sysctl)的配置至关重要,本文主要来聊聊如何配置与验证这些Linux内核参数,希望对大家有一定的帮助... 目录1. 引言2. 内核参数的作用3. 如何设置内核参数3.1 临时设置(重启失效)3.2 永久设置(重启仍生效

Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案

《Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案》:本文主要介绍Vue3组件中getCurrentInstance()获取App实例,但是返回nu... 目录vue3组件中getCurrentInstajavascriptnce()获取App实例,但是返回n

SQL表间关联查询实例详解

《SQL表间关联查询实例详解》本文主要讲解SQL语句中常用的表间关联查询方式,包括:左连接(leftjoin)、右连接(rightjoin)、全连接(fulljoin)、内连接(innerjoin)、... 目录简介样例准备左外连接右外连接全外连接内连接交叉连接自然连接简介本文主要讲解SQL语句中常用的表