试验的代码1

/*
 ============================================================================
 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__ );
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值