Is it possible to call a command inside a thread

Viewed 210

Generally while writing a c/cpp program, if i ever need to execute any bash command for which there is no c library, i fork a child and then do execvp on it by passing a script name.

But i want to know is it possible to call like this in a thread without forking(if forking then there's no advantage of threads), or is there any other way to do this?

Actual Problem:

Background: there's a commandline utility called criu which can checkpoint a running program and creates a folder containing files for the program state. This command has options to leave the program in running state after creating the checkpoint.

Goal: I've to call this utility multiple times periodically(say in a period of 50 milliseconds). Send this checkpoint folder to the destination(i use rsync utility to do this) asynchronously which means the program should not wait for the transmission of checkpoint to complete as soon as it creates a checkpoint by calling criu utility it resumes for another iteration, wait for another 50 ms and then again makes a call to criu to create a checkpoint.

A diagram will help more to understand the flow of program: see image The blue colored execute duration is for say 50 ms. Do SomeTask(4) and execution is in parallel.

My approach: see below pseudo code for my design of the program:

while(true){

 sleep(50ms) // wait for 50 milliseconds
 
 call criu utility // which takes checkpoint of another running  program.This part is not so much of focus right now, can assume there is something which just creates a folder.

 send the checkpoint to the destination in parallel // using rsync utility for this, so i've no idea how will i build the part where this program will recieve an acknowledgement back and perform another task parallely with the help of threads.

}

Note: program takes a checkpoint of another running program using pid of that program, mentioning this to remove any confusion.

Please comment below if you want further clarifications. I hope this question is not creating any confusions with the word criu, rsync command line utilities. you can forget those and just see the flow which is the main problem right now.

Reason for question statement: I've thought of creating threads instead of forking each time i call a command usng bash script inside this c/cpp program as this program is running infintely at some point it system will have a large number of process created and may throw an error of system resources unavailable if use forking. Thtat's why wanted to know if it's possible to call commands inside a thread.

0 Answers
Related