文件加锁问题
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/mman.h>
typedef struct
{
char *GET1;
char *GET2;
}GETX_T;
GETX_T Test(char *str)
{
GETX_T TestGet;
STR PMode = Divide_Para(str);
pid_t pid;
int i, val;
char buf[20];
memset(buf, '\0', sizeof(buf));
creat("/home/zzh/Desktop/Mmap/010", PMode.mode);
int fd = open("/home/zzh/Desktop/Mmap/010", O_RDWR);
write(fd, "Dawning", sizeof("Dawning"));
struct flock lock;
lock.l_type = F_RDLCK;
lock.l_start = 0;
lock.l_whence = SEEK_SET;
lock.l_len = sizeof(buf);
if((pid = fork()) < 0)
{
TestGet.GET1 = strerror(errno);
return TestGet;
}
else if(pid > 0)
{
if(fcntl(fd, F_GETLK, &lock) < 0)
{
TestGet.GET1 = strerror(errno);
return TestGet;
}
sleep(3);
printf("Parent!\n");
if(waitpid(pid, NULL, 0) < 0)
printf("Waitpid(): %s\n", strerror(errno));
}
else
{
sleep(1);
int fp = open("/home/zzh/Desktop/Mmap/010", O_RDONLY | O_NONBLOCK);
if(read(fd, buf, sizeof(buf)) < 0)
{
TestGet.GET1 = strerror(errno);
return TestGet;
}
else printf("Buf: %s\n", buf);
}
return TestGet;
}
int main()
{
GETX_T TestGet = Test();
printf("%s\n%s\n", TestGet.GET1, TestGet.GET2);
return 0;
}
各位大侠,我在父进程中给文件进行排他加锁,可是在子进程中可以读取次文件,就是说锁根本没有效果,请各位大侠帮忙,看看有什么问题啊!谢谢!
--------------------------------------------------------------------------------
网易历六年耗亿资打造,3D国韵网游《天下贰》,免费领光盘