1.模拟CP复制功能
<span style="font-size:18px;"> 1 #include <stdio.h>
2 #include <unistd.h>
3 #include <fcntl.h>
4 #include <stdlib.h>
5
6 #define BUFFERSIZE 4096
7 #define COPYMODE 0644
8
9 int main(int argc,char * argv[])
10 {
11 int in_fd,out_fd,numofchars;
12 char buf[BUFFERSIZE];
13 if(argc != 3)
14 {
15 printf("Number of params is not enough!");
16 exit(1);
17 }
18 if((in_fd=open(argv[1],O_RDONLY))==-1)
19 {
20 printf("cannot creat%s",argv[1]);
21 }
22 if((out_fd=creat(argv[2],COPYMODE))==-1)
23 {
24 printf("Cannot create%s",argv[2]);
25 }
26 while((numofchars = read(in_fd,buf,BUFFERSIZE)) > 0)
27 {
28 if(write(out_fd,buf,numofchars) != numofchars)
29 printf("Write error to %s",argv[2]);
30
31 }
32 if(numofchars == -1)
33 {
34 printf("Read error from %s",argv[1]);
35 }
36 if(close(in_fd) == -1 || close(out_fd) == -1)
37 printf("closing files error!");
38 }</span>
2.flock实现异步读写
flock(锁定文件或解除锁定)
表头文件 #include<sys/file.h>
定义函数 int flock(int fd,int operation);
函数说明 flock()会依参数operation所指定的方式对参数fd所指的文件做各
种锁定或解除锁定的动作。此函数只能锁定整个文件,无法锁定文
件的某一区域。
参数 operation有下列四种情况:
LOCK_SH 建立共享锁定。多个进程可同时对同一个文件作共享锁
定。
LOCK_EX 建立互斥锁定。一个文件同时只有一个互斥锁定。
LOCK_UN 解除文件锁定状态。
LOCK_NB 无法建立锁定时,此操作可不被阻断,马上返回进程。通
常与LOCK_SH或LOCK_EX 做OR(|)组合。
单一文件无法同时建立共享锁定和互斥锁定,而当使用dup()或fork
()时文件描述词不会继承此种锁定。
返回值 返回0表示成功,若有错误则返回-1,错误代码存于errno。
testOne.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <fcntl.h>
4 #include <stdlib.h>
5 #include <string.h>
6
7 #define BUFFERSIZE 4096
8 #define COPYMODE 0644
9
10 int main(int argc,char * argv[])
11 {
12 int fd,numofchars;
13 char buf[BUFFERSIZE];
14 if(argc != 2)
15 {
16 printf("Number of params is not fit!\n");
17 exit(1);
18 }
19 if((fd=open(argv[1],O_RDWR|O_APPEND))==-1)
20 {
21 printf("cannot open%s\n",argv[1]);
22 }
23 int cnt = 10;
24 while(cnt)
25 {
26 if(flock(fd,LOCK_EX)==0)
27 printf("file lock success!\n");
28 else
29 printf("file lock failed!\n");
30 sprintf(buf,"%s","testFlock!\n");
31 int wrNum = write(fd,buf,strlen(buf));
32 if(wrNum <= 0)
33 printf("write file error!\n");
34 int ret = flock(fd,LOCK_UN);
35 if(ret == 0)
36 printf("file unlock success!\n");
37 else
38 printf("file unlock failed!code:%d\n",ret);
39 cnt--;
40 usleep(1000*1000);
41 }
42 if(close(fd) == -1)
43 printf("closing files error!\n");
44 }
testTwo.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <fcntl.h>
4 #include <stdlib.h>
5 #include <string.h>
6
7 #define BUFFERSIZE 4096
8 #define COPYMODE 0644
9
10 int main(int argc,char * argv[])
11 {
12 int fd,numofchars;
13 char buf[BUFFERSIZE];
14 if(argc != 2)
15 {
16 printf("Number of params is not fit!\n");
17 exit(1);
18 }
19 if((fd=open(argv[1],O_RDWR|O_APPEND))==-1)
20 {
21 printf("cannot open%s\n",argv[1]);
22 }
23 int cnt = 10;
24 while(cnt)
25 {
26 if(flock(fd,LOCK_EX)==0)
27 printf("file lock success!\n");
28 else
29 printf("file lock failed!\n");
30 sprintf(buf,"%s","Hello linux!\n");
31 int wrNum = write(fd,buf,strlen(buf));
32 if(wrNum <= 0)
33 printf("write file error!\n");
34 int ret = flock(fd,LOCK_UN);
35 if(ret == 0)
36 printf("file unlock success!\n");
37 else
38 printf("file unlock failed!code:%d\n",ret);
39 cnt--;
40 usleep(1200*1000);
41 }
42 if(close(fd) == -1)
43 printf("closing files error!\n");
44 }
运行结果: