Thread: Time sharing

  1. #1
    Registered User Rains Maramara's Avatar
    Join Date
    Oct 2014
    Posts
    4

    Time sharing

    I THINK MY MISTAKE IS IN THE MAIN FUNCTION. CAN SOMEBODY PLEASE HELP ME?

    Code:
    
    
    #include<stdio.h>
    #include<conio.h>
    #include<windows.h>
    #include<stdbool.h>
    #include<string.h>
    #define N 6
    
    
    typedef struct{
            char pn[N];
            int dur;
            }PROCESS;
    
    
    typedef struct{
            PROCESS que[N];
            int head;
            int tail;
            }QUE;
    
    
    PROCESS getInput(int);
    void createQ(QUE *);
    void addQ(QUE *, PROCESS);
    PROCESS delQ(QUE *);
    bool emptyQ(int, int);
    bool fullQ(int, int);
    
    
    int main(void){
        int i, j;
        QUE input;
        PROCESS data;
        bool isFull, isEmpty;
        
        createQ(&input);
        
        for(i=0;i<N-1;i++){
                            data=getInput(i);
                            isFull=fullQ(input.head,input.tail);
                            if(isFull){
                                        printf("Error: Queue is full.");
                                        return;
                                      }
                            else
                                    addQ(&input,data);
                              }
        
        isEmpty=emptyQ(input.head,input.tail);                                                
        while(!isEmpty){    
                        data=delQ(&input);
                        printf("Processing %s ", data.pn);
                                                                
                        if(data.dur>10){
                                            for(j=0;j<data.dur;j++){
                                            printf(".");
                                            Sleep(1000);
                                                                    }
                                                                    data.dur=data.dur-10;
                                                                    printf("Remaining time after processing is: %d", data.dur);
                                                                    addQ(&input,data);                                             
                                        }
    
    
                        else{
                                                for(j=0;j<data.dur;j++){
                                                                        printf(".");
                                                                        Sleep(1000);
                                                                       }
                                            }
                        }
                    
        printf("\nProcessing complete!");
        
        getch();
        return 0;
        }
        
    PROCESS getInput(int i){
        PROCESS input;
        
        printf("Input a process name (%d): ", i+1);
        scanf("%s", input.pn);  
        printf("Input duration of process: ");
           scanf("%d", &input.dur);
           
        if(input.dur<=0){
                            do{
                            printf("Please input a number greater than zero: ");
                            scanf("%d", &input.dur);
                            }while(input.dur<=0);                      
                         }
        
        printf("\n");
        
        return input;
        }
    
    
    void createQ(QUE *q){
         int i;
         
         for(i=0;i<N;i++){
                         strcpy(q->que[i].pn," ");
                         q->que[i].dur=0;
                          }
                          
                         q->head=0;
                         q->tail=0;
         
         return;
         }
    
    
    void addQ(QUE *q, PROCESS data){
         q->que[q->tail]=data;
         
         if(q->tail==(N-1))
             q->tail=0;
         else
             q->tail++;
         }
         
    PROCESS delQ(QUE *q){
            PROCESS r;
            
            r=q->que[q->head];
            strcpy(q->que[q->head].pn," ");
            q->que[q->head].dur=0;
            if(q->head==(N-1))
                q->head=0;
            else
                q->head++;
            
            return r;
            }
    
    
    bool emptyQ(int head, int tail){
         if(head==tail)
             return true;
         else
             return false;
         }
    
    
    bool fullQ(int head, int tail){
         if(head==((tail+1)%N))
             return true;
         else
             return false;
         }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Please indent your code properly, i.e., be consistent and choose a reasonable indent width (e.g., 2 to 8 spaces per indent level, or a single tab).

    As for your problem: you do not seem to have mentioned how your code does not work. For example, you could tell us what is your test input, the expected output, and the actual output.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User Rains Maramara's Avatar
    Join Date
    Oct 2014
    Posts
    4
    Quote Originally Posted by laserlight View Post
    Please indent your code properly, i.e., be consistent and choose a reasonable indent width (e.g., 2 to 8 spaces per indent level, or a single tab).

    As for your problem: you do not seem to have mentioned how your code does not work. For example, you could tell us what is your test input, the expected output, and the actual output.
    its output is endless.
    my test input: a & 1, b & 2, c & 3, d & 4, e & 5
    expected output: processing <number of periods equal to inputted number>

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-17-2013, 11:32 PM
  2. Replies: 2
    Last Post: 04-17-2013, 12:25 AM
  3. CPU time-sharing
    By Sebastiani in forum Windows Programming
    Replies: 6
    Last Post: 08-18-2004, 12:51 PM
  4. Sharing a C struct
    By rotis23 in forum Linux Programming
    Replies: 3
    Last Post: 02-22-2004, 03:47 PM
  5. time-sharing
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 11-25-2002, 07:45 AM