我试图在Linux上实现一个库,以检查正在运行的程序的更多实例。
程序获取正在运行的程序的可执行文件名称,并将其与程序PID一起保存到二进制文件(.existence
)中。
最近,在程序运行之前(再次),通过检查是否存在.existence
文件来检查该程序的实例是否存在,如果存在,则进行比较。
一切正常,在codeblocks
中可以正常工作,但是当我在命令行中运行它时,返回程序PID的函数(int find_pid ( const char *const instance )
)返回0
。
这是程序的一部分,可以对其进行编译和测试:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
void write_bin_file ( const char *const fileName, const char *const msg );
char *read_bin_File ( const char *const fileName );
char *my_strtok ( char *msg );
int find_pid ( const char *const instance );
int main ( int argc, char *argv[] )
{
char instance[ strlen( argv[0] ) + 1 ];
memset( instance, '\0', sizeof( instance ) );
if ( argc < 2 )
{
strcpy( instance, argv[0] );
char *buffer = my_strtok( instance );
write_bin_file ( ".existence", buffer );
char *ret = read_bin_File( ".existence" );
printf( "\n%s\n", ret );
printf("Actc_pid =\t%d\n", getpid() );
printf("Find_pid =\t%d\n", find_pid ( buffer ) );
free( ret );
}
sleep( 5 );
}
char *read_bin_File ( const char *const fileName )
{
FILE *fp = fopen( fileName, "rb" );
if ( fp == NULL )
{
return NULL;
}
unsigned short len = 0;
fread( &len, sizeof(unsigned short), 1, fp);
char *buffer = malloc( sizeof( *buffer ) * len );
fread( buffer, sizeof( *buffer ), len, fp );
fclose(fp);
return buffer;
}
void write_bin_file( const char *const fileName, const char *const msg )
{
FILE *fp = fopen( fileName, "wb");
if ( fp == NULL )
{
printf( "Error, write_bin_file()\n" );
exit( EXIT_FAILURE );
}
size_t len = strlen( msg ) + 1;
fwrite( &len, sizeof( unsigned short ), 1, fp);
fwrite( msg, sizeof(char), len, fp);
fclose( fp );
}
int find_pid ( const char *const instance )
{
char line[256];
memset( line, '\0', sizeof( *line ) );
char buffer[555];
memset( buffer, '\0', sizeof( *buffer ) );
sprintf( buffer, "ps -A | grep %s", instance );
FILE *cmd = popen( buffer, "r");
fgets( line, 256, cmd);
pid_t pid = (pid_t)strtoul(line, NULL, 10);
pclose( cmd );
return pid;
}
char *my_strtok( char *msg )
{
char *ret = NULL;
char *tmp = strtok( msg, "/" );
while ( tmp != NULL )
{
ret = tmp;
tmp = strtok( NULL, "/");
}
if ( ret == NULL )
{
return NULL;
}
return ret;
}
代码块输出:
program
Actc_pid = 11890
Find_pid = 11890
命令行输出:
program
Actc_pid = 11900
Find_pid = 0
Valgrind:
valgrind --leak-check=full --track-origins=yes --show-leak-kinds=all Templates/Program/bin/Debug/Program
==12414== Memcheck, a memory error detector
==12414== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==12414== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==12414== Command: Templates/Program/bin/Debug/Program
==12414==
Program
Actc_pid = 12414
Find_pid = 0
==12414==
==12414== HEAP SUMMARY:
==12414== in use at exit: 0 bytes in 0 blocks
==12414== total heap usage: 8 allocs, 8 frees, 14,686 bytes allocated
==12414==
==12414== All heap blocks were freed -- no leaks are possible
==12414==
==12414== For counts of detected and suppressed errors, rerun with: -v
==12414== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)