Link to home
Start Free TrialLog in
Avatar of errang
errangFlag for Afghanistan

asked on

problem with an infinite loop in a thread

Hey, I'm writing a program that will watch a file and beep if there has been any change in that file, I'm using a thread to make the loop which will keep checking if the file has changed or not.

I have been trying to test it for some time now, but it doesn't do anything.. and I'm not sure if its because I'm doing something wrong, or if the scheduler is never picking this thread.

Appreciate any help on this. Thanks in advance!
void *watch(void *time){
  char *filename = (char *) time;
 
  struct stat watch_mail;
  int status;
  time_t old_time, new_time;
 
  status = stat(filename, &watch_mail);
  old_time = watch_mail.st_mtime;
 
  while(1){
    status = stat(filename, &watch_mail);
    new_time = watch_mail.st_mtime;
 
    if(old_time!=new_time){
      printf("\a\aThis file has been modified at time %s", ctime(new_time));
      break;
    }
  }
 
}

Open in new window

Avatar of ozo
ozo
Flag of United States of America image

does the stat call complete successfully?
what is the value of status?
Avatar of errang

ASKER

Hm... status doesn't seem to be printing anything.

 pid = fork();
      if(strcmp(second_string, "off") == 0)
        pthread_cancel(tid);
      else if ( pid == 0 ) { /* child process */
        pthread_attr_init(&attr);
        pthread_create(&tid,&attr,watch,second_string);
        pthread_join(tid,NULL);
      }
      else if ( pid > 0 ) { /* parent process */
          wait(NULL);
      }

Am I doing something wrong calling it? second_string is a character array.

void *watch(void *time){
  char *filename = (char *) time;

  struct stat watch_mail;
  int status;
  time_t old_time, new_time;

  status = stat(filename, &watch_mail);
  printf("status = %d", status);
  old_time = watch_mail.st_mtime;

  while(1){
    status = stat(filename, &watch_mail);
    new_time = watch_mail.st_mtime;

    if(old_time!=new_time){
      printf("\a\aThis file has been modified at time %s", ctime(new_time));
      break;
    }
  }

}

SOLUTION
Avatar of mrjoltcola
mrjoltcola
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of errang

ASKER

Ok... apparently I'm doing something wrong?

> ./mysh
doing fine
/home/usra/7e/32407/cisc361/project#2>watchmail zzzz.txt
I am here...

But where though? I copied the fork part from a program that was working...

pid = fork();
      if(strcmp(second_string, "off") == 0)
        pthread_cancel(tid);
      else if ( pid == 0 ) { /* child process */
        pthread_attr_init(&attr);
        pthread_create(&tid,&attr,watch,second_string);
        pthread_join(tid,NULL);
      }
      else if ( pid > 0 ) { /* parent process */
          wait(NULL);
      }

So it can't be that...

void *watch(void *time){
  char *filename = (char *) time;

  struct stat watch_mail;
  int status, slp;
  time_t old_time, new_time;

  fprintf(stderr, "I am here...\n");

  status = stat(filename, &watch_mail);
  printf("status = %d", status);
  old_time = watch_mail.st_mtime;

  while(1){
    status = stat(filename, &watch_mail);
    new_time = watch_mail.st_mtime;

    if(old_time!=new_time){
      printf("\a\aThis file has been modified at time %s", ctime(new_time));
      break;
    }

    slp = sleep(5);
  }

}

Am I using time wrong?

why are you mixing forks with pthreads?
did pthread_create successfully return 0?
Is  your output line buffered? if so the output may be waiting for a newline.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of errang

ASKER

>>why are you mixing forks with pthreads?

Well, I'm writing a shell... can't have the main program get stuck in a loop right..? I figured threads had time slices, and the scheduler switches them out.

>> did pthread_create successfully return 0?
Yes

> ./mysh
doing fine
/home/usra/7e/32407/cisc361/project#2>watchmail zzzz.txt
I am here...

pid = fork();
      if(strcmp(second_string, "off") == 0)
        pthread_cancel(tid);
      else if ( pid == 0 ) { /* child process */
        pthread_attr_init(&attr);
        thread_c = pthread_create(&tid,&attr,watch,second_string);
        if(thread_c == 0){
          printf("Thread successfully created.\n");
        }
        pthread_join(tid,NULL);
      }
      else if ( pid > 0 ) { /* parent process */
          wait(NULL);
      }

>> Is  your output line buffered? if so the output may be waiting for a newline.

I don't know what you are asking me... =(
Avatar of errang

ASKER

>> ctime takes a time_t * argument

Ok... that fixed it... although, I don't get why its still printing the error message:

> ./mysh
doing fine
/home/usra/7e/32407/cisc361/project#2>watchmail zzzz.txt
Thread successfully created.
I am here...
status = 0This file has been modified at time Sun Apr 26 16:59:16 2009
/home/usra/7e/32407/cisc361/project#2>^Z

I take it its not a conditional? or am I doing something wrong?
what error message?
Avatar of errang

ASKER

>>I am here...

void *watch(void *time){
  char *filename = (char *) time;

  struct stat watch_mail;
  int status, slp;
  time_t old_time, new_time;

  fprintf(stderr, "I am here...\n"); <-------------- this is the error message (i put it in after mrjoltcola suggest it http:#24237533)

  status = stat(filename, &watch_mail);
  printf("status = %d", status);
  old_time = watch_mail.st_mtime;

  while(1){
    status = stat(filename, &watch_mail);
    new_time = watch_mail.st_mtime;

    if(old_time!=new_time){
      printf("\a\aThis file has been modified at time %s", ctime(new_time));
      break;
    }

    slp = sleep(5);
  }

}
> I don't get why its still printing the error message:

Its still printing because the function that prints it is still in the code, and still being executed
Avatar of errang

ASKER

>> Its still printing because the function that prints it is still in the code, and still being executed

Ok, so it wasn't one of those conditional things, and everything will be fine if I remove it?
what conditional thing?
If you want to print it some times and not others you might do something like

if( condition_when_I_want_to_print_this ){
   fprintf(stderr, "I am here...\n");
}
Avatar of errang

ASKER

kk, thanks =)
>>fprintf(stderr, "I am here...\n"); <-------------- this is the error message (i put it in after mrjoltcola suggest it http:#24237533)

I recommended it as a general approach. Debugging threaded code is tough, its really tough to do it realtime with a debugger, so it is a good practice to use non-buffered, plain-old print statements to generate a run log of the program that you can analyze. priting to stderr is one way to accomplish it.