POSIX unbuffered file I/O and Variable Parameter play

Source: Internet
Author: User
Tags flock lock
The previously written test program includes the comprehensive application of POSIX unbuffered file I/O, file addition and unlocking, process forks, and variable parameters. There is no technical content. I just want to describe it and don't want to lose it one day.

C is really small and flexible, and there are still some features that I could not think of before in terms of language ~ However, can I only use C and not c ++ for this job? Do you really have to say goodbye to your favorites?

  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. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.