消费者进程启动时,检查是否存在已启动的消费者进程,避免重复启动

This commit is contained in:
guo_peixu
2022-06-16 17:11:34 +08:00
parent 21b84aa885
commit 0ce7424fa1

View File

@@ -10,6 +10,7 @@
#include "MESA_shm_ring_queue.h"
#define BUFSIZE 256
void init_ring_queue_head_arrray(struct MESA_shm_overview *ovw, struct MESA_shm_queue_head **ring_queue_head)
{
int i = 0;
@@ -57,8 +58,50 @@ void consumer_daemo()
}
return ;
}
int main(int argc, char **args)
int consumer_is_running(char *process_name)
{
FILE *fp = NULL;
char buf[BUFSIZE] = {0};
int count = 0;
char command[BUFSIZE] = {0};
if(process_name == NULL){
return 0;
}
snprintf(command, sizeof(command), "ps -ef | grep %s | grep -v grep | wc -l", process_name);
fp = popen(command, "r");
if(fp == NULL){
return 0;
}
if((fgets(buf, sizeof(buf), fp)) != NULL){
count = atoi(buf);
}
pclose(fp);
if(count > 1){
return 1;
}else{
return 0;
}
return 0;
}
char *get_exe_name(char *argv)
{
char * p = NULL;
p = rindex(argv, '/');
if(p == NULL){
p = argv;
}else{
p = p + 1;
}
return p;
}
int main(int argc, char **argv)
{
char *exe_name = get_exe_name(argv[0]);
if(consumer_is_running(exe_name)){
printf("consumer process is already running\n");
return 0;
}
consumer_daemo();
int i = 0;
struct MESA_shm_overview *shm_overview = NULL;