打印本文 打印本文  关闭窗口 关闭窗口  
C++利用shmget实现start,stop交替执行
作者:佚名  文章来源:不详  点击数  更新时间:2008/11/1 20:41:32  文章录入:杜斌  责任编辑:杜斌

  C++利用共享内存实现start执行之后已经要执行stop才能在执行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;
  }
打印本文 打印本文  关闭窗口 关闭窗口