2 processes ‘A’ and ‘B’ consist of an alternating sequence of CPU burst and IO burst. Each burst is of 1 second duration. There are 10 CPU burst and 9 IO burst in each job. If job ‘A’ runs first than job ‘B’, how much time does is take to run the two jobs ‘A’ and ‘B’.
How much time will the job ‘A’ take for completion?
How much time will job ‘B’ take for completion?
How much time does the CPU remains idle?
What is the percentage utilization of CPU?
If two jobs are multi-programmed we start with job A. How much time will it take?
/* CALCULATING THE TIME TAKEN BY THE TWO PROCESSES
* AUTHOR : Saurajeet Dutta
* EMAIL : <vention.gothics> AT <gmail> DOT COM
* DATE : JAN 26 2010
* INSTITUTE : Birla Institute of Technology, Mesra
* LICENSE : FREE (as in freedom not as in beer)
* THE COMPILATION USES POSIX THREADING LIBRARIES.
* DEVELOPED IN openSuSE 11.2
*/
#include <stdio.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
//MACRO SECTION
#define SINGLE_PROGRAMMING 0
#define MULTI_PROGRAMMING 1
//END MACRO SECTION
//GLOBAL VARIABLE
unsigned short cpu_timeA = 0;
unsigned short cpu_timeB = 0;
unsigned short io_timeA = 0;
unsigned short io_timeB = 0;
//END GLOBAL VARIABLE
typedef struct
{ unsigned short thread_id;
bool mode;
} thread_control_block;
void * fnA (void * tcbA)
{ thread_control_block *myTCB;
unsigned short cpu_burst= 10;
unsigned short io_burst = 9;
unsigned long localCtr = 0;
int dummy, i;
myTCB = (thread_control_block *) tcbA;
if (myTCB->mode == SINGLE_PROGRAMMING)
{ for (localCtr = 0; localCtr < cpu_burst; localCtr++)
{ cpu_timeA++;
}
for (localCtr = 0; localCtr < io_burst; localCtr++)
{ io_timeA++;
}
}
if (myTCB->mode == MULTI_PROGRAMMING)
{ //CPU burst
for (localCtr = 0; localCtr < 800000000; localCtr++)
{}
sleep(9);
}
}
void * fnB (void * tcbB)
{ int dummy, i;
thread_control_block * myTCB;
unsigned short cpu_burst= 10;
unsigned short io_burst = 9;
unsigned long localCtr = 0;
myTCB = (thread_control_block *) tcbB;
if (myTCB->mode == SINGLE_PROGRAMMING)
{ for (localCtr = 0; localCtr < cpu_burst; localCtr++)
{ cpu_timeB++;
}
for (localCtr = 0; localCtr < io_burst; localCtr++)
{ io_timeB++;
}
}
if (myTCB->mode == MULTI_PROGRAMMING)
{ sleep (9);
for (localCtr = 0; localCtr < 800000000; localCtr++) //an average machine takes about 10s of cpu burst to compute this loop
{}
}
}
int main (void)
{ pthread_t threadA, threadB;
int retCode = 0;
int total_time = 0;
float eff = 0;
time_t time1, time2;
//create thread control block
thread_control_block tcb1;
thread_control_block tcb2;
tcb1.thread_id = 1;
tcb1.mode = SINGLE_PROGRAMMING;
tcb2.thread_id = 2;
tcb2.mode = SINGLE_PROGRAMMING;
//entering single programming mode
fnA ((void *) &tcb1);
fnB ((void *) &tcb2);
//analysing results
total_time = cpu_timeA + io_timeA;
printf ("\n\n\tSINGLE PROGRAMMING\n\tThe total time taken by process A is %d\n", total_time);
printf ("\tCPU time for A is %d\n", cpu_timeA);
printf ("\tIO time for A is %d\n", io_timeA);
printf ("\tCPU idle time for A is %d\n\n", total_time - cpu_timeA);
total_time = cpu_timeB + io_timeB;
printf ("\tThe total time taken by process B is %d\n",total_time);
printf ("\tCPU time for B is %d\n", cpu_timeB);
printf ("\tIO time for B is %d\n", io_timeB);
printf ("\tCPU idle time for B is %d\n", total_time - cpu_timeB);
eff = cpu_timeA + cpu_timeB;
eff /= cpu_timeA + cpu_timeB + io_timeA + io_timeB;
eff *= 100;
printf ("\n CPU EFFICIENCY [SINGLE PROGRAMMING] : %f\n", eff);
//creating thread a;
tcb1.mode = MULTI_PROGRAMMING;
tcb2.mode = MULTI_PROGRAMMING;
time (&time1);
retCode = pthread_create (&threadA, NULL, fnA, (void *)&tcb1);
if (retCode < 0)
{ printf ("Error in creating the thread.\n");
pthread_exit(NULL);
return EXIT_FAILURE;
}
//creating thread b
retCode = pthread_create (&threadB, NULL, fnB, (void *)&tcb2);
if (retCode < 0)
{ printf ("Error in creating thread B.\n");
pthread_exit(NULL);
return EXIT_FAILURE;
}
pthread_join (threadB, NULL);
time(&time2);
printf ("The total time taken for execution of 2 threads is: %d", time2 - time1);
eff = 20;
eff /= (time2 - time1 + 1);
eff *= 100;
printf ("\n\n\t CPU EFFICIENCY [MULTITASKING]: %f\n", eff);
return EXIT_SUCCESS;
}