提交环形缓冲区添加代码

This commit is contained in:
guo_peixu
2022-06-10 16:14:32 +08:00
parent 65ed1c3a92
commit d7b64b4577
4 changed files with 382 additions and 0 deletions

View File

@@ -0,0 +1,99 @@
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "MESA_shm_ring_queue.h"
void init_ring_queue_head_arrray(struct MESA_shm_overview *ovw, struct MESA_shm_queue_head **ring_queue_head)
{
int i = 0;
struct MESA_shm_overview *tmp_ovw = NULL;
for(i = 0; i< MESA_SHM_RING_QUEUE_NUM; i++){
tmp_ovw = ovw + i;
if(tmp_ovw->shmid == -1){
break;
}
ring_queue_head[i] = shmat(tmp_ovw->shmid, NULL, 0);
}
return ;
}
void consumer_daemo()
{
int fd = -1;
switch (fork()) {
case -1:
printf("fork error\n");
return ;
case 0:
break;
default:
exit(0);
}
if (setsid() == -1) {
return ;
}
umask(0);
fd = open("/dev/null", O_RDWR);
if (fd == -1) {
return ;
}
if (dup2(fd, STDIN_FILENO) == -1) {
return ;
}
if (dup2(fd, STDOUT_FILENO) == -1) {
return ;
}
if (fd > STDERR_FILENO) {
if (close(fd) == -1) {
return ;
}
}
return ;
}
int main(int argc, char **args)
{
consumer_daemo();
int i = 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};
int log_file_fd = -1;
shm_overview = MESA_shm_alloc_overview();
if(shm_overview == NULL){
return 0;
}
init_ring_queue_head_arrray(shm_overview, ring_queue_head);
log_file_fd = open("/root/MESA_log", O_RDWR | O_CREAT | O_APPEND, 0666);
if(log_file_fd < 0){
return 0;
}
while(1){
for(i = 0; i< MESA_SHM_RING_QUEUE_NUM; i++){
tmp_ovw = shm_overview + i;
if(tmp_ovw->shmid == -1){
break;
}
if(ring_queue_head[i] == NULL){
ring_queue_head[i] = shmat(tmp_ovw->shmid, NULL, 0);
if(ring_queue_head[i] == NULL){
break ;
}
}
if(tmp_ovw->status == MESA_SHM_RING_QUEUE_USED || tmp_ovw->status == MESA_SHM_RING_QUEUE_HALF_IDLE){
MESA_shm_write_ring_queue_to_file(log_file_fd, ring_queue_head[i]);
if(tmp_ovw->status == MESA_SHM_RING_QUEUE_HALF_IDLE){
tmp_ovw->status = MESA_SHM_RING_QUEUE_IDLE;
}
}
}
usleep(5000);
}
return 0;
}

26
shm_consumer/Makefile Normal file
View File

@@ -0,0 +1,26 @@
vpath %.h ../inc
vpath %.a ../lib
CC=gcc
CFLAGS= -g3 -Wall -fPIC -O -Werror
CFLAGS+=-I../inc/
#LIB=-L../lib/
LIB=../lib/libMESA_handle_logger.a ../lib/libMESA_snprintf.a
LIB+=-lpthread
LIB_FILE=$(wildcard ../lib/*.a)
SRC=MESA_shm_consumer.c
TARGET=MESA_shm_consumer
all:$(TARGET)
$(TARGET):$(SRC) $(LIB_FILE)
$(CC) $(CFLAGS) $(INC) $(LIBPATH) $< $(LIB) -o $@
clean :
rm -f $(TARGET)