Wednesday, March 9, 2011

Source Code

Hai Guys if your in need of any Source code for Program.

Reply I will Help U by my own or i will collect it from Some Where Else.

Here i post some program usefull to handling windows console...








Some of the Programming Code:

                                                                                                              
HANDLING  CONSOLE USING WIN32API
                                                                                                              





                                                                                 
FORCE THE CONSOLE TO FULL SCREEN: 
                                                                                 
void fullscreen()
{
    keybd_event(VK_MENU,0x38,0,0);
    keybd_event(VK_RETURN,0x1c,0,0);
    keybd_event(VK_RETURN,0x1c,KEYEVENTF_KEYUP,0);
    keybd_event(VK_MENU,0x38,KEYEVENTF_KEYUP,0);
}



                                                              

CHANGE THE CONSOLE COLOR
                                                              



    void color(int cl)
    {
        HANDLE hOut;
        hOut = GetStdHandle(STD_OUTPUT_HANDLE);
        SetConsoleTextAttribute(hOut,cl);

    }

                                                                                  
    MOVE THE CURSOR TO X,Y POSITION 
                                                                                   

    void gotoxy(int x,int y)
    {
    COORD c;
    c.X=x;
    c.Y=y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);
    }




                                                                                                                                                             


     SJF CPU Scheduling Algorithm


     
    #include
    #include
    #include;
    void main()
    
    {
    char p[10][5],temp[5];
    
    int tot=0,wt[10],pt[10],i,j,n,temp1; 
    float avg=0;
    clrscr();
    printf("enter no of processes:");
    
    scanf("%d",&n);
    for(i=0;i<n;i++)
    
    {
    printf("enter process%d name:\n",i+1);
    scanf("%s",&p[i]);
    
    printf("enter process time");
    scanf("%d",&pt[i]);
    
    }
    for(i=0;i<n-1;i++)
    
    {
    for(j=i+1;j<n;j++)
    
    {
    if(pt[i]>pt[j])
    {
    temp1=pt[i];
    
    pt[i]=pt[j];
    pt[j]=temp1;
    
    strcpy(temp,p[i]);
    strcpy(p[i],p[j]);
    
    strcpy(p[j],temp);
    }
    }
    }
    
    wt[0]=0;
    for(i=1;i<n;i++)
    
    {
    wt[i]=wt[i-1]+et[i-1];
    
    tot=tot+wt[i];
    }
    avg=(float)tot/n;
    
    printf("p_name\t P_time\t w_time\n");
    for(i=0;i<n;i++)
    
    printf("%s\t%d\t%d\n",p[i],et[i],wt[i]);
    
    printf("total waiting time=%d\n avg waiting time=%f",tot,avg);
    getch();
    
    }

    OUTPUT:

    enter no of processes: 5
    enter process1 name: aaa

    enter process time: 4

    enter process2 name: bbb

    enter process time: 3

    enter process3 name: ccc


    enter process time: 2

    enter process4 name: ddd

    enter process time: 5

    enter process5 name: eee

    enter process time: 1
    p_name P_time w_time
    eee 1 0

    ccc 2 1


    bbb 3 3

    aaa 4 6

    ddd 5 10

    total waiting time=20

    avg waiting time=4.00


    Hai Guys THIS Program is not wittren of my own i just download it from another site and post it on the basis of my friend request.
    ROUND ROBIN SCHEDULING:

    /* Scheduling Simulation*/

    #include
    #include

    /* Process Data Structure */
    struct process {
    int pid; /* Process ID */
    int burst; /* CPU Burst Time */
    int priority; /* Priority */
    int working; /* Working time, for round-robin scheduling */
    int waiting; /* Waiting time, for round-robin scheduling */
    struct process *next;
    };

    /* Function Prototype Declarations */
    struct process *init_process (int pid, int burst, int priority);
    void fcfs (struct process *proc);
    void listprocs (struct process *proc);
    void priority (struct process *proc);
    void rr (struct process *proc, int quantum);
    void sjf (struct process *proc);

    /* Main Program Segment */
    int main (void) {
    /* Initialize process list */
    struct process *plist, *ptmp;
    plist = init_process(1, 10, 3);
    plist->next = init_process(2, 1, 1); ptmp = plist->next;
    ptmp->next = init_process(3, 2, 3); ptmp = ptmp->next;
    ptmp->next = init_process(4, 1, 4); ptmp = ptmp->next;
    ptmp->next = init_process(5, 5, 2);

    /* Perform simulations */
    listprocs(plist);
    fcfs(plist);
    sjf(plist);
    priority(plist);
    rr(plist, 1);

    /* Terminate cleanly */
    while (plist != NULL) {
    ptmp = plist;
    plist = plist->next;
    free(ptmp);
    };
    return(0);
    };


    /* Process list entry initialization routine */
    struct process *init_process (int pid, int burst, int priority) {
    struct process *proc;
    proc = malloc(sizeof(struct process));
    if (proc == NULL) {
    printf("Fatal error: memory allocation failure.\nTerminating.\n");
    exit(1);
    };
    proc->pid = pid;
    proc->burst = burst;
    proc->priority = priority;
    proc->working = 0;
    proc->waiting = 0;
    proc->next = NULL;
    return(proc);
    };


    /* First-Come-First-Served scheduling simulation */
    void fcfs (struct process *proc) {
    int time = 0, start, end;
    struct process *tmp = proc;

    printf("BEGIN:\tFirst-Come-First-Served scheduling simulation\n");

    while (tmp != NULL) {
    start = time;
    time += tmp->burst;
    end = time;
    printf("Process: %d\tEnd Time: %d\tWaiting: %d\tTurnaround: %d\n", tmp->pid, time, start, end);
    tmp = tmp->next;
    };

    printf("END:\tFirst-Come-First-served scheduling simulation\n\n");
    };


    /* Process listing */
    void listprocs (struct process *proc) {
    struct process *tmp = proc;

    printf("BEGIN:\tProcess Listing\n");

    while (tmp != NULL) {
    printf("PID: %d\t\tPriority: %d\tBurst: %d\n", tmp->pid, tmp->priority, tmp->burst);
    tmp = tmp->next;
    };

    printf("END:\tProcess Listing\n\n");
    };


    /* Priority scheduling simulation
    * Note: lower priority value gets a higher priority
    */
    void priority (struct process *proc) {
    int time, start, end, highest;
    struct process *copy, *tmpsrc, *tmp, *beforehighest;

    printf("BEGIN:\tPriority scheduling simulation\n");

    /* Duplicate process list */
    tmpsrc = proc;
    copy = tmp = NULL;
    while (tmpsrc != NULL) {
    if (copy == NULL) {
    copy = init_process(tmpsrc->pid, tmpsrc->burst, tmpsrc->priority);
    tmp = copy;
    } else {
    tmp->next = init_process(tmpsrc->pid, tmpsrc->burst, tmpsrc->priority);
    tmp = tmp->next;
    };
    tmpsrc = tmpsrc->next;
    };

    /* Main routine */
    time = 0;
    while (copy != NULL) {
    /* Find the next job */
    beforehighest = NULL;
    highest = copy->priority;
    tmp = copy->next;
    tmpsrc = copy;
    while (tmp != NULL) {
    if (tmp->priority < highest) { highest = tmp->priority;
    beforehighest = tmpsrc;
    };
    tmpsrc = tmp;
    tmp = tmp->next;
    };

    /* Process job and remove from copy of process list */
    if (beforehighest == NULL) {
    /* Handle first job is highest priority case */
    start = time;
    time += copy->burst;
    end = time;
    printf("Process: %d\tEnd Time: %d\tWaiting: %d\tTurnaround: %d\n", copy->pid, time, start, end);
    tmpsrc = copy->next;
    free(copy);
    copy = tmpsrc;
    } else {
    /* Handle first job is not highest priority case */
    tmp = beforehighest->next;
    start = time;
    time += tmp->burst;
    end = time;
    printf("Process: %d\tEnd Time: %d\tWaiting: %d\tTurnaround: %d\n", tmp->pid, time, start, end);
    beforehighest->next = tmp->next;
    free(tmp);
    };
    };

    printf("END:\tPriority scheduling simulation\n\n");
    };


    /* Round-Robin scheduling simulation */
    void rr (struct process *proc, int quantum) {
    int jobsremain, passes;
    struct process *copy, *tmpsrc, *tmp, *slot;

    printf("BEGIN:\tRound-Robin scheduling simulation (Quantum: %d)\n", quantum);
    /* Duplicate process list */
    tmpsrc = proc;
    copy = tmp = NULL;
    while (tmpsrc != NULL) {
    if (copy == NULL) {
    copy = init_process(tmpsrc->pid, tmpsrc->burst, tmpsrc->priority);
    tmp = copy;
    } else {
    tmp->next = init_process(tmpsrc->pid, tmpsrc->burst, tmpsrc->priority);
    tmp = tmp->next;
    };
    tmpsrc = tmpsrc->next;
    };

    /* Main routine */
    jobsremain = 1;
    slot = NULL;
    while (jobsremain) {
    jobsremain = 0;

    /* Pick next working slot */
    if (slot == NULL) {
    slot = copy;
    jobsremain = 1;
    } else {
    passes = 0;
    do {
    if (slot->next == NULL) {
    passes++;
    slot = copy;
    } else {
    slot = slot->next;
    };
    } while (passes <= 2 && slot->burst == slot->working);
    if (passes <= 2) { jobsremain = 1; }; }; /* Perform a cycle */ tmp = copy; while (tmp != NULL) { if (tmp->burst > tmp->working) {
    if (tmp == slot) {
    tmp->working += quantum;
    } else {
    tmp->waiting += quantum;
    };
    };
    tmp = tmp->next;
    };
    };

    /* Display statistics and clean up copy */
    tmp = copy;
    while (tmp != NULL) {
    printf("Process: %d\tWorking: %d\tWaiting: %d\tTurnaround: %d\n", tmp->pid, tmp->working, tmp->waiting, tmp->working + tmp->waiting);
    tmpsrc = tmp;
    tmp = tmp->next;
    free(tmpsrc);
    };

    printf("END:\tRR scheduling simulation\n\n");
    };


    /* Shortest Job First scheduling simulation */
    void sjf (struct process *proc) {
    int time, start, end, shortest;
    struct process *copy, *tmpsrc, *tmp, *beforeshortest;

    printf("BEGIN:\tShortest Job First scheduling simulation\n");

    /* Duplicate process list */
    tmpsrc = proc;
    copy = tmp = NULL;
    while (tmpsrc != NULL) {
    if (copy == NULL) {
    copy = init_process(tmpsrc->pid, tmpsrc->burst, tmpsrc->priority);
    tmp = copy;
    } else {
    tmp->next = init_process(tmpsrc->pid, tmpsrc->burst, tmpsrc->priority);
    tmp = tmp->next;
    };
    tmpsrc = tmpsrc->next;
    };

    /* Main routine */
    time = 0;
    while (copy != NULL) {
    /* Find the next job */
    beforeshortest = NULL;
    shortest = copy->burst;
    tmp = copy->next;
    tmpsrc = copy;
    while (tmp != NULL) {
    if (tmp->burst < shortest) { shortest = tmp->burst;
    beforeshortest = tmpsrc;
    };
    tmpsrc = tmp;
    tmp = tmp->next;
    };

    /* Process job and remove from copy of process list */
    if (beforeshortest == NULL) {
    /* Handle first job is shortest case */
    start = time;
    time += copy->burst;
    end = time;
    printf("Process: %d\tEnd Time: %d\tWaiting: %d\tTurnaround: %d\n", copy->pid, time, start, end);
    tmpsrc = copy;
    copy = copy->next;
    free(tmpsrc);
    } else {
    /* Handle first job is not shortest case */
    tmp = beforeshortest->next;
    start = time;
    time += tmp->burst;
    end = time;
    printf("Process: %d\tEnd Time: %d\tWaiting: %d\tTurnaround: %d\n", tmp->pid, time, start, end);
    beforeshortest->next = tmp->next;
    free(tmp);
    };
    };

    printf("END:\tShortest Job First scheduling simulation\n\n");
    };

    Hai Guys THIS Program is not wittren of my own i just download it from another site and post it on the basis of my friend request.

    PRIORITY SCHEDULING

    SOURCE CODE:                          


    #include
    #include
    #include
    void main()
     {
       clrscr();
       int x,n,p[10],pp[10],pt[10],w[10],t[10],awt,atat,i;
       printf("Enter the number of process : ");
       scanf("%d",&n);
       printf("\n Enter process : time priorities \n");
       for(i=0;i<n;i++)
        {
          printf("\nProcess no %d : ",i+1);
          scanf("%d  %d",&pt[i],&pp[i]);
          p[i]=i+1;
        }
      for(i=0;i<n-1;i++)
       {
         for(int j=i+1;j<n;j++)
         {
           if(pp[i]<pp[j])
           {
             x=pp[i];
             pp[i]=pp[j];
             pp[j]=x;
             x=pt[i];
             pt[i]=pt[j];
             pt[j]=x;
             x=p[i];
             p[i]=p[j];
             p[j]=x;
          }
       }
    }
    w[0]=0;
    awt=0;
    t[0]=pt[0];
    atat=t[0];
    for(i=1;i<n;i++)
     {
       w[i]=t[i-1];
       awt+=w[i];
       t[i]=w[i]+pt[i];
       atat+=t[i];
     }
    printf("\n\n Job \t Burst Time \t Wait Time \t Turn Around Time   Priority \n");
    for(i=0;i<n;i++)
      printf("\n %d \t\t %d  \t\t %d \t\t %d \t\t %d \n",p[i],pt[i],w[i],t[i],pp[i]);
    awt/=n;
    atat/=n;
    printf("\n Average Wait Time : %d \n",awt);
    printf("\n Average Turn Around Time : %d \n",atat);
    getch();
    }

    Hai Guys THIS Program is not wittren of my own i just download it from another site and post it on the basis of my friend request.





                               FIRST COME FIRST SERVE(FCFS)                                          

    #include
    #include
    #include
    void main()
    {
    char p[10][5];
    int tot=0,wt[10],i,n;
    float avg=0;
    clrscr();
    printf("enter no of processes:");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
    printf("enter process%d name:\n",i+1);
    scanf("%s",&p[i]);
    printf("enter process time");
    scanf("%d",&pt[i]);
    }
     
    wt[0]=0;
    for(i=1;i<n;i++)
    {
    wt[i]=wt[i-1]+et[i-1];
    tot=tot+wt[i];
    }
    avg=(float)tot/n;
    printf("p_name\t P_time\t w_time\n");
    for(i=0;i<n;i++)
    printf("%s\t%d\t%d\n",p[i],et[i],wt[i]);
    printf("total waiting time=%d\n avg waiting time=%f",tot,avg);
    getch();
    }
    Hai Guys THIS Program is not wittren of my own i just download it from another site and post it on the basis of my friend request.