fork在创建守护进程时失败

时间:2011-12-04 18:49:42

标签: c fork

我有一个用可变参数调用的C程序。此程序使用参数形成命令,然后分叉以创建子进程。但是,我发现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

1 个答案:

答案 0 :(得分:0)

这里的问题是fork没有足够的内存来分配给子进程。我增加了运行该程序的虚拟机的内存,并且工作正常。