-2
我有一個C程序,它被調用了可變參數。該程序使用參數形成命令,然後分叉創建子進程。但是,當程序的參數數量較多時,我發現fork()
命令失敗。fork創建守護進程失敗
任何線索爲什麼會發生這種情況?
int main(int argc, char *argv[])
{
FILE *logfile;
int other=0;
int i, pid;
char *conn="conntrack -E";
char *logger="logger -t log-conntrack -p daemon.notice";
char *fother="grep -vE 'tcp|udp|icmp'";
char cmd[1024];
int length = 0;
char * temp_cmd = cmd;
for (i=1; i<argc; i++) {
printf("for loop\n");
switch(argv[i][1]) {
case 'p': if (i+1 < argc && argv[i+1][0] != '-') {
if (strncmp(argv[i+1], "other",
strlen(argv[i+1])) == 0) {
other=1;
snprintf(cmd, sizeof (cmd), "%s", conn);
length = strlen (cmd);
temp_cmd = cmd + length;
i++;
} else if ((strncmp(argv[i+1], "tcp",
strlen(argv[i+1])) == 0) ||
(strncmp(argv[i+1], "udp",
strlen(argv[i+1])) == 0) ||
(strncmp(argv[i+1], "icmp",
strlen(argv[i+1])) == 0)) {
snprintf(cmd, sizeof (cmd), "%s%s%s", conn, " -p ", argv[i+1]);
other=0;
length = strlen (cmd);
temp_cmd = cmd + length;
i++;
}
}
break;
case 'e':
if (i+1 < argc && argv[i+1][0] != '-') {
if ((strncmp(argv[i+1], "NEW",
strlen(argv[i+1])) == 0) ||
(strncmp(argv[i+1], "UPDATES",
strlen(argv[i+1])) == 0) ||
(strncmp(argv[i+1], "DESTROY",
strlen(argv[i+1])) == 0)) {
if (other == 1) {
snprintf(temp_cmd, sizeof (cmd) - length, "%s%s%s%s%s%s%s", " -e ", argv[i+1], " -o id", " | ", fother, " | ", logger);
cmds[pcounter] = malloc(strlen+1);
strcpy(cmds[pcounter],cmd);
pcounter++;
} else if ((strncmp(argv[i-1], "tcp",strlen(argv[i-1]))==0) &&
(strncmp(argv[i+1], "UPDATES",strlen(argv[i+1])) == 0)){
snprintf(temp_cmd, sizeof (cmd) - length, "%s%s", " -e ", argv[i+1]);
} else {
snprintf(temp_cmd, sizeof (cmd) - length, "%s%s%s%s%s", " -e ", argv[i+1], " -o id", " | ", logger);
cmds[pcounter] = malloc(strlen+1);
strcpy(cmds[pcounter],cmd);
pcounter++;
}
length = strlen (cmd);
temp_cmd = cmd + length;
i++;
}
}
break;
case 's': if (i+1 < argc && argv[i+1][0] != '-') {
if ((strncmp(argv[i+1], "SYN_RECV",
strlen(argv[i+1])) == 0) ||
(strncmp(argv[i+1], "ESTABLISHED",
strlen(argv[i+1])) == 0) ||
(strncmp(argv[i+1], "FIN_WAIT",
strlen(argv[i+1])) == 0) ||
(strncmp(argv[i+1], "CLOSE_WAIT",
strlen(argv[i+1])) == 0) ||
(strncmp(argv[i+1], "LAST_ACK",
strlen(argv[i+1])) == 0) ||
(strncmp(argv[i+1], "TIME_WAIT",
strlen(argv[i+1])) == 0)) {
snprintf(temp_cmd, sizeof (cmd) - length, "%s%s%s%s%s", " --state ", argv[i+1], " -o id", " | ", logger);
cmds[pcounter] = malloc(strlen+1);
strcpy(cmds[pcounter],cmd);
pcounter++;
length = strlen (cmd);
temp_cmd = cmd + length;
i++;
}
}
break;
}
}
daemonize();
}
的ulimit -a輸出 -
ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 16382
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) unlimited
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
free -m
total used free shared buffers cached
Mem: 502 123 379 0 19 72
-/+ buffers/cache: 31 471
Swap: 0 0 0
獲得這些問題的答案的最佳方式是顯示一些代碼,最好是可運行的示例。 – cnicutar
請發佈您的代碼。 100次中有99次是這樣的,這些問題是由於實現錯誤,而不是API的功能中的錯誤。 – Polynomial
最明顯的將是你正在達到一定的資源限制。但是由於您(a)沒有發佈代碼, (b)**甚至沒有提供失敗的fork後面的'errno' /'perror',很難提供很多幫助。 – derobert