fork for windows ???


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: fork for windows ???

  1. #1
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819

    Arrow fork for windows ???

    Hey everybody ..
    just taking a look in Netbeans examples for c++, and found this one for Multi-Processing :

    PHP Code:
    /*
     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     *
     * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
     *
     * The contents of this file are subject to the terms of either the GNU
     * General Public License Version 2 only ("GPL") or the Common
     * Development and Distribution License("CDDL") (collectively, the
     * "License"). You may not use this file except in compliance with the
     * License. You can obtain a copy of the License at
     * http://www.netbeans.org/cddl-gplv2.html
     * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
     * specific language governing permissions and limitations under the
     * License.  When distributing the software, include this License Header
     * Notice in each file and include the License file at
     * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
     * particular file as subject to the "Classpath" exception as provided
     * by Sun in the GPL Version 2 section of the License file that
     * accompanied this code. If applicable, add the following below the
     * License Header, with the fields enclosed by brackets [] replaced by
     * your own identifying information:
     * "Portions Copyrighted [year] [name of copyright owner]"
     *
     * Contributor(s):
     *
     * The Original Software is NetBeans. The Initial Developer of the Original
     * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
     * Microsystems, Inc. All Rights Reserved.
     *
     * If you wish your version of this file to be governed by only the CDDL
     * or only the GPL Version 2, indicate your decision by adding
     * "[Contributor] elects to include this software in this distribution
     * under the [CDDL or GPL Version 2] license." If you do not indicate a
     * single choice of license, a recipient has the option to distribute
     * your version of this file under either the CDDL, the GPL Version 2 or
     * to extend the choice of license to its licensees as provided above.
     * However, if you add GPL Version 2 code and therefore, elected the GPL
     * Version 2 license, then the option applies only if the new code is
     * made subject to such option by the copyright holder.
     */

    #include <sys/types.h>
    #include <unistd.h>
    #include <iostream.h>

    int main(int argcchar**argv) {
        
    pid_t pid;
        
    pid_t f_res;

        
    // Prints welcome message...
        
    cout << "Welcome ...\n";
        
    pid=getpid();

        
    // Prints arguments...
        
    if (argc 1) {
            
    f_res fork();
            if (
    == f_res) {
                
    /* Child */
                
    pid=getpid();
                
    cout << "\nPID child = " << pid << "\n";
                
    _exit(0);
            }
            
    cout << "PID parent = " << pid  << "  PID child = " << f_res << "\n";
            
    cout << "\nArguments:\n";
            for (
    int i 1argci++) {
                
    cout << << ": " << argv[i] << "\n";
            }
        }
        return 
    0;

    this c source compile and run just good, I'm asking how "fork" is working in windows ? windows doesn't support the "fork" method for processes .. it has only the "CreatProcess" API which doesn't follow the fork method so how this code is compiled and working in windows ? I mean what's going on exactly ??
    I don't think the compiled exe is some system independent for example !!
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  2. #2
    Join Date
    Dec 2007
    Posts
    401
    netbeans uses the toolset from cygwin. it provides a linux-like environment on windows. It consists of two parts: A DLL (cygwin1.dll) which acts as a Linux API emulation layer providing substantial Linux API functionality. and a collection of tools which provide Linux look and feel.

    The fork call in Cygwin is particularly interesting because it does not map well on top of the Win32 API. This makes it very difficult to implement correctly. Currently, the Cygwin fork is a non-copy-on-write implementation similar to what was present in early flavors of UNIX.

    The first thing that happens when a parent process forks a child process is that the parent initializes a space in the Cygwin process table for the child. It then creates a suspended child process using the Win32 CreateProcess call. Next, the parent process calls setjmp to save its own context and sets a pointer to this in a Cygwin shared memory area (shared among all Cygwin tasks). It then fills in the child's .data and .bss sections by copying from its own address space into the suspended child's address space. After the child's address space is initialized, the child is run while the parent waits on a mutex. The child discovers it has been forked and longjumps using the saved jump buffer. The child then sets the mutex the parent is waiting on and blocks on another mutex. This is the signal for the parent to copy its stack and heap into the child, after which it releases the mutex the child is waiting on and returns from the fork call. Finally, the child wakes from blocking on the last mutex, recreates any memory-mapped areas passed to it via the shared area, and returns from fork itself.

    While we have some ideas as to how to speed up our fork implementation by reducing the number of context switches between the parent and child process, fork will almost certainly always be inefficient under Win32. Fortunately, in most circumstances the spawn family of calls provided by Cygwin can be substituted for a fork/exec pair with only a little effort. These calls map cleanly on top of the Win32 API. As a result, they are much more efficient. Changing the compiler's driver program to call spawn instead of fork was a trivial change and increased compilation speeds by twenty to thirty percent in our tests.

    However, spawn and exec present their own set of difficulties. Because there is no way to do an actual exec under Win32, Cygwin has to invent its own Process IDs (PIDs). As a result, when a process performs multiple exec calls, there will be multiple Windows PIDs associated with a single Cygwin PID. In some cases, stubs of each of these Win32 processes may linger, waiting for their exec'd Cygwin process to exit.
    from cygwin documentation http://cygwin.com/cygwin-ug-net/high...#ov-hi-process

  3. #3
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    That's really great thanks a lot vijayan .. but like that seems I have to include "cygwin1.dll" in the setup package as well ? (in case I use any of those system calls I mean)
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  4. #4
    Join Date
    Dec 2007
    Posts
    401
    > seems I have to include "cygwin1.dll" in the setup package as well ?
    yes, cygwin needs to be installed on the target machine. cygwin is freely redistributable; by default, all executables link against the cygwin library (and in the process include GPL'd cygwin glue code).
    to cover the GNU GPL requirements, the basic rule is if you give out any binaries, you must also make the source code available.

  5. #5
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    The "Open Source" is not my problem now but installing cygwin in the target machine doesn't sounds good ... I'm not inclined to make my project like that .. is this the case for any compiled project using g++ (cygwin's) under netbeans ? I haven't noticed that before .. actually I was plaining to MOVE from VS 6 to netbeans but I think something like that may prevent me or what do you think ?
    thanks anyway
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  6. #6
    Join Date
    Dec 2007
    Posts
    401
    the other option available would be to use netbeans c/c++ pack with MinGW instead of Cygwin.

    Cygwin relies on a Unix emulation layer, code compiled with Cygwin uses the standard GNU runtime library (glibc) on an emulation of Unix system calls like fork, and semantics like mount points, pipes and path separators.

    MinGW uses native Windows DLLs for everything; code compiled with MinGW will use standard Microsoft runtime libraries such as MSVCRT.DLL; so no special setup is required on target machines. MinGW is also available under a non-restrictive free license; so you do not have any GPL issues. many popular open-source applications (like Firefox) have their Windows releases compiled using it.

    for instructions on installing MinGW for use with netbeans c/c++ pack, see
    http://www.netbeans.org/community/magazine/html/03/c++/ (scroll all the way down to the sidebar titled 'Installing MinGW')

    other ide/compiler options that you have are
    microsoft visual c++ express 2008 http://www.microsoft.com/express/vc/
    code::blocks (MinGW based) http://www.codeblocks.org/
    turbo c++ explorer http://www.turboexplorer.com/cpp/
    Last edited by vijayan; 02-08-2008 at 11:21 AM.

  7. #7
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    that was very helpful, thanks vijayan ... at lest that was a very important thing to know before doing anything ... thanks again :)
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  8. #8
    Join Date
    Nov 2003
    Posts
    4,118
    fork() is a POSIX function which isn't supported in Windows. The Windows/DOS way of launching a child process is calling _spawnl() or any other member of the spawn() family.
    Danny Kalev

Similar Threads

  1. between fork, exec, & system
    By debkumar in forum C++
    Replies: 2
    Last Post: 02-09-2006, 11:24 AM
  2. SQL Tutorial (Answer Q's & post your reply)
    By bigbastard4 in forum Database
    Replies: 2
    Last Post: 05-16-2001, 06:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center