unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Problems with Select + threads + pipes
@ 2003-08-05  2:58 Robert Marlow
  0 siblings, 0 replies; 3+ messages in thread
From: Robert Marlow @ 2003-08-05  2:58 UTC (permalink / raw)


[-- Attachment #1: Type: text/plain, Size: 784 bytes --]

Hi all

I'm really stuck on this problem. I have one thread which listens on a
network socket for content. It uses select so the accept bug doesn't
block all threads. I then have another thread which waits for a message
string and then spits it out to another program via a pipe. The
combination of threads + select + pipes seems to cause problems in my
program.

I can get rid of the problem if I lock the mutex while using the pipe,
however this defeats the purpose of using threads; I don't want the time
consuming piped process wasting time that could be used by other
threads.

I have attached two trimmed-down scripts as an example of my problem.
I'd really, really appreciate it if someone could shed some light on
what the problem is. Thanks.


-- 
Regards,

Robert Marlow




[-- Attachment #2: buggy.scm --]
[-- Type: text/x-scheme, Size: 1721 bytes --]

#!/usr/bin/guile \
-e main -s
!#

(use-modules (ice-9 threads)
	     (ice-9 popen))

(define mutex (make-mutex))
(define message-ready (make-condition-variable))

(define message "")

(define (main args)
  (begin-thread (piper))
  (let ((sock #f)
        (fileno #f))
    (lock-mutex mutex)
    (set! sock (socket AF_INET SOCK_STREAM 0))
    (unlock-mutex mutex)
    (bind sock AF_INET INADDR_ANY 6009)
    (listen sock 5)
    (while
     #t
     (let* ((client-connection (accept/no-block sock))
            (client-details (cdr client-connection))
            (client (car client-connection)))
       (lock-mutex mutex)
       (do ((line (read-line client) (read-line client)))
	   ((eof-object? line)
	    (shutdown client 2))
	 (set! message (string-append message line "\n")))
       (signal-condition-variable message-ready)
       (unlock-mutex mutex)))))


(define (piper)
  (while
   #t
   
   (lock-mutex mutex)
   (if (not (> (string-length message) 0))
       (wait-condition-variable message-ready mutex)
       (let ((outgoing message))
	 (set! message "")
	 (unlock-mutex mutex)  ; Comment this to stop select complaining
	 (let ((pipe (open-output-pipe "./buggy-companion.scm")))
	   (display "Print this\n" pipe)
	   (close-port pipe)))) ; Remove 3 brackets to stop select complaining
;	   (unlock-mutex mutex)))) ; Uncomment this to stop select complaining
   (unlock-mutex mutex)))


(define (accept/no-block s)
  (if (null? (car (select (vector s) '() '()))) ; This select complains
      (accept/no-block s)
      (let ((client #f))
        (lock-mutex mutex)
        (set! client (accept s))
        (unlock-mutex mutex)
        client)))

[-- Attachment #3: buggy-companion.scm --]
[-- Type: text/x-scheme, Size: 242 bytes --]

#!/usr/bin/guile \
-e main -s
!#

(define (main args)
  (let ((str ""))
    (do ((line (read-line) (read-line)))
	((eof-object? line))
      (set! str (string-append str line "\n")))
     (sleep 10)
     (display str)))
	    
    

[-- Attachment #4: Type: text/plain, Size: 139 bytes --]

_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-user

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Problems with Select + threads + pipes
@ 2003-08-05  4:15 Robert Marlow
  2003-08-05 13:51 ` Robert Marlow
  0 siblings, 1 reply; 3+ messages in thread
From: Robert Marlow @ 2003-08-05  4:15 UTC (permalink / raw)


*sigh* seems that's not quite the same bug. My actual program mutex
locks during the pipe creation and closing times but unlocks for
everything imbetween. If I do that in these example script it fixes the
bug so it seems the bug produced by these scripts is slightly different.
I'm highly confused. Does anyone know if there's bugs in the guile
implementation of select? I forgot to mention but the error I keep
getting is as follows:

program.scm:379:19: In procedure select in expression (select (vector s)
(quote ()) ...):
program.scm:379:19: Bad file number
Broken Pipe

Anyway, I'll continue to see if I can narrow down where this elusive bug
is and post again then *mutter mutter*


On Tue, 2003-08-05 at 10:58, Robert Marlow wrote:
> Hi all
> 
> I'm really stuck on this problem. I have one thread which listens on a
> network socket for content. It uses select so the accept bug doesn't
> block all threads. I then have another thread which waits for a message
> string and then spits it out to another program via a pipe. The
> combination of threads + select + pipes seems to cause problems in my
> program.
> 
> I can get rid of the problem if I lock the mutex while using the pipe,
> however this defeats the purpose of using threads; I don't want the time
> consuming piped process wasting time that could be used by other
> threads.
> 
> I have attached two trimmed-down scripts as an example of my problem.
> I'd really, really appreciate it if someone could shed some light on
> what the problem is. Thanks.
> 
> 
> -- 
> Regards,
> 
> Robert Marlow


-- 
Regards,

Robert Marlow







_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Problems with Select + threads + pipes
  2003-08-05  4:15 Problems with Select + threads + pipes Robert Marlow
@ 2003-08-05 13:51 ` Robert Marlow
  0 siblings, 0 replies; 3+ messages in thread
From: Robert Marlow @ 2003-08-05 13:51 UTC (permalink / raw)


[-- Attachment #1: Type: text/plain, Size: 467 bytes --]

Ok, this time it gives the error. I just needed to start more threads.
If anyone can point out the cause of this bug I'd be very appreciative.
Just chuck the buggy.scm and buggy-companion.scm in the same directory,
run the former and start dumping stuff on port 6008. Multiple connection
attempts may be needed to see it happen.

Error message should look something like:
ERROR: In procedure select:
ERROR: Bad file descriptor


Thanks!


-- 
Regards,

Robert Marlow

[-- Attachment #2: buggy.scm --]
[-- Type: text/x-scheme, Size: 1801 bytes --]

#!/usr/bin/guile \
-e main -s
!#

(use-modules (ice-9 threads)
             (ice-9 popen))

(define mutex (make-mutex))
(define message-ready (make-condition-variable))

(define message "")

(define (main args)
  (begin-thread (thread))
  (begin-thread (thread))
  (begin-thread (thread))
  (begin-thread (thread))
  (begin-thread (thread))
  (let ((sock #f)
        (fileno #f))
    (lock-mutex mutex)
    (set! sock (socket AF_INET SOCK_STREAM 0))
    (unlock-mutex mutex)
    (bind sock AF_INET INADDR_ANY 6008)
    (listen sock 5)
    (while
     #t
     (let* ((client-connection (accept/no-block sock))
            (client-details (cdr client-connection))
            (client (car client-connection)))
       (lock-mutex mutex)
       (do ((line (read-line client) (read-line client)))
           ((eof-object? line)
            (shutdown client 2))
         (set! message (string-append message line "\n")))
       (signal-condition-variable message-ready)
       (unlock-mutex mutex)))))


(define (thread)
  (while
   #t
   
   (lock-mutex mutex)
   (if (not (> (string-length message) 0))
       (wait-condition-variable message-ready mutex)
       (let ((outgoing message))
         (set! message "")
	 (unlock-mutex mutex)
	 (piper outgoing)))
   (unlock-mutex mutex)))


(define (piper message)
  (lock-mutex mutex)
  (let ((pipe (open-output-pipe "./buggy-companion.scm")))
    (display message pipe)
    (close-port pipe))
  (unlock-mutex mutex))
    



(define (accept/no-block s)
  (if (null? (car (select (vector s) '() '()))) ; This select complains
      (accept/no-block s)
      (let ((client #f))
        (lock-mutex mutex)
        (set! client (accept s))
        (unlock-mutex mutex)
        client)))


[-- Attachment #3: buggy-companion.scm --]
[-- Type: text/x-scheme, Size: 236 bytes --]

#!/usr/bin/guile \
-e main -s
!#

(define (main args)
  (let ((str ""))
    (do ((line (read-line) (read-line)))
        ((eof-object? line))
      (set! str (string-append str line "\n")))
     (sleep 10)
     (display str)))

[-- Attachment #4: Type: text/plain, Size: 139 bytes --]

_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-user

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2003-08-05 13:51 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-08-05  4:15 Problems with Select + threads + pipes Robert Marlow
2003-08-05 13:51 ` Robert Marlow
  -- strict thread matches above, loose matches on Subject: below --
2003-08-05  2:58 Robert Marlow

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).