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?
- // POSIX
- # Include <unistd. h>
- # Include <sys/types. h> // For pid_t
- # Include <sys/STAT. h>
- # Include <sys/file. h>
- # Include <fcntl. h>
- // Randoming
- # Include <stdlib. h> // for random
- # Include <time. h> // For srand with time
- // Variable Parameter List
- # Include <stdarg. h>
- // Common
- # Include <stdio. h>
- # Include <string. h>
- Void my_read ();
- Void my_write ();
- Void lock_set (int fd, int type );
- Void my_lock_test (INT n_tests ,...);
- Int main ()
- {
- My_read ();
- My_write ();
- Srand (unsigned INT) time (0 ));
- Fork ();
- My_lock_test (4, f_wrlck, f_unlck, f_rdlck, f_unlck );
- Exit (0 );
- }
- Void my_read ()
- {
- Int FD;
- If (FD = open ("/tmp/Hello. c", o_creat | o_trunc | o_wronly, 0600) <0)
- {
- Perror ("open :");
- Exit (1 );
- }
- Else
- Printf ("Open File: Hello. C % d/N", FD );
- If (close (FD) <0)
- {
- Perror ("close :");
- Exit (1 );
- }
- Else
- Printf ("Close hello. C/N ");
- }
- Void my_write ()
- {
- Int FD, size, Len;
- Char * Buf = "Hello! I'm writing to this file ";
- Char buf_r [10];
- Len = strlen (BUF );
- If (FD = open ("/tmp/Hello. c", o_creat | o_trunc | o_rdwr, 0666) <0)
- {
- Perror ("open :");
- Exit (1 );
- }
- Else
- Printf ("Open File: Hello. C % d/N", FD );
- If (size = write (FD, Buf, Len) <0)
- {
- Perror ("write :");
- Exit (1 );
- }
- Else
- Printf ("writes: % s/n", Buf );
- Lseek (FD, 0, seek_set );
- If (size = read (FD, buf_r, 10) <0)
- {
- Perror ("read :");
- Exit (1 );
- }
- Else
- Printf ("read first 10 characters from file: % s/n", buf_r );
- If (close (FD) <0)
- {
- Perror ("close :");
- Exit (1 );
- }
- Else
- Printf ("Close hello. C/N ");
- }
- Void lock_set (int fd, int type)
- {
- Struct flock lock;
- Lock. l_whence = seek_set;
- Lock. l_start = 0;
- Lock. l_len = 0;
- While (1)
- {
- Lock. l_type = type;
- If (fcntl (FD, f_setlk, & lock) = 0)
- {
- If (lock. l_type = f_rdlck)
- Printf ("read lock set by % d/N", getpid ());
- Else if (lock. l_type = f_wrlck)
- Printf ("Write lock set by % d/N", getpid ());
- Else if (lock. l_type = f_unlck)
- Printf ("Release lock by % d/N", getpid ());
- Return;
- }
- Fcntl (FD, f_getlk, & lock );
- If (lock. l_type! = F_unlck)
- {
- If (lock. l_type = f_rdlck)
- Printf ("% d: Read lock already set by % d/N", getpid (), lock. l_pid );
- Else if (lock. l_type = f_wrlck)
- Printf ("% d: Write lock already set by % d/N", getpid (), lock. l_pid );
- Sleep (random () % 3 );
- }
- }
- }
- Void my_lock_test (INT n_tests ,...)
- {
- Va_list test_cases;
- Int cur;
- Int FD;
- FD = open ("/tmp/Hello. c", o_rdwr | o_creat, 0666 );
- If (FD <0)
- {
- Perror ("lock test open :");
- Exit (1 );
- }
- Va_start (test_cases, n_tests );
- For (cur = 0; cur <n_tests; ++ cur)
- {
- Lock_set (FD, va_arg (test_cases, INT ));
- Sleep (random () % 5 );
- }
- Va_end (test_cases );
- }