hiaslosch17
hiaslosch17

Reputation: 67

GNU Makefile parallel execution of two files

i am using makefile for the first time and i want to execute two files at the same time. It is a server and a client file, every file has it's own main, and the two files communicate with each other. Till now i have written following Makefile:

all: server client

server: server.c
        gcc server.c -o server
        ./server

client: client.c
        gcc client.c -o client
        ./client

So i want to start the server first so the client can connect to it afterwards. And this is my problem: If i execute the makefile only the server starts. Is it possible to start the client directly after the server instead of only one file at a time?

Upvotes: 0

Views: 452

Answers (1)

Halzephron
Halzephron

Reputation: 328

It's very unusual to try to both build and run programs from a Makefile (except for running automated regression tests, for example.)

Normally, you run make, then run the code. Personally, I'd start the sever in another terminal window then run the client in the first one.

But if you do want to do this from the Makefile, you could try the following:

all: server client run

server: server.c
    gcc server.c -o server

client: client.c
    gcc client.c -o client

run:
    ./server &
    ./client
    killall server

The last line kills off the server after the client has finished. This may not be necessary if the server has some other means of shutting itself down but if it doesn't, you'll end up with a lot of copies of the server processes.

You might find it easier to write a separate shell script to run your tests.

Upvotes: 3

Related Questions