5-2.c

来自「fsmlabs的real time linux的内核」· C语言 代码 · 共 122 行

C
122
字号
/*    * Copyright (c) 2002, Intel Corporation. All rights reserved. * Created by:  julie.n.fleischer REMOVE-THIS AT intel DOT com * This file is licensed under the GPL license.  For the full content * of this license, see the COPYING file at the top level of this  * source tree. * Test that if timer_settime() is using an absolute clock and the * time has already taken place when the test is running that * timer_settime() succeeds and the expiration notification is made. * * Steps: * - Setup and then call timer_settime() with date in past. * - If timer_settime() returns -1 fail. * - If signal handler is not called after a sufficient amount of sleeping, *   fail. * ***  There is a potential false failure if timer_settime() does not *      return before the signal is sent.  TBD if this _should_ happen in *      reality, though. * For this test, signal SIGTOTEST will be used, clock CLOCK_REALTIME * will be used. */#include <time.h>#include <signal.h>#include "compat.h"#define PASS 0#define FAIL 1#define UNRESOLVED 2#define SIGTOTEST SIGALRM#define DATEINPAST 1037128358 //Nov 13, 2002 ~11:13am#define LONGSLEEPTIME 10void handler(int signo){	printf("Caught signal\n");	printf("Test PASSED\n");	exit(PASS);}timer_t tid;pthread_t th;void *th_code(void  *arg){	struct sigaction act;	struct itimerspec its;	int flags = 0;	act.sa_handler=handler;	act.sa_flags=0;	its.it_interval.tv_sec = 0;	its.it_interval.tv_nsec = 0;	/* THIS SEEMS A BUG. DATEINPAST > 4G. Linux and RTLinux uses both	   longs for tv_sec & tv_nsecs*/	/* 	its.it_value.tv_sec = DATEINPAST;	its.it_value.tv_nsec = 0;	*/	its.it_value.tv_sec= (DATEINPAST / NSECS_PER_SEC);	its.it_value.tv_nsec= (DATEINPAST % NSECS_PER_SEC);	/*	if (sigemptyset(&act.sa_mask) == -1) {		perror("Error calling sigemptyset\n");		return UNRESOLVED;	}	*/	if (sigaction(SIGTOTEST, &act, 0) == -1) {		perror("Error calling sigaction\n");		return UNRESOLVED;	}	flags |= TIMER_ABSTIME;	if (timer_settime(tid, flags, &its, NULL) != 0) {		perror("timer_settime() did not return success\n");		return FAIL;	}	sleep(LONGSLEEPTIME);	printf("signal was not sent\n");	return FAIL;}int init_module(void){	struct sigevent ev;	int err=0;	ev.sigev_notify = SIGEV_SIGNAL;	ev.sigev_signo = SIGTOTEST;	if (timer_create(CLOCK_REALTIME, &ev, &tid) != 0) {		perror("timer_create() did not return success\n");		return UNRESOLVED;	}	err=pthread_create(&th,NULL,th_code,(void *) 0);	return err;}void cleanup_module(void){  timer_delete(tid);  pthread_delete_np(th);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?