Helping ordinary people create extraordinary websites!

Port Windows IPC Apps to Linux, Part 1: Processes and Threads

By Srinivasan S. Muthuswamy, Kavitha Varadarajan
2005-06-16

Examples
The following examples illustrate what we've discussed in this section. Listing 1. Windows process code


//Sample Application that explain process concepts

//Parameters Declaration/Definition

int TimetoWait;
STARTUPINFO si;
PROCESS_INFORMATION pi;
LPTSTR lpszCurrValue,LPTSTR lpszVariable;
TCHAR tchBuf[BUFSIZE];
BOOL fSuccess;

if(argc > 2)
{
printf("InvalidArgument");
ExitProcess(1); //Failure
}

//Get and display an environment variable PATH
lpszCurrValue = ((GetEnvironmentVariable("PATH",tchBuf, BUFSIZE) > 0) ? tchBuf : NULL);
lpszVariable = lpszCurrValue;

//Display the environment variable
while (*lpszVariable)
putchar(*lpszVariable++);
putchar('\n');

//Initialise si and pi
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );

//Create a childProcess
if( !CreateProcess( NULL, // No module name (use command line).
"SomeProcess", // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi ) // Pointer to PROCESS_INFORMATION structure.
)
{
printf( "CreateProcess failed." );
}

// Wait until child process exits.
if(argc == 2)
{
TIMEOUT = atoi(argv[1]);
ret = WaitForSingleObject( pi.hProcess, TIMEOUT );
if(ret == WAIT_TIMEOUT)
{
TerminateProcess(pi.hProcess);
}
}
else
{
WaitForSingleObject( pi.hProcess, INFINITE );
...
}
ExitProcess(0); //Success

Listing 2. Equivalent Linux process code



#include <stdlib.h>

int main(int argc,char *argv[])
{
//Parameters Declaration/Definition


char PathName[255];
char *Argptr[20];
int rc;
char *EnvValue,*lpszVariable;

if(argc > 1)
{
printf(" Wrong parameters !!");
exit(EXIT_FAILURE);
}
//Get and display an environment variable PATH
EnvValue = getenv("PATH");
if(EnvValue == NULL)
{
printf("Invalid environment variable passed as param !!");
}else
{
lpszVariable = EnvValue;
while (*lpszVariable)
putchar(*lpszVariable++);
putchar('\n');
}
rc = fork(); //variable rc's value on success would be process ID in the parent
//process, and 0 in the child's thread of execution.
switch(rc)
{
case -1:
printf("Fork() function failed !!");
ret = -1;
break;
case 0:
printf("Child process...");
setpgid(0,0); //Change the parent grp ID to 0


ret = execv(PathName,Argptr); // there are other flavours of exec available,
// u can use any of them based on the arguments.
if(ret == -1)
{
kill(getpid(),0);
}
break;
default:
// infinitely waits for child process to die
Waitpid(rc,&status,WNOHANG);
//Note RC will have PID returned since this is parent process.

break;
}
exit(EXIT_SUCCESS);
}




Tutorial pages:

First published by IBM DeveloperWorks


 3 Votes

You might also want to check these out:


Leave a Comment on "Port Windows IPC Apps to Linux, Part 1: Processes and Threads"
You must be logged in to post a comment.

Link to This Tutorial Page!


GET OUR NEWSLETTERS