Created
September 16, 2012 21:53
-
-
Save hnakamur/3734545 to your computer and use it in GitHub Desktop.
test on how to copy worker env for spawning
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
gcc env_copy_test.c; ./a.out | |
*/ | |
#include <stdio.h> | |
#include <time.h> /* clock */ | |
#include <stdlib.h> /* free */ | |
#include <string.h> /* sprintf */ | |
extern char **environ; | |
static inline void type_a(const char *worker_id, const char *ipc_filename) { | |
int env_cnt = 0; | |
char **entry; | |
for (entry = environ; *entry; entry++, env_cnt++); | |
char *worker_env[2 + env_cnt + 1]; | |
worker_env[0] = malloc(sizeof("LEN_WORKER_ID=") + strlen(worker_id)); | |
sprintf(worker_env[0], "LEV_WORKER_ID=%s", worker_id); | |
worker_env[1] = malloc(sizeof("LEV_IPC_FILENAME=") + strlen(ipc_filename)); | |
sprintf(worker_env[1], "LEV_IPC_FILENAME=%s", ipc_filename); | |
memcpy(worker_env + 2, environ, sizeof(char *) * (env_cnt + 1)); | |
free( worker_env[0] ); /* free LEV_WORKER_ID */ | |
free( worker_env[1] ); /* free LEV_IPC_FILENAME */ | |
} | |
static inline void type_b(const char *worker_id, const char *ipc_filename) { | |
int n = 0; | |
char **env_item; | |
char env_temp[1024]; | |
char *worker_env[256]; | |
sprintf(env_temp, "LEV_WORKER_ID=%s", worker_id); | |
worker_env[n++] = strdup(env_temp); | |
sprintf(env_temp, "LEV_IPC_FILENAME=%s", ipc_filename); | |
worker_env[n++] = strdup(env_temp); | |
for (env_item=environ;*env_item&&n<254;env_item++) { | |
worker_env[n++] = *env_item; | |
} | |
worker_env[n] = NULL; /* terminate */ | |
free( worker_env[0] ); /* free LEV_WORKER_ID */ | |
free( worker_env[1] ); /* free LEV_IPC_FILENAME */ | |
} | |
static inline void type_c(const char *worker_id, const char *ipc_filename) { | |
int env_cnt = 0; | |
char **entry; | |
for (entry = environ; *entry; entry++, env_cnt++); | |
char *worker_env[2 + env_cnt + 1]; | |
char worker_env0_buf[sizeof("LEN_WORKER_ID=") + strlen(worker_id)]; | |
sprintf(worker_env0_buf, "LEV_WORKER_ID=%s", worker_id); | |
worker_env[0] = worker_env0_buf; | |
char worker_env1_buf[sizeof("LEV_IPC_FILENAME=") + strlen(ipc_filename)]; | |
sprintf(worker_env1_buf, "LEV_IPC_FILENAME=%s", ipc_filename); | |
worker_env[1] = worker_env1_buf; | |
memcpy(worker_env + 2, environ, sizeof(char *) * (env_cnt + 1)); | |
} | |
int main(void) | |
{ | |
int i; | |
clock_t begin, end; | |
char worker_id[8]; | |
char *ipc_filename = "/tmp/lev_12345.tmp"; | |
begin = clock(); | |
for (i=0;i<2000000;i++) { | |
sprintf(worker_id, "%d", i); | |
type_a(worker_id, ipc_filename); | |
} | |
end = clock(); | |
printf("type_a: %f seconds\n", (double)(end - begin) / CLOCKS_PER_SEC); | |
begin = clock(); | |
for (i=0;i<2000000;i++) { | |
sprintf(worker_id, "%d", i); | |
type_b(worker_id, ipc_filename); | |
} | |
end = clock(); | |
printf("type_b: %f seconds\n", (double)(end - begin) / CLOCKS_PER_SEC); | |
begin = clock(); | |
for (i=0;i<2000000;i++) { | |
sprintf(worker_id, "%d", i); | |
type_c(worker_id, ipc_filename); | |
} | |
end = clock(); | |
printf("type_c: %f seconds\n", (double)(end - begin) / CLOCKS_PER_SEC); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment