- 论坛徽章:
- 0
|
unix下多进程实现问题
发一个我以前写的程序,在生产上用的,看对你有没有帮助
- void main( int argc, char **argv )
- {
- EXEC SQL BEGIN DECLARE SECTION;
- long pid, secuid;
- EXEC SQL END DECLARE SECTION;
- int ret, i, p_stat;
- char profile[FILENAME_MAX], tmp[40];
- struct Process *proc;
- if( (proc=GetProcess( (char *)getenv("LOGNAME"), argv[0], &i))
- !=(struct Process *)NULL && i>;1 )
- {
- printf( "Other process %s is running!\n", argv[0] );
- while( i>;0 ){
- if( proc[i-1].pid!=getpid() )
- printf( "pid=[%d]\n", proc[i-1].pid );
- i--;
- }
- exit( -1 );
- }
- if( argc>;=2 && atoi(argv[1])!=0 )
- t_interval=atoi( argv[1] );
- else {
- sprintf( profile, "%s/etc/%s", (char *)getenv("HOME"), PROFILE );
- if( GetProfileString( profile, "LOCALE INFO",
- "reverse_interval", tmp )==0 )
- {
- t_interval=atoi(tmp);
- } else
- t_interval=30;
- }
- signal(SIGINT,SIG_IGN);
- signal(SIGPIPE,SIG_IGN);
- signal(SIGQUIT,SIG_IGN);
- if (( pid=fork() )<0) {
- printf("REVERSE SERVER START FAILED!!!\n");
- exit(-1);
- } else if (pid>;0 )
- exit(0);
- if(setpgrp()<0) {
- printf("CHANGE GROUP ERROR!!!\n");
- fflush(stdout);
- exit(-1);
- }
- signal(SIGHUP,SIG_IGN);
- signal(SIGTERM,EXIT);
- EXEC SQL database secudb;
- if( SQLCODE!=0 ){
- printf( "OPEN DATABASE ERROR[%d]! EXIT\n", SQLCODE );
- exit( -1 );
- }
- EXEC SQL declare revcur cursor for
- select unique(secuid) from reverse
- where trans_date=today
- and result NOT in ( "11", "10", "01" );
- if( SQLCODE!=0 ){
- printf( "DATABASE ERROR[%d]! EXIT\n", SQLCODE );
- exit( -1 );
- }
- printf("AUTO REVERSE SERVER START OK!!!\n");
- fflush(stdout);
- while( 1 ) {
- sleep( t_interval );
- EXEC SQL open revcur;
- if( SQLCODE!=0 ) {
- SDKerrlog( REVERSELOG, "%s|%d| open cursor error[%d]",
- __FILE__, __LINE__, SQLCODE );
- continue;
- }
- p_head=(struct rev_t*)NULL;
- p=p_head;
- p_prev=NULL;
- p_next=NULL;
- i=0;
- while( 1 ) {
- EXEC SQL fetch revcur into :secuid;
- if( SQLCODE!=0 ){
- if( SQLCODE==100 ) {
- EXEC SQL close revcur;
- break;
- } else {
- SDKerrlog( REVERSELOG, "%s|%d| fetch \
- next error[%d]", __FILE__, __LINE__, SQLCODE );
- EXEC SQL close revcur;
- break;
- }
- }
- if( (pid=fork())<0 ) {
- SDKerrlog( REVERSELOG, "%s|%d| Create new process \
- error[%s] secuid[%d]", __FILE__, __LINE__, strerror(errno), secuid );
- continue;
- }
- if( pid>;0 ) { /* main process */
- if( RegistProc( pid, secuid )<0 ){
- SDKerrlog( REVERSELOG, "%s|%d| regist \
- process[%d] secuid[%d] error", __FILE__, __LINE__, pid, secuid );
- continue;
- }
- i++;
- } else {/* sub process */
- SubReverse( secuid );
- exit( 1 );
- }
- }
- SDKerrlog( REVERSELOG, "%s|%d| Total [%d] reverse process. MainProcess[%d]",
- __FILE__, __LINE__, i, getpid() );
- while( i>;0 ) {
- if( (pid=wait( &p_stat ))<0 ) {
- SDKerrlog( REVERSELOG, "%s|%d| wait error[%s]",
- __FILE__, __LINE__, strerror(errno) );
- continue;
- }
- SDKerrlog( REVERSELOG, "%s|%d| process[%d] finished",
- __FILE__, __LINE__, pid );
- if( ReleRevProc( pid )<0 )
- continue;
- i--;
- }
- ReleRevTbl();
- }
- }
复制代码
里面有些自定义函数,可以忽略 |
|