?
Solved

problem with an infinite loop in a thread

Posted on 2009-04-26
15
Medium Priority
?
284 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:errang
  • 7
  • 6
  • 2
15 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 24237509
does the stat call complete successfully?
what is the value of status?
0
 

Author Comment

by:errang
ID: 24237524
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;
    }
  }

}

0
 
LVL 40

Assisted Solution

by:mrjoltcola
mrjoltcola earned 80 total points
ID: 24237533
1) If you are wondering what the thread is doing, add some debugging output to stderr inside the function
   fprintf(stderr, "I am here...\n");

2) Your while loop is a busy loop with no sleep or other blocking call, so it will use 100% of the CPU and cause all sorts of problems. Add a sleep or nanosleep call in there.


0
Evaluating UTMs? Here's what you need to know!

Evaluating a UTM appliance and vendor can prove to be an overwhelming exercise.  How can you make sure that you're getting the security that your organization needs without breaking the bank? Check out our UTM Buyer's Guide for more information on what you should be looking for!

 

Author Comment

by:errang
ID: 24237552
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?

0
 
LVL 85

Expert Comment

by:ozo
ID: 24237559
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.
0
 
LVL 85

Accepted Solution

by:
ozo earned 1920 total points
ID: 24237570
ctime takes a time_t * argument
0
 

Author Comment

by:errang
ID: 24237577
>>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... =(
0
 

Author Comment

by:errang
ID: 24237586
>> 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?
0
 
LVL 85

Expert Comment

by:ozo
ID: 24237630
what error message?
0
 

Author Comment

by:errang
ID: 24237643
>>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);
  }

}
0
 
LVL 85

Expert Comment

by:ozo
ID: 24237666
> 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
0
 

Author Comment

by:errang
ID: 24237671
>> 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?
0
 
LVL 85

Expert Comment

by:ozo
ID: 24237863
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");
}
0
 

Author Comment

by:errang
ID: 24237868
kk, thanks =)
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24237955
>>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.
0

Featured Post

Easily manage email signatures in Office 365

Managing email signatures in Office 365 can be a challenging task if you don't have the right tool. CodeTwo Email Signatures for Office 365 will help you implement a unified email signature look, no matter what email client is used by users. Test it for free!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
There's never been a better time to become a computer scientist. Employment growth in the field is expected to reach 22% overall by 2020, and if you want to get in on the action, it’s a good idea to think about at least minoring in computer science …
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

589 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question