【问题标题】:Makefile - How to run concurrently clients after server?Makefile - 如何在服务器之后同时运行客户端?
【发布时间】:2016-04-02 16:11:31
【问题描述】:

目前,我通过共享内存使用 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

【问题讨论】:

  • 这适用于我使用 GNU Make 4.1。当您说它适用于 2 个目标时,您的意思是它适用于 2 个客户端(client1 和 client2),还是适用于服务器和一个客户端?
  • 我想告诉你,我做了 2 次(一次用于服务器,一次用于客户端)之前它有 run: run_server run_c1 run_c2 run_c3 并运行 make run -j4 但这不能正常工作,因为服务器没有总是在客户端处理之前执行
  • 但是你能同时运行 2 个客户端吗?您正在运行什么操作系统以及什么品牌的品牌?我不认为问题出在 Makefile 中。
  • 我有 GNU Make 4.0,问题不在于 make,效果很好,但我运行的方式并没有给我预期的流程是逻辑问题,并且 makefile 中没有编程.
  • 你确定 Make 不会运行所有 3 个客户端吗?也许它运行所有这些但不是所有的都可以连接到服务器?

标签: c linux shell makefile


【解决方案1】:

为了在不改变客户端或服务器实现的情况下完成这项工作,我们必须假设服务器将在固定时间内准备好,比如 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 在后台运行它)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-24
    • 1970-01-01
    相关资源
    最近更新 更多