经典编程900例(c语言)(第十一篇)

本文展示了多个C语言程序示例,涉及计算程序运行时间、遍历目录打印文件名、中断处理和程序逆向执行。通过示例124了解如何测量函数执行时间,示例125演示遍历目录并打印文件名,示例126和127介绍如何处理中断事件,示例128获取进程ID,示例129和130展示程序逆向执行,最后的示例131和132展示了DOS相关系统调用的应用。这些示例涵盖了程序性能分析和系统级编程的基本概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

例124:通过系统时间计算程序运行时间

#include <stdio.h>
#include <time.h>

int  add_fast(int a, int b)
{
    return(a + b);
}

int add_slow(int a, int b)
{
    return(a + b);
}

int main(int argc, char const *argv[])
{
    unsigned long int i, result;

    clock_t start_time, stop_time;

    printf("Processing...\n");

    // clock()获取当前时间
    start_time = clock();

    // 循环调用2000000次函数和赋值操作
    for (i = 0; i < 2000000L; i++)
        result = add_fast(i, -i);

    stop_time = clock();
    printf("Processing time for fast call %d ticks\n", stop_time - start_time);

    // -----------------------------------------------------------------------

    start_time = clock();

    for (i = 0; i < 2000000L; i++)
        result = add_slow(i, -i);

    stop_time = clock();
    printf("Processing time for normal call %d ticks\n", stop_time - start_time);

    return 0;
}

例125:打印目标文件夹下的文件夹名

#include <stdio.h>

// 这个头文件vs下没有
#include <dirent.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    DIR *directory_pointer;
    struct dirent *entry;

    struct FileList {
        char filename[64];
        struct FileList *next;
    } start, *node;


    if ((directory_pointer = opendir(argv[1])) == NULL)
        printf("Error opening %s\n", argv[1]);
    else
    {
        start.next = NULL;
        node = &start;

        while (entry = readdir(directory_pointer))
        { 
            node->next = (struct FileList *) malloc(sizeof(struct FileList));           
            if (node == NULL)
            {
                printf("Insufficient memory to store list\n");
                exit(1);
            }
            node = node->next;
            strcpy(node->filename, entry);
            node->next = NULL;
        }
    }

    closedir(directory_pointer);

    node = start.next;

    while (node)
    {
        printf("%s\n", node->filename);
        node = node->next;
    }

    return 0;
}

例126:dos.h…

#include <stdio.h>
#include <dos.h>
#include <stdlib.h>

void interrupt far (*original_handler)();

void interrupt far handler(void)
{
    printf("Some event just happened\n");
    _disable();
    _dos_setvect(0xFF, original_handler);
    _enable();

    exit(0);
}

void main(void)
{
    int i = 0;


    original_handler = _dos_getvect(0xFF);

    _disable();
    _dos_setvect(0xFF, handler);
    _enable();

    while (i++ < 100)
        ;
    geninterrupt(0xFF);
}

例127:dos.h…

#include <stdio.h>
#include <dos.h>

int main(int argc, char const *argv[])
{
    int i;

    for (i = 0; i <= 255; i++)
        printf("Interrupt: %x Vector: %lx\n", i, _dos_getvect(i));
    
    return 0;
}

例128:获取程序的进程id

#include <stdio.h>
#include <process.h>

int main(int argc, char const *argv[])
{
	// getpid()需要包含process.h
    printf("Process id: %X\n", getpid());
    return 0;
}

例129:一段会逆向执行的代码

#include <stdio.h>
#include <setjmp.h>
#include <stdlib.h>

// 全局变量
jmp_buf location;

void function(void)
{
    printf("About to longjmp\n");
    // void longjmp(jmp_buf env,int val);
    // longjmp的作用是使用setjmp保存在location中栈环境信息返回到setjmp的位置,
    // 也就是当执行longjmp时程序又回到setjmp处(恢复现场)
    // 第二个参数是具有非0值的val,它将成为从setjmp处返回的值
    longjmp(location, 1);
}

int main(int argc, char const *argv[])
{
    // int setjmp(jmp_buf  env); - 设置执行点(保护现场)
    // 返回值:若直接调用则返回0,若从longjmp调用返回则返回非0值的longjmp中的val值
    if (setjmp(location) != 0)
     {
       printf("Returning from longjmp\n");
       exit(1);
     }

    function();

    return 0;
}

例130:又来了…

#include <stdio.h>
#include <dos.h>
#include <conio.h>

int count = 0;

void interrupt handler(void)
{
    count++;
}

int main(int argc, char const *argv[])
{
    void interrupt (*original_handler)();

    int old_count = 0;

    original_handler = _dos_getvect(5);

   _disable();  // Turn off interrupts during _dos_setvect
   _dos_setvect(5, handler);
   _enable();

   printf("Press Shift-PrtSc three times or any key to end\n");
     
   while (count < 3)
     if (count != old_count)
       {
         printf("Shift-PrtSc pressed\n");
         old_count = count;
       }
   
   _disable();
   _dos_setvect(5, original_handler);
   _enable();
    return 0;
}

例131:还有…(如果有条件的可以试试,反正我是找不到编译通过的方法了)

#include <stdio.h>
#include <dos.h>

void main (void)
 {
   printf("Operating system version number %d.%d\n",
    _osmajor, _osminor);
   
   printf("Operating system version number %d.%d\n",
    _version & 255, _version >> 8);
 }

例132:dos.h(该说的前面都说了)

#include <stdio.h>
#include <dos.h>

void main (void)
 {
   printf("The Program Segment Prefix begins at %X\n", _psp);
 }

例133:

#include <stdio.h>
#include <dos.h>
#include <conio.h>

int alphanum = 0;
int counter = 0;

void interrupt far handler(void)
 {
   if (++counter == 273)  // 15 seconds   
     {
       alphanum = !alphanum;  // Toggle
       counter = 0;
     }
 }

void main(void)
 {
   int i;

   void interrupt far (*original_handler)();

   original_handler = _dos_getvect(0x1C);

   
   _disable();
   _dos_setvect(0x1c, handler);
   _enable();

   while (! kbhit())
     if (alphanum)
       for (i = 'A'; i <= 'Z'; i++)
          printf("%c\n", i);
     else
       for (i = 0; i <= 100; i++)
          printf("%d\n", i);

    _disable();
    _dos_setvect(0x1c, original_handler);
    _enable();
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值