[Linux-cluster] Re: Simple dlm demo program, and a bug...

Daniel Phillips phillips at redhat.com
Wed Oct 13 15:40:07 UTC 2004


On Wednesday 13 October 2004 11:10, you wrote:
> >  struct dlm_lksb *lksb;
>
> This should point to something.

Thanks Patrick, just a little too bleary-eyed to see that right away...
OK, here is the little libdlm demo that grabs and releases a lock,
without the benefit of the dumbed-down pthreads interface, including
forking off a thread to run the dispatch function.  This should make
things a little easier for the next innocent victim to head into this
part of the woods.

I'd like to encourage you to divide up libdlm as you suggested, there's
no way of getting rid of libpthread otherwise, not to mention that real
men only write reentrant code ;-)

Without the sleeps, the asts never get called, which right and proper.

Regards,

Daniel

------------------

#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <signal.h>
#include <libdlm.h>

#define warn(string, args...) do { fprintf(stderr, "%s: " string "\n", __func__, ##args); } while (0)
#define error(string, args...) do { warn(string, ##args); } while (0)

void my_ast(void *arg)
{
 warn("ast got arg %p", arg);
}

int main(void)
{
 int fd, child;
 struct dlm_lksb lksb;

 if ((fd = dlm_get_fd()) < 0)
  error("dlm error %i, %s", errno, strerror(errno));

 switch (child = fork()) {
 case -1:
  error("fork error %i, %s", 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;
}




More information about the Linux-cluster mailing list