/*
============================================================================
Name : ywtest8.c
Author : yw
Version :
Copyright : Your copyright notice
Description : Hello World in C, Ansi-style
============================================================================
*/
#include <sys/time.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
static char msg[] = "Timer=2(S) time is running out/n";
static int len;
// 向标准错误输出信息,告诉用户时间到了 Www.Svn8.Com
void prompt_info(int signo)
{
write(STDERR_FILENO, msg, len);
}
// 建立信号处理机制
void init_sigaction(void)
{
struct sigaction tact;
/*信号到了要执行的任务处理函数为prompt_info*/
tact.sa_handler = prompt_info;
tact.sa_flags = 0;
/*初始化信号集*/
sigemptyset(&tact.sa_mask);
/*建立信号处理机制*/
sigaction(SIGALRM, &tact, NULL);
}
void init_time()
{
struct itimerval value;
/*设定执行任务的时间间隔为2秒0微秒*/
value.it_value.tv_sec = 2;
value.it_value.tv_usec = 0;
/*设定初始时间计数也为2秒0微秒*/
value.it_interval = value.it_value;
/*设置计时器ITIMER_REAL*/
setitimer(ITIMER_REAL, &value, NULL);
}
int main()
{
len = strlen(msg);
init_sigaction();
init_time();
while ( 1 );
exit(0);
}
///
///
#include<unistd.h>
#include<signal.h>
#define SIGALRM 14
void handler() {
printf("hello/n");
}
main()
{
int i;
signal(SIGALRM,handler);
alarm(5);
for(i=1;i<20;i++){
printf("sleep %d .../n",i);
sleep(1);
}
}
/
#include<unistd.h>
#include<signal.h>
#include<sys/types.h>
#include<sys/wait.h>
main()
{
pid_t pid;
int status;
if(!(pid= fork())){
printf("Hi I am child process!/n");
sleep(10);
return;
}
else{
printf("send signal to child process (%d) /n",pid);
sleep(1);
kill(pid ,SIGABRT);
wait(&status);
if(WIFSIGNALED(status))
printf("chile process receive signal %d/n",WTERMSIG(status));
}
}
//
void why_me();
int main()
{
printf( "The file is %s./n", __FILE__ );
printf( "The date is %s./n", __DATE__ );
printf( "The time is %s./n", __TIME__ );
printf( "This is line %d./n", __LINE__ );
printf( "This function is %s./n", __FUNCTION__ );
why_me();
return 0;
}
void why_me()
{
printf( "This function is %s/n", __func__ );
printf( "The file is %s./n", __FILE__ );
printf( "This is line %d./n", __LINE__ );
}
试验的代码1
最新推荐文章于 2024-03-12 14:48:20 发布