threads and nanosleep.

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

threads and nanosleep.

Scott Balneaves
Hello all!

Congratulations to Miklos on fuse mainlining in 2.6.14!  It's a great
piece of code!

I'm having a bit of a head scratcher that I'm wondering if someone might
be able to point me in the right direction.

I'm implementing a networked filesystem in fuse called ltspfs, that's
custom tailored to providing local storage device support for thin
clients (cvs at cvs.ltsp.org:/usr/local/cvsroot co ltspfsd and ltspfs).

I've got the basic filesystem working great, and am now into the
fine-tuning phase.  Part of that is to provide a "ping" or keepalive
function in the filesystem, so that dead mounts automatically remove
themselves after a while.

In my mainline, I've got the following:

  pthread_t ping_thread;

  ...

  /*
   * Kick off our pinger thread.
   */

  if (pthread_create(&ping_thread, NULL, (void *)&ping_timeout,
      (void *)NULL) < 0) {
       fprintf(stderr, "Couldn't spawn ping thread\n");
       close(sockfd);
       exit(1);
  }
                                             
  pthread_detach(ping_thread);

  fuse_main(...);

My ping_timeout function looks like the following:

ping_timeout(void *nothing __attribute__((unused)))
{
  char buffer[LTSP_MAXBUF];
  struct timespec time_to_pause;

  time_to_pause.tv_sec = 300;
  time_to_pause.tv_nsec = 0;

  syslog(LOG_INFO, "In the ping thread\n");

  while (1)
  {
    syslog(LOG_INFO, "before sleep\n");
    nanosleep(&time_to_pause, NULL);
    syslog(LOG_INFO, "after sleep\n");
    pthread_mutex_lock(&lock);                  /* Lock mutex */
    writen(sockfd, "PING\n", 5);       /* Send command */
    readline(sockfd, buffer, LTSP_MAXBUF);     /* Read response packet */
    pthread_mutex_unlock(&lock);                /* Unlock mutex */
  }
}

Here's what's got me flummoxed:

If I launch ltspfs with the -d option, it sits in the foreground, prints
the debug info, and the pinger thread works fine.

If I launch it normally, the filesystem works fine, but the pinger
thread hangs indefinitely during the nanosleep call.  I get the "before
sleep" syslog message, but never get the "after sleep" message.

So, why would this work WITH the -d, but not WITHOUT the -d?  I can't
for the life of me figure out what I've messed up.  If anyone can shed
some light on this, I'd appreciate it!

Thanks,
Scott

--
(o_  Scott L. Balneaves | "You are the Universe's only chance to appreciate
//\  Systems Department |  its own beauty."
V_/_ Legal Aid Manitoba |    -- MSG, POE news forums

signature.asc (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: threads and nanosleep.

Miklos Szeredi
> Here's what's got me flummoxed:
>
> If I launch ltspfs with the -d option, it sits in the foreground, prints
> the debug info, and the pinger thread works fine.
>
> If I launch it normally, the filesystem works fine, but the pinger
> thread hangs indefinitely during the nanosleep call.  I get the "before
> sleep" syslog message, but never get the "after sleep" message.
>
> So, why would this work WITH the -d, but not WITHOUT the -d?  I can't
> for the life of me figure out what I've messed up.  If anyone can shed
> some light on this, I'd appreciate it!

This because normally (without '-d') fuse_main() calls the daamon()
function, which puts starts a new process and exits the current one.
So the pinger thread is lost.

One solution is to start the thread from the init() method, which is
called after the process is backgrounded.

Miklos


-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf
_______________________________________________
fuse-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/fuse-devel
Reply | Threaded
Open this post in threaded view
|

Re: threads and nanosleep.

Scott Balneaves
On Mon, Sep 12, 2005 at 04:25:38PM +0200, Miklos Szeredi wrote:

> > Here's what's got me flummoxed:
> >
> > So, why would this work WITH the -d, but not WITHOUT the -d?  I can't
> > for the life of me figure out what I've messed up.  If anyone can shed
> > some light on this, I'd appreciate it!
>
> This because normally (without '-d') fuse_main() calls the daamon()
> function, which puts starts a new process and exits the current one.
> So the pinger thread is lost.
>
> One solution is to start the thread from the init() method, which is
> called after the process is backgrounded.
>
> Miklos
>
Bingo!!  This works perfectly!  Knew I was doing something stupid!

Thanks for the ultra-fast response!

Just some info, Miklos: the next version of Ubuntu (Breezy) will include
Fuse by default.  

Thanks for the help, and thanks for Fuse!  It's amazing that I can
implement a complete network filesystem in less than 2600 lines of code!

Regards,
Scott

--
(o_  Scott L. Balneaves | "You are the Universe's only chance to appreciate
//\  Systems Department |  its own beauty."
V_/_ Legal Aid Manitoba |    -- MSG, POE news forums

signature.asc (196 bytes) Download Attachment