Message queues

Updated: 2025-09-18

This example shows a model with a complex narrative that becomes natural to express in GPSS-Plus thanks to its primitives for agents, task lists, and event-based synchronization. The simulation represents a simplified restaurant: clients arrive, take a table, place an order, and wait to be served. Cooks (agents) wake up if they were sleeping, take an order from a shared list, and generate dish entities for each task. When all dishes for a client are ready, the client can start eating. All coordination is handled through a shared FIFO task list (using savevalue.push / pop) and a common RESTROOM that regulates sleeping and waking of agents. No SIGNAL, ON_QUEUE, or ON_RELEASE blocks are required, yet the system flows correctly. This model demonstrates how agents can self-manage their workload without external interruptions, simply by polling a shared work queue. Here, the RESTROOM acts as a sleep lock: the first agent to wake up handles the task, while the others continue waiting.

Example

/*

  	 	Message queues
 
*/
SYSTEM {TYPE:PRE_RUN,TRIGGER:PRE_RUN}
START 1000

Facility {NAME:tables,X:221,Y:60,capacity:7}
Restroom {NAME:waitToServe,X:323,Y:60}
Restroom {NAME:sleepingAgents,X:348,Y:357}

POSITION {NAME:EATING, X:579, Y:46}

GRAPHIC {NAME:INFOA_1, TYPE:TEXT, X:149, Y:296, TEXT:"Agent1 ready"}
GRAPHIC {NAME:INFOB_1, TYPE:TEXT, X:147, Y:237, TEXT:"infoB1"}
GRAPHIC {NAME:INFOC_1, TYPE:TEXT, X:147, Y:268, TEXT:"infoC1"}

GRAPHIC {NAME:INFOA_2, TYPE:TEXT, X:577, Y:292, TEXT:"Agent2 ready"}
GRAPHIC {NAME:INFOB_2, TYPE:TEXT, X:576, Y:238, TEXT:"infoB2"}
GRAPHIC {NAME:INFOC_2, TYPE:TEXT, X:576, Y:266, TEXT:"infoC2"}

Graphic {NAME:TextOrder,Type:TEXT,X:270,Y:132,Text:"Order"}
Graphic {NAME:TextA,Type:TEXT,X:271,Y:102,Text:"Not served"}
Graphic {NAME:TextB,Type:TEXT,X:469,Y:84,Text:"Eating"}

;--------------------------------
;		CLIENTS
;--------------------------------
GENERATE 8,5,0,8 {NAME:Clients, X:53, Y:61}
    ADVANCE 3 {TO:tables,FLOW:1}
    seize tables
    call add_order
    rest waitToServe
    ADVANCE 120,30 {FROM:waitToServe,TO:EATING,FLOW:1} ; eating
    release tables
    if (R$(tables,IN)<=0)
	    stop
    endif
ENDGENERATE 1

;**********************************
procedure add_order

    ASSIGN cOven, FLOOR(RANDOM * 4)
    ASSIGN cStove, FLOOR(RANDOM * 6)
    ASSIGN cFryer, FLOOR(RANDOM * 3)
    
    ASSIGN pendingDishes, P$cOven + P$cStove + P$cFryer

    savevalue.push orders, [1,D$N,P$cOven,P$cStove,P$cFryer]
    MOVE {name:TextOrder,text:"Oven: P$cOven ; Stove: P$cStove ; Fryer: P$cFryer"}
    
	; wake up kitchen staff, the first to arrive will take the order
    wake sleepingAgents

ENDPROCEDURE

;--------------------------------
;		COOKS / AGENTS
;--------------------------------

PROCEDURE PRE_RUN
	timeout agent.main,0,1
	timeout agent.main,0,2
	TERMINATE_VE 
ENDPROCEDURE

procedure agent.main
    if (P$PARAM_A==1)
        savevalue nAgent1,D$N
        assign nAgent,1
    else
        savevalue nAgent2,D$N
        assign nAgent,2
    endif
    
    MOVE {NAME:"INFOA_P$nAgent", TEXT:"Agent active P$PARAM_A"}
	savevalue orders, []
    assign taskInProgress,0

	WHILE (1==1)
        if (VD$(orders,LENGTH)<=0)
        	MOVE {NAME:"INFOA_P$nAgent", TEXT:"T: AC1$ Cook P$nAgent : SLEEPING...", color:red}
            rest sleepingAgents
        endif
		assign nTasks,VD$(orders,LENGTH)
		MOVE {NAME:"INFOA_P$nAgent", TEXT:"Cook: I have P$nTasks tasks, Awake", color:green}
  
    	savevalue.pop orders,order
		assign taskInProgress,P$(order.0)
		assign nEntity,P$(order.1)
		assign cOven,P$(order.2)
		assign cStove,P$(order.3)
		assign cFryer,P$(order.4)
  
		if (P$taskInProgress > 0)
        MOVE {NAME:"INFOB_P$nAgent", TEXT:"Processing client P$nEntity"}
        MOVE {NAME:"INFOC_P$nAgent", TEXT:"Oven: P$cOven ; Stove: P$cStove ; Fryer: P$cFryer"}

		assign counter,0
        while (P$counter<P$cOven)
            assign counter,P$counter + 1
            MOVE {NAME:"INFOB_P$nAgent", TEXT:"sending OVEN dish P$counter of P$cOven"}
            advance 8,10
            NEW dishes,0,P$nEntity,"Oven"
        endwhile

		assign counter,0
        while (P$counter<P$cStove)
            assign counter,P$counter + 1
            MOVE {NAME:"INFOB_P$nAgent", TEXT:"sending STOVE dish P$counter of P$cStove"}
            advance 8,10
            NEW dishes,0,P$nEntity,"Stove"
        endwhile

		assign counter,0
        while (P$counter<P$cFryer)
            assign counter,P$counter + 1
            MOVE {NAME:"INFOB_P$nAgent", TEXT:"sending FRYER dish P$counter of P$cFryer"}
            advance 5,5
            NEW dishes,0,P$nEntity,"Fryer"
        endwhile 3

		MOVE {NAME:"INFOC_P$nAgent", TEXT:" "}
		MOVE {NAME:"INFOB_P$nAgent", TEXT:"finished"}
        assign taskInProgress,0
    	endif
    ENDWHILE

    TERMINATE
ENDPROCEDURE

;--------------------------------
;		KITCHEN
;--------------------------------

Facility {NAME:Oven,X:348,Y:566,capacity:2}
Facility {NAME:Stove,X:347,Y:499,capacity:4}
Facility {NAME:Fryer,X:347,Y:437,capacity:3}

Graphic {NAME:TextDishes1,Type:TEXT,X:160,Y:579,Text:"Dishes..."}
Graphic {NAME:TextDishes2,Type:TEXT,X:533,Y:578,Text:"Dishes2..."}

POSITION {NAME:ENDKITCHEN, X:649, Y:509}

GENERATE 0,0,0,0,0 {NAME:dishes, X:69, Y:508}
move {name:TextDishes1,text:"Preparing dish for P$PARAM_A type P$PARAM_B"}
assign targetEntity,P$PARAM_A
assign orderType,"P$PARAM_B"

switch "P$orderType"
case ==,"Oven"
	ADVANCE 4 {TO:Oven,FLOW:1,DECISION:KITCHEN}
    seize Oven
    ADVANCE 40,5
    release Oven
endcase

case ==,"Stove"
	ADVANCE 4 {TO:Stove,FLOW:1,DECISION:KITCHEN}
    seize Stove
    ADVANCE 30,5
    release Stove
endcase

case ==,"Fryer"
	ADVANCE 4 {TO:Fryer,FLOW:1,DECISION:KITCHEN}
    seize Fryer
    ADVANCE 22,3
    release Fryer
endcase

endswitch

ADVANCE 5 {TO:ENDKITCHEN,FLOW:1,MERGE:KITCHEN}
move {name:TextDishes2,text:"Finished dish for P$targetEntity type P$orderType"}

assign pendingDishes, P$(pendingDishes,P$targetEntity) - 1,P$targetEntity

if (P$(pendingDishes,P$targetEntity)<=0)
	wake waitToServe,0,P$targetEntity
endif

ENDGENERATE 0