1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <signal.h>
#include <libdlm.h>
/*
* Simple libdlm locking demo
*
* Daniel Phillips, phillips@redhat.com
*
* (c) 2004, Redhat Software Inc.
*/
#define error(string, args...) do { printf(string, ##args); exit(1); } while (0)
void my_ast(void *arg)
{
printf("ast got arg %p\n", arg);
}
int main(void)
{
int fd, child;
struct dlm_lksb lksb;
if ((fd = dlm_get_fd()) < 0)
error("dlm error %i, %s\n", errno, strerror(errno));
switch (child = fork()) {
case -1:
error("fork error %i, %s\n", errno, strerror(errno));
case 0:
while (1)
dlm_dispatch(fd);
}
if (dlm_lock(LKM_PWMODE, &lksb, LKF_NOQUEUE, "foo", 3,
0, my_ast, (void *)&fd, NULL, NULL) < 0)
error("dlm error %i, %s\n", errno, strerror(errno));
sleep(1);
if (dlm_unlock(lksb.sb_lkid, 0, &lksb, NULL) < 0)
error("dlm error %i, %s\n", errno, strerror(errno));
sleep(1);
kill(child, SIGTERM);
return 0;
}
|