multi proceses

This is a discussion on multi proceses within the Linux Programming forums, part of the Platform Specific Boards category; hi, I want to ask that how 2 processes run in linux. In windows, i preapare a .exe file called ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    4

    multi proceses

    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?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by hkullana View Post
    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:

    Code:
    system("xterm -e ./prog2 &");
    Or if you want, do the fork()/exec() thing instead of using system().

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Salem View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by matsp View Post
    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.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Brewbuck: Yes of course!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    4
    here is my program
    Code:
    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?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    I guess you need to exec() an xterm or something.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Salem View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    4
    Quote Originally Posted by matsp View Post
    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:

    Code:
    int main()
    {
    cout <<"i am the child proccess"<< endl;
    return 0;
    }
    Then my prog1 should call prog2 in new window:
    Code:
    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

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I guess there are two solutions. The simple one is:
    Code:
    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
    Last edited by matsp; 11-12-2007 at 07:35 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Nov 2007
    Posts
    4
    so, is it possible to use semaphores with this type of process calling?

    Code:
    system("xterm prog2");

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by hkullana View Post
    so, is it possible to use semaphores with this type of process calling?

    Code:
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi dimensional array
    By $l4xklynx in forum C Programming
    Replies: 7
    Last Post: 01-03-2009, 03:56 AM
  2. Help printing a multi array
    By cjohnman in forum C Programming
    Replies: 4
    Last Post: 05-05-2008, 02:35 PM
  3. multi dimensional array at runtime
    By sawer in forum C++ Programming
    Replies: 21
    Last Post: 05-10-2006, 11:59 PM
  4. starting to learn multi threading
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 06-09-2004, 02:44 PM
  5. Replies: 6
    Last Post: 04-26-2004, 11:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21