Hi!

I am developing a project in which i want to run 2 programs simultaneously in such a way that when the user runs 1st program,the 2nd program gets called from the 1st program and now both the 1st program and the 2nd program start executing simultaneously.

For ex :Suppose there is a program called work.exe in my current directory.

now i would call it in the following code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#inclue<dos.h>

void main()
{
       system("work.exe");

        while(1)
        {
               printf("Hi!,How are you?");
         }
}

The work.exe prints ("Hello!") on the screen.

Now i want that both "Hello!" and "Hi!How are you?" gets printer simultaneosly and contineously on the screen.

Normally what happens that when we call another program from our code through the system command,then till the execution of the called program stops,the code of the calling program doesn't gets executed.

Hence i want that both the called program and the calling program gets executed simultaneously.

Any suggestions,Kindly help.I am just stucked very badly in my project due to this.

Thanks in advance.

Regards
Rituraj Raina

Dani AI

Generated

Short answer: run the second program without blocking the first, or merge the work into one program and run the two tasks concurrently (threads). The original code uses a blocking call, so the caller will wait until the called program exits. There are two practical paths depending on whether you have the source for work.exe.

If you have work.cpp (source): don’t #include the .cpp file into another translation unit. Instead extract the reusable logic into a function or a library and call it from a single program. That lets you spawn a thread for the “Hello” work while the main thread prints “Hi” — threads share the same console and memory, so output coordination and data sharing are straightforward. ’s idea of turning the work into a callable unit is on the right track; do it cleanly by refactoring and linking rather than textually including .cpp.

If you only have the binary: start it as an independent process that does not block. As suggested, use the OS process-creation facility rather than a blocking system call. On Windows you can create the child process and choose whether it uses the same console or a new one; on POSIX you’d fork/exec or run the job in the background. If you need both programs’ text interleaved in one console, redirect the child’s stdout into a pipe and have the parent read and print it (this avoids separate windows and gives you control over ordering).

Troubleshooting tips: add small sleeps in the loops so the console isn’t flooded; explicitly flush stdout after prints to avoid buffering surprises; check whether the child was launched with a new console (that will put output in another window). If concurrency or IPC is new to you, refactoring work.exe into a callable module and using threads is usually the simplest way to get clean, simultaneous output.

Recommended Answers

All 2 Replies

If your working with MFC, theres a function

CreateProcess(.........)

That will launch an exe in its own thread. Its up to you to keep peeking at any messages it sends. Dont know if this is what you want, but its the closest thing i could think of

i think after the execution of "work.exe" the c++ exits from the whole process. to stop this don`t use void main() in the "work.cpp" but use any other function, say
void printhello()

and then do whatever u want to in the "work.cpp" and close the function.
now after creating the first program, in the second proram include the file work.cpp with the other header files like this
#include<stdio.h>
#include"work.cpp"

and then call the function from anywhere inside the main() function and it will work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.