消费者通过注册信号处理函数,在退出时向共享内存中添加状态标记,生产者通过此标记确定消费者是否在运行。删除信号量相关代码
This commit is contained in:
@@ -18,22 +18,8 @@
|
|||||||
|
|
||||||
#define MESA_SEM_KEY 35719
|
#define MESA_SEM_KEY 35719
|
||||||
|
|
||||||
|
#define MESA_CONSUMER_RUNNING 1
|
||||||
struct MESA_shm_overview *MESA_shm_alloc_overview();
|
#define MESA_CONSUMER_NOT_RUNNING 0
|
||||||
struct MESA_shm_queue_head *MESA_shm_get_ring_queue();
|
|
||||||
void MESA_shm_init_mutex();
|
|
||||||
void MESA_shm_init_overview();
|
|
||||||
void MESA_shm_recycle_ring_queue(struct MESA_shm_queue_head *ring_queue_head);
|
|
||||||
int MESA_shm_copy_buf_to_ring_queue(char *buf, int buflen, struct MESA_shm_queue_head *head, char *log_file, int log_file_len);
|
|
||||||
int MESA_shm_ring_queue_is_empty(struct MESA_shm_queue_head *head);
|
|
||||||
int MESA_shm_ring_queue_is_full(struct MESA_shm_queue_head *head);
|
|
||||||
void MESA_shm_ring_queue_set_empty(struct MESA_shm_queue_head *head);
|
|
||||||
int MESA_shm_alloc_sem();
|
|
||||||
int MESA_shm_consumer_is_running();
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
struct MESA_shm_overview{
|
struct MESA_shm_overview{
|
||||||
int shmkey;
|
int shmkey;
|
||||||
@@ -51,6 +37,18 @@ struct MESA_shm_queue_head{
|
|||||||
int ovw_idx;
|
int ovw_idx;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
int MESA_shm_alloc_overview(struct MESA_shm_overview **ovw, int **consumer_status);
|
||||||
|
struct MESA_shm_queue_head *MESA_shm_get_ring_queue();
|
||||||
|
void MESA_shm_init_mutex();
|
||||||
|
void MESA_shm_init_overview();
|
||||||
|
void MESA_shm_recycle_ring_queue(struct MESA_shm_queue_head *ring_queue_head);
|
||||||
|
int MESA_shm_copy_buf_to_ring_queue(char *buf, int buflen, struct MESA_shm_queue_head *head, char *log_file, int log_file_len);
|
||||||
|
int MESA_shm_ring_queue_is_empty(struct MESA_shm_queue_head *head);
|
||||||
|
int MESA_shm_ring_queue_is_full(struct MESA_shm_queue_head *head);
|
||||||
|
void MESA_shm_ring_queue_set_empty(struct MESA_shm_queue_head *head);
|
||||||
|
int MESA_shm_get_consumer_status();
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <sys/ipc.h>
|
#include <sys/ipc.h>
|
||||||
#include <sys/shm.h>
|
#include <sys/shm.h>
|
||||||
#include <sys/sem.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
@@ -10,8 +9,8 @@
|
|||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
#include <signal.h>
|
||||||
#include "list.h"
|
#include "list.h"
|
||||||
|
|
||||||
#include "MESA_shm_ring_queue.h"
|
#include "MESA_shm_ring_queue.h"
|
||||||
|
|
||||||
#define DEFAUT_BUF_SIZE 256
|
#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;
|
struct care_pid_list g_care_pid_list;
|
||||||
int g_output_mode = CONSUMER_OUTPUT_MODE_FILE;
|
int g_output_mode = CONSUMER_OUTPUT_MODE_FILE;
|
||||||
int g_care_pid = CONSUMER_CARE_PID_ALL;
|
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)
|
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);
|
pclose(fp);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
void MESA_shm_sem_p(int semid)
|
void signal_handler_exit(int signum)
|
||||||
{
|
{
|
||||||
if(g_semid == -1){
|
if(g_status == NULL){
|
||||||
return ;
|
exit(0);
|
||||||
}
|
}
|
||||||
struct sembuf sem_b;
|
*g_status = MESA_CONSUMER_NOT_RUNNING;
|
||||||
sem_b.sem_num = 0;
|
exit(0);
|
||||||
sem_b.sem_op = -1;
|
}
|
||||||
sem_b.sem_flg = SEM_UNDO;
|
void register_sginal_handler()
|
||||||
semop(semid, &sem_b, 1);
|
{
|
||||||
|
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 ;
|
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)
|
void print_help(char *exe_name)
|
||||||
{
|
{
|
||||||
printf("-pid default all, optional parameter\n");
|
printf("-pid default all, optional parameter\n");
|
||||||
@@ -567,17 +562,18 @@ int main(int argc, char **argv)
|
|||||||
}else{
|
}else{
|
||||||
g_cur_tty_fd = get_cur_tty_fd();
|
g_cur_tty_fd = get_cur_tty_fd();
|
||||||
}
|
}
|
||||||
g_semid = MESA_shm_alloc_sem();
|
|
||||||
MESA_shm_sem_p(g_semid);
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
int ret = 0;
|
||||||
struct MESA_shm_overview *shm_overview = NULL;
|
struct MESA_shm_overview *shm_overview = NULL;
|
||||||
struct MESA_shm_overview *tmp_ovw = NULL;
|
struct MESA_shm_overview *tmp_ovw = NULL;
|
||||||
struct MESA_shm_queue_head *ring_queue_head[MESA_SHM_RING_QUEUE_NUM] = {NULL};
|
struct MESA_shm_queue_head *ring_queue_head[MESA_SHM_RING_QUEUE_NUM] = {NULL};
|
||||||
INIT_LIST_HEAD(&g_log_file_list.list);
|
INIT_LIST_HEAD(&g_log_file_list.list);
|
||||||
shm_overview = MESA_shm_alloc_overview();
|
ret = MESA_shm_alloc_overview(&shm_overview, &g_status);
|
||||||
if(shm_overview == NULL){
|
if(ret < 0){
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
*g_status = MESA_CONSUMER_RUNNING;
|
||||||
|
register_sginal_handler();
|
||||||
init_ring_queue_head_arrray(shm_overview, ring_queue_head);
|
init_ring_queue_head_arrray(shm_overview, ring_queue_head);
|
||||||
while(1){
|
while(1){
|
||||||
for(i = 0; i< MESA_SHM_RING_QUEUE_NUM; i++){
|
for(i = 0; i< MESA_SHM_RING_QUEUE_NUM; i++){
|
||||||
|
|||||||
@@ -26,10 +26,6 @@ static char tmp_conf_filepath[MAX_HANDLE_LOG_PATH] = "";
|
|||||||
pthread_key_t MESA_pthread_key;
|
pthread_key_t MESA_pthread_key;
|
||||||
pthread_once_t MESA_pthread_key_once = PTHREAD_ONCE_INIT;
|
pthread_once_t MESA_pthread_key_once = PTHREAD_ONCE_INIT;
|
||||||
|
|
||||||
#define MESA_CONSUMER_RUNNING 1
|
|
||||||
#define MESA_CONSUMER_NOT_RUNNING 0
|
|
||||||
|
|
||||||
int MESA_consumer_status = MESA_CONSUMER_NOT_RUNNING;
|
|
||||||
|
|
||||||
struct MESA_pthread_private{
|
struct MESA_pthread_private{
|
||||||
char *fmt_buf;
|
char *fmt_buf;
|
||||||
@@ -265,21 +261,10 @@ void MESA_free_pthread_private(void *arg)
|
|||||||
}
|
}
|
||||||
return ;
|
return ;
|
||||||
}
|
}
|
||||||
void *MESA_check_consumer_status(void *arg)
|
|
||||||
{
|
|
||||||
while(1){
|
|
||||||
MESA_consumer_status = MESA_shm_consumer_is_running();
|
|
||||||
sleep(1);
|
|
||||||
};
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
void MESA_alloc_pthread_key()
|
void MESA_alloc_pthread_key()
|
||||||
{
|
{
|
||||||
pthread_key_create(&MESA_pthread_key, MESA_free_pthread_private);
|
pthread_key_create(&MESA_pthread_key, MESA_free_pthread_private);
|
||||||
MESA_shm_init_overview();
|
MESA_shm_init_overview();
|
||||||
pthread_t tid;
|
|
||||||
pthread_create(&tid, NULL, MESA_check_consumer_status, NULL);
|
|
||||||
pthread_detach(tid);
|
|
||||||
return ;
|
return ;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -344,7 +329,7 @@ void MESA_destroy_runtime_log_handle(void *handle)
|
|||||||
|
|
||||||
void MESA_handle_runtime_log(void *handle, int level, const char *module, const char *fmt, ...)
|
void MESA_handle_runtime_log(void *handle, int level, const char *module, const char *fmt, ...)
|
||||||
{
|
{
|
||||||
if(MESA_consumer_status == MESA_CONSUMER_NOT_RUNNING){
|
if(MESA_shm_get_consumer_status() == MESA_CONSUMER_NOT_RUNNING){
|
||||||
return ;
|
return ;
|
||||||
}
|
}
|
||||||
log_handle_t *p_handle = (log_handle_t *)handle;
|
log_handle_t *p_handle = (log_handle_t *)handle;
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <sys/ipc.h>
|
#include <sys/ipc.h>
|
||||||
#include <sys/shm.h>
|
#include <sys/shm.h>
|
||||||
#include <sys/sem.h>
|
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -14,14 +13,7 @@
|
|||||||
|
|
||||||
|
|
||||||
struct MESA_shm_overview *MESA_shm_ovw = NULL;
|
struct MESA_shm_overview *MESA_shm_ovw = NULL;
|
||||||
int MESA_shm_semid = -1;
|
int *MESA_consumer_status = NULL;
|
||||||
union semun {
|
|
||||||
int val;
|
|
||||||
struct semid_ds *buf;
|
|
||||||
unsigned short *array;
|
|
||||||
struct seminfo *__buf;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
void MESA_shm_init_ring_queue_mutex(struct MESA_shm_overview *ovw)
|
void MESA_shm_init_ring_queue_mutex(struct MESA_shm_overview *ovw)
|
||||||
{
|
{
|
||||||
@@ -33,34 +25,17 @@ void MESA_shm_init_ring_queue_mutex(struct MESA_shm_overview *ovw)
|
|||||||
return ;
|
return ;
|
||||||
}
|
}
|
||||||
|
|
||||||
int MESA_shm_alloc_sem()
|
int MESA_shm_get_consumer_status()
|
||||||
{
|
{
|
||||||
union semun semopt;
|
if(MESA_consumer_status == NULL){
|
||||||
int semid = semget(MESA_SEM_KEY, 0 , 0);
|
return MESA_CONSUMER_NOT_RUNNING;
|
||||||
if(semid == -1){
|
|
||||||
semid = semget(MESA_SEM_KEY, 1 ,IPC_CREAT | 0666);
|
|
||||||
if(semid == -1){
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
semopt.val = 1;
|
|
||||||
semctl(semid, 0, SETVAL, semopt);
|
|
||||||
}
|
}
|
||||||
return semid;
|
return *MESA_consumer_status;
|
||||||
}
|
}
|
||||||
|
|
||||||
int MESA_shm_consumer_is_running()
|
int MESA_shm_alloc_overview(struct MESA_shm_overview **ovw, int **consumer_status)
|
||||||
{
|
{
|
||||||
if(MESA_shm_semid == -1){
|
int shmsize = (sizeof(struct MESA_shm_overview) * MESA_SHM_RING_QUEUE_NUM) + sizeof(int);
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
int semval = semctl(MESA_shm_semid, 0 ,GETVAL, 0);
|
|
||||||
return !semval ;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
struct MESA_shm_overview *MESA_shm_alloc_overview()
|
|
||||||
{
|
|
||||||
int shmsize = sizeof(struct MESA_shm_overview) * MESA_SHM_RING_QUEUE_NUM;
|
|
||||||
struct MESA_shm_overview *shm_overview = NULL;
|
struct MESA_shm_overview *shm_overview = NULL;
|
||||||
struct MESA_shm_overview *tmp_ovw = NULL;
|
struct MESA_shm_overview *tmp_ovw = NULL;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
@@ -68,11 +43,11 @@ struct MESA_shm_overview *MESA_shm_alloc_overview()
|
|||||||
if(shmid == -1){
|
if(shmid == -1){
|
||||||
shmid = shmget(MESA_SHM_KEY_OVERVIEW, shmsize, (SHM_R|SHM_W|IPC_CREAT));
|
shmid = shmget(MESA_SHM_KEY_OVERVIEW, shmsize, (SHM_R|SHM_W|IPC_CREAT));
|
||||||
if(shmid == -1){
|
if(shmid == -1){
|
||||||
return NULL;
|
return -1;
|
||||||
}else{
|
}else{
|
||||||
shm_overview = (struct MESA_shm_overview *)shmat(shmid, NULL, 0);
|
shm_overview = (struct MESA_shm_overview *)shmat(shmid, NULL, 0);
|
||||||
if(shm_overview == (struct MESA_shm_overview *)-1){
|
if(shm_overview == (struct MESA_shm_overview *)-1){
|
||||||
return NULL;
|
return -1;
|
||||||
}
|
}
|
||||||
memset((void *)shm_overview, 0, shmsize);
|
memset((void *)shm_overview, 0, shmsize);
|
||||||
for(i = 0; i < MESA_SHM_RING_QUEUE_NUM; i++){
|
for(i = 0; i < MESA_SHM_RING_QUEUE_NUM; i++){
|
||||||
@@ -82,21 +57,24 @@ struct MESA_shm_overview *MESA_shm_alloc_overview()
|
|||||||
tmp_ovw->idx = i;
|
tmp_ovw->idx = i;
|
||||||
MESA_shm_init_ring_queue_mutex(tmp_ovw);
|
MESA_shm_init_ring_queue_mutex(tmp_ovw);
|
||||||
}
|
}
|
||||||
|
*ovw = shm_overview;
|
||||||
|
*consumer_status = (int *)(shm_overview + MESA_SHM_RING_QUEUE_NUM);
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
shm_overview = (struct MESA_shm_overview *)shmat(shmid, NULL, 0);
|
shm_overview = (struct MESA_shm_overview *)shmat(shmid, NULL, 0);
|
||||||
if(shm_overview == (struct MESA_shm_overview *)-1){
|
if(shm_overview == (struct MESA_shm_overview *)-1){
|
||||||
return NULL;
|
return -1;
|
||||||
}
|
}
|
||||||
|
*ovw = shm_overview;
|
||||||
|
*consumer_status = (int *)(shm_overview + MESA_SHM_RING_QUEUE_NUM);
|
||||||
}
|
}
|
||||||
return shm_overview;
|
return 0;
|
||||||
}
|
}
|
||||||
void MESA_shm_init_overview()
|
void MESA_shm_init_overview()
|
||||||
{
|
{
|
||||||
if(MESA_shm_ovw == NULL){
|
if(MESA_shm_ovw == NULL || MESA_consumer_status == NULL){
|
||||||
MESA_shm_ovw = MESA_shm_alloc_overview();
|
MESA_shm_alloc_overview(&MESA_shm_ovw, &MESA_consumer_status);
|
||||||
}
|
}
|
||||||
MESA_shm_semid = MESA_shm_alloc_sem();
|
|
||||||
return ;
|
return ;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user