PDA

View Full Version : multi proceses



hkullana
11-09-2007, 12:20 PM
hi,

I want to ask that how 2 processes run in linux. In windows, i preapare a .exe file called prog1.exe (win32 console application), then i create a new .exe called prog2. In prog2 i call(CreateProcess()) prog1.exe as a process. Then when i click prog2.exe, a new window is open and prog2.exe runs in the different window. I want to do the same thing in linux(ubuntu). But in linux i can only run my executable through the terminal. I cd to the directory of my prog1 executable and write ./prog1. If i call prog2 inside of prog1 does it create a new terminal window and runs there? How can i achieve that i call a executable from another executable and a new window appears and both of them run together?

Salem
11-09-2007, 12:51 PM
How did you run them?
If you just did system("./prog2"), then yes you'll get another console.

Use fork() and exec() system calls (see the FAQ) to get more control over how parent/child processes are to be run.

brewbuck
11-09-2007, 12:52 PM
I want to ask that how 2 processes run in linux. In windows, i preapare a .exe file called prog1.exe (win32 console application), then i create a new .exe called prog2. In prog2 i call(CreateProcess()) prog1.exe as a process. Then when i click prog2.exe, a new window is open and prog2.exe runs in the different window. I want to do the same thing in linux(ubuntu). But in linux i can only run my executable through the terminal. I cd to the directory of my prog1 executable and write ./prog1. If i call prog2 inside of prog1 does it create a new terminal window and runs there? How can i achieve that i call a executable from another executable and a new window appears and both of them run together?

Instead of running prog2 directly, you can run it through xterm. This will open a new terminal window with the program running in it:



system("xterm -e ./prog2 &");


Or if you want, do the fork()/exec() thing instead of using system().

matsp
11-09-2007, 02:24 PM
How did you run them?
If you just did system("./prog2"), then yes you'll get another console.


Really? I didn't think so... However, I do think that system() waits for the created process to finish before the the owning process gets to run again, so it's not exactly running multiple processes in parallel.

fork()/exec() is the correct solution here.

--
Mats

brewbuck
11-09-2007, 02:38 PM
Really? I didn't think so... However, I do think that system() waits for the created process to finish before the the owning process gets to run again, so it's not exactly running multiple processes in parallel.

system() goes through the shell. Putting '&' at the end of the command will background it and let system() return.

matsp
11-09-2007, 02:52 PM
Brewbuck: Yes of course!

--
Mats

hkullana
11-12-2007, 06:50 AM
here is my program


int main()
{
int pid = fork();

if(pid != 0)
{
cout << "i am the main process" << endl;
}

else
{
cout << "i am the child process" << endl;
}
}


when i cd to the program directory (say my application is "prog"). I write "./prog" without quotes. The it writes:

"i am the main process"
"i am the child process"

in the same terminal. Why does not it opens a new terminal for the child process? how can i achieve that?

Salem
11-12-2007, 06:59 AM
I guess you need to exec() an xterm or something.

matsp
11-12-2007, 07:20 AM
I guess you need to exec() an xterm or something.

Yes, if you want a new window, you'll have to start a new xterm, with your "prog2" running inside it.

--
Mats

hkullana
11-12-2007, 07:30 AM
Yes, if you want a new window, you'll have to start a new xterm, with your "prog2" running inside it.

--
Mats

ok lets say my prog2 is that:



int main()
{
cout <<"i am the child proccess"<< endl;
return 0;
}


Then my prog1 should call prog2 in new window:


int main()
{
cout << "i am the parent proccess"<< endl;
....
return 0;
}


can you please fill the empty part in the prog1 source? :)

or did i get the idea wrong? Should i add something inside of prog2?

thanks,
hkullana

matsp
11-12-2007, 07:32 AM
I guess there are two solutions. The simple one is:


system("xterm prog2");


The complex one is to write your own x-windows application that opens a new window. I have never written such a program myself, so I wouldn't know how
to do that.

It may have got lost, but post #3 actually gives an example similar to the above. Actually looking at "man xterm", it seems like post #3 is correct, mine is missing "-e".

--
Mats

hkullana
11-12-2007, 07:42 AM
so, is it possible to use semaphores with this type of process calling?



system("xterm prog2");

matsp
11-12-2007, 07:48 AM
so, is it possible to use semaphores with this type of process calling?



system("xterm prog2");


Yes you can use semaphores. See http://linux.die.net/man/7/sem_overview

Alsop see my edit above - you need "xterm -e prog2".

--
Mats