C++利用共享內(nèi)存實(shí)現(xiàn)start執(zhí)行之后已經(jīng)要執(zhí)行stop才能在執(zhí)行start,考試大把mian的源代碼貼出來說明這么個問題
int main(int argc, char *argv[])
{
int iShmId;
if( argc <= 1)
{
DisplayHelp();
}
else if( !strcmp( argv[1], "start" ) )
{
iShmId = shmget( ftok("dvrcollector",1), 1 , IPC_CREAT|IPC_EXCL|0666 );
if( -1 == iShmId && errno == EEXIST )
{
printf( "dvrcollector already running.\n");
return -1;
}
// printf( "start...\n");
StartDownFile();
}
else if( !strcmp( argv[1], "restart" ) )
{
iShmId = shmget( ftok("dvrcollector",1), 1 , IPC_CREAT|IPC_EXCL|0666 );
if( -1 == iShmId && errno == EEXIST )
{
StopDownFile();
// printf( "stop...\n");
sleep(1);
}
// printf( "start...\n");
StartDownFile();
}
else if( !strcmp( argv[1], "stop" ) )
{
iShmId = shmget( ftok("dvrcollector",1), 1 , IPC_CREAT|IPC_EXCL|0666 );
if( -1 == iShmId && errno == EEXIST )
{
if( -1 != (iShmId = shmget( ftok("dvrcollector",1), 1 , IPC_CREAT | 0666 ) ) )
shmctl( iShmId, IPC_RMID, NULL);
// printf( "stop...\n");
StopDownFile();
}
else
{
shmctl( iShmId, IPC_RMID, NULL );
printf( "dvrcollector not running.\n" );
}
}
else
{
DisplayHelp();
}
return 0;
}
int main(int argc, char *argv[])
{
int iShmId;
if( argc <= 1)
{
DisplayHelp();
}
else if( !strcmp( argv[1], "start" ) )
{
iShmId = shmget( ftok("dvrcollector",1), 1 , IPC_CREAT|IPC_EXCL|0666 );
if( -1 == iShmId && errno == EEXIST )
{
printf( "dvrcollector already running.\n");
return -1;
}
// printf( "start...\n");
StartDownFile();
}
else if( !strcmp( argv[1], "restart" ) )
{
iShmId = shmget( ftok("dvrcollector",1), 1 , IPC_CREAT|IPC_EXCL|0666 );
if( -1 == iShmId && errno == EEXIST )
{
StopDownFile();
// printf( "stop...\n");
sleep(1);
}
// printf( "start...\n");
StartDownFile();
}
else if( !strcmp( argv[1], "stop" ) )
{
iShmId = shmget( ftok("dvrcollector",1), 1 , IPC_CREAT|IPC_EXCL|0666 );
if( -1 == iShmId && errno == EEXIST )
{
if( -1 != (iShmId = shmget( ftok("dvrcollector",1), 1 , IPC_CREAT | 0666 ) ) )
shmctl( iShmId, IPC_RMID, NULL);
// printf( "stop...\n");
StopDownFile();
}
else
{
shmctl( iShmId, IPC_RMID, NULL );
printf( "dvrcollector not running.\n" );
}
}
else
{
DisplayHelp();
}
return 0;
}