- 论坛徽章:
- 0
|
apueP457Determine whether mandatory locking is supported的例子,我测试下来的结果是我的系统不支持,可是书上前面讲linux2.4的内核是支持的
怎么回事呢
附上书中的测试示例
- #include <stdio.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <sys/wait.h>
- #include <sys/stat.h>
- #include <string.h>
- #include <stdlib.h>
- #include <unistd.h>
- //-----------------------------
- static volatile sig_atomic_t sigflag;
- static sigset_t newmask, oldmask, zeromask;
- static void sig_usr(int signo)
- {
- while((fd = func(a, b, c)) != NULL)
- sigflag = 1;
- return;
- }
- void TELL_WAIT(void)
- {
- if (signal(SIGUSR1, sig_usr) == SIG_ERR)
- {
- printf("signal(SIGUSR1) error:%s\n", strerror(errno));
- return;
- }
- if (signal(SIGUSR2, sig_usr) == SIG_ERR)
- {
- printf("signal(SIGUSR2) error:%s\n", strerror(errno));
- return;
- }
-
- sigemptyset(&zeromask);
- sigemptyset(&newmask);
- sigaddset(&newmask, SIGUSR1);
- sigaddset(&newmask, SIGUSR2);
-
- if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)
- {
- printf("SIG_BLOCK error:%s\n", strerror(errno));
- return;
- }
- }
- void TELL_PARENT(pid_t pid)
- {
- kill(pid, SIGUSR2);
- }
- void WAIT_PARENT(void)
- {
- while (sigflag == 0)
- sigsuspend(&zeromask);
- sigflag = 0;
-
- if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
- {
- printf("SIG_SETMASK error:%s\n", strerror(errno));
- return;
- }
- return;
- }
- void TELL_CHILD(pid_t pid)
- {
- kill(pid, SIGUSR1);
- }
- void WAIT_CHILD(void)
- {
- while (sigflag == 0)
- sigsuspend(&zeromask);
- sigflag = 0;
- if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
- {
- printf("SIG_SETMASK error:%s\n", strerror(errno));
- return;
- }
- return;
- }
- //---------------------------------------------
- static int lock_reg(int fd, int cmd, int type, off_t offset, int whence, off_t len)
- {
- struct flock lock;
- lock.l_type = type;
- lock.l_start = offset;
- lock.l_whence = whence;
- lock.l_len = len;
- return (fcntl(fd, cmd, &lock));
- }
- #define read_lock(fd, offset, whence, len) \
- lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len))
- #define readw_lock(fd, offset, whence, len) \
- lock_reg((fd), F_SETLKW, F_RDLCK, (offset), (whence), (len))
- #define write_lock(fd, offset, whence, len) \
- lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len))
- #define writew_lock(fd, offset, whence, len) \
- lock_reg((fd), F_SETLKW, F_WRLCK, (offset), (whence), (len))
- #define un_lock(fd, offset, whence, len) \
- lock_reg((fd), F_SETLK, F_UNLCK, (offset), (whence), (len))
- //------------------------------------------
- void set_fl(int fd, int setflag)
- {
- int flag;
- if ((flag = fcntl(fd, F_GETFL, 0)) == -1)
- {
- printf("fcntl(F_GETFL) error:%s\n", strerror(errno));
- return;
- }
- flag |= setflag;
- if (fcntl(fd, F_SETFL, flag) == -1)
- {
- printf("fcntl(F_SETFL) error:%s\n", strerror(errno));
- return;
- }
- return;
- }
- //----------------
- int main(int argc, char *argv[])
- {
- int fd;
- pid_t pid;
- char buf[5];
- struct stat statbuf;
- if (argc != 2)
- {
- printf("usage:%s filename\n", argv[0]);
- exit(0);
- }
- if ((fd = open(argv[1], O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)) == -1)
- {
- printf("open() error:%s\n", strerror(errno));
- exit(0);
- }
- if (write(fd, "abcdef", sizeof("abcdef")) != sizeof("abcdef"))
- {
- printf("write() error:%s\n", strerror(errno));
- exit(0);
- }
- if (fstat(fd, &statbuf) == -1)
- {
- printf("fstat() error:%s\n", strerror(errno));
- exit(0);
- }
- if (fchmod(fd, (statbuf.st_mode & ~S_IXGRP) | S_ISGID) == -1)
- {
- printf("fchmod error:%s\n", strerror(errno));
- exit(0);
- }
- TELL_WAIT();
-
- if ((pid = fork()) == -1)
- {
- printf("fork() error:%s\n", strerror(errno));
- exit(0);
- }
- else if (pid > 0)
- {
- if (write_lock(fd, 0, SEEK_SET, 0) < 0)
- {
- printf("write_lock error:%s\n", strerror(errno));
- exit(0);
- }
- TELL_CHILD(pid);
- if (waitpid(pid, NULL, 0) == -1)
- {
- printf("waitpid error:%s\n", strerror(errno));
- }
- }
- else
- {
- WAIT_PARENT();
- set_fl(fd, O_NONBLOCK);
- if (read_lock(fd, 0, SEEK_SET, 0) != -1)
- {
- printf("child:read_lock() succeeded\n");
- exit(0);
- }
- printf("read_lock of already-locked region returns errno = %d(%s)\n", errno, strerror(errno));
- if (lseek(fd, 0, SEEK_SET) == -1)
- {
- printf("lseek() error:%s\n", strerror(errno));
- exit(0);
- }
- if (read(fd, buf, 2) < 0)
- {
- printf("read failed(mandatory locking works)\n");
- exit(0);
- }
- else
- printf("read ok(no mandatory locking), buf = %2.2s\n", buf);
- }
- exit(0);
- }
复制代码 |
|