Prozesstabelle ausgeben
-
Hallo zusammen,
ich habe ein Problem mit meinem derzeitigen Programm und zwar soll des eine Prozesstabelle ausgeben und dann nach ein paar Sekunden eine neue Ausgeben und so weit bis es mit ctrl+c beendet wird(Signalhandling soll auch dabei sein.)Leider bekomme ich nur die erste Prozesstabelle und danach bricht das Programm ab.
woran kann das liegen?und wie kann ich am besten die EInträge sortieren?
hier mein c code(sorry wegen formatierung ):
[code]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include <ctype.h>
#include <sys/param.h>
#include <stdint.h>
#include <signal.h>//Signalhandler
void crtlc(int signal )
{
if(fd1 != NULL)
{
fclose(fd1);
fd1=NULL
}
if(fd2 != NULL)
{
fclose (fd2);
fd2=NULL;
}
if(dp != NULL)
{
closedir(dp);
dp=NULL;
}
if(ep!=NULL)
{
closedir(ep);
ep=NULL;
}
if(signal != -1)
{
printf("Progamm erfolgreich beendet\n);
exit ;
}
}//Eingabe Anweisung
void usage(void)
{
printf("Parametereingabe alle freiwillig.Eingabe wie folgt:\n");
printf(" ./memusage [-m] [-N <num>] [-T <byte>] [-P <pid>]\n");
}//Print des Status
void print_stat(const char *name)
{
char path_buf[PATH_MAX + 1];
int err1 = snprintf(path_buf, sizeof(path_buf), "/proc/%s/stat", name);
if (err1 <= 0 || err1 >= (int)sizeof(path_buf))
{
fprintf(stderr, "snprintf error: %d, name: %s\n", err1, name);
return;
}
FILE *fd1 = fopen(path_buf,"r");
if (fd1 == NULL)
{
perror("Could not open stat file");
return;
}
uint32_t pid;
char state;
unsigned long int size;
err1 = fscanf(fd1, "%d (%[^)]) %c %lu", &pid, path_buf, &state, &size);
if (err1 <= 0)
{
perror("Fscanf error");
fclose(fd1);
return;
}//Ausgabe cmdline
char cmdline[PATH_MAX + 1];
int err2 = snprintf(cmdline, sizeof(cmdline), "/proc/%s/cmdline", name);
if(err2 <= 0 || err2 >= (int)sizeof(cmdline))
{
fprintf(stderr, "snprintf error: %d, name: %s\n", err2, name);
return;
}
fprintf(stdout, "%5d %6c %14lu ", pid, state, size);
FILE *fd2 = fopen(cmdline,"r");
int c;
if(fd2 != NULL)
{
while((c=fgetc(fd2)) != EOF)
{
putchar(c);
}
}
else
{
printf("cmdline konnte nicht geoeffnet werden");
return;
}
printf("\n");//Schließen der Streams
fclose(fd1);
fclose(fd2);
}//Inhalt von proc auflisten
void list_dir(DIR *dp, int sortierung,int anzahlProzess, int prozess)
{int count,vgl;
count= vgl=0;
struct dirent *ep;
unsigned int i;
char buf[5];//Ausgabe von nur einem Prozess
if(prozess>0)
{
sprintf(buf,"%d",prozess);
ep=readdir(dp);
ep->d_name=buf;
print_stat(ep->d_name);
return;
}//Ausgabe aller Prozesse
while (((ep = readdir(dp)) != NULL))
{
vgl=(anzahlProzess==count);
if (ep->d_name == NULL)
{
continue;
}for(i = 0; isdigit(ep->d_name[i]); i++)
{
// count++;
}
/* digits only? */
if (i == strlen(ep->d_name))
{
print_stat(ep->d_name);
count++;
}
if(vgl==1)return;}
}int main(int argc, char **argv)
{//Initialisierung
int i,vgl;
int sortierung, anzahlProzess, taktLaenge, prozess;
char first[2],second[2],third[2],four[2];
DIR *dp;//Deklaration
first[0]=second[0]=third[0]=four[0]='-';
first[1]='m';
second[1]='N';
third[1]='T';
four[1]='P';//Defaultwerte
taktLaenge=3;
sortierung=0;//aufsteigend sortiert
anzahlProzess=0;
usage();//SIgnalhandler installieren
signal(SIGINT,crtlc);//Variable Wertzuweisung der Parametereingabe
for(i=1;i<argc;i++)
{
vgl=strncmp(argv[i],first,2);
if(vgl==0)
{
sortierung=1;
}vgl=strncmp(argv[i],second,2);
if(vgl==0)
{
anzahlProzess=atol(argv[i+1]);
}vgl=strncmp(argv[i],third,2);
if(vgl==0)
{
taktLaenge=atol(argv[i+1]);
}vgl=strncmp(argv[i],four,2);
if(vgl==0)
{
prozess=atol(argv[i+1]);
}
}//Ausgabe der Prozesse in einer Tabelle
dp=opendir("/proc");
while(i<200)
{
sleep(taktLaenge);
printf(" [pid] [state] [vsize] [cmdline]\n");if (dp == NULL)
{
perror("Couldn't open the directory");
return -1;
}
list_dir(dp, sortierung, anzahlProzess, prozess);
if (closedir(dp) != 0)
{
perror("Couldn't close the directory");
return -1;
}
}return 0;
}
-
Zu viel Code, unvollständig, fehlende Vairable, Null Einrückung, zu viele Fehler, nicht ausführbar.
Wäre nicht schlecht, wenn du daraus ein Minimalbeispiel machen könntest.
-
also in diesem teil muss irgendwo der Fehler sein warum die tabelle nicht zweimal ausgedruckt wird ... ich weiß leider nicht warum.
p.s. wie kann ich die einträge aufsteigend und absteigend sortieren?
[code]//Inhalt von proc auflisten
void list_dir(DIR *dp, int sortierung,int anzahlProzess, int prozess)
{int count,vgl;
count= vgl=0;
struct dirent *ep;
unsigned int i;
char buf[5];//Ausgabe aller Prozesse
while (((ep = readdir(dp)) != NULL))
{
vgl=(anzahlProzess==count);
if (ep->d_name == NULL)
{
continue;
}for(i = 0; isdigit(ep->d_name[i]); i++)
{
// count++;
}
/* digits only? */
if (i == strlen(ep->d_name))
{
print_stat(ep->d_name);
count++;
}
if(vgl==1)return;}
}
-
Zu den Tags gehhört immer noch ein schließendes Tag mit einem / dazu.
Am besten Code markieren und auf den C-Button klicken, damit es auch farbig und bunt wird:daniev schrieb:
//Inhalt von proc auflisten void list_dir(DIR *dp, int sortierung,int anzahlProzess, int prozess) { int count,vgl; count= vgl=0; struct dirent *ep; unsigned int i; char buf[5]; //Ausgabe aller Prozesse while (((ep = readdir(dp)) != NULL)) { vgl=(anzahlProzess==count); if (ep->d_name == NULL) { continue; } for(i = 0; isdigit(ep->d_name[i]); i++) { // count++; } /* digits only? */ if (i == strlen(ep->d_name)) { print_stat(ep->d_name); count++; } if(vgl==1)return; } }
Für das Sortieren schau dir qsort an.
Für die Fehlersuche am besten den Debugger.