消费者通过注册信号处理函数,在退出时向共享内存中添加状态标记,生产者通过此标记确定消费者是否在运行。删除信号量相关代码

This commit is contained in:
guo_peixu
2022-06-29 18:24:51 +08:00
parent dab9b5c03d
commit a5bffd2fe7
4 changed files with 56 additions and 99 deletions

View File

@@ -1,7 +1,6 @@
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
@@ -10,8 +9,8 @@
#include <fcntl.h>
#include <time.h>
#include <ctype.h>
#include <signal.h>
#include "list.h"
#include "MESA_shm_ring_queue.h"
#define DEFAUT_BUF_SIZE 256
@@ -44,7 +43,7 @@ struct log_file_list g_log_file_list;
struct care_pid_list g_care_pid_list;
int g_output_mode = CONSUMER_OUTPUT_MODE_FILE;
int g_care_pid = CONSUMER_CARE_PID_ALL;
int g_semid = -1;
int *g_status = NULL;
void init_ring_queue_head_arrray(struct MESA_shm_overview *ovw, struct MESA_shm_queue_head **ring_queue_head)
{
@@ -513,31 +512,27 @@ error:
pclose(fp);
return -1;
}
void MESA_shm_sem_p(int semid)
void signal_handler_exit(int signum)
{
if(g_semid == -1){
return ;
if(g_status == NULL){
exit(0);
}
struct sembuf sem_b;
sem_b.sem_num = 0;
sem_b.sem_op = -1;
sem_b.sem_flg = SEM_UNDO;
semop(semid, &sem_b, 1);
*g_status = MESA_CONSUMER_NOT_RUNNING;
exit(0);
}
void register_sginal_handler()
{
signal(SIGKILL, signal_handler_exit);
signal(SIGTRAP, signal_handler_exit);
signal(SIGABRT, signal_handler_exit);
signal(SIGBUS, signal_handler_exit);
signal(SIGFPE, signal_handler_exit);
signal(SIGSEGV, signal_handler_exit);
signal(SIGTERM, signal_handler_exit);
signal(SIGINT, signal_handler_exit);
signal(SIGQUIT, signal_handler_exit);
return ;
}
void MESA_shm_sem_v(int semid)
{
if(g_semid == -1){
return ;
}
struct sembuf sem_b;
sem_b.sem_num = 0;
sem_b.sem_op = 1;
sem_b.sem_flg = SEM_UNDO;
semop(g_semid, &sem_b, 1);
return ;
}
void print_help(char *exe_name)
{
printf("-pid default all, optional parameter\n");
@@ -567,17 +562,18 @@ int main(int argc, char **argv)
}else{
g_cur_tty_fd = get_cur_tty_fd();
}
g_semid = MESA_shm_alloc_sem();
MESA_shm_sem_p(g_semid);
int i = 0;
int ret = 0;
struct MESA_shm_overview *shm_overview = NULL;
struct MESA_shm_overview *tmp_ovw = NULL;
struct MESA_shm_queue_head *ring_queue_head[MESA_SHM_RING_QUEUE_NUM] = {NULL};
INIT_LIST_HEAD(&g_log_file_list.list);
shm_overview = MESA_shm_alloc_overview();
if(shm_overview == NULL){
ret = MESA_shm_alloc_overview(&shm_overview, &g_status);
if(ret < 0){
return 0;
}
*g_status = MESA_CONSUMER_RUNNING;
register_sginal_handler();
init_ring_queue_head_arrray(shm_overview, ring_queue_head);
while(1){
for(i = 0; i< MESA_SHM_RING_QUEUE_NUM; i++){