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;
}
}
Re: How to fork process or create thread?
Quote:
Originally Posted by
joeu2004
How can I implement the paradigm demonstrated by the code below to run on WinXP?
There are literally thousands, if not hundreds of thousands of examples of Windows and threads:
http://msdn.microsoft.com/en-us/libr...=vs.71%29.aspx
http://msdn.microsoft.com/en-us/libr...=vs.80%29.aspx
Quote:
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.
Use code tags
Code:
int main()
{
int x = 10;
int y = 20;
}
The way you use code tags is
[code]
Your code goes here
[/code]
Regards,
Paul McKenzie
Re: How to fork process or create thread?
Quote:
Originally Posted by
joeu2004
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.
Got it on my own! To wit:
Code:
#include "stdafx.h"
#include <stdlib.h>
#include <time.h>
#include <Windows.h>
DWORD initTime;
long curCount;
int isRunning;
DWORD WINAPI myWork(LPVOID parm);
int _tmain(int argc, char* argv[])
{
const long delay = 500; // msec
curCount = 0;
isRunning = 1;
initTime = GetTickCount();
CreateThread(NULL, 0, myWork, NULL, 0, NULL);
while (isRunning) {
printf("%ld at %lu msec\n", curCount, GetTickCount()-initTime);
Sleep(delay);
}
printf("%ld at %lu msec\n", curCount, GetTickCount()-initTime);
printf("press Enter to terminate\n");
getchar();
}
DWORD WINAPI myWork(LPVOID parm)
{
const long delay = 50; // msec
long i = 0;
for (curCount = 1; curCount <= 1000000; curCount++) {
i++;
if (i == 10000) {
Sleep(delay);
i = 0;
}
}
curCount--;
isRunning = 0;
return 1;
}
Re: How to fork process or create thread?
Quote:
Originally Posted by
Paul McKenzie
The way you use code tags is
[code]
Your code goes here
[/code]
Thanks a bunch! That was very helpful.
Re: How to fork process or create thread?
Quote:
Originally Posted by
joeu2004
Got it on my own! To wit:
Your program is ill-formed; it may loop indefinately. The reason is that you have race conditions.
See the introduction to threads by Hans Boehm: http://www.hpl.hp.com/personal/Hans_...eadsintro.html