提交环形缓冲区添加代码
This commit is contained in:
99
shm_consumer/MESA_shm_consumer.c
Normal file
99
shm_consumer/MESA_shm_consumer.c
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user