Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

178
Views
Modern C++ way of starting and terminating a linux program

I have a C++ program that, based on user input, needs to start and stop a given Linux program.

I've simplified the logic I'm currently using in the following code:

int pid = fork();

if (pid == -1)
{
    //Handle error
}
else if (pid == 0)
{
    execlp("my_program", nullptr);
    exit(0);
}
else
{
    //Main program stuff

    if(/* user selects close "my_program"*/)
    {
        kill(pid, SIGTERM);
    }

    //Other main program stuff
}

Everything is working, but I was wondering if there were any other approaches, maybe more in the modern C++ style, that could be used in this situation.

Thanks in advance.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Take a look at boost::process.

There are several ways a process can be spawned. For example, in your example, you fork the process, but you don't close the cloned file descriptors in the child process. That is an often forgotten practice by those that invoke fork and can lead to binding to port problems (port/address already in use) or other hard-to-debug issues.

Even boost didn't do that quite correctly for some time.

over 4 years ago · Santiago Trujillo Report

0

The modern way would be to use a library like Boost.Process https://www.boost.org/doc/libs/1_78_0/doc/html/process.html or Qt https://doc.qt.io/qt-5/qprocess.html.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!