Re: Linux programme - different results each run



ulyses@xxxxxxxxxxx writes:
> Hi i have got quite strange problem. I wrote programme which shows all
> runnign processes. This info is get from /proc dir. When a dir that is
> process dir is found stat file is read and pid, name and state are get
> from it. The thing is that on my machine every time I run it, it gives
> different results. Sometimes it shows nothing - most of the times.
> Sometimes it shows info about 4 processes. Sometimes it works
> correctly. I don't now whats wrong, I 've tried to find the mistake,
> but I could'n. I begginer programmer and I don't know how to cope with
> this problem. Please help me!

This is Linux-specific and off-topic in comp.nlag.c. Try
comp.os.linux.programmer.

A few things did jump out at me:

> int main()
> {
>
> showRunningProcesses();
>
> return 0;
>
> }

showRunningProcesses() returns an int; you ignore the result.

> /*
>
> Function indicates if char given in data variable is number.
> If it is it returns 0. If it is not a number it returns 1.
>
> */
> int isNum(char data)
> {
>
> switch(data)
> {
> case '0': return 0;
> case '1': return 0;
> case '2': return 0;
> case '3': return 0;
> case '4': return 0;
> case '5': return 0;
> case '6': return 0;
> case '7': return 0;
> case '8': return 0;
> case '9': return 0;
> default : return 1;
> }
>
> }

This is backwards; logically, it should return 0 for false, 1 for
true. Names starting with "is" and a lowercase letter are reserved;
"is_Num" or "is_num" would be ok. And there's already a function that
does exactly what you want: isdigit(), declared in <ctype.h>.

> /*
>
> Function indicates if char given by *path pointer
> is process dir (in /proc file system). If it is it
> returns 0. If it is not it returns 1.
>
> */
> int isProcDir(char *path)

Some of the same comments as above apply here.

> {
> struct stat st;
> int i;
> int path_length;
>
> lstat(path, &st);
> path_length = strlen(path);
>
> //printf("running.. %s\n", path);
> //printf("%o", st.st_mode);
> if (S_ISDIR(st.st_mode))
> return 1;
> else if(S_ISLNK(st.st_mode))
> return 1;
> else if(S_ISCHR(st.st_mode))
> return 1;
> else if(S_ISBLK(st.st_mode))
> return 1;
> else if(S_ISFIFO(st.st_mode))
> return 1;
> else if(S_ISSOCK(st.st_mode))
> return 1;
> else if(S_ISREG(st.st_mode))
> return 1;
> else
> {
> //printf("path: %s \n", path);
> //fprintf(stderr, "found");
>
>
> for(i=0; i<path_length; i++)
> {
> if (isNum(path[i]))
> {
> return 1;
> }
> }
>
> return 0;
> }
>
> }
>
> int showRunningProcesses()
> {
>
> int i;
> DIR *directory, *process_directory;
> FILE *fp;
> struct dirent *dir_entry;
> char *path = (char *)malloc(50);
> char *name;
> char pid[10], procName[15], state[5];
>
> strcat(path,"/proc/");
>
> if( ( directory=opendir(path) ) == NULL )
> {
> fprintf(stderr, "Couldn't open /proc dir.");
> return 1;
>
> }
>
> while(dir_entry=readdir(directory))
> {
>
> /*
> Check if read directory is process
> directory.
> */
>
> if( !( isProcDir(dir_entry->d_name) ) )
> {
>
> //printf("running..");
>
> /*
> A process directory was found.
> */
>
> /*
> Add to path variable name of the
> process directory and then add "/"
> to make path correct.
> */
> strcat(path, dir_entry->d_name);
> strcat(path,"/");
>
> //printf("%s\n", path);
>
> if ( (process_directory=opendir(path) ) == NULL)
> {
> printf("Couldn't open process directory.");
> return 1;
> }
> else
> {
> strcat(path,"stat");
>
> if ( ( fp=fopen(path,"r") ) == NULL)
> {
> printf("Couldn't open process stat file.");
> return 1;
> }
> else
> {
> fscanf(fp,"%s", &pid);
> printf("PID: %s",pid);
> fscanf(fp,"%s", &procName);
> printf("\t\tName: %s", procName);
> fscanf(fp,"%s", &state);
> printf("\t\tState: %s\n", state);
>
> fclose(fp);
> }
>
> }
>
> closedir(process_directory);
>
> strcpy(path,"");
> strcat(path,"/proc/");
>
> }
>
> }
>
> closedir(directory);
>
> }
>
> [/code]
>
> Thank you,
> John
>

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.



Relevant Pages

  • Linux programme - different results each run
    ... Hi i have got quite strange problem. ... I wrote programme which shows all ... int isProcDir; ... Function indicates if char given by *path pointer ...
    (comp.lang.c)
  • Re: Linux programme - different results each run
    ... > Hi I have got quite strange problem. ... I wrote programme which shows all ... > int isProcDir; ... What happens if malloc() fails? ...
    (comp.unix.programmer)
  • Linux programme - different results each run
    ... Hi I have got quite strange problem. ... int isProcDir; ... Function indicates if char given in data variable is number. ... Function indicates if char given by *path pointer ...
    (comp.unix.programmer)
  • Re: how to count rows and columns of integers/doubles in a file?
    ... the reading and the writing programme are in the same locale, ... unsigned rows, cols, oldcols; ... int countColsInNextLine ... warning C4013: 'system' undefined; assuming extern returning int ...
    (comp.lang.c)
  • doubt about socket programing
    ... my client side programme ... int k=Integer.parseInt); ... OutputStream out1=s.getOutputStream; ... response from server for that input.and then program is hanging ...
    (comp.lang.java.programmer)