|
楼主 |
发表于 2021-10-12 18:41:07
|
显示全部楼层
- /* lock_set.c �ú������ã���fd�ļ�����������Ӧ���ļ���type���͵���(������������д�������) */
- int lock_set(int fd, int type)
- {
- struct flock old_lock, lock;
- lock.l_whence = SEEK_SET;/* �ļ���дָ����ʼλ�����ļ���ͷ */
- lock.l_start = 0; /*���λ���������������ļ�??*/
- lock.l_len = 0; /*�������򳤶ȣ����������ļ�*/
- lock.l_type = type; /*type������������д���ͽ����������ͣ���Ψһ��Ҫ��ֵ��������l_type��ֵ�ļ���*/
- lock.l_pid = -1; /*���ļ�ӵ���ļ����Ľ��̺ţ�Ĭ��Ϊ-1*/
-
- fcntl(fd, F_GETLK, &lock);/* �ж��ļ��Ƿ�������� */
- /*F_GETLK:����lock����ֵ�������Ƿ�������ֻ�д��ڽ���״̬����������*/
- /*&lock:ȡflock�ṹ�����lock���׵�ַ*/
-
- /*�˴���fcntl()����ֻ��Ϊ�˲���fd�Ƿ�����������Ϊֻ���ļ�Ϊ����(������)״̬��������������ļ��Ѿ�������
- ����F_GETLK������Ҫ������жϣ�
- ���֮ǰ�Ѿ����ڵ��Ƕ����򻹿����϶����������д�������϶�����д����
- Ҳ����˵��Ҫ��һ��û�������ļ�����Ҫִ������fcntl()������
- ��һ��ִ�У�F_GETLK�������ļ�����Ϊ����״̬��fcntl()��������-1�����ĺ��徭�����Ը��ļ�����������
- �ڶ���fcntl()����ִ�оͿ��Է��Ĵ󵨵�������fcntl()�����᷵��0�������ɹ�*/
-
-
- if (lock.l_type != F_UNLCK)/*���lock.l_type���ǽ���״̬����ζ���Ѿ�����������*/
- {
- /* �ж��ļ�����������ԭ�� */
- if (lock.l_type == F_RDLCK) /* ���ļ����ж��� */
- {
- printf("Read lock already set by process PID:%d\n", lock.l_pid);
- }
- else if (lock.l_type == F_WRLCK) /* ���ļ�����д�� */
- {
- printf("Write lock already set by process PID:%d\n", lock.l_pid);
- }
- }
-
-
- lock.l_type = type;/* l_type �����ѱ�F_GETLK�޸Ĺ���������Ҫ���¸�ֵ*/
-
- /* ���ݲ�ͬ��typeֵ��������ʽ�������������ν����ʽ����ζ�����û�гɹ������������׽���źţ������һֱ��������״̬ */
- if ((fcntl(fd, F_SETLKW, &lock)) < 0)
- /*F_SETLKW�����޷���ȡ��ʱ,����˯�ߣ�����ɻ������׽���źţ��򷵻�*/
- /*&lock:ȡflock�ṹ�����lock���׵�ַ*/
- /*�˴�fcntl()�������ã���fd�ļ�����������Ӧ���ļ���lock�ṹ�������ȷ����l_type���͵�����
- ��ʱ��F_GETLKWֱ�Ӹ��ļ����ļ�������Ϊ��һ��fcntl()�����Ѿ����Թ�
- ���ļ��������������DZ���fcntl()�������Գɹ�ִ�У�������0 */
- {
- printf("Lock failed:type = %d\n", lock.l_type);
- return 1;
- }
-
- switch(lock.l_type)
- {
- case F_RDLCK:
- {
- printf("Read lock set by process PID:%d\n", getpid());/*���ļ��ѱ�getpid()�������˶���*/
- }
- break;
- case F_WRLCK:
- {
- printf("Write lock set by process PID:%d\n", getpid());/*���ļ��ѱ�getpid()��������д��*/
- }
- break;
- case F_UNLCK:
- {
- printf("Release lock by process PID:%d\n", getpid());/*���ļ��ѱ�getpid()���̽���*/
- return 1;
- }
- break;
- default:
- break;
- }/* end of switch */
-
- return 0;
- }
- /* read_lock.c */
- #include <unistd.h>
- #include <sys/file.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include "lock_set.c"
- int main(void)
- {
- int fd;
-
- fd = open("hello",O_RDWR | O_CREAT, 0644);
- if(fd < 0)
- {
- printf("Open file error\n");
- exit(1);
- }
-
- lock_set(fd, F_RDLCK);/* ���ļ��϶�ȡ��������������ڵȴ����������ַ��ĵȴ�״̬�� */
- getchar();/*ȡ���̼�����ַ��������ó������ִ����ȥ*/
-
- lock_set(fd, F_UNLCK);/* ���ļ����� */
- getchar();
-
- close(fd);
- exit(0);
- }
- /* write_lock.c */
- #include <unistd.h>
- #include <sys/file.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include "lock_set.c"
- int main(void)
- {
- int fd;
-
- fd = open("hello",O_RDWR | O_CREAT, 0644);/* ���ȴ��ļ� */
- if(fd < 0)
- {
- printf("Open file error\n");
- exit(1);
- }
-
- lock_set(fd, F_WRLCK);/* ���ļ���д���� */
- getchar();
-
- lock_set(fd, F_UNLCK);/* ���ļ����� */
- getchar();
-
- close(fd);
-
- exit(0);
- }
复制代码 |
|