setenv,getenv,fork

我们在实现CGI程序的时候,有时候会用setenv设置环境变量传递给子进程。那么父进程是怎么传递给子进程的呢?

//father.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include<sys/types.h>

extern char **environ;

int main()
{
	char *str = "Hello From Father";
	char *emptylist[] = { NULL, };
	char *filename = "./child";

	if(fork() == 0)
	{	
		setenv("QUERY_STRING",str,1);
		if (execve(filename, emptylist, environ) < 0)
		{
			printf("Execve error");
		}	
	}
	wait(NULL);   

	return 0;
}

//child.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    printf("this is in son process\n");
    char *p2 = (char *)getenv("QUERY_STRING");
    if ( p2 )
        printf("%s\n",p2);
    
    return 0;
}

extern char **environ
这句话指向的就是环境变量的字符串数组。

libc中定义的全局变量environ指向环境变量表,environ没有包含在任何头文件中,所以在使用时要用extern声明。要是没有extern就会无法在子进程中获取环境变量。下面看一个environ获取环境变量获取的例子:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include<sys/types.h>
extern char **environ;

int main()
{
	char **p = environ;
        while (*p != NULL)
        {
                printf("%s (%p)\n", *p, *p);
                *p++;
        }
	return 0;
}
另外还要注意的一点就是,在setenv设置环境变量的时候是不能随便设置的,比如你要设置HOLLO=“hello”,这是不行的。为什么呢?调用fork产生子进程的时候,子进程会做一份父进程环境变量的拷贝,因为前面说了,拷贝就是通过environ来实现的,而environ是已经定义好的了。你要是非想用setenv传递环境变量,那么就只能用系统的环境变量来作为临时交换进行传递。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值