send one message, but child process gets many

Q&A's, tips, howto's

send one message, but child process gets many

Postby csfreebird » Sun Aug 07, 2016 1:29 pm

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)
csfreebird
 
Posts: 107
Joined: Tue Jan 15, 2013 11:54 am
Location: China, Beijing

Re: send one message, but child process gets many

Postby ralph.ronnquist » Mon Aug 08, 2016 12:57 pm

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.
ralph.ronnquist
 
Posts: 209
Joined: Mon Jun 02, 2014 1:40 am
Location: Melbourne, Australia

Re: send one message, but child process gets many

Postby csfreebird » Mon Aug 08, 2016 4:00 pm

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)
     )
   )
    )
  )
csfreebird
 
Posts: 107
Joined: Tue Jan 15, 2013 11:54 am
Location: China, Beijing


Return to newLISP in the real world

Who is online

Users browsing this forum: No registered users and 1 guest

cron