tags:

views:

178

answers:

4

I need to sleep my program in Windows. What header file has the sleep function?

+1  A: 

MSDN: Header: Winbase.h (include Windows.h)

abelenky
When I include windows.h my compiler gives "error: parameter name omitted" and " error: expected expression before ',' token"
Snigger
@Snigger Post a short example that demonstrates this, and tell us what compiler you are using.
anon
#include <windows.h> void writeData(result * res ,char *OUT){ } int main(){ return 1; } gives " error: expected ')' before '*' token" and I'm using GCC (mingw)
Snigger
@Snigger Your program does not declare the type "result" - nothing to do with windows or Sleep. and it does not give the error messages you said it did (it gives different ones).
anon
it does! Not in the code above but in the main program it does.
Snigger
@Snigger The code in your comment IS the main program. There is no point in posting code that does not illustrate what you are asking about.
anon
No I mean I forgot to write the definition of that types in code above but in the other code that gives error those types are defined.
Snigger
@Snigger What happens when you compile the code I posted in my answer?
anon
I posted entire code of that code in a response to my code.
Snigger
Your code compiles without problem!
Snigger
This is NOT discussion/comments on my answer. This is an entirely tangential discussion that belongs in its own answer.
abelenky
+6  A: 
#include <windows.h>
Sleep( sometime_in_millisecs );   // note uppercase S

This is the first result when googling for "msdn sleep", which I think you could have managed yourself.

And here's a small example that compiles with MinGW and does what it says on the tin:

#include <windows.h>
#include <stdio.h>

int main() {
    printf( "starting to sleep...\n" );
    Sleep( 3000 );   // sleep three seconds
    printf( "sleep ended\n" );
}
anon
A: 

SleepEx function (see http://msdn.microsoft.com/en-us/library/ms686307.aspx) is the best choise if your program directly or indirectly creates windows (for example use some COM objects). In the simples cases you can also use Sleep.

Oleg
A: 

Hi
After adding windows.h the code rises this error:

roundrobin.c: In function 'writeData':
roundrobin.c:137:1: error: parameter name omitted
roundrobin.c:144:18: error: expected expression before ',' token

This is the code:

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// Data Structures
typedef struct process{
    char    jobName;
    int     arrivalTime;
    int     execTime;
    struct process *next;
} P;

typedef struct result{
    char Name;
    struct result *next;
} result;
// End of Data Structures

int quantum;
int counter=0;
int jobCounts=0;
int ResponseTime=0;
double timeEsp=0;

// Function Prototypes
void add_to_process_list(P **List,P *data);
void add_to_result_list(result **List,char name);
P *readData(char* fileName);
void writeData(result *res,char *OUT);
P *removeFromList(P **head,P **El);
int jobCount(P *head);
double get_time(void);
// End of Function Prototypes

int main(int argc, char *argv[]){
    P *pList=NULL,*tmpNode=NULL;
    result *RR_result=NULL,*JR=NULL;
    double start, stop;     

    pList=readData(argv[1]);
    jobCounts=jobCount(pList);

    start = get_time();

    if(pList!=NULL){
        tmpNode=pList;

        while(tmpNode!=NULL){
            if(tmpNode->arrivalTime<=(get_time()-start)){
                JR=(result *)malloc(sizeof(result *));
                //Sleep(quantum*1000); //simulate process time
                add_to_result_list(&RR_result,tmpNode->jobName);
                tmpNode->execTime-=quantum;
                if(tmpNode->execTime==0){
                    ResponseTime+=((get_time()-start)-tmpNode->arrivalTime);
                    tmpNode=removeFromList(&pList,&tmpNode);
                }else
                    tmpNode=tmpNode->next;  
                counter+=quantum;
            }else
                tmpNode=tmpNode->next;
        }

        stop= get_time();
        timeEsp=(stop-start);           
        writeData(RR_result,argv[2]);
        return 0;
    }else{
        return 1;
    }

}

void add_to_process_list(P **List,P *data){
    P *new=malloc(sizeof(P));
    new->arrivalTime=data->arrivalTime;
    new->execTime=data->execTime;
    new->jobName=data->jobName;

    if(*List==NULL){
        new->next=new;
        *List=new;
    }else{
        P *tmp;
        tmp=*List;
        while(tmp->next!=(*List)) tmp=tmp->next;
        tmp->next=new;
        new->next=*List;
    }   
}

void add_to_result_list(result **List,char name){
    result *new=malloc(sizeof(result));
    new->Name=name;
    new->next=NULL;

    if(*List==NULL){
        *List=new;
    }else{
        result *tmp;
        tmp=*List;
        while(tmp->next!=NULL) tmp=tmp->next;
        tmp->next=new;
    }   
}

P *readData(char* fileName){
    P *head=NULL,*cur=NULL;
    char Name,*tmp;
    int AT,ET;
    FILE *iF;
    tmp=(char*)malloc(sizeof(char*));

    if((iF=fopen(fileName,"r"))>0){
        fgets(tmp,255,iF);
        sscanf(tmp,"Interval:%d\n",&quantum);
        fgets(tmp,255,iF); //waste

        while(!feof(iF) &&  fgets(tmp,255,iF)){
            sscanf(tmp,"%20c %20d %20d\n",&Name,&AT,&ET);
            cur=(P *)malloc(sizeof(P *));
            cur->jobName=Name;
            cur->arrivalTime=AT;
            cur->execTime=ET;
            add_to_process_list(&head,cur);
        }

        fclose(iF);
        return head;
    }else{
        printf("Fatal error:\n\tError openning input file.\n");
        return NULL;        
    }
}

void writeData(result * res ,char *OUT){
    result *tmp=res;
    FILE *oF;
    int tmpCounter=1;
    double throughput=jobCounts/timeEsp;
    double RR=ResponseTime/jobCounts;

    if((oF=fopen(OUT,"w"))>0){
        fprintf(oF,"%-15s %-15s\n","Job Name","Execution Time");
        while(tmp!=NULL){
            if(tmp->next!=NULL && tmp->Name==tmp->next->Name)
                tmpCounter++;
            else{
                fprintf(oF,"%-15c %-15d\n",tmp->Name,tmpCounter*quantum);
                tmpCounter=1;
            }
            tmp=tmp->next;
        }
        fprintf(oF,"Response Time: %0.2f\n",RR);
        fprintf(oF,"Throghput: %0.2f\n",throughput);        

        fclose(oF);
    }else{
        printf("Fatal error:\n\tError openning output file.\n");
    }
}


P *removeFromList(P **head,P **El){
    P *tmp=*head;
    if((*El)->next==*El){
        free(*El);
        return NULL;
    }else{
        while(tmp->next!=NULL && tmp->next!=*El) tmp=tmp->next;
        tmp->next=tmp->next->next;
        free(*El);
        return tmp->next;
    }
}

int jobCount(P *head){
    P *tmp=head;
    int count=1;
    if(tmp->next==tmp){
        return 1;
    }

    while(tmp->next!=head) {
        tmp=tmp->next;
        count++;
    }   
    return count;
}


double get_time(void){
    return (clock()/CLOCKS_PER_SEC);
}
Snigger
It looks like something is redefining OUT as a macro somewhere - change it to "out" (in all places) and all should be OK. Of course, this has nothing to do with Sleep.
anon
Thank you very much. you are right.
Snigger
It is actually defined (as nothing) in windef.h - for no very good reason I can see. Really, the use of #define in the Windows headers is so badly thought out it isn't funny.
anon