Untitled
unknown
c_cpp
2 years ago
1.1 kB
3
Indexable
#include <stdio.h> #include <stdlib.h> #include <sys/file.h> #include <unistd.h> #include <fcntl.h> #define BUF_SIZE 4096 int main(int argc, char **argv) { int fd = 0; char buf[BUF_SIZE]; int offset = 0; fd = open ( "./flock.db", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); write ( fd, "3500", 4); int id = 0; sscanf ( argv[1], "%d", &id ); printf ( "Process %d start.\n", id); printf ("Process start.\n"); for ( int i=0; i<1000; i++){ flock ( fd, LOCK_EX ); //移動到檔案尾端 lseek ( fd, 0, SEEK_END ); //輸入值為3500,故往前5格並設定 lseek ( fd, -4, SEEK_CUR); read ( fd, buf, 4); //printf ("buf = %s\n", buf); sscanf ( buf, "%d", &offset ); //rintf ("offset = %d\n", offset); lseek ( fd, 0, SEEK_END); lseek ( fd, offset, SEEK_CUR); offset += 1; sprintf ( buf, "%d", offset ); write ( fd, buf, 4); flock ( fd, LOCK_UN); //getchar(); usleep(10000); } //printf ( "Process %d end.\n", id); printf ("Process end.\n"); return 0; }
Editor is loading...