Problem mit Prozessen

Cordell

Cordell

Mitglied
Hi,

ich habe folgenden Code:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <errno.h>

#define CHILDREN 3                        /* max. Anzahl Soehne           */

int eval_wait_stat (int stat):
    
int main (void)
{
    pid_t parent_pid, child_pid,        /* Prozessnummern               */
          fork_pid, wait_pid,
          parent_grp, child_grp;        /* Prozessgruppen               */
    int   child_stat,                   /* Zustand des Sohnes           */
          exit_val,                     /* "exit"-Wert des Sohnes       */
          i,j,                          /* Zählvariablen                */ 
          anz_children = 0;                 /* Anzahl Soehne                */
    
    exit_val = 10;                      /* zum Testen beliebiger Wert   */
    parent_pid = getpid ();
    parent_grp = getpgrp ();
            
    while (anz_children < CHILDREN)
    {  
        fork_pid = fork ();
        switch(fork_pid)
        {
             case -1:                         /* Fehler: kein Prozess erzeugt */
                 perror ("fork failed");
                 errno = 0;
                 break;
       
             case 0:                          /* Sohnprozesse                 */
                 child_pid = getpid ();
                 child_grp = getpgrp ();
                
                 printf ("Sohnprozess. Prozess-Id: %ld     Gruppen-Id: %ld     "
                         "Vaterprozess-Id: %ld     ", (long) child_pid, 
                         (long) child_grp, (long) getppid ());
                 anz_children++;
                 exit (exit_val);
                 break;
       
             default:
                  while (anz_children > 0)
                  {
                        wait_pid = wait (&child_stat);
                        if (wait_pid == -1)
                        {
                           perror ("wait");
                           errno = 0;
                        }
                        else
                        {
                            printf ("Vaterprozess. Sohnprozess %ld endet.\n",
                                   (long) wait_pid);
                            eval_wait_stat (child_stat);
                            anz_children--;
                        }
                  }
       }
    }
    return 0;

    int eval_wait_stat (int stat)
    {
        if (WIFEXITED (stat) != 0)
        {
           printf ("\t\tSohnprozess endet mit Status %d.\n",
        WEXITSTATUS (stat));
        return 0;
        }
        if (WIFSIGNALED (stat) != 0)
        {
           printf ("\t\tSohnprozess wurde durch Signal %d beendet.\n",
           WTERMSIG (stat));
           return 0;
        }
        if (WIFSTOPPED (stat) != 0)
        {
           printf ("\t\tSohnprozess wurde durch Signal %d gestoppt.\n",
           WSTOPSIG (stat));
        }
        #if (defined(SunOS) || defined(IRIX)) && !defined(_POSIX_C_SOURCE)
        if (WIFCONTINUED (stat) != 0)
        {
           printf ("\t\tSohnprozess wurde fortgesetzt\n.");
        }
        #endif
        return 1;
    }            
}

Nach dem Kompilieren bekomme ich folgende Meldung:
------------------
in function main:
fork.c: (.text+0x104): undefined reference to 'eval_wait_stat'
collect2: ld returned 1 exit status
------------------

Kann jemand helfen?

Gruß,
Cordell
 
bist du sicher dass du das kompilieren kannst???

Code:
int eval_wait_stat (int stat):

Da is doch en Doppelpunkt statt einem Strichpunkt am Ende...
 
Das ist schon ausgemerzt! :)
Hast du ne Ahnung zu meinem problem?
 
Mahltid,

Du hast da eine 'nested-function' gebastelt, und bist damit am ANSI-C-Standard gescheitert, der ja die Deklaration von globalen Funktionen (das hast Du auch gemacht) und die Definition ebenso (das hast Du versäumt!), außerhalb des Aufrufkontexts erfordert ;)

also statt:

previous bad code schrieb:
}
return 0;

int eval_wait_stat (int stat)
{
if (WIFEXITED (stat) != 0)
...
#endif
return 1;
}
} // exit main

previous bad code schrieb:
}
return 0;
} // exit int main()

int eval_wait_stat (int stat)
{
if (WIFEXITED (stat) != 0)
...
#endif
return 1;
}

Ab da hats auch bei mir funktioniert :D

EDIT: gcc unter macosx bietet auch die möglichkeit per schalter -fnested-functions hier auch explizit von ANSI-C abzuweichen

MfG...
 
Zuletzt bearbeitet:
Ja hatte ich mitlerweile auch bemerkt! Jedoch hab ich den Code auch noch umgeschrieben! Jetzt ist alles schön!! :)

Danke für die Antworten!

Gruß,
Cordell
 

Ähnliche Themen

Unix Webserver mit HTML Seite erstellen

Prozesskommunikation mit PIPES - wie funktioniert das?

Problem mit HSPA+ Modem Huawei E353 - Installation unmöglich?

NagiosGrapher 1.7.1 funktioniert nicht

C HTTP request

Zurück
Oben