send one message, but child process gets many

Q&A's, tips, howto's
Post Reply
csfreebird
Posts: 107
Joined: Tue Jan 15, 2013 11:54 am
Location: China, Beijing
Contact:

send one message, but child process gets many

Post by csfreebird »

Hi,
I am using cilk api, one parent process sends one message to 5 child processes. But each child process receives the message many times. Why?
Following is my code:

Code: Select all

(load "args.lsp")
(parse-args)

(setq process-number (int (ArgsTree "--process-number")))

(define (child-process)
  (setq ppid (sys-info 6)) ; get parent pid
  (setq cur-pid (sys-info 7))
  (while true
    (until (receive ppid msg)
      (append-file (string cur-pid ".log") (string msg "\n"))
      (sleep 1000)
      )
    )
  )

; parent starts child processes
(dotimes (i process-number)
  (spawn 'result (child-process) true)
  )

;; parent send one msg to each child process
(setf child-process-list (sync))
(println "child-process-list: " child-process-list)
(dolist (cpid child-process-list)
  (println "child process: " cpid)
  (send cpid "test msg")
  )

;; quit in 10 seconds
(sleep 10000)
(abort)
(exit)
to spawn 5 child processes, use this command
./run-task.lsp --process-number=5

Then in five child process log files, I have these
nil
test msg
test msg
test msg
test msg
test msg
test msg
test msg
test msg
test msg
My questions are:
1. why get nil message
2. why get one message repeatedly
3. why receive function needs parent process id(sender id) instead of child process id(receiver id)

ralph.ronnquist
Posts: 216
Joined: Mon Jun 02, 2014 1:40 am
Location: Melbourne, Australia

Re: send one message, but child process gets many

Post by ralph.ronnquist »

1 and 2: The printout happens in the until clause body, i.e. "until there is a message, keep printing the last received message." 10 times = 10 seconds. First one nil due to thread race.

3. I suppose it's to identify the sender; the child process could have its own children sending to it.

csfreebird
Posts: 107
Joined: Tue Jan 15, 2013 11:54 am
Location: China, Beijing
Contact:

Re: send one message, but child process gets many

Post by csfreebird »

Thanks, ralph.
After replace until with if, it works.

Code: Select all

(define (child-process)
  (setq ppid (sys-info 6)) ; get parent pid
  (setq cur-pid (sys-info 7))
  (while true
    (if (receive ppid msg)
	(begin
	  ;; get new message from queue
	  (append-file (string cur-pid ".log") (string msg "\n"))
	  )
	(begin
	  ;; get nothing from queue, sleep 1 second
	  (sleep 1000)
	  )
	)
    )
  )

Post Reply