自测之Lesson9:时钟与信号

题目一:编写一个获取当前时间的程序,并将其以“year-mon-day time”的形式输出。

程序代码:

#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <string.h>

void TestTime()
{
        time_t tNow;
        tNow = time(NULL);
        struct tm *plocalTime = localtime(&tNow);

        printf("Year:%d
", plocalTime->tm_year);
        printf("Month:%d
", plocalTime->tm_mon);
        printf("Day:%d
", plocalTime->tm_mday); 
        printf("Hour:%d
", plocalTime->tm_hour); 
        printf("Minute:%d
", plocalTime->tm_min);
        printf("Second:%d
", plocalTime->tm_sec);
        char szBuf[256] = {"0"};
        strftime(szBuf, 255, "%Y-%m-%d %X", plocalTime);
        printf("FORMAT TIME(%%Y-%%m-%%d %%X): %s
", szBuf);
}

void getMicrotime()
{
        time_t tNow;
        tNow = time(NULL);
        struct timeval tv;
        gettimeofday(&tv, NULL);
        printf("%u
", (unsigned int)tNow);
        printf("%u	%u
", (unsigned int)tv.tv_sec, (unsigned int)tv.tv_usec);
}

int main()
{
        TestTime();
        getMicrotime();
        return 0;
}

  

题目二:使用signal函数,编写一个处理信号的程序。

程序代码:

#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void func(int sig)
{
        printf("
Received param is %d
", sig);
        printf("You type ctrl + c
");
}


void testSignal()
{
        signal(SIGINT, func);                   // 当接收到SIGINT信号时,交由func处理
        signal(SIGTERM, SIG_IGN);               // 当接收到SIGTERM信号时,忽略信号
        while(1);
}


int main()
{
        testSignal();
        return 0;
}

  

题目三:使用sigaction函数,编写一个处理信号的程序。

程序代码:

#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>


void func(int sig, siginfo_t *info, void *ex)
{
        printf("I received signal: %d
", sig);
        printf("signal number: %d
", info->si_signo);
        printf("process[PID: %d] send this signal
", info->si_pid);
        printf("User[UID: %d] send this signal
", info->si_uid);
        printf("extra value: %d
", info->si_value.sival_int);
}


void SigactionTest()
{
        struct sigaction act;
        act.sa_sigaction = func;
        act.sa_flags = 0;
        act.sa_flags |= SA_SIGINFO;
        printf("My pid is %d
", getpid());
        sigaction(SIGINT, &act, NULL);
        while(1);
}


int main()
{
        SigactionTest();
        return 0;
}

  

题目四:使用alarm函数编写一个定时器程序。

程序代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>


void func_alarm(int sig)
{
        // 定时事件在此处理
        printf("定时器时间到了...
");
}


void TestAlarm()
{
        signal(SIGALRM, func_alarm);
        alarm(2);       // 设置一个两秒的定时器
}


int main()
{
        TestAlarm();
        while(1);       // 使程序不得结束,不然等不到信号处理函数就结束了
        return 0;
}

题目五:使用setitimer函数编写一个定时器程序。

 

程序代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/time.h>

void func_getitimer(int sig)
{
        // 定时事件在此处理
        switch(sig) {
                case SIGALRM:
                        printf("第一种定时器
");
                        break;
                case SIGVTALRM:
                        printf("第二种定时器
");
                        break;
                case SIGPROF:
                        printf("第三种定时器
");
                        break;
        }
}


void TestGetitimer()
{
        struct itimerval itime1, itime2, itime3;
        itime1.it_interval.tv_sec = 0;                  // 下次定时取值的秒部分
        itime1.it_interval.tv_usec = 500000;            // 下次定时取值的微秒部分
        itime1.it_value.tv_sec = 0;                     // 本次定时设置值的秒部分
        itime1.it_value.tv_usec = 500000;               // 本次定时设置值的微妙部分
        itime2 = itime1;
        itime3 = itime1;
        signal(SIGALRM, func_getitimer);
        signal(SIGVTALRM, func_getitimer);
        signal(SIGPROF, func_getitimer);
        setitimer(ITIMER_REAL, &itime1, NULL);          // 自动多次执行  此为第一种定时器
        setitimer(ITIMER_VIRTUAL, &itime2, NULL);       // 此为第二种定时器
        setitimer(ITIMER_PROF, &itime3, NULL);          // 此为第三种定时器
}


int main()
{
        TestGetitimer();
        while(1);       // 使程序不得结束,不然等不到信号处理函数就结束了
        return 0;
}

  

  

原文地址:https://www.cnblogs.com/xzxl/p/8526332.html