1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930 |
- int main()
- {
- ;
- }
- /*
- ....................................................... TCP C SERVER ....................................................................................
- #include<netinet/in.h>
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- int main()
- {
- int i, receiver ,listensock , size;
- char recvBuffer[100] , sendbuffer[100];
-
- struct sockaddr_in thisSocket , clientSock ;
-
- listensock = socket(PF_INET , SOCK_STREAM , 0);
-
- thisSocket.sin_family = AF_INET;
- thisSocket.sin_addr.s_addr = inet_addr("127.0.0.1");
- thisSocket.sin_port = htons(7778);
-
- bind(listensock , (struct sockaddr *)&thisSocket , sizeof(thisSocket));
- listen(listensock , 1);
- size = sizeof(struct sockaddr_in);
- receiver = accept(listensock , (struct sockaddr *)&clientSock , &size);
- while(1)
- {
- //////////// RECV PART ////////////
- i = recv(receiver,recvBuffer,100,0);
- if(i > 0)
- {
- recvBuffer[i] = '\0';
- printf("%s",recvBuffer);
- }
- //////////// SEND PART ////////////
- fgets(sendbuffer,100,stdin);
- send(receiver , sendbuffer , strlen(sendbuffer),0);
- }
- }
- ....................................................... TCP C CLIENT ....................................................................................
- #include<netinet/in.h>
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- int main()
- {
- int i, receiver;
- char recvBuffer[100] , sendbuffer[100];
-
- struct sockaddr_in thisSocket;
-
- receiver = socket(PF_INET , SOCK_STREAM , 0);
-
- thisSocket.sin_family = AF_INET;
- thisSocket.sin_addr.s_addr = inet_addr("127.0.0.1");
- thisSocket.sin_port = htons(7778);
- connect(receiver,(struct sockaddr *)&thisSocket,sizeof(thisSocket));
- while(1)
- {
- //////////// SEND PART ////////////
- fgets(sendbuffer,100,stdin);
- send(receiver,sendbuffer,strlen(sendbuffer),0);
- //////////// RECV PART ////////////
- i =recv(receiver,recvBuffer,100,0);
- if(i>0)
- {
- recvBuffer[i]='\0';
- printf("%s",recvBuffer);
- }
- }
- }
- ........................................................ TCP JAVA SERVER ...................................................................................
- import java.io.*;
- import java.net.*;
- import java.util.*;
- public class Main {
- public static void main(String[] args) throws IOException {
- ServerSocket ss = null;
- Socket s = null;
- ss = new ServerSocket(7777);
- s = ss.accept();
- DataOutputStream dos = new DataOutputStream(s.getOutputStream());
- DataInputStream dis = new DataInputStream(s.getInputStream());
- while(true)
- {
- String from_Client = dis.readUTF();
- System.out.println(from_Client);
- Scanner sc = new Scanner(System.in);
- String to_client = sc.nextLine();
- dos.writeUTF(to_client);
- }
- }
- }
- ........................................................ TCP JAVA CLIENT ...................................................................................
- import java.io.*;
- import java.net.*;
- import java.util.*;
- public class Main {
- public static void main(String[] args) throws IOException {
- Socket s = null;
- s = new Socket("127.0.0.1",7777);
- DataOutputStream dos = new DataOutputStream(s.getOutputStream());
- DataInputStream dis = new DataInputStream(s.getInputStream());
- while (true)
- {
- Scanner sc = new Scanner(System.in);
- String to_server = sc.nextLine();
- dos.writeUTF(to_server);
- String from_server = dis.readUTF();
- System.out.println(from_server);
- }
- }
- }
- ........................................................ UDP C CLIENT ...................................................................................
- #include<stdio.h>
- #include<netinet/in.h>
- #include<sys/socket.h>
- #include<sys/types.h>
- #include<string.h>
- #include<unistd.h>
- int main()
- {
- int sd,portno,i;
- struct sockaddr_in cli;
- char msg[80],ip[80];
- sd=socket(AF_INET,SOCK_DGRAM,0);
- bzero(&cli,sizeof(cli));
- printf("enter the port no");
- scanf("%d",&portno);
- cli.sin_port=htons(portno);
- cli.sin_family=AF_INET;
- printf("enter the ip address");
- scanf("%s",ip);
- cli.sin_addr.s_addr=inet_addr(ip);
- printf("\nenter the message to send\n");
- scanf("%s",msg);
- sendto(sd,msg,80,0,(struct sockaddr*)&cli,sizeof(cli));
- printf("\nreply from server\n");
- i=sizeof(cli);
- recvfrom(sd,msg,80,0,(struct sockaddr*)&cli,&i);
- printf("%s",msg);
- return 0;
- //scanf("%s",msg);
- }
- ........................................................ UDP C SERVER ...................................................................................
- #include<stdio.h>
- #include<netinet/in.h>
- #include<sys/socket.h>
- #include<sys/types.h>
- #include<string.h>
- #include<unistd.h>
- int main()
- {
- int sd,portno,i;
- struct sockaddr_in cli;
- char msg[80],ip[80];
- sd=socket(AF_INET,SOCK_DGRAM,0);
- bzero(&cli,sizeof(cli));
- printf("enter the port no");
- scanf("%d",&portno);
- cli.sin_port=htons(portno);
- cli.sin_family=AF_INET;
- cli.sin_addr.s_addr=INADDR_ANY;
- bind(sd,(struct sockaddr*)&cli,sizeof(cli));
-
- printf("\nreply from client\n");
- i=sizeof(cli);
- recvfrom(sd,msg,80,0,(struct sockaddr*)&cli,&i);
- printf("%s",msg);
- printf("\nenter the message to send\n");
- scanf("%s",msg);
- sendto(sd,msg,80,0,(struct sockaddr*)&cli,sizeof(cli));
- return 0;
- }
- ........................................................ UDP JAVA SERVER ...................................................................................
- import java.io.*;
- import java.net.*;
- import java.util.Scanner;
- public class Main
- {
- public static void main(String args[])throws IOException {
- DatagramSocket ds = new DatagramSocket(6058);
- byte receiver[] = new byte[1024];
- byte sender[] = new byte[1024];
- DatagramPacket RECVdp = new DatagramPacket(receiver, receiver.length);
- ds.receive(RECVdp);
- String sentence = new String(RECVdp.getData());
- System.out.println(sentence);
- InetAddress ip = RECVdp.getAddress();
- int port = RECVdp.getPort();
- Scanner sc = new Scanner(System.in);
- String reply = sc.nextLine();
- sender = reply.getBytes();
- DatagramPacket SENDdp = new DatagramPacket(sender, sender.length, ip, port);
- ds.send(SENDdp);
- ds.close();
- }
- }
- ........................................................ UDP JAVA CLIENT ...................................................................................
- import java.io.*;
- import java.net.*;
- import java.util.*;
- public class Main {
- public static void main(String[] args)throws IOException {
- byte receiver[] = new byte[1024];
- byte sender[] = new byte[1024];
- DatagramSocket ds = new DatagramSocket();
- InetAddress ip = InetAddress.getByName("localhost");
- Scanner sc = new Scanner(System.in);
- String reply = sc.nextLine();
- sender = reply.getBytes();
- DatagramPacket SENDdp = new DatagramPacket(sender,sender.length,ip,6058);
- ds.send(SENDdp);
- DatagramPacket RECVdp = new DatagramPacket(receiver,receiver.length);
- ds.receive(RECVdp);
- String from_server = new String(RECVdp.getData());
- System.out.println(from_server);
- ds.close();
- }
- }
- ........................................................ FILE IN C ...................................................................................
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- int main()
- {
- FILE *FP;
- int i=0;
- FP = fopen("test.txt","r");
- char str[1024];
- char c;
- while(!feof(FP))
- {
- str[i]=fgetc(FP);;
- i++;
- }
- str[i-1]='\0';
- puts(str);
- }
- ........................................................ FILE IN JAVA ...................................................................................
- import java.io.*;
- public class Main {
- public static void main(String args[]) throws IOException
- {
- int i=0;
- FileInputStream in = null;
- char str[] = new char[1000];
- in = new FileInputStream("test.txt");
- int c;
- while ((c = in.read()) != -1) {
- str[i++]=(char)c;
- }
- System.out.println(str);
- }
- }
-
- */
- .
- .
- .
- .
- .
- .
- .
- .
- .
- .
- .
- .
- .
- .
- .
- .
- .
- .
- .
- .
- .
- .
- .
- .
- .
- .
- .
- #include<stdio.h>
- int main()
- {
- printf("OSSSSSS");
- }
- /* --------------------------------- reader writer--------------------------------
- #include<stdio.h>
- #include<pthread.h>
- #include<string.h>
- #include<semaphore.h>
- void *read(), *write();
- int a;
- int hr=0, min=0, sec=0;
- void *ret;
- pthread_t rth, wth;
- pthread_mutex_t m;
- void main()
- {
- int r1=1, r2=1;
- a=10;
- //create thread
- r1=pthread_create(&rth,NULL,&read,NULL);
- r2=pthread_create(&wth,NULL,&write,NULL);
-
- //check for success/failure
- if(r1==0)
- printf("rth created successfully.\n");
- else
- printf("rth thread creation failed.\n");
- if(r2==0)
- printf("wth created successfully.\n");
- else
- printf("wth thread creation failed.\n");
- //initializing mutex
- pthread_mutex_init(&m,NULL);
- //Join threads
- pthread_join(rth,ret);
- pthread_join(wth,ret);
- }
- void *read()
- {
- while(1)
- {
- pthread_mutex_lock(&m);
- //printf("%d\n", a);
- printf("%d:%d:%d\n", hr, min, sec);
- pthread_mutex_unlock(&m);
- sleep(1);
- }
- pthread_exit(&ret);
- }
- void *write()
- {
- while(1)
- {
- pthread_mutex_lock(&m);
- sec++;
- if(sec==60)
- {
- sec=0;
- min++;
- }
- if(min==60)
- {
- min=0;
- hr++;
- }
- if(hr==24)
- {
- hr=0;
- }
- pthread_mutex_unlock(&m);
- sleep(1);
- }
- pthread_exit(&ret);
- }
- //-----------------------------------------------------------------------------------------------------------------
- */
- /*
- ....................................................... producer-consumer-semaphore ....................................................................................
- #include<stdio.h>
- #include<semaphore.h>
- #include<pthread.h>
- int buffer[10],tid1,tid2,cnt = 0;
- void *producer();
- void *consumer();
- void *ret;
- sem_t s1,s2;
- pthread_t pro,con;
- void main()
- {
- sem_init(&s1,0,0);
- sem_init(&s2,0,1);
- tid1 = pthread_create(&pro,NULL,&producer,NULL);
- if(tid1 == 0)
- printf("\nproducer created successfully");
- tid2 = pthread_create(&con,NULL,&consumer,NULL);
- if(tid2 == 0)
- printf("\nconsumer created successfully");
- pthread_join(pro,NULL);
- pthread_join(con,NULL);
- sem_destroy(&s1);
- sem_destroy(&s2);
- }
- void *producer()
- {
- int item_produced = 0;
- printf("\nproducing..");
- while(cnt < 10)
- {
- sem_wait(&s2);
- item_produced++;
- buffer[cnt] = item_produced;
- printf("\nitem produced : %d",item_produced);
- cnt++;
- sem_post(&s1);
- sleep(1);
- }
- return(&ret);
- }
- void *consumer()
- {
- int item_consumed;
- printf("\nconsuming..");
- while(cnt >= 0)
- {
- sem_wait(&s1);
- cnt--;
- item_consumed = buffer[cnt];
- printf("\nitem consumed : %d",item_consumed);
- sem_post(&s2);
- sleep(1);
- }
- return(&ret);
- }
- ....................................................... job scheduling ....................................................................................
- #include<cstdio>
- #include<iostream>
- #include<algorithm>
- #include<queue>
- using namespace std;
- void FCFS(int num_proc, int arrival_time[], int burst_time[])
- {
- int time_cntr = 0,next = 0,j,i,finish_time[num_proc],turnaround_time[num_proc],waiting_time[num_proc],done[num_proc+1];
- for(i=0; i<=num_proc; ++i)
- {
- done[i] = 0;
- }
- for(i=0; i<num_proc; ++i)
- {
- next = num_proc;
- for(j=0; j<=num_proc; ++j)
- {
- if(arrival_time[j] < arrival_time[next] && done[j] == 0)
- {
- //cout<<"next changed"<<endl<<endl;
- next = j;
- }
- }
- cout<<"next : "<<next<<endl;
- done[next] = 1;
- time_cntr = time_cntr + burst_time[next];
- finish_time[next] = time_cntr;
- cout<<"FT : "<<finish_time[next]<<endl;
- turnaround_time[next] = finish_time[next] - arrival_time[next];
- cout<<"TT : "<<turnaround_time[next]<<endl;
- waiting_time[next] = turnaround_time[next] - burst_time[next];
- cout<<"WT : "<<waiting_time[next]<<endl<<endl;
- }
- return;
- }
- void FCFS_new(int num_proc, int arrival_time[], int burst_time[])
- {
- int time_cntr=0,i,j,k,min_proc = 100,next,ft,wt,tt,done[num_proc];
- queue< int > ready_q;
- for(i=0; i<=num_proc; ++i)
- {
- done[i] = 0;
- }
- for(i=0; i<num_proc; ++i)
- {
- min_proc = 100;
- for(j=0; j<num_proc; ++j)
- {
- if(arrival_time[j] < min_proc && done[j] == 0)
- {
- min_proc = arrival_time[j];
- next = j;
- }
- }
- ready_q.push(next);
- done[next] = 1;
- }
- for(k=0; k<num_proc; ++k)
- {
- cout<<endl<<endl;
- next = ready_q.front();
- ready_q.pop();
- cout<<"next proc : "<<next;
- time_cntr = time_cntr + burst_time[next];
- ft = time_cntr;
- cout<<"finish time : "<<ft<<endl;
- tt = ft - arrival_time[next];
- cout<<"turnaround time : "<<tt<<endl;
- wt = tt - burst_time[next];
- cout<<"waiting time : "<<wt<<endl;
- }
- return;
- }
- void SJF_nonP(int num_proc, int arrival_time[], int burst_time[])
- {
- int i,j,k,done[num_proc],next,min_proc = 100,time_cntr = 0,ft,wt,tt;
- for(i=0; i<num_proc; ++i)
- {
- done[i] = 0;
- if(arrival_time[i] == 0)
- {
- done[i] = 1;
- next = i;
- }
- }
- time_cntr = burst_time[next];
- ft = time_cntr;
- cout<<"Finish time : "<<ft<<endl;
- tt = ft - arrival_time[next];
- cout<<"Turnaround time : "<<tt<<endl;
- wt = tt - burst_time[next];
- cout<<"Waiting time : "<<wt<<endl;
- for(i=1; i<num_proc; ++i)
- {
- min_proc = 100;
- for(j=0; j<num_proc; ++j)
- {
- if(arrival_time[j] < time_cntr)
- {
- if(burst_time[j] < min_proc && done[j] == 0)
- {
- min_proc = burst_time[j];
- next = j;
- }
- }
- }
- time_cntr = time_cntr + burst_time[next];
- ft = time_cntr;
- cout<<endl<<"Finish time : "<<ft<<endl;
- cout<<"Turnaround time : "<<tt<<endl;
- cout<<"Waiting time : "<<wt<<endl;
-
- }
- return;
- }
- void print_job(int ft,int tt,int wt)
- {
- cout<<endl<<"Finish time : "<<ft<<endl;
- cout<<"Turnaround time : "<<tt<<endl;
- cout<<"Waiting time : "<<wt<<endl;
- }
- void SJF_P(int num_proc, int arrival_time[], int burst_time[])
- {
- int i,j,k,done[num_proc],next,min_proc = 100,time_cntr = 0,ft,wt,tt;
- int bt1[100];
- for(i=0; i<num_proc; ++i)
- {
- done[i] = 0;
- bt1[i] = burst_time[i];
- // if(arrival_time[i] == 0)
- // {
- // if(burst_time[i] == 1)
- // done[i] = 1;
- // next = i;
- // }
- }
- int counter = 0;
-
- while(1)
- {
- min_proc = 100;
- for(i = 0;i < num_proc;i++)
- {
- if(burst_time[i] < min_proc && done[i] == 0 && arrival_time[i] <= counter)
- {
- min_proc = burst_time[i];
- next = i;
- }
- }
- counter++;
- time_cntr++;
- cout<<"selected "<<min_proc<<endl;
- if(min_proc == 100)
- break;
-
- if(burst_time[next] == 1)
- {
- done[next] = 1;
- //next = i;
- //time_cntr = time_cntr + bt1[next];
- ft = time_cntr;
- tt = ft - arrival_time[next];
- wt = tt - bt1[next];
- print_job(ft,tt,wt);
- burst_time[next]--;
- }
- else
- burst_time[next]--;
- // for(int i = 0;i < num_proc;i++)
- // cout<<burst_time[i]<<"\t"<<done[i]<<endl;
- }
- return;
- }
- void Priority_nonP(int num_proc, int arrival_time[], int burst_time[], int priority[])
- {
- int i,j,k,done[num_proc],next,min_proc = 100,time_cntr = 0,ft,wt,tt;
- for(i=0; i<num_proc; ++i)
- {
- done[i] = 0;
- if(arrival_time[i] == 0)
- {
- done[i] = 1;
- next = i;
- }
- }
- time_cntr = time_cntr + burst_time[next];
- ft = time_cntr;
- cout<<"Finish time : "<<ft<<endl;
- tt = ft - arrival_time[next];
- cout<<"Turnaround time : "<<tt<<endl;
- wt = tt - burst_time[next];
- cout<<"Waiting time : "<<wt<<endl;
- for(i=1; i<num_proc; ++i)
- {
- int max_priority = 100;
- for(j=0; j<num_proc; ++j)
- {
- if(arrival_time[j] < time_cntr)
- {
- if(priority[j] < max_priority && done[j] == 0)
- {
- max_priority = priority[j];
- next = j;
- }
- }
- }
- time_cntr = time_cntr + burst_time[next];
- ft = time_cntr;
- cout<<"Finish time : "<<ft<<endl;
- tt = ft - arrival_time[next];
- cout<<"Turnaround time : "<<tt<<endl;
- wt = tt - burst_time[next];
- cout<<"Waiting time : "<<wt<<endl;
- done[next] = 1;
- }
- return;
- }
- void Priority_P(int num_proc, int arrival_time[], int burst_time[], int priority[])
- {
- int i,j,k,done[num_proc],next,min_proc = 100,time_cntr = 0,ft,wt,tt;
- int bt1[num_proc];
- for(i=0; i<num_proc; ++i)
- {
- done[i] = 0;
- bt1[i] = burst_time[i];
- // if(arrival_time[i] == 0)
- // {
- // done[i] = 1;
- // next = i;
- // }
- }
- // time_cntr = time_cntr + burst_time[next];
- // ft = time_cntr;
- // cout<<"Finish time : "<<ft<<endl;
- // tt = ft - arrival_time[next];
- // cout<<"Turnaround time : "<<tt<<endl;
- // wt = tt - burst_time[next];
- // cout<<"Waiting time : "<<wt<<endl;
- while(1)
- {
- int max_priority = 100;
- for(j=0; j<num_proc; ++j)
- {
- if(arrival_time[j] <= time_cntr)
- {
- if(priority[j] < max_priority && done[j] == 0)
- {
- max_priority = priority[j];
- next = j;
- }
- }
- }
- time_cntr++;
- //cout<<"selected "<<max_priority<<endl;
- if(max_priority == 100)
- break;
-
- if(burst_time[next] == 1)
- {
- done[next] = 1;
- //next = i;
- //time_cntr = time_cntr + bt1[next];
- ft = time_cntr;
- tt = ft - arrival_time[next];
- wt = tt - bt1[next];
- print_job(ft,tt,wt);
- burst_time[next]--;
- }
- else
- burst_time[next]--;
- // ft = time_cntr;
- // tt = ft - arrival_time[next];
- // wt = tt - burst_time[next];
- //done[next] = 1;
- }
- return;
- }
- void RR(int num_proc, int arrival_time[], int burst_time[], int priority[])
- {
- return;
- }
- int main()
- {
- int i,j,k,num_proc,choice;
- while(1)
- {
- cout<<"Enter your choice :"<<endl;
- cout<<"1] FCFS"<<endl<<"2] SJF(Non-Preemptive)"<<endl<<"3] SJF(Preemptive)"<<endl<<"4] Priority(Non-Preemptive)"<<endl<<"5] Priority(Preemptive)"<<endl<<"6] Round-Robin"<<endl<<"7] EXIT"<<endl<<endl;
- cin>>choice;
- if(choice == 7)
- break;
- cout<<"Enter number of processes :"<<endl<<endl;
- cin>>num_proc;
- int arrival_time[num_proc+1],burst_time[num_proc],priority[num_proc];
- cout<<"Enter Arrival Times, Burst Times and Priority of processes :"<<endl<<endl;
- for(i=0; i<num_proc; i++)
- {
- cin>>arrival_time[i];
- cin>>burst_time[i];
- cin>>priority[i];
- }
- arrival_time[num_proc] = 10000;
- switch(choice)
- {
- case 1:
- FCFS_new(num_proc, arrival_time, burst_time);
- break;
- case 2:
- SJF_nonP(num_proc, arrival_time, burst_time);
- break;
- case 3:
- SJF_P(num_proc, arrival_time, burst_time);
- break;
- case 4:
- Priority_nonP(num_proc, arrival_time, burst_time, priority);
- break;
- case 5:
- Priority_P(num_proc, arrival_time, burst_time, priority);
- break;
- case 6:
- RR(num_proc, arrival_time, burst_time, priority);
- break;
- }
- }
- return 0;
- }
- ........................................................ page replacement FIFO ONLY ...................................................................................
- #include<stdio.h>
- #include<stdlib.h>
- #include<iostream>
- #include<queue>
- using namespace std;
- void FIFO(int num_of_frames,int num_of_pages,int pages[],int time[],int cnt)
- {
- int i,j,k,m,n,memory[num_of_frames],num_of_pf = 0,no_pf = 0, valid_pf = 0;
- queue< int > mem;
- for(j=0; j<num_of_frames; ++j)
- memory[j] = 0;
- for(j=0; j<cnt; ++j)
- {
- for(k=0; k<num_of_frames; ++k)
- {
- if(memory[k] == pages[j])
- {
- no_pf = 1;
- break;
- }
- else
- continue;
- }
- if(no_pf == 1)
- {
- cout<<"j : "<<j<<" no pf"<<endl;
- no_pf = 0;
- continue;
- }
- else
- {
- num_of_pf++;
- cout<<"j : "<<j<<" pf : "<<num_of_pf<<endl;
- for(m=0; m<num_of_frames; ++m)
- {
- if(memory[m] == 0)
- {
- valid_pf = 1;
- break;
- }
- }
- if(valid_pf == 1)
- {
- memory[m] = pages[j];
- mem.push(pages[j]);
- valid_pf = 0;
- continue;
- }
- else
- {
- int replace_page = mem.front();
- mem.pop();
- mem.push(pages[j]);
- for(n=0; n<num_of_frames; ++n)
- {
- if(memory[n] == replace_page)
- {
- memory[n] = pages[j];
- break;
- }
- }
- }
- }
- }
- cout<<"number of page faults : "<<num_of_pf<<endl;
- }
- void Optimal(int pages[],int time[],int num_of_frames,int num_of_pages)
- {
- }
- int main()
- {
- int num_of_frames,num_of_pages,i=0,j,k,cnt=0;
- cout<<"Enter no. of frames : ";
- cin>>num_of_frames;
- cout<<"Enter no. of pages : ";
- cin>>num_of_pages;
- int pages[num_of_pages],time[num_of_pages],choice;
- cout<<"Enter the Reference String : ";
- while(1)
- {
- cin>>pages[cnt];
- if(pages[cnt] == 0)
- break;
- cnt++;
- }
- cout<<"Enter your choice : "<<endl;
- cout<<"1] FIFO"<<endl<<"2] Optimal"<<endl<<"3] LRU"<<endl;
- cin>>choice;
- switch(choice)
- {
- case 1:
- FIFO(num_of_frames,num_of_pages,pages,time,cnt);
- break;
- case 2:
- Optimal(pages,time,num_of_frames,num_of_pages);
- break;
- case 3:
- //LRU(pages,time,num_of_frames,num_of_pages);
- break;
- case 4:
- exit(0);
- }
- return 0;
- }
- ........................................................ DISC SCHEDULING ...................................................................................
- #include<stdio.h>
- #include<queue>
- #include<iostream>
- #include<algorithm>
- using namespace std;
- void fcfs(int num, int req[], int current)
- {
- int i,seek=0;
- for(i=0; i<num; ++i)
- {
- if(current <= req[i])
- seek = seek + (req[i] - current);
- if(current > req[i])
- seek = seek + (current - req[i]);
- current = req[i];
- }
- cout<<"total seek time : "<<seek<<endl;
- }
- int find_min(int num, int current, int req[], int done[])
- {
- int temp,i,j,arr[num];
- for(i=0; i<num; ++i)
- {
- temp = current - req[i];
- if(temp < 0)
- temp = temp - (2 * temp);
- arr[i] = temp;
- }
- int min_req = 10000,min_index;
- for(i=0; i<num; ++i)
- {
- if(min_req > arr[i] && done[i] == 0)
- {
- min_req = arr[i];
- min_index = i;
- }
- }
- return min_index;
- }
- void sstf(int num, int req[], int current)
- {
- int i,j,done[num],next,seek = 0;
- for(i=0; i<num; ++i)
- done[i] = 0;
- for(i=0; i<num; ++i)
- {
- next = find_min(num,current,req,done);
- if(current > req[next])
- seek = seek + (current - req[next]);
- else if(current <= req[next])
- seek = seek + (req[next] - current);
- current = req[next];
- done[next] = 1;
- }
- cout<<seek;
- }
- void scan(int num, int req[], int current)
- {
- int i,j,newreq[num+1],cur_index;
- for(i=0; i<num; ++i)
- {
- newreq[i] = req[i];
- }
- newreq[num] = current;
- sort(newreq,(newreq+num+1));
- for(i=0; i<(num+1); ++i)
- {
- if(newreq[i] == current)
- {
- cur_index = i;
- break;
- }
- }
- cout<<"current at : "<<cur_index<<endl;
- int seek = 0,next = cur_index;
- for(i=0; i<(num-cur_index); ++i)
- {
- seek = seek + (newreq[next+1] - newreq[next]);
- next++;
- }
- next = cur_index-1;
- seek = seek + (newreq[num] - newreq[next]);
- for(i=1; i<cur_index; ++i)
- {
- seek = seek + (newreq[next] - newreq[next-1]);
- next--;
- }
- cout<<"final : "<<seek<<endl;
- }
- int main()
- {
- int i,num,cur;
- cout<<"enter current head position : ";
- cin>>cur;
- cout<<"enter number of requests : ";
- cin>>num;
- int req[num];
- cout<<"enter requests : ";
- for(i=0; i<num; ++i)
- cin>>req[i];
- //fcfs(num,req,cur);
- //sstf(num,req,cur);
- scan(num,req,cur);
- return 0;
- }
- ........................................................ BUBBLE SORT ...................................................................................
- #/bin/sh
- echo "Number of elements : "
- read num
- echo "Enter the elements : "
- for((i=0; i<num; ++i))
- do
- read arr[$i]
- done
- for((i=0; i<num; ++i))
- do
- for((j=0; j<num-1; ++j))
- do
- if [ ${arr[$j]} -gt ${arr[$j+1]} ]
- then
- temp=${arr[$j]}
- arr[$j]=${arr[$j+1]}
- arr[$j+1]=$temp
- fi
- done
- done
- echo "Sorted elements are : "
- for((i=0; i<num; ++i))
- do
- echo ${arr[$i]}
- done
- ........................................................ LRU ...................................................................................
- #include <stdio.h>
- #define MAX 20
- typedef struct page
- {
- int data;
- int hit;
- } page;
- void main()
- {
- int i,j,k,flag,cnt=0,q=0,flag2=0,temp,max,mix=0,f3=0;
- page p[MAX];
- int frame[MAX];
- int fs,nr;
- printf("\nEnter number of references: ");
- scanf("%d",&nr);
- printf("\nEnter %d references: ",nr);
- for(i=0; i<nr; i++)
- {
- scanf("%d",&frame[i]);
- }
- printf("\nEnter frame size: ");
- scanf("%d",&fs);
- for(i=0; i<fs; i++)
- {
- p[i].data=-1;
- p[i].hit=0;
- }
- j=0;
- for(i=0; i<nr; i++)
- {
- for(k=0; k<fs; k++)
- {
- if(p[k].data!=-1)
- p[k].hit++;
- }
- flag=0;
- printf("\nREF: %d",frame[i]);
- for(k=0; k<fs; k++)
- {
- if(p[k].data==frame[i])
- {
- p[k].hit=1;
- flag=1;
- temp=k;
- break;
- }
- }
- if(flag!=1 && f3!=1)
- {
- p[j].data=frame[i];
- p[j].hit=1;
- temp=j;
- cnt++;
- j++;
- }
- if(f3==1 && flag!=1)
- {
- max=0;
- mix=0;
- for(k=0; k<fs; k++)
- {
- if(max < p[k].hit)
- {
- max=p[k].hit;
- mix=k;
- }
- }
- flag2=0;
- for(k=0; k<fs; k++)
- {
- if(flag2!=1)
- {
- j=mix;
- p[j].data=frame[i];
- p[j].hit=1;
- temp=j;
- cnt++;
- flag2=1;
- }
- }
- }
- if(j==fs)
- {
- j=0;
- f3=1;
- }
- for(k=0; k<fs; k++)
- {
- if(temp==k)
- {
- printf("\n->%d-%d",p[k].data,p[k].hit);
- }
- else
- {
- printf("\n%d-%d",p[k].data,p[k].hit);
- }
- }
- printf("\n-------\n");
- }
- printf("\nPage Fault: %d\n",cnt);
- }
- ........................................................ IDEAL process SCHEDULING PROGRAM ...................................................................................
- #include<stdio.h>
- //Definition of structure for process
- struct process
- {
- char name[20];
- int flag,arrival,burst,initburst,prio;
- };
- //Queue Definition along with queue functions
- struct queue
- {
- struct process data[20];
- int r,f;
- };
- void init(struct queue *q)
- {
- q->r=q->f=-1;
- }
- int empty(struct queue q)
- {
- if(q.r==-1)
- return(1);
- return(0);
- }
- //Normal enqueue(used in FCFS and Round Robin)
- void enqueue(struct queue *q,struct process d)
- {
- if(empty(*q))
- q->r=q->f=0;
- else q->r=q->r+1;
- q->data[q->r]=d;
- }
- /*Enqueue for sjf scheduling. In this, we maintain a priority based queue
- where priority is given to the shortest job, i.e, the job with min. burst time is
- always at the front of the queue*/
- void sjfenqueue(struct queue *q, struct process d)
- {
- int x,y;
- if(empty(*q))
- {
- q->r=q->f=0;
- q->data[q->r]=d;
- }
- else
- {
- x=q->f;
- y=q->r;
- //Go through the queue to find position of new process d
- while(q->data[x].burst<=d.burst&&x<=y)
- x++;
- //Shift elements one place to the right to make space for d
- while(y>=x)
- {
- q->data[y+1]=q->data[y];
- y--;
- }
- q->data[x]=d;
- q->r=q->r+1;
- }
- }
- /*Enqueue for priority scheduling. Same as above except the priority is now
- using process priority instead of burst time*/
- void prioenqueue(struct queue *q, struct process d)
- {
- int x,y;
- if(empty(*q))
- {
- q->r=q->f=0;
- q->data[q->r]=d;
- }
- else
- {
- x=q->f;
- y=q->r;
- //Go through the queue to find position of new process d
- while(q->data[x].prio<=d.prio&&x<=y)
- x++;
- //Shift elements one place to the right to make space for d
- while(y>=x)
- {
- q->data[y+1]=q->data[y];
- y--;
- }
- q->data[x]=d;
- q->r=q->r+1;
- }
- }
- struct process dequeue(struct queue *q)
- {
- struct process d;
- d=q->data[q->f];
- if(q->f==q->r)
- q->f=q->r=-1;
- else q->f=q->f+1;
- return(d);
- }
- void accept(struct process p[20],int n);
- void fcfs(struct process p[20],int n);
- void sjf(struct process p[20],int n);
- void prio(struct process p[20],int n);
- void sjfpre(struct process p[20],int n);
- void priopre(struct process p[20],int n);
- void rr(struct process p[20],int n);
- void main()
- {
- int ch,n,i;
- struct process p[20];
- printf("\nEnter number of processes: ");
- scanf("%d",&n);
- accept(p,n);
- do
- {
- for(i=0;i<n;i++)
- p[i].flag=0;
- printf("\nMENU:\n1.FCFS\n2.SJF\n3.SJF-preemptive\n4.Priority\n5.Priority-preemptive\n6.Round Robin\n7.Re-enter processes\n8.Exit\n\n\tEnter your choice: ");
- scanf("%d",&ch);
- switch(ch)
- {
- case 1: fcfs(p,n);
- break;
- case 2: sjf(p,n);
- break;
- case 3: sjfpre(p,n);
- break;
- case 4: prio(p,n);
- break;
- case 5: priopre(p,n);
- break;
- case 6: rr(p,n);
- break;
- case 7: printf("\nEnter number of processes: ");
- scanf("%d",&n);
- accept(p,n);
- break;
- case 8: break;
- default:printf("\nWrong choice entered\n");
- break;
- }
- }while(ch!=8);
- }
- void accept(struct process p[20],int n)
- {
- int i;
- char s[2];
- for(i=0;i<n;i++)
- {
- printf("\nProcess %d: ",i+1);
- //We use sprintf to give thee processes names like 'P1','P2',etc.
- sprintf(p[i].name,"P%d",i+1);
- printf("\nEnter arrival time: ");
- scanf("%d",&p[i].arrival);
- printf("\nEnter burst time: ");
- scanf("%d",&p[i].initburst);
- //initburst is the initial burst time, and 'burst' is current burst time
- p[i].burst=p[i].initburst;
- }
- }
- void fcfs(struct process p[20],int n)
- {
- int i,j,turnaround,waiting,pretime[30],cnt=0,flag1=0,comp=0;
- float avgta=0,avgwt=0;
- struct queue q;
- init(&q);
- struct process pc;
- /* pc is the current process. Since initially, there is no process in
- pc, we set its flag to -1*/
- pc.flag=-1;
- printf("\nGANTT CHART\n\n\t|");
- /*In this loop, i signifies the moment in time. A process with arrival time 1 will be said to have arrived when i=1. The loop will run until completed processes(comp) equals no. of proceses*/
- for(i=0;comp<n;i++)
- {
- /*In this loop, we find the processes that have arrived and place them in the queue. A process has arrived if its arrival time is less than the current moment (i)
- flag is used to signify whether the process has been enqueued or not. If it has already been enqueued, it does not need to be enqueued again.
- */
- for(j=0;j<n;j++)
- {
- if(p[j].arrival<=i && p[j].flag==0)
- {
- p[j].flag=1;
- enqueue(&q,p[j]);
- }
- }
- /*If pc.flag=-1, it means no process has arrived upto the current moment or all arrived processes have completed execution*/
- if(pc.flag==-1)
- {
- /*flag1 is used to indicate a gap between process execution. Eg: if p1 finishes execution at 2 and p2 begins execution at 4, we will need to show a gap in the gantt chart*/
- if(empty(q) && flag1==0)
- flag1=1;
- else if(empty(q) && flag1==1)
- continue;
- else if(!empty(q) && flag1==1)
- {
- printf(" |");
- /*Variable pretime is used to keep track of the times that will be required when displaying the Gantt chart,such as process preemption or completion. Variable cnt is the count of these times */
- pretime[cnt++]=i;
- flag1=0;
- pc=dequeue(&q);
- }
- else pc=dequeue(&q);
- }
- /*Decrement burst time to indicate completion of one burst */
- pc.burst--;
- /* If burst time is zero, process is completed. So we calculate its turnaround time and waiting time, which will later be used to calculate the average TA and WT. We print the process name in Gantt Chart and put pc.flag=-1 to indicate that there is no current process */
- if(pc.burst==0)
- {
- turnaround=(i+1)-pc.arrival;
- waiting=turnaround-pc.initburst;
- avgta+=turnaround;
- avgwt+=waiting;
- pretime[cnt++]=i+1;
- comp++;
- printf("%s|",pc.name);
- pc.flag=-1;
- }
- }
- //Display the times below the Gantt Chart
- printf("\n\t0 ");
- for(i=0;i<cnt;i++)
- {
- if(pretime[i]>9)
- printf("%d ",pretime[i]);
- else printf (" %d ",pretime[i]);
- }
- //Calculate avg. TA and WT
- avgta=avgta/n;
- avgwt=avgwt/n;
- printf("\n\nAvg. TA time: %.2f",avgta);
- printf("\n\nAvg. WT time: %.2f",avgwt);
- }
- //Exactly the same as FCFS, except we use sjfenqueue function instead of enqueue function
- void sjf(struct process p[20],int n)
- {
- int i,j,turnaround,waiting,pretime[30],cnt=0,flag1=0,comp=0;
- float avgta=0,avgwt=0;
- struct queue q;
- init(&q);
- struct process pc;
- /* pc is the current process. Since initially, there is no process in
- pc, we set its flag to -1*/
- pc.flag=-1;
- printf("\nGANTT CHART\n\n\t|");
- /*In this loop, i signifies the moment in time. A process with arrival time 1 will be said to have arrived when i=1. The loop will run until completed processes(comp) equals no. of proceses
- */
- for(i=0;comp<n;i++)
- {
- /*In this loop, we find the processes that have arrived and place them in the queue. A process has arrived if its arrival time is less than the current moment (i)
- flag is used to signify whether the process has been enqueued or not. If it has already been enqueued, it does not need to be enqueued again.
- */
- for(j=0;j<n;j++)
- {
- if(p[j].arrival<=i && p[j].flag==0)
- {
- p[j].flag=1;
- sjfenqueue(&q,p[j]);
- }
- }
- /*If pc.flag=-1, it means no process has arrived upto the current moment or all arrived processes have completed execution*/
- if(pc.flag==-1)
- {
- /*flag1 is used to indicate a gap between process execution. Eg: if p1 finishes execution at 2 and p2 begins execution at 4, we will need to show a gap in the gantt chart*/
- if(empty(q) && flag1==0)
- flag1=1;
- else if(empty(q) && flag1==1)
- continue;
- else if(!empty(q) && flag1==1)
- {
- printf(" |");
- /*Variable pretime is used to keep track of the times that will be required when displaying the Gantt chart,such as process preemption or completion. Variable cnt is the count of these times */
- pretime[cnt++]=i;
- flag1=0;
- pc=dequeue(&q);
- }
- else pc=dequeue(&q);
- }
- /*Decrement burst time to indicate completion of one burst */
- pc.burst--;
- /* If burst time is zero, process is completed. So we calculate its turnaround time and waiting time, which will later be used to calculate the average TA and WT. We print the process name in Gantt Chart and put pc.flag=-1 to indicate that there is no current process */
- if(pc.burst==0)
- {
- turnaround=(i+1)-pc.arrival;
- waiting=turnaround-pc.initburst;
- avgta+=turnaround;
- avgwt+=waiting;
- pretime[cnt++]=i+1;
- comp++;
- printf("%s|",pc.name);
- pc.flag=-1;
- }
- }
- //Display the times below the Gantt Chart
- printf("\n\t0 ");
- for(i=0;i<cnt;i++)
- {
- if(pretime[i]>9)
- printf("%d ",pretime[i]);
- else printf (" %d ",pretime[i]);
- }
- //Calculate avg. TA and WT
- avgta=avgta/n;
- avgwt=avgwt/n;
- printf("\n\nAvg. TA time: %.2f",avgta);
- printf("\n\nAvg. WT time: %.2f",avgwt);
- }
- //Exactly the same as sjf,but with preemption
- void sjfpre(struct process p[20],int n)
- {
- int i,j,turnaround,waiting,pretime[30],cnt=0,flag1=0,comp=0;
- float avgta=0,avgwt=0;
- struct queue q;
- init(&q);
- struct process pc;
- /* pc is the current process. Since initially, there is no process in
- pc, we set its flag to -1*/
- pc.flag=-1;
- printf("\nGANTT CHART\n\n\t|");
- /*In this loop, i signifies the moment in time. A process with arrival time 1 will be said to have arrived when i=1. The loop will run until completed processes(comp) equals no. of proceses
- */
- for(i=0;comp<n;i++)
- {
- /*In this loop, we find the processes that have arrived and place them in the queue. A process has arrived if its arrival time is less than the current moment (i)
- flag is used to signify whether the process has been enqueued or not. If it has already been enqueued, it does not need to be enqueued again.
- */
- for(j=0;j<n;j++)
- {
- if(p[j].arrival<=i && p[j].flag==0)
- {
- p[j].flag=1;
- sjfenqueue(&q,p[j]);
- }
- }
- /*If pc.flag=-1, it means no process has arrived upto the current moment or all arrived processes have completed execution*/
- if(pc.flag==-1)
- {
- /*flag1 is used to indicate a gap between process execution. Eg: if p1 finishes execution at 2 and p2 begins execution at 4, we will need to show a gap in the gantt chart*/
- if(empty(q) && flag1==0)
- flag1=1;
- else if(empty(q) && flag1==1)
- continue;
- else if(!empty(q) && flag1==1)
- {
- printf(" |");
- /*Variable pretime is used to keep track of the times that will be required when displaying the Gantt chart,such as process preemption or completion. Variable cnt is the count of these times */
- pretime[cnt++]=i;
- flag1=0;
- pc=dequeue(&q);
- }
- else pc=dequeue(&q);
- }
- //The following 'else if' gives the condition for pre-emption. If the process at the front of the queue has smaller burst time than the current process, then current process is enqueued and the new process is dequeued into pc
- else if(q.f!=-1 && pc.burst > q.data[q.f].burst)
- {
- printf("%s|",pc.name);
- pretime[cnt++]=i;
- sjfenqueue(&q,pc);
- pc=dequeue(&q);
- }
- /*Decrement burst time to indicate completion of one burst */
- pc.burst--;
- /* If burst time is zero, process is completed. So we calculate its turnaround time and waiting time, which will later be used to calculate the average TA and WT. We print the process name in Gantt Chart and put pc.flag=-1 to indicate that there is no current process */
- if(pc.burst==0)
- {
- turnaround=(i+1)-pc.arrival;
- waiting=turnaround-pc.initburst;
- avgta+=turnaround;
- avgwt+=waiting;
- pretime[cnt++]=i+1;
- comp++;
- printf("%s|",pc.name);
- pc.flag=-1;
- }
- }
- //Display the times below the Gantt Chart
- printf("\n\t0 ");
- for(i=0;i<cnt;i++)
- {
- if(pretime[i]>9)
- printf("%d ",pretime[i]);
- else printf (" %d ",pretime[i]);
- }
- //Calculate avg. TA and WT
- avgta=avgta/n;
- avgwt=avgwt/n;
- printf("\n\nAvg. TA time: %.2f",avgta);
- printf("\n\nAvg. WT time: %.2f",avgwt);
- }
- //Exactly the same as FCFS, except we use prioenqueue function instead of enqueue function
- void prio(struct process p[20],int n)
- {
- int i,j,turnaround,waiting,pretime[30],cnt=0,flag1=0,comp=0;
- float avgta=0,avgwt=0;
- struct queue q;
- init(&q);
- struct process pc;
- /* pc is the current process. Since initially, there is no process in
- pc, we set its flag to -1*/
- pc.flag=-1;
- //Accept the priority for the processes
- for(i=0;i<n;i++)
- {
- printf("\nEnter priority for process %d: ",i+1);
- scanf("%d",&p[i].prio);
- }
- printf("\nGANTT CHART\n\n\t|");
- /*In this loop, i signifies the moment in time. A process with arrival time 1 will be said to have arrived when i=1. The loop will run until completed processes(comp) equals no. of proceses
- */
- for(i=0;comp<n;i++)
- {
- /*In this loop, we find the processes that have arrived and place them in the queue. A process has arrived if its arrival time is less than the current moment (i)
- flag is used to signify whether the process has been enqueued or not. If it has already been enqueued, it does not need to be enqueued again.
- */
- for(j=0;j<n;j++)
- {
- if(p[j].arrival<=i && p[j].flag==0)
- {
- p[j].flag=1;
- prioenqueue(&q,p[j]);
- }
- }
- /*If pc.flag=-1, it means no process has arrived upto the current moment or all arrived processes have completed execution*/
- if(pc.flag==-1)
- {
- /*flag1 is used to indicate a gap between process execution. Eg: if p1 finishes execution at 2 and p2 begins execution at 4, we will need to show a gap in the gantt chart*/
- if(empty(q) && flag1==0)
- flag1=1;
- else if(empty(q) && flag1==1)
- continue;
- else if(!empty(q) && flag1==1)
- {
- printf(" |");
- /*Variable pretime is used to keep track of the times that will be required when displaying the Gantt chart,such as process preemption or completion. Variable cnt is the count of these times */
- pretime[cnt++]=i;
- flag1=0;
- pc=dequeue(&q);
- }
- else pc=dequeue(&q);
- }
- /*Decrement burst time to indicate completion of one burst */
- pc.burst--;
- /* If burst time is zero, process is completed. So we calculate its turnaround time and waiting time, which will later be used to calculate the average TA and WT. We print the process name in Gantt Chart and put pc.flag=-1 to indicate that there is no current process */
- if(pc.burst==0)
- {
- turnaround=(i+1)-pc.arrival;
- waiting=turnaround-pc.initburst;
- avgta+=turnaround;
- avgwt+=waiting;
- pretime[cnt++]=i+1;
- comp++;
- printf("%s|",pc.name);
- pc.flag=-1;
- }
- }
- //Display the times below the Gantt Chart
- printf("\n\t0 ");
- for(i=0;i<cnt;i++)
- {
- if(pretime[i]>9)
- printf("%d ",pretime[i]);
- else printf (" %d ",pretime[i]);
- }
- //Calculate avg. TA and WT
- avgta=avgta/n;
- avgwt=avgwt/n;
- printf("\n\nAvg. TA time: %.2f",avgta);
- printf("\n\nAvg. WT time: %.2f",avgwt);
- }
- //Exactly the same as prio,but with preemption
- void priopre(struct process p[20],int n)
- {
- int i,j,turnaround,waiting,pretime[30],cnt=0,flag1=0,comp=0;
- float avgta=0,avgwt=0;
- struct queue q;
- init(&q);
- struct process pc;
- /* pc is the current process. Since initially, there is no process in
- pc, we set its flag to -1*/
- pc.flag=-1;
- //Accept the priority for the processes
- for(i=0;i<n;i++)
- {
- printf("\nEnter priority for process %d: ",i+1);
- scanf("%d",&p[i].prio);
- }
- printf("\nGANTT CHART\n\n\t|");
- /*In this loop, i signifies the moment in time. A process with arrival time 1 will be said to have arrived when i=1. The loop will run until completed processes(comp) equals no. of proceses
- */
- for(i=0;comp<n;i++)
- {
- /*In this loop, we find the processes that have arrived and place them in the queue. A process has arrived if its arrival time is less than the current moment (i)
- flag is used to signify whether the process has been enqueued or not. If it has already been enqueued, it does not need to be enqueued again.
- */
- for(j=0;j<n;j++)
- {
- if(p[j].arrival<=i && p[j].flag==0)
- {
- p[j].flag=1;
- prioenqueue(&q,p[j]);
- }
- }
- /*If pc.flag=-1, it means no process has arrived upto the current moment or all arrived processes have completed execution*/
- if(pc.flag==-1)
- {
- /*flag1 is used to indicate a gap between process execution. Eg: if p1 finishes execution at 2 and p2 begins execution at 4, we will need to show a gap in the gantt chart*/
- if(empty(q) && flag1==0)
- flag1=1;
- else if(empty(q) && flag1==1)
- continue;
- else if(!empty(q) && flag1==1)
- {
- printf(" |");
- /*Variable pretime is used to keep track of the times that will be required when displaying the Gantt chart,such as process preemption or completion. Variable cnt is the count of these times */
- pretime[cnt++]=i;
- flag1=0;
- pc=dequeue(&q);
- }
- else pc=dequeue(&q);
- }
- //The following 'else if' gives the condition for pre-emption. If the process at the front of the queue has higher priority than the current process, then current process is enqueued and the new process is dequeued into pc
- else if(q.f!=-1 && pc.prio > q.data[q.f].prio)
- {
- printf("%s|",pc.name);
- pretime[cnt++]=i;
- prioenqueue(&q,pc);
- pc=dequeue(&q);
- }
- /*Decrement burst time to indicate completion of one burst */
- pc.burst--;
- /* If burst time is zero, process is completed. So we calculate its turnaround time and waiting time, which will later be used to calculate the average TA and WT. We print the process name in Gantt Chart and put pc.flag=-1 to indicate that there is no current process */
- if(pc.burst==0)
- {
- turnaround=(i+1)-pc.arrival;
- waiting=turnaround-pc.initburst;
- avgta+=turnaround;
- avgwt+=waiting;
- pretime[cnt++]=i+1;
- comp++;
- printf("%s|",pc.name);
- pc.flag=-1;
- }
- }
- //Display the times below the Gantt Chart
- printf("\n\t0 ");
- for(i=0;i<cnt;i++)
- {
- if(pretime[i]>9)
- printf("%d ",pretime[i]);
- else printf (" %d ",pretime[i]);
- }
- //Calculate avg. TA and WT
- avgta=avgta/n;
- avgwt=avgwt/n;
- printf("\n\nAvg. TA time: %.2f",avgta);
- printf("\n\nAvg. WT time: %.2f",avgwt);
- }
- //Procedure for Round Robin
- void rr(struct process p[20],int n)
- {
- int i,j,qt,tr=0,turnaround,waiting,pretime[30],cnt=0,flag1=0,comp=0;
- float avgta=0,avgwt=0;
- struct queue q;
- init(&q);
- struct process pc;
- /* pc is the current process. Since initially, there is no process in
- pc, we set its flag to -1*/
- pc.flag=-1;
- //Obtain quantum time for Round Robin
- printf("\nEnter quantum time: ");
- scanf("%d",&qt);
- printf("\nGANTT CHART\n\n\t|");
- /*In this loop, i signifies the moment in time. A process with arrival time 1 will be said to have arrived when i=1. The loop will run until completed processes(comp) equals no. of proceses*/
- //Slight modification in the loop for RR. Since the same process will run for qt moments, we increment i by qt instead of by 1
- for(i=0;comp<n;i++)
- {
- /*In this loop, we find the processes that have arrived and place them in the queue. A process has arrived if its arrival time is less than the current moment (i)
- flag is used to signify whether the process has been enqueued or not. If it has already been enqueued, it does not need to be enqueued again.
- */
- for(j=0;j<n;j++)
- {
- if(p[j].arrival<=i && p[j].flag==0)
- {
- p[j].flag=1;
- enqueue(&q,p[j]);
- }
- }
- /*If pc.flag=-1, it means no process has arrived upto the current moment or all arrived processes have completed execution*/
- if(pc.flag==-1)
- {
- /*flag1 is used to indicate a gap between process execution. Eg: if p1 finishes execution at 2 and p2 begins execution at 4, we will need to show a gap in the gantt chart*/
- if(empty(q) && flag1==0)
- {
- flag1=1;
- continue;
- }
- else if(empty(q) && flag1==1)
- continue;
- else if(!empty(q) && flag1==1)
- {
- printf(" |");
- /*Variable pretime is used to keep track of the times that will be required when displaying the Gantt chart,such as process preemption or completion. Variable cnt is the count of these times */
- pretime[cnt++]=i;
- flag1=0;
- pc=dequeue(&q);
- }
- else pc=dequeue(&q);
- }
- /*If queue is not empty and the current process has already run for qt moments, then we replace the current process by the new process*/
- else if(!empty(q) && tr==qt)
- {
- pretime[cnt++]=i;
- printf("%s|",pc.name);
- enqueue(&q,pc);
- pc=dequeue(&q);
- tr=0; //Reset time-run.
- }
- //Decrement burst time
- pc.burst--;
- //tr is time-run. It is required to keep track of whether a process has run for the given quantum time.
- tr++;
- /* If burst time is zero, process is completed. So we calculate its turnaround time and waiting time, which will later be used to calculate the average TA and WT. We print the process name in Gantt Chart and put pc.flag=-1 to indicate that there is no current process */
- if(pc.burst==0)
- {
- turnaround=(i+1)-pc.arrival;
- waiting=turnaround-pc.initburst;
- avgta+=turnaround;
- avgwt+=waiting;
- pretime[cnt++]=i+1;
- comp++;
- printf("%s|",pc.name);
- pc.flag=-1;
- tr=0; //Reset time-run
- }
- }
- //Display the times below the Gantt Chart
- printf("\n\t0 ");
- for(i=0;i<cnt;i++)
- {
- if(pretime[i]>9)
- printf("%d ",pretime[i]);
- else printf (" %d ",pretime[i]);
- }
- //Calculate avg. TA and WT
- avgta=avgta/n;
- avgwt=avgwt/n;
- printf("\n\nAvg. TA time: %.2f",avgta);
- printf("\n\nAvg. WT time: %.2f",avgwt);
- }
- -----------------------------------------------------------------------------------
-
- */
|