C++利用shmget实现start,stop交替执行
++使用共享内存实现启动。执行启动后,必须先停止,然后才能启动。考试贴了很多mian的源码解释这个问题
int main (int argc,char * argv[])
{
int ish mid;
if(argc {
display help();
}
else if(!strcmp( argv[1]," start ")
{
ish mid = shmget(ftok(" DVR collector ",1),1,IPC _ CREAT | IPC _ EXCL | 0666);
if(-1 = = ish mid & & errno = = e exist)
{
printf(" DVR collector已经在运行。\ n ");
return-1;
}
// printf( "start...\ n ");
StartDownFile();
}
else if(!strcmp( argv[1]," restart ")
{
ish mid = shmget(ftok(" DVR collector ",1),1,IPC _ CREAT | IPC _ EXCL | 0666);
if(-1 = = ish mid & & errno = = e exist)
{
stop downfile();
// printf( "stop...\ n ");
睡眠(1);
}
// printf( "start...\ n ");
StartDownFile();
}
else if(!strcmp( argv[1]," stop ")
{
ish mid = shmget(ftok(" DVR collector ",1),1,IPC _ CREAT | IPC _ EXCL | 0666);
if(-1 = = ish mid & & errno = = e exist)
{
if(-1!=(ish mid = shm get(ftok(" DVR collector ",1),1,IPC _ CREAT | 0666))
shm CTL(ish mid,IPC_RMID,NULL);
// printf( "stop...\ n ");
stop downfile();
}
else
{
shm CTL(ish mid,IPC_RMID,NULL);
printf( "dvrcollector没有运行。\ n ");
}
}
else
{
display help();
}
返回0;
}
0条评论