Os Programs 1
Os Programs 1
#include <sys/types.h>
#include<sys/wait.h>
#include <unistd.h>
#include<stdio.h>
#include<stdlib.h>
void main()
{
if(p<0)
{
perror("fork error\n");
exit(-1);
}
else if(p==0)
{
printf("\nchild process\n");
printf("child process id=%d\n",getpid());
printf("parent process id=%d\n",getppid());
execv(argv[0], argv);
exit(0);
}
else
{
wait(stat);
printf("\nparent process\n");
printf("process id=%d\n",getpid());
exit(0);
}
}
Ouput:
child process
child process id=21452
parent process id=21451
total 64
-rw-r--r-- 1 manasa manasa 76 Aug 23 08:57 1.c
-rw-r--r-- 1 manasa manasa 1264 Oct 6 12:26 2a.c
-rw-r--r-- 1 manasa manasa 1549 Oct 6 12:48 2b.c
-rw-r--r-- 1 manasa manasa 1532 Oct 6 12:44 2d.c
-rw-r--r-- 1 manasa manasa 463 Sep 24 10:38 4a.c
-rw-r--r-- 1 manasa manasa 474 Sep 24 10:33 4b.c
-rwxr-xr-x 1 manasa manasa 16296 Oct 6 12:49 a.out
-rw-r--r-- 1 manasa manasa 212 Aug 29 09:25 ar.c
-rwxrwxrwx 1 manasa manasa 196 Aug 30 11:57 arg.c
-rw-r--r-- 1 manasa manasa 321 Aug 29 13:34 ch.c
-rw-r--r-- 1 manasa manasa 635 Sep 25 15:20 ch1.c
-rw-r--r-- 1 manasa manasa 414 Aug 30 11:37 ch2.c
-rw-r--r-- 1 manasa manasa 265 Sep 3 13:43 ex.c
-rw-r--r-- 1 manasa manasa 198 Oct 6 12:16 ex1.c
-rw-r--r-- 1 manasa manasa 159 Sep 3 14:24 ex3.c
parent process
process id=21451
//2. Simulate the following CPU scheduling algorithms to find turnaround
time and waiting //time a) FCFS b) SJF c) Round Robin d) Priority.
//2a.FCFS
#include<stdio.h>
int main()
{
int pid[15];
int bt[15];
int n;
printf("Enter the number of processes: ");
scanf("%d",&n);
printf("Enter process id of all the processes: ");
for(int i=0;i<n;i++)
{
scanf("%d",&pid[i]);
}
printf("Enter burst time of all the processes: ");
for(int i=0;i<n;i++)
{
scanf("%d",&bt[i]);
}
int i, wt[n];
wt[0]=0;
for(i=1; i<n; i++) //for calculating waiting time of each process
{
wt[i]= bt[i-1]+ wt[i-1];
}
printf("Process ID Burst Time Waiting Time TurnAround Time\n");
float twt=0.0;
float tat= 0.0;
for(i=0; i<n; i++)
{
printf("%d\t\t", pid[i]);
printf("%d\t\t", bt[i]);
printf("%d\t\t", wt[i]);
printf("%d\t\t", bt[i]+wt[i]);
//calculating and printing turnaround time of each process
printf("\n");
twt += wt[i]; //for calculating total waiting time
tat += (wt[i]+bt[i]); //for calculating total turnaround time
}
float att,awt;
awt = twt/n; //for calculating average waiting time
att = tat/n; //for calculating average turnaround time
printf("Avg. waiting time= %f\n",awt);
printf("Avg. turnaround time= %f",att);
}
Output:
Enter the number of processes: 3
Enter process id of all the processes: 1
2
3
Enter burst time of all the processes: 24
3
3
Process ID Burst Time Waiting Time TurnAround Time
1 24 0 24
2 3 24 27
3 3 27 30
Avg. waiting time= 17.000000
Avg. turnaround time= 27.000000
//2b.SJF
#include<stdio.h>
int main()
{
int bt[20],pid[20],wt[20],i,j,n,total,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);
printf("\nEnter Burst Time:\n");
for(i = 0; i < n; i++)
{
printf("p%d:", i + 1);
scanf("%d", &bt[i]);
pid[i] = i + 1;
}
for(i=0;i<n;i++) //sorting of burst times
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos]) pos=j;
}
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=pid[i];
pid[i]=pid[pos];
pid[pos]=temp;
}
wt[0] = 0;
float twt=0.0;
float tat= 0.0;
float att,awt;
Output:
Enter number of process:4
int main()
{
int n,m,temp;
printf("Enter Number of Processes: ");
scanf("%d",&n);
int burst[n],priority[n],index[n];
for(int i=0;i<n;i++)
{
printf("Enter Burst Time and Priority Value for Process %d: ",i+1);
scanf("%d %d",&burst[i],&priority[i]);
index[i]=i+1;
}
for(int i=0;i<n;i++)
{
temp=priority[i];
m=i;
for(int j=i;j<n;j++)
{
if(priority[j] > temp)
{
temp=priority[j];
m=j;
}
}
swap(&priority[i], &priority[m]);
swap(&burst[i], &burst[m]);
swap(&index[i],&index[m]);
}
int t=0;
printf("Order of process Execution is\n");
for(int i=0;i<n;i++)
{
printf("P%d is executed from %d to %d\n",index[i],t,t+burst[i]);
t+=burst[i];
}
printf("\n");
printf("Process Id\tBurst Time\tWait Time\n");
int wait_time=0;
int total_wait_time = 0;
int t_tat=0;
for(int i=0;i<n;i++)
{
printf("P%d\t\t%d\t\t%d\n",index[i],burst[i],wait_time);
total_wait_time += wait_time;
t_tat += wait_time + burst[i];
wait_time += burst[i]; }
float avg_wait_time = (float) total_wait_time / n;
printf("Average waiting time is %f\n", avg_wait_time);
float avgtat=t_tat/(float)n;
printf("Average TurnAround Time is %f\n",avgtat);
return 0;
}
Output:
Enter Number of Processes: 5
Enter Burst Time and Priority Value for Process 1: 10 3
Enter Burst Time and Priority Value for Process 2: 1 5
Enter Burst Time and Priority Value for Process 3: 2 2
Enter Burst Time and Priority Value for Process 4: 1 1
Enter Burst Time and Priority Value for Process 5: 5 4
Order of process Execution is
P2 is executed from 0 to 1
P5 is executed from 1 to 6
P1 is executed from 6 to 16
P3 is executed from 16 to 18
P4 is executed from 18 to 19
#include <stdio.h>
#include <stdlib.h>
int mutex = 1; // Initialize a mutex to 1
int full = 0; // Number of full slots as 0
int empty = 10, x = 0; // Number of empty slots as size of buffer
switch (n) {
case 1:
if ((mutex == 1)&& (empty != 0))
// If mutex is 1 and empty is non-zero, then it is possible to produce
{
producer();
}
case 2:
// If mutex is 1 and full is non-zero, then it is possible to consume
if ((mutex == 1) && (full != 0))
{
consumer();
}
else // Otherwise, print Buffer is empty
{
printf("Buffer is empty!");
}
break;
Output:
. Press 1 for Producer
2. Press 2 for Consumer
3. Press 3 for Exit
Enter your choice:2
Buffer is empty!
1. Press 1 for Producer
2. Press 2 for Consumer
3. Press 3 for Exit
Enter your choice:1
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <sys/wait.h>
void writer_process() {
int fd;
char message[] = "Hello,welcome to OS Lab";
if (pid == -1) {
perror("fork");
exit(-1);
}
else if (pid == 0) {
// Child process (writer)
writer_process();
}
else {
// Parent process (reader)
reader_process();