Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running two processes in parallel from makefile

Tags:

linux

makefile

I am trying to run both a server and client to run from a makefile:

target:

   ./server&
   ./client

The problem is that server& never returns the control back even though I assume it should run in the background. It keeps listening for client which is never invoked as the makefile does not seem to get the control back from server. How can I solve this issue?. without writing any additional target or scripts?

like image 465
Vishal Ladha Avatar asked Feb 09 '12 23:02

Vishal Ladha


People also ask

How do I run a makefile in parallel?

To start GNU Make in parallel mode it's enough to specify either the -j or --jobs option on the command-line. The argument to the option is the maximum number of processes that GNU Make will run in parallel. For example, typing make --jobs=4 will allow GNU Make to run up to four subprocesses in parallel.

What is $@ in makefile?

The file name of the target of the rule. If the target is an archive member, then ' $@ ' is the name of the archive file. In a pattern rule that has multiple targets (see Introduction to Pattern Rules), ' $@ ' is the name of whichever target caused the rule's recipe to be run.


2 Answers

You should be able to do this by combining the commands on a single line:

target:
     ./server& ./client

Make hands commandlines to the shell ($(SHELL)) one line at a time.

Alternatively, you could define two independent targets:

target: run_server run_client

run_server:
     ./server
run_client:
     ./client

and run make with the -j option to make it parallelize build steps:

make -j2

This would not appear the most natural solution for launching your program (e.g. for test) but works best when you have large numbers of build rules that can be partly built in parallel. (For a little more control on make-s parallellization of targets, see also

.NOTPARALLEL

If .NOTPARALLEL is mentioned as a target, then this invocation of make will be run serially, even if the ‘-j’ option is given. Any recursively invoked make command will still run recipes in parallel (unless its makefile also contains this target). Any prerequisites on this target are ignored.

like image 98
sehe Avatar answered Sep 17 '22 13:09

sehe


server runs in background. You may put in foreground using command fg. And then kill it with Ctrl-C

Or maybe this method: killall server

like image 35
mikithskegg Avatar answered Sep 18 '22 13:09

mikithskegg