写一个陷阱门的测试程序

这篇文章详细描述了一个使用C语言编写的测试程序,通过内存映射(mmap)创建多个共享内存区域,实现实时数据更新和计数器功能,当计数达到1000时程序退出。
摘要由CSDN通过智能技术生成
//写一个陷阱门的测试程序
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <semaphore.h>
#include <pthread.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <sys/uio.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/un.h>


int main(int argc, char *argv[])
{
    int fd;
    int ret;
    int i;
    int count;
    int *p;
    int *q;
    int *r;
    int *s;
    int *t;
    int *u;
    int *v;
    int *w;
    int *x;
    int *y;
    int *z;
    int *aa;
    int *bb;
    int *cc;
    int *dd;


    fd = open("trapdoor", O_RDWR);
    if(fd < 0)
    {
        perror("open");
        return -1;
    }
    p = (int *)mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
    if(p == MAP_FAILED)
    {
        perror("mmap");
        return -1;
    }
    q = (int *)mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 4096);
    if(q == MAP_FAILED)
    {
        perror("mmap");
        return -1;
        }
        r = (int *)mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 8192);
        if(r == MAP_FAILED)
        {
            perror("mmap");
            return -1;
         }

         s = (int *)mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 12288);
                 if(s== MAP_FAILED)
        {
            perror("mmap");
            return -1;
         }
         t = (int *)mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 16384);
                 if(t== MAP_FAILED)
        {
            perror("mmap");
            return -1;
         }

         u = (int *)mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 20480);
                 if(u== MAP_FAILED)
        {
            perror("mmap");
            return -1;
         }
         v = (int *)mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 24576);
                 if(v == MAP_FAILED)
        {
            perror("mmap");
            return -1;
         }
         w = (int *)mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 28672);
                 if(w == MAP_FAILED)
        {
            perror("mmap");
            return -1;
         }
         x = (int *)mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 32768);
                 if(x == MAP_FAILED)
        {
            perror("mmap");
            return -1;   
         }
         y = (int *)mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 36864);
                 if(y == MAP_FAILED)
        {
            perror("mmap");
            return -1;
         }
         z = (int *)mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 40960);
                 if(z== MAP_FAILED)
        {
            perror("mmap");
            return -1;
         }
         aa = (int *)mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 45056);
                 if(aa == MAP_FAILED)
        {
            perror("mmap");
            return -1;
         }
         bb = (int *)mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 49152);
                 if(bb == MAP_FAILED)
        {
            perror("mmap");
            return -1;
         }
         cc = (int *)mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 53248);
                 if(cc== MAP_FAILED)
        {
            perror("mmap");
            return -1;
         }
         dd = (int *)mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 57344);
                 if(dd == MAP_FAILED)
        {
            perror("mmap");
            return -1;
         }
         count = 0;
         while(1)
         {
             *p = count;
             *q = count;
             *r = count;
             *s = count;
             *t = count;
             *u = count;
             *v = count;
             *w = count;
             *x = count;
             *y = count;
             *z = count;
             *aa = count;
             *bb = count;
             *cc = count;
             *dd = count;
             count++;
             sleep(1);
             printf("count = %d\n", count);
             if(count == 1000)
             {
                 break;
                 }
                 }
                 return 0;
                 }












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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值