c - Makefile - 如何在服务器之后同时运行客户端?

标签 c linux shell makefile

目前,我通过共享内存用 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 目标并行运行。

其他选项是让客户端重复尝试几秒钟,或者让服务器 fork 到后台并在准备好接受连接时终止(然后不要从 Makefile 在后台运行它)。

关于c - Makefile - 如何在服务器之后同时运行客户端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36375624/

相关文章:

c - C 中的变量名

c - 如何在C中为未知大小的结构数组分配内存

python - 如何将文件夹中的所有文件类型更改为特定类型?

shell - 有没有adb命令可以启用开发者选项?

c - 整数指针作为函数的参数

c - 为什么 fscanf 只从文件中读取一个字符串的第一个单词

linux - 使用 snmptrap 命令 Linux 发送通知陷阱 SNMP

linux - 必须启用 Payara Admin 才能远程访问 DAS

linux - Shell 脚本 - 日期/时间和用户列表

bash - 使用 AWK 组合三个文件的 Shell 脚本