- 论坛徽章:
- 0
|
在Linux下写了一段小代码测试一下文件锁,创建文件并写入文件锁(强制锁,写入锁),结果无论怎么测试其它线程和自身都可以随意写入数据,文件锁丝毫不起作用,将代码贴出来,各位前辈看看什么情况。- /*file_lock_set.c*/
- int lock_set(int fd,int type)
- {
- struct flock old_lock,lock;
- lock.l_whence=SEEK_SET;
- lock.l_start=0;
- lock.l_len=25;
- lock.l_type=type;
- lock.l_pid=-1;
- /*judge whether the file has been locked*/
- fcntl(fd,F_GETLK,&lock);
- if (lock.l_type!=F_UNLCK)/*there is already a lock*/
- {
- if(lock.l_type==F_RDLCK)
- printf("read lock is already setted by %d",lock.l_pid);
- else if(lock.l_type==F_WRLCK)
- printf("read lock is already setted by %d",lock.l_pid);
- }
- lock.l_type=type;/*l_type may be changed by F_GETLK*/
-
- if(fcntl(fd,F_SETLKW,&lock)<0)
- {
- printf("lock failed:type = %d\n",lock.l_type);
- }
-
- else if(fcntl(fd,F_SETLKW,&lock)==0)
- {
- switch(lock.l_type)
- {
- case F_RDLCK:
- printf("read lock set by %d\n",getpid());break;
- case F_WRLCK:
- printf("write lock set by %d\n",getpid());break;
- case F_UNLCK:
- printf("release lock by %d\n",getpid());
- }
- }
- }
-
-
- #include<sys/types.h>
- #include<unistd.h>
- #include<fcntl.h>
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- #include<signal.h>
- #include<pthread.h>
- #include<semaphore.h>
- #include"file_lock_set.c"
- sem_t mywrite,mylock;
- pthread_t thread[2];
- /*func******************************************************************/
- void func(void)
- {
- int fd;
- printf("thread1 is going to open the locked file\n");
- fd=open("./datafile",O_RDWR);
- if (fd<0)
- perror("open datafile fail");
- if(fd>0)
- {
- while(1)
- {
- if(write(fd,"thread1 has been here \n",22)>0)
- printf("thread1 write data sucessfully \n");
- else if(write(fd,"thread1 has been here \n",22)<=0)
- printf("thread1 failed to write data \n");
- sleep(1);
- }
- }
- }
- /*locker***************************************************************/
- void* locker(void* arg)
- {
- int fd;
- sem_wait(&mylock);
- printf("child1 is creating a datafile\n");
- fd=open("./datafile",O_RDWR|O_CREAT,0666);/*创建数据文件*/
- if(fd<0)
- perror("creat datafile fail");
- lock_set(fd,F_WRLCK);/*文件上锁*/
- if(write(fd,"thread0 has been here \n",22)>0)
- printf("thread0 write data sucessfully \n");
- else if(write(fd,"thread1 has been here \n",22)<=0)
- printf("thread2 failed to write data \n");
- sem_post(&mywrite);
- sleep(3);
- lock_set(fd,F_UNLCK);
- sleep(2);
- pthread_cancel(thread[1]);
- }
- /*writer***************************************************************/
- void* writer(void* arg)
- {
- sem_wait(&mywrite);
- func();
- }
- /*main****************************************************************/
- int main (int argc,char* argv[])
- {
- int ret;
-
- /*创建两个线程*/
- ret=sem_init(&mylock,0,1);
- ret+=sem_init(&mywrite,0,0);
- if(ret!=0)
- printf("At least one sem init failed");
- ret=pthread_create(&thread[0],NULL,locker,NULL);
- ret+=pthread_create(&thread[1],NULL,writer,NULL);
- if(ret!=0)
- printf("At least one thread created failed");
- pthread_join(thread[0],NULL);
- pthread_join(thread[1],NULL);
- return 0;
- exit(0);
- }
复制代码 执行结果在附件的图片里,锁完全没起作用啊 |
|