簡體   English   中英

Makefile-服務器后如何同時運行客戶端?

[英]Makefile - How to run concurrently clients after server?

當前,我通過共享內存使用IPC來做一個客戶端-服務器程序,當用make運行此程序時,我有一個小問題,我想運行服務器並在一個make目標中同時運行3個客戶端,但不能運行,但使用2個針對它的目標。 有人可以幫我弄這個嗎? 謝謝!

這對我有用的代碼:

OPT_GCC = -std=c99 -Wall -Wextra
#compiler options and libraries for Linux
OPT = -D_XOPEN_SOURCE=700
LIB = -lrt -lpthread

CLIENTS = 3
MFLAGS = -j$(CLIENTS)

all: client server

client: mediasharingclient.c
    gcc $(OPT_GCC) $(OPT) -o client mediasharingclient.c $(LIB)

server: mediasharingserver.c
    gcc $(OPT_GCC) $(OPT) -o server mediasharingserver.c $(LIB)

run_server: server
   ./server ../sample1/send-order.txt&

run_clients: client1 client2 client3

client1:
    ./client 1 client1

client2:
    ./client 2 client2

client3:
    ./client 3 client3

clean:
   rm -f client server

我這樣做: make run_servermake run_clients -j3

為了在不更改客戶端或服務器實現的情況下完成這項工作,我們將不得不假設服務器將在固定的時間內(例如2秒)准備就緒:

OPT_GCC = -std=c99 -Wall -Wextra
#compiler options and libraries for Linux
OPT = -D_XOPEN_SOURCE=700
LIB = -lrt -lpthread

all: client server

client: mediasharingclient.c
    gcc $(OPT_GCC) $(OPT) -o client mediasharingclient.c $(LIB)

server: mediasharingserver.c
    gcc $(OPT_GCC) $(OPT) -o server mediasharingserver.c $(LIB)

run_server: server
    ./server ../sample1/send-order.txt &
    sleep 2

run_clients: client1 client2 client3

client1: client run_server
    ./client 1 client1

client2: client run_server
    ./client 2 client2

client3: client run_server
    ./client 3 client3

clean:
    rm -f client server

並使用make -j run_clients運行它。 每個客戶端對run_server的依賴關系確保客戶端目標在休眠和啟動服務器時不會與run_server目標並行運行。

其他選擇是讓客戶端反復嘗試幾秒鍾,或者讓服務器派生到后台並在准備好接受連接時終止(然后不要從Makefile在后台運行它)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM