send one message, but child process gets many

Started by csfreebird, August 07, 2016, 06:29:21 AM

Previous topic - Next topic

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:

(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
Quote
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)

rrq

#1
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

#2
Thanks, ralph.

After replace until with if, it works.

(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)
 )
)
    )
  )