Helping ordinary people create extraordinary websites!
GET OUR NEWSLETTER
Your Email:
 

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

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


Environment variables

Each process has an environment block associated with it, basically name=value pairs that specify various environments the process can access. Even though we can specify the environment when we create the process, there are also specific functions to set and obtain environment variables after the process is created.

In Windows, you can use GetEnvironmentVariable() and SetEnvironmentVariable() to get and set the environment variables.



DWORD GetEnvironmentVariable(
LPCTSTR lpName, // environment variable name
LPTSTR lpBuffer, // buffer for variable value
DWORD nSize // size of buffer
);

This function returns the size of the value buffer on success and 0 if the name specified is not a valid environment variable name. The SetEnvironmentVariable() function sets the contents of the specified environment variable for the current process.



BOOL SetEnvironmentVariable(
LPCTSTR lpName, // environment variable name
LPCTSTR lpValue // new value for variable
);

If the function succeeds, the return value is non-zero. If the function fails, the return value is zero.

In Linux, getenv() and setenv() system calls provide the equivalent functionality.



char *getenv(const char *name);
int setenv(const char *name, const char *value, int overwrite);

The getenv() function searches the environment list for a string that matches the string pointed to by name. This function returns a pointer to the value in the environment or NULL if there is no match. The setenv() function adds the variable name to the environment with the value if the name does not already exist. If the name does exist in the environment, then its value is changed to value if overwrite is non-zero. If overwrite is zero, then the value of name is not changed. The setenv() function returns zero on success or -1 if there was insufficient space in the environment.



Tutorial Pages:
» A mapping guide for complex, multithreaded, multiprocess applications
» Processes
» Creating a process
» Terminating a process
» Using wait functions
» Exiting a process
» Environment variables
» Examples
» Threads
» Examples of processes and threads
» Next in the series
» Resources


First published by IBM DeveloperWorks


 | Bookmark
Related Tutorials:
» How to Install PHP 5 on Linux
» How to Install Apache 2 on Linux
» How to Install MySQL 5.0 on Linux
» SMB Caching
» Mound --Bind
» Tar Wild Card Interpretation

Advertise with Us!


Tutorials Scripts Web Hosting Developer Manuals
Resources