0% found this document useful (0 votes)
9 views14 pages

Os Lab Program

The document contains multiple C programs demonstrating various scheduling algorithms including Shortest Job First, Priority Scheduling, Round Robin, and memory allocation strategies like First Fit, Best Fit, and Worst Fit. Each program prompts the user for process details, calculates waiting and turnaround times, and displays results such as Gantt charts and average times. Additionally, there are Java and C programs for HTTP client-server communication.

Uploaded by

apratheesh2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views14 pages

Os Lab Program

The document contains multiple C programs demonstrating various scheduling algorithms including Shortest Job First, Priority Scheduling, Round Robin, and memory allocation strategies like First Fit, Best Fit, and Worst Fit. Each program prompts the user for process details, calculates waiting and turnaround times, and displays results such as Gantt charts and average times. Additionally, there are Java and C programs for HTTP client-server communication.

Uploaded by

apratheesh2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Program

#include <stdio.h>
#include <string.h>
int main() {
int n, Bu[20], Twt = 0, Ttt = 0, A[20], Wt[20], i, j, temp, temp1;
float Awt, Att;
char pname[20][20];
printf("\nEnter the number of processes: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("\nEnter the process name: ");
scanf("%s", pname[i]);
printf("Burst Time for %s: ", pname[i]);
scanf("%d", &Bu[i]);
printf("Arrival Time for %s: ", pname[i]);
scanf("%d", &A[i]);
}
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (A[i] > A[j]) {
temp = Bu[i];
Bu[i] = Bu[j];
Bu[j] = temp;
temp1 = A[i];
A[i] = A[j];
A[j] = temp1;
char tempName[20];
strcpy(tempName, pname[i]);
strcpy(pname[i], pname[j]);
strcpy(pname[j], tempName);
}
Wt[0] = 0;
for (i = 1; i < n; i++) {
Wt[i] = Bu[i - 1] + Wt[i - 1];
}
for (i = 0; i < n; i++) {
Twt += (Wt[i] - A[i]);
Ttt += (Wt[i] + Bu[i] - A[i]);
}
Att = (float)Ttt / n;
Awt = (float)Twt / n;
printf("\nAverage Turnaround Time = %.2f ms", Att);
for (i = 0; i < n; i++) {
printf("|\t%s\t", pname[i]);
}
printf("|\n");
for (i = 0; i < n; i++) {
printf("%d\t\t", Wt[i]);
}
printf("%d\n", Wt[n - 1] + Bu[n - 1]);
return 0;
}
Program
#include <stdio.h>
#include <string.h>
int main() {
int n, Bu[20], Twt = 0, Ttt = 0, A[20], Wt[20], i, j, temp;
float Awt, Att;
char pname[20][20];
printf("\nEnter the number of processes: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("\nEnter the process name: ");
scanf("%s", pname[i]);
printf("Burst Time for %s: ", pname[i]);
scanf("%d", &Bu[i]);
}
printf("\n\nSHORTEST JOB FIRST SCHEDULING ALGORITHM\n\n");
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (Bu[j] > Bu[j + 1]) {
temp = Bu[j];
Bu[j] = Bu[j + 1];
Bu[j + 1] = temp;
char tempName[20];
strcpy(tempName, pname[j]);
strcpy(pname[j], pname[j + 1]);
strcpy(pname[j + 1], tempName);
}
Wt[0] = 0;
for (i = 1; i < n; i++) {
Wt[i] = Bu[i - 1] + Wt[i - 1];
}
for (i = 0; i < n; i++) {
Ttt += (Wt[i] + Bu[i]);
Twt += Wt[i];
}
Att = (float)Ttt / n;
Awt = (float)Twt / n;
printf("\nAverage Turnaround Time = %.2f ms", Att);
printf("\nAverage Waiting Time = %.2f ms", Awt);
printf("\n\n\t\tGANTT CHART\n");
for (i = 0; i < n; i++) {
printf("|\t%s\t", pname[i]);
}
printf("|\n");
printf("\n");
for (i = 0; i < n; i++) {
printf("%d\t\t", Wt[i]);
}
printf("%d", Wt[n - 1] + Bu[n - 1]);
printf("\n-------------------------------\n");
return 0;
}
Program
#include <stdio.h>
#include <string.h>
struct Process {
char name[20];
int burstTime, priority, waitingTime, turnaroundTime;
};
int main() {
int n, totalWT = 0, totalTAT = 0;
printf("Enter the number of processes: ");
scanf("%d", &n);
struct Process p[n];
for (int i = 0; i < n; i++) {
printf("\nEnter process name: ");
scanf("%s", p[i].name);
printf("Burst Time for %s: ", p[i].name);
scanf("%d", &p[i].burstTime);
printf("Enter Priority for %s: ", p[i].name);
scanf("%d", &p[i].priority);
}
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (p[i].priority > p[j].priority) {
struct Process temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}
}
p[0].waitingTime = 0;
for (int i = 1; i < n; i++)
p[i].waitingTime = p[i - 1].waitingTime + p[i - 1].burstTime;
for (int i = 0; i < n; i++) {
p[i].turnaroundTime = p[i].waitingTime + p[i].burstTime;
totalWT += p[i].waitingTime;
totalTAT += p[i].turnaroundTime;
}
printf("\nPRIORITY SCHEDULING ALGORITHM\n");
printf("Average Turnaround Time = %.2f ms\n", (float)totalTAT / n);
printf("Average Waiting Time = %.2f ms\n", (float)totalWT / n);
printf("\nGANTT CHART\n|");
int time = 0;
for (int i = 0; i < n; i++)
printf(" %s |", p[i].name);
printf("\n0");
for (int i = 0; i < n; i++) {
time += p[i].burstTime;
printf(" %d", time);
}
return 0;
}
Program
#include <stdio.h>
#include <string.h>
struct Process {
char name[10];
int burstTime, arrivalTime, remainingTime, waitingTime, turnaroundTime;
};
int main() {
int n, timeSlice, totalWT = 0, totalTAT = 0, time = 0, completed = 0;
printf("Enter the number of processes: ");
scanf("%d", &n);
struct Process p[n];
for (int i = 0; i < n; i++) {
printf("\nEnter process name: ");
scanf("%s", p[i].name);
printf("Burst Time for %s: ", p[i].name);
scanf("%d", &p[i].burstTime);
printf("Arrival Time for %s: ", p[i].name);
scanf("%d", &p[i].arrivalTime);
p[i].remainingTime = p[i].burstTime;
}
printf("Enter the time slice: ");
scanf("%d", &timeSlice);
printf("\n\nROUND ROBIN SCHEDULING ALGORITHM\n");
printf("\nGantt Chart:\n|");
while (completed < n) {
int executed = 0;
for (int i = 0; i < n; i++) {
if (p[i].remainingTime > 0 && p[i].arrivalTime <= time) {
printf(" %s |", p[i].name);
if (p[i].remainingTime > timeSlice) {
time += timeSlice;
p[i].remainingTime -= timeSlice;
} else {
time += p[i].remainingTime;
p[i].waitingTime = time - p[i].burstTime - p[i].arrivalTime;
p[i].turnaroundTime = time - p[i].arrivalTime;
p[i].remainingTime = 0;
totalWT += p[i].waitingTime;
totalTAT += p[i].turnaroundTime;
completed++;
}
executed = 1;
}
}
if (!executed) time++;
}
printf("\n\nAverage Waiting Time = %.2f ms", (float)totalWT / n);
printf("\nAverage Turnaround Time = %.2f ms\n", (float)totalTAT / n);
return 0;
}
Program
#include<stdio.h>
int main()
{
int p,s,i,n,j,unf=0,usedf=0,usedw=0,usedb=0,unb=0;
int unw=0,total=0,t;
struct segment
{
int size,pos,flag,flag2,flag3;
}seg[20];
struct process
{
int size,flag;
}proc[20];
printf("Enter the no. of process : ");
scanf("%d",&p);
printf("\nEnter the process size : ");
for(i=0;i<p;i++)
{
scanf("%d",&proc[i].size);
proc[i].flag=0;
}
printf("Enter the no.of partitions :");
scanf("%d",&s);
printf("Enter the partition size :");
for(i=0;i<s;i++)
{
scanf("%d",&seg[i].size);
seg[i].pos=i;
seg[i].flag=seg[i].flag2=seg[i].flag3=0;
total=total+seg[i].size;
}
printf("\nFIRST FIT ALLOCATION STRATEGY\n");
printf("process-id\tprocess size\tpartition no.\tpartition size\n");
for(i=0;i<p;i++)
{
proc[i].flag=0;
for(j=0;j<s;j++)
{
if((proc[i].size<=seg[j].size)&&(seg[j].flag==0))
{
seg[j].flag=1;
proc[i].flag=1;
usedf=usedf+proc[i].size;
printf("\n%d\t\t%d\t\t%d\t\t\t%d\t",i,proc[i].size,seg[j].pos,seg[j].size);
break;
}
}
}
for(i=0;i<p;i++)
{
if(proc[i].flag==0)
{
printf("\nprocess %d cannot be stored in any partitions.\n",i);
}
}
unf=total-usedf;
printf("\nUnused space after first fit allocation =%d",unf);
printf("\nBEST FIT ALLOCATION STRATEGY\n");
for(i=0;i<s;i++)
{
for(j=i+1;j<s;j++)
{
if(seg[i].size>=seg[j].size)
{
t=seg[i].size;
seg[i].size=seg[j].size;
seg[j].size=t;
t=seg[i].pos;
seg[i].pos=seg[j].pos;
seg[j].pos=t;
}
}
}
printf("process-id\tprocess size\tpartition number\tpartition size\n");
for(i=0;i<p;i++)
{
proc[i].flag=0;
for(j=0;j<s;j++)
{
if((proc[i].size<=seg[j].size)&&(seg[j].flag2==0))
{
seg[j].flag2=1;
proc[i].flag=1;
usedb=usedb+proc[i].size;
printf("\n%d\t\t%d\t\t%d\t\t\t%d\t",i,proc[i].size,seg[j].pos,seg[j].size);
break;
}
}
}
for(i=0;i<p;i++)
{
if(proc[i].flag==0)
{
printf("\nprocess %d cannot be stored in any partitions.\n",i);
}
}
unb=total-usedb;
printf("\nUnused space after best fit allocation =%d",unb);
printf("\nWORST FIT ALLOCATION STRATEGY\n");
for(i=0;i<s;i++)
{
for(j=i+1;j<s;j++)
{
if(seg[i].size<=seg[j].size)
{
t=seg[i].size;
seg[i].size=seg[j].size;
seg[j].size=t;
t=seg[i].pos;
seg[i].pos=seg[j].pos;
seg[j].pos=t;
}
}
}
printf("process-id\tprocess size\tpartition number\tpartition size\n");
for(i=0;i<p;i++)
{
proc[i].flag=0;
for(j=0;j<s;j++)
{
if((proc[i].size<=seg[j].size)&&(seg[j].flag3==0))
{
seg[j].flag3=1;
proc[i].flag=1;
usedw=usedw+proc[i].size;
printf("\n%d\t\t%d\t\t%d\t\t\t%d\t",i,proc[i].size,seg[j].pos,seg[j].size);
break;
}
}
}
for(i=0;i<p;i++)
{
if(proc[i].flag==0)
{
printf("\nprocess %d cannot be stored in any partitions.\n",i);
}
}
unw=total-usedw;
printf("\nUnused space after worst fit allocation =%d",unw);
printf("\nEfficient algorithm among the three strategies...\n");
if((unf<=unb)&&(unb<=unw))
printf("\nFirst fit algorithm is efficient\n");
if((unb<=unw)&&(unb<=unf))
printf("\nBest fit algorithm is efficient\n");
if((unw<=unf)&&(unw<=unb))
printf("\nworst fit algorithm is efficient\n");
}
Program
import java.io.*;
import java.net.*;
public class HttpClient {
public static void main(String[] args) {
String path = "http://www.google.com";
BufferedReader reader = null;
try {
URL url = new URL(path);
reader = new BufferedReader(new InputStreamReader(url.openStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
System.out.println("An exception occurred");
e.printStackTrace();
} finally {
try {
if (reader != null) reader.close();
} catch (IOException ioe) {
System.out.println("IOException occurred");
ioe.printStackTrace();
}
}
}
}
Program
Client:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define SERV_TCP_PORT 3500
int main()
{
int sockfd;
struct sockaddr_in serv_add;
char buffer[4096];
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
perror("Socket creation failed");
exit(1);
}
bzero(&serv_add, sizeof(serv_add));
serv_add.sin_family = AF_INET;
serv_add.sin_addr.s_addr = inet_addr("127.0.0.1"); // localhost
serv_add.sin_port = htons(SERV_TCP_PORT);
if (connect(sockfd, (struct sockaddr*)&serv_add, sizeof(serv_add)) < 0)
{
perror("Connect failed");
close(sockfd);
exit(1);
}
printf("Connected to server.\n");
do
{
bzero(buffer, sizeof(buffer));
printf("\nClient: ");
fgets(buffer, sizeof(buffer), stdin);
if (write(sockfd, buffer, strlen(buffer)) < 0)
{
perror("Write failed");
break;
}
bzero(buffer, sizeof(buffer));
if (read(sockfd, buffer, sizeof(buffer)) < 0)
{
perror("Read failed");
break;
}
printf("Server message: %s", buffer);
} while (strcmp(buffer, "bye\n") != 0);
close(sockfd);
printf("Client disconnected.\n");
return 0;
}
Program
Server:
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <strings.h>
#include <stdlib.h> // for exit()
#include <string.h> // for strcmp()
#include <unistd.h> // for read(), write(), close()
#define SERV_TCP_PORT 3500
int main()
{
int sockfd, new_sock, clen;
struct sockaddr_in serv_add, cli_add;
char buffer[4096];
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
perror("Socket creation failed");
exit(1);
}
bzero(&serv_add, sizeof(serv_add));
serv_add.sin_family = AF_INET;
serv_add.sin_addr.s_addr = INADDR_ANY;
serv_add.sin_port = htons(SERV_TCP_PORT);
if (bind(sockfd, (struct sockaddr*)&serv_add, sizeof(serv_add)) < 0)
{
perror("Bind failed");
close(sockfd);
exit(1);
}
if (listen(sockfd, 5) < 0)
{
perror("Listen failed");
close(sockfd);
exit(1);
}
printf("Server is listening on port %d...\n", SERV_TCP_PORT);
clen = sizeof(cli_add);
new_sock = accept(sockfd, (struct sockaddr*)&cli_add, (socklen_t *)&clen);
if (new_sock < 0)
{
perror("Accept failed");
close(sockfd);
exit(1);
}
printf("Client connected.\n");
do
{
bzero(buffer, sizeof(buffer));
if (read(new_sock, buffer, sizeof(buffer)) < 0)
{
perror("Read failed");
break;
}
printf("\nClient message: %s", buffer);
bzero(buffer, sizeof(buffer));
printf("Server: ");
fgets(buffer, sizeof(buffer), stdin);
if (write(new_sock, buffer, strlen(buffer)) < 0)
{
perror("Write failed");
break;
}
} while (strcmp(buffer, "bye\n") != 0);
close(new_sock);
close(sockfd);
printf("Server closed.\n");
return 0;
}
Program
//A1.dtd
<?xml version='1.0' encoding='UTF-8'?>
<!--
TODO define vocabulary identification
PUBLIC ID: -//vendor//vocabulary//EN
SYSTEM ID: http://server/path/A1.dtd -->
<!ELEMENT customer (first|lastname|companyname|email|message)*>
<!--- Put your DTDDoc comment here. -->
<!ELEMENT first (#PCDATA)>
<!--- Put your DTDDoc comment here. -->
<!ELEMENT lastname (#PCDATA)>
23
<!--- Put your DTDDoc comment here. -->
<!ELEMENT companyname (#PCDATA)>
<!--- Put your DTDDoc comment here. -->
<!ELEMENT email (#PCDATA)>
<!--- Put your DTDDoc comment here. -->
<!ELEMENT message (#PCDATA)>
//customer.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE customer SYSTEM "A1.dtd">
<customer>
<first>John</first>
<lastname>Doe</lastname>
<companyname>Section</companyname>
<email>[email protected]</email>
<message>Welcome message</message>
</customer>
Output:
XML validation started.
Checking
file:/C:/Users/Lenovo/Documents/NetBeansProjects/WebApplication6/web/
newXMLDocum
ent1.xml...
Referenced entity at
"file:/C:/Users/Lenovo/Documents/NetBeansProjects/WebApplication6/web/A1.dtd".
XML validation finished.

You might also like