Advertisement

03.29.2008 at 02:43PM PDT, ID: 23279955
[x]
Attachment Details

Unexpected behavior from sleep function using C

Asked by mtnrunner2001a in C Programming Language, C++ Programming Language, CYGWIN

Tags: C

I am writing a program in C on my PC using Cygwin.  At the moment I am encountering unexpected behavior when I run my program.  What I want my program to do is first call the create_analyzed_file and determine if a file needs to be appended to or an old file deleted and a new file created.  Once that function has run I want the program to go into a while loop, call another function (not written yet) to look at a directory on the system and then sleep for 60 seconds.  I put a printf place holder where the function call to check a directory would be so I expect when the program runs it would first analyze whether my rawlog_analyzed file is from today (it is so I expect it to have a new timestamp and print Appending to file on the screen) and then I would get the string "Right before sleep" printed to the screen every minute.

What actually happens is that when I kick off my executable it just hangs.  No messages are printed to the screen and my rawlog_analyzed file does not get an updated timestamp.  I don't understand why this is happening and how to fix it.  I let the program run for 5 minutes and the behavior of the program did not change.  I'm using Cygwin which I think might have something to do with this odd behavior but it seemed like that was my best option for compiling C code on a PC.  Any help would be much appreciated.

Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
 
#include "../Inc/tlm_defs.h"
#include "../Inc/raw_defs.h"
/******************************************************************************
* local prototypes
******************************************************************************/
 
static void create_analyzed_file (void);
 
/******************************************************************************
* global variables
******************************************************************************/
int Current_Day;
int system_up =1;
 
/******************************************************************************
* Main program       
******************************************************************************/
 
main ( )
{
 
  create_analyzed_file ();
 
  while (system_up)
  {   
    //This is where I need to start checking the directory for rawlogs
    printf("Right before sleep");    
    sleep(60);
  }
}
 
static void create_analyzed_file (void)
{
 
FILE * pFile;
struct tm *now_ptr;
time_t now;
char date_str[5];  // MMDDYY plus NULL
char file_name[24];
char tmp_name[26];
char tmp[26]={0x0};
char cmd[300];
 
//time(&now);
now = time(NULL);
 
now_ptr = localtime(&now);
 
Current_Day = now_ptr->tm_yday;
//Current_Day = localtime(&now);
 
/** WARNING: tm_year is defined as (Actual year - 1900) this means
               that in the year 2000 %d, tm_year will print "100"
               without this additional test.
               ---------------------------------------------------- */
if(now_ptr->tm_year >= RT_V_YEAR_2000_MOD)
{
    now_ptr->tm_year %= RT_V_YEAR_2000_MOD;
}
strftime(date_str, sizeof(date_str), "%m%d", now_ptr);
 
sprintf(file_name, "rawlog_analyzed_%s.log",date_str);
 
//find out how many rawlog_analyzed files have been abandend
FILE *in=popen("ls rawlog_analyzed*","r");
 
if (fgets(tmp,sizeof(tmp),in)!=NULL)
{
    strcpy(tmp_name,tmp);
 
    if (strstr(tmp_name,file_name))
    {
       pFile = fopen (file_name, "a");
       printf("Appending to file");
    }
    else
     {
       //this is an old file and needs to be printed to the webpage
       sprintf(cmd, "rm %s", tmp_name);
       system(cmd);
       printf("Deleting old file\n");
       pFile = fopen (file_name, "w");
       printf("OPening new file\n");      
     }
}
 
     //todays file exists so just append to it
     
}
[+][-]03.29.2008 at 03:08PM PDT, ID: 21238341

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]03.29.2008 at 03:09PM PDT, ID: 21238345

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: C Programming Language, C++ Programming Language, CYGWIN
Tags: C
Sign Up Now!
Solution Provided By: josgood
Participating Experts: 3
Solution Grade: B
 
 
[+][-]03.29.2008 at 03:33PM PDT, ID: 21238423

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]03.29.2008 at 06:23PM PDT, ID: 21239007

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628