📄 nameshm1.c
字号:
PRStatus rc; PRSem *sem1, *sem2; PRSharedMemory *shm; PRUint32 *addr; PRInt32 i; PR_LOG( lm, msgLevel, ("nameshm1: DoClient(): Starting")); sem1 = PR_OpenSemaphore( SEM_NAME1, 0, 0, 0 ); PR_ASSERT( sem1 ); sem2 = PR_OpenSemaphore( SEM_NAME2, 0, 0, 0 ); PR_ASSERT( sem1 ); shm = PR_OpenSharedMemory( optName, optSize, 0, SHM_MODE ); if ( NULL == shm ) { PR_LOG( lm, msgLevel, ( "nameshm1: DoClient(): Create: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError())); failed_already = 1; return; } PR_LOG( lm, msgLevel, ( "nameshm1: DoClient(): Create: success: %p", shm )); addr = PR_AttachSharedMemory( shm , 0 ); if ( NULL == addr ) { PR_LOG( lm, msgLevel, ( "nameshm1: DoClient(): Attach: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError())); failed_already = 1; return; } PR_LOG( lm, msgLevel, ( "nameshm1: DoClient(): Attach: success: %p", addr )); PR_LOG( lm, msgLevel, ( "Client found: %s", addr)); PR_Sleep(PR_SecondsToInterval(4)); for ( i = 0 ; i < optPing ; i++ ) { rc = PR_WaitSemaphore( sem2 ); PR_ASSERT( PR_FAILURE != rc ); (*addr)++; PR_ASSERT( (*addr % 2) == 0 ); if ( optVerbose ) PR_LOG( lm, msgLevel, ( "nameshm1: Client ping: %d, i: %d", *addr, i)); rc = PR_PostSemaphore( sem1 ); PR_ASSERT( PR_FAILURE != rc ); } rc = PR_CloseSemaphore( sem1 ); PR_ASSERT( PR_FAILURE != rc ); rc = PR_CloseSemaphore( sem2 ); PR_ASSERT( PR_FAILURE != rc ); rc = PR_DetachSharedMemory( shm, addr ); if ( PR_FAILURE == rc ) { PR_LOG( lm, msgLevel, ( "nameshm1: DoClient(): Detach: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError())); failed_already = 1; return; } PR_LOG( lm, msgLevel, ( "nameshm1: DoClient(): Detach: success: " )); rc = PR_CloseSharedMemory( shm ); if ( PR_FAILURE == rc ) { PR_LOG( lm, msgLevel, ( "nameshm1: DoClient(): Close: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError())); failed_already = 1; return; } PR_LOG( lm, msgLevel, ( "nameshm1: DoClient(): Close: success: " )); return;} /* end DoClient() */static void ClientServerTest( void ){ PRStatus rc; PRSem *sem1, *sem2; PRProcess *proc; PRInt32 exit_status; PRSharedMemory *shm; PRUint32 *addr; PRInt32 i; char *child_argv[8]; char buf[24]; PR_LOG( lm, msgLevel, ( "nameshm1: Begin ClientServerTest" )); rc = PR_DeleteSharedMemory( optName ); if ( PR_FAILURE == rc ) { PR_LOG( lm, msgLevel, ( "nameshm1: Server: Destroy: failed. No problem")); } else PR_LOG( lm, msgLevel, ( "nameshm1: Server: Destroy: success" )); shm = PR_OpenSharedMemory( optName, optSize, (PR_SHM_CREATE | PR_SHM_EXCL), SHM_MODE); if ( NULL == shm ) { PR_LOG( lm, msgLevel, ( "nameshm1: Server: Create: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError())); failed_already = 1; return; } PR_LOG( lm, msgLevel, ( "nameshm1: Server: Create: success: %p", shm )); addr = PR_AttachSharedMemory( shm , 0 ); if ( NULL == addr ) { PR_LOG( lm, msgLevel, ( "nameshm1: Server: Attach: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError())); failed_already = 1; return; } PR_LOG( lm, msgLevel, ( "nameshm1: Server: Attach: success: %p", addr )); sem1 = PR_OpenSemaphore( SEM_NAME1, PR_SEM_CREATE, SEM_MODE, 0 ); PR_ASSERT( sem1 ); sem2 = PR_OpenSemaphore( SEM_NAME2, PR_SEM_CREATE, SEM_MODE, 1 ); PR_ASSERT( sem1 ); strcpy( (char*)addr, "FooBar" ); child_argv[0] = "nameshm1"; child_argv[1] = "-C"; child_argv[2] = "-p"; sprintf( buf, "%d", optPing ); child_argv[3] = buf; child_argv[4] = optName; child_argv[5] = NULL; proc = PR_CreateProcess(child_argv[0], child_argv, NULL, NULL); PR_ASSERT( proc ); PR_Sleep( PR_SecondsToInterval(4)); *addr = 1; for ( i = 0 ; i < optPing ; i++ ) { rc = PR_WaitSemaphore( sem1 ); PR_ASSERT( PR_FAILURE != rc ); (*addr)++; PR_ASSERT( (*addr % 2) == 1 ); if ( optVerbose ) PR_LOG( lm, msgLevel, ( "nameshm1: Server pong: %d, i: %d", *addr, i)); rc = PR_PostSemaphore( sem2 ); PR_ASSERT( PR_FAILURE != rc ); } rc = PR_WaitProcess( proc, &exit_status ); PR_ASSERT( PR_FAILURE != rc ); rc = PR_CloseSemaphore( sem1 ); PR_ASSERT( PR_FAILURE != rc ); rc = PR_CloseSemaphore( sem2 ); PR_ASSERT( PR_FAILURE != rc ); rc = PR_DeleteSemaphore( SEM_NAME1 ); PR_ASSERT( PR_FAILURE != rc ); rc = PR_DeleteSemaphore( SEM_NAME2 ); PR_ASSERT( PR_FAILURE != rc ); rc = PR_DetachSharedMemory( shm, addr ); if ( PR_FAILURE == rc ) { PR_LOG( lm, msgLevel, ( "nameshm1: Server: Detach: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError())); failed_already = 1; return; } PR_LOG( lm, msgLevel, ( "nameshm1: Server: Detach: success: " )); rc = PR_CloseSharedMemory( shm ); if ( PR_FAILURE == rc ) { PR_LOG( lm, msgLevel, ( "nameshm1: Server: Close: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError())); failed_already = 1; return; } PR_LOG( lm, msgLevel, ( "nameshm1: Server: Close: success: " )); rc = PR_DeleteSharedMemory( optName ); if ( PR_FAILURE == rc ) { PR_LOG( lm, msgLevel, ( "nameshm1: Server: Destroy: Error: %ld. OSError: %ld", PR_GetError(), PR_GetOSError())); failed_already = 1; return; } PR_LOG( lm, msgLevel, ( "nameshm1: Server: Destroy: success" )); return;} /* end ClientServerTest() */PRIntn main(PRIntn argc, char *argv[]){ { /* ** Get command line options */ PLOptStatus os; PLOptState *opt = PL_CreateOptState(argc, argv, "Cdvw:s:p:i:"); while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) { if (PL_OPT_BAD == os) continue; switch (opt->option) { case 'v': /* debug mode */ optVerbose = 1; /* no break! fall into debug option */ case 'd': /* debug mode */ debug = 1; msgLevel = PR_LOG_DEBUG; break; case 'w': /* try writing to memory mapped read-only */ optWriteRO = 1; break; case 'C': optClient = 1; break; case 's': optSize = atol(opt->value) * 1024; break; case 'p': optPing = atol(opt->value); break; case 'i': optClientIterations = atol(opt->value); break; default: strcpy( optName, opt->value ); break; } } PL_DestroyOptState(opt); } lm = PR_NewLogModule("Test"); /* Initialize logging */ PR_LOG( lm, msgLevel, ( "nameshm1: Starting" )); if ( optClient ) { DoClient(); } else { BasicTest(); if ( failed_already != 0 ) goto Finished; ReadOnlyTest(); if ( failed_already != 0 ) goto Finished; ClientServerTest(); }Finished: if ( debug ) printf("%s\n", (failed_already)? "FAIL" : "PASS" ); return( (failed_already)? 1 : 0 );} /* main() *//* end instrumt.c */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -