Results 1 to 13 of 13

Thread: fork vs. exec

  1. #1

    Thread Starter
    Hyperactive Member Sneeden's Avatar
    Join Date
    Oct 2001
    Location
    Sneedville
    Posts
    258

    fork vs. exec

    I need to exec a process. I have been using:
    Code:
    Form fMain = new frmMain();
    fMain.Show();
    but of course that just forks another form. I want the child to stay alive after the parent has been killed.

    Is there a way to do this without shelling itself? I mean, I suppose I could:
    Code:
    System.Diagnostics.Process proc = new System.Diagnostics.Process();
    proc.StartInfo.FileName="path to self";
    proc.Start();
    How to you reference the app path? Like VB's app.path & "\" & app.exename?
    Are there actual fork and exec type commands in c#?
    balls deep in bad code

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: fork vs. exec

    With no VB6 experience, I'm guessing that Application.StartupPath and Application.ExecutablePath are what you are after.

    In this situation your child window will only close if its parent is the app's startup object. If you use a Main method to start your app you can arrange it so that closing your first form will not exit the app if other forms are still open.

    You'll be pleased to know that VS 2005 has a project property that defines whether the app exits when the first window is closed or when all windows are closed.

    Edit:
    The middle paragraph is not 100% accurate. Even with a Main method as the startup object you do still need to do something other than just call Application.Run(new Form1()). I don't know specifically as I've never done it but I've seen examples of others who've done it.
    Last edited by jmcilhinney; Jun 10th, 2005 at 12:42 AM.

  3. #3
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    Re: fork vs. exec

    i could be wrong here but i'm pretty sure there is no way to keep a child alive if you kill the parent. Unless you change the child's parent, which i've never done. but that's worth a try.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: fork vs. exec

    Let's say that your app consists of two forms, with form1 being the startup object. At the moment, form1 opens form2, so if form1 is closed then form2 closes and the whole app exits. If you make a Main method the startup object and open both forms independently from that method, there is no parent-child relationship between the forms and the app doesn't exit until the Main method completes.

  5. #5
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    Re: fork vs. exec

    what is a 'fork' anyway? is that a c++ thing?

  6. #6

    Thread Starter
    Hyperactive Member Sneeden's Avatar
    Join Date
    Oct 2001
    Location
    Sneedville
    Posts
    258

    Re: fork vs. exec

    (My text diagram didn't come out so good. Just imaging that the pipes are below the "fork" calls)

    Fork is a multithreading thing. It is not specific to C++. Any language that supports threading will have fork and exec (c, java, c++, c#,etc..)

    Fork:
    original process---->fork---->original process
    |
    |------>second process

    Exec:
    original process---->exec---->original process
    |
    |------>Shelled process (like notepad or something

    In exec, you can call it so that the original process will wait for a return from the shelled process, or not wait.

    Each process can spawn as many processes as it wants.
    balls deep in bad code

  7. #7
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    Re: fork vs. exec

    very interesting indeed. I'm gonna do some more reasearch on that. I've never heard that term. I only started using multi-threading about a year ago though

  8. #8

    Thread Starter
    Hyperactive Member Sneeden's Avatar
    Join Date
    Oct 2001
    Location
    Sneedville
    Posts
    258

    Re: fork vs. exec

    How could you do multithreading and not know about forking? Well, I guess on Windows it is called CreateProcess()? Doesn't it have like 9 parameters or something? On linux it is called fork() and has very few parameters. Also when you fork in windows, the child starts from the beginning of the routine that called CreateProcess, where as in linux the child starts at the fork statement with all the variables initialized to the same contents as the parent.
    balls deep in bad code

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: fork vs. exec

    In VB.NET, you create a new thread and then call Thread.Start. This is what you are calling forking. If you want to create a new, independent process you call Process.Start. If you do a help search on "fork" you'll get a grand total of 3 results. The term is not commonly used in .NET. Create a new thread or create a new process. In either case, you can wait for the new entity to complete, using Thread.Join or Process.WaitForExit, or you can carry on execution.

  10. #10
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    Re: fork vs. exec

    that's one problem my last supervisor had. He had NO CLUE about .net and was using older, unused terms to try and explain how he wanted things done. He never learned the .net framwork and insisted that vb6 code, for example, would simply port over to .net line by line. the framwork has done a lot to hide certain low-level processes from the developer and if that developer is new, he'll never have a clue about the low-level stuff that had to be done by hand.

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: fork vs. exec

    Quote Originally Posted by jmcilhinney
    In VB.NET, you create a new thread and then call Thread.Start. This is what you are calling forking. If you want to create a new, independent process you call Process.Start. If you do a help search on "fork" you'll get a grand total of 3 results. The term is not commonly used in .NET. Create a new thread or create a new process. In either case, you can wait for the new entity to complete, using Thread.Join or Process.WaitForExit, or you can carry on execution.
    I do the majority of my posting in the VB.NET forums and I just realised that this is the C# forum. It would be reasonable to believe that, since C# is based on C, fork might be a more common term than it is in VB. However, while a help search filtered on VB yields 3 results for fork, a search filtered on C# yields none.

  12. #12
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    Re: fork vs. exec

    I know is straying from the subject but...I did a search and i got NOTHING from my help files...I havent searched the online MSDN but rather my installed help files.

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: fork vs. exec

    Like I said, Visual Basic returns 3 matches and C# returns none. C++ returns 19 matches, including one entitled "Port from UNIX to Win32" that may be of interest. It mentions the CreateProcess and CreateThread APIs, although using .NET there are other methods available so APIs are unnecessary.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width