POSIX无缓冲文件I/O及可变参数小玩

这段以前写的测试程序包含了对POSIX无缓冲文件I/O、文件加解锁、进程分叉、可变参数的综合运用。没什么技术含量,纯粹记述一下,不想有一天弄丢了。

C真是又小巧又灵活,在语言的犄角旮旯还是存在一些之前完全想不到会有的特性,呵呵~不过,现在可能会从事的这个工作是不是只能用C不能用C++呢?难道真的得告别自己的最爱?

  1. //posix
  2. #include <unistd.h>
  3. #include <sys/types.h>   //for pid_t
  4. #include <sys/stat.h>
  5. #include <sys/file.h>
  6. #include <fcntl.h>
  7. //randoming
  8. #include <stdlib.h> //for random
  9. #include <time.h> //for srand with time
  10. //variable parameter list
  11. #include <stdarg.h>
  12. //common
  13. #include <stdio.h>
  14. #include <string.h>
  15. void my_read();
  16. void my_write();
  17. void lock_set(int fd, int type);
  18. void my_lock_test(int n_tests, ...);
  19. int main()
  20. {
  21.     my_read();
  22.     my_write();
  23.     srand( (unsigned int) time(0));
  24.     fork();
  25.     my_lock_test(4, F_WRLCK, F_UNLCK, F_RDLCK, F_UNLCK);
  26.     exit(0);
  27. }
  28. void my_read()
  29. {
  30.     int fd;
  31.     if((fd = open("/tmp/hello.c", O_CREAT|O_TRUNC|O_WRONLY, 0600)) < 0)
  32.     {
  33.         perror("open:");
  34.         exit(1);
  35.     }
  36.     else
  37.         printf("Open file: hello.c %d/n", fd);
  38.     if(close(fd) < 0)
  39.     {
  40.         perror("close:");
  41.         exit(1);
  42.     }
  43.     else
  44.         printf("Close hello.c/n");
  45. }
  46. void my_write()
  47. {
  48.     int fd, size, len;
  49.     char* buf = "Hello! I'm writing to this file";
  50.     char buf_r[10];
  51.     len = strlen(buf);
  52.     if((fd = open("/tmp/hello.c", O_CREAT|O_TRUNC|O_RDWR, 0666)) < 0)
  53.     {
  54.         perror("open:");
  55.         exit(1);
  56.     }
  57.     else
  58.         printf("Open file: hello.c %d/n", fd);
  59.     if((size = write(fd, buf, len)) < 0)
  60.     {
  61.         perror("write:");
  62.         exit(1);
  63.     }
  64.     else
  65.         printf("Writes: %s/n", buf);
  66.     lseek(fd, 0, SEEK_SET);
  67.     if((size = read(fd, buf_r, 10)) < 0)
  68.     {
  69.         perror("read:");
  70.         exit(1);
  71.     }
  72.     else
  73.         printf("read first 10 characters from file: %s/n", buf_r);
  74.     if(close(fd) < 0)
  75.     {
  76.         perror("close:");
  77.         exit(1);
  78.     }
  79.     else
  80.         printf("Close hello.c/n");
  81. }
  82. void lock_set(int fd, int type)
  83. {
  84.     struct flock lock;
  85.     lock.l_whence = SEEK_SET;
  86.     lock.l_start = 0;
  87.     lock.l_len = 0;
  88.     while(1)
  89.     {
  90.         lock.l_type = type;
  91.         if((fcntl(fd, F_SETLK, &lock)) == 0)
  92.         {
  93.             if(lock.l_type == F_RDLCK)
  94.                 printf("read lock set by %d/n", getpid());
  95.             else if(lock.l_type == F_WRLCK)
  96.                 printf("write lock set by %d/n", getpid());
  97.             else if(lock.l_type == F_UNLCK)
  98.                 printf("release lock by %d/n", getpid());
  99.             return;
  100.         }
  101.         fcntl(fd, F_GETLK, &lock);
  102.         if(lock.l_type != F_UNLCK)
  103.         {
  104.             if(lock.l_type == F_RDLCK)
  105.                 printf("%d: read lock already set by %d/n", getpid(), lock.l_pid);
  106.             else if(lock.l_type == F_WRLCK)
  107.                 printf("%d: write lock already set by %d/n", getpid(), lock.l_pid);
  108.             sleep(random() % 3);
  109.         }
  110.     }
  111. }
  112. void my_lock_test(int n_tests, ...)
  113. {
  114.     va_list test_cases;
  115.     int cur;
  116.     int fd;
  117.     fd = open("/tmp/hello.c", O_RDWR|O_CREAT, 0666);
  118.     if(fd < 0)
  119.     {
  120.         perror("lock test open:");
  121.         exit(1);
  122.     }
  123.     va_start(test_cases, n_tests);
  124.     for(cur = 0; cur < n_tests; ++cur)
  125.     {
  126.         lock_set(fd, va_arg(test_cases, int));
  127.         sleep(random() % 5);
  128.     }
  129.     va_end(test_cases);
  130. }


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值