How to fork or create thread?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: How to fork or create thread?

Threaded View

  1. #1
    Join Date
    Dec 2012
    Posts
    13

    How to fork process or create thread?

    How can I implement the paradigm demonstrated by the code below to run on WinXP?

    I want to fork a process or create a thread that shares global variables with the parent process/thread.

    The child process/thread monitors the progress of the parent process/thread.

    I cannot find documentation for a fork function per se, a Unix term. It might be called something different for WinXP.

    I would be happy to use threads instead. But I'm rusty even with Unix application threads; and I know nothing of WinXP application threads.

    So I would appreciate it if someone would be kind enough to provide a turnkey implementation that demonstrates the simplest use of process or thread functions for my purpose demonstrated below.

    In either case, do "forked" processes and threads share global address space in WinXP, as they do in Unix?

    I hope so. I would prefer to avoid the overhead of IPC mechanisms. The "overhead" includes my own relearning curve. :-(

    PS: Arrgghh! The GUI screwed up my indentation. I would try to correct it. But the proper indentation appears when I edit the posting. I suppose I need to insert real tabs. Haven't figured out how (yet). My apologies for the unreadability.

    PPS: Double arrgghh! Not even real tabs work; and I cannot get the "paste as text" button to behave as I expect. Can someone tell me the trick for posting indented text in this GUI?

    -----

    #include "stdafx.h"
    #include <stdlib.h>
    #include <Windows.h>

    long curCount;
    int isRunning;

    int _tmain(int argc, char* argv[])
    {
    curCount = 0;
    isRunning = 1;
    if (fork(NULL) == 0) {
    // child (monitor)
    const long delay = 500; // msec
    while (isRunning) {
    printf("%ld\n", curCount);
    Sleep(delay);
    }
    printf("%ld\n", curCount);
    }
    else {
    // parent (do work)
    const long delay = 50; // msec
    long i = 0;
    Sleep(delay); // for child set-up
    for (curCount = 1; curCount <= 1000000; curCount++) {
    i++;
    if (i == 10000) {
    Sleep(delay);
    i = 0;
    }
    }
    isRunning = 0;
    }
    }
    Last edited by joeu2004; December 23rd, 2012 at 04:31 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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center