0% found this document useful (0 votes)
33 views17 pages

Os Programs 1

Operating system

Uploaded by

kizmawhat1
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)
33 views17 pages

Os Programs 1

Operating system

Uploaded by

kizmawhat1
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/ 17

/*1.

Develop a c program to implement the Process system calls (fork (),


exec(), wait(), create process, terminate process)*/

#include <sys/types.h>
#include<sys/wait.h>
#include <unistd.h>
#include<stdio.h>
#include<stdlib.h>
void main()
{

char *const argv[] = {"/bin/ls","-l", NULL};


int p = fork();
int *stat;

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;

for(i=1; i<n; i++)


{
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 number of process:4

Enter Burst Time:


p1:6
p2:8
p3:7
p4:3
Process ID Burst Time Waiting Time TurnAround Time
4 3 0 3
1 6 3 9
3 7 9 16
2 8 16 24
Avg. waiting time= 7.000000
Avg. turnaround time= 13.000000
//2d. Priority
#include <stdio.h>

void swap(int *a,int *b)


{
int temp=*a;
*a=*b;
*b=temp;
}

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

Process Id Burst Time Wait Time


P2 1 0
P5 5 1
P1 10 6
P3 2 16
P4 1 18
Average waiting time is 8.200000
Average TurnAround Time is 12.000000
//3. Develop a C program to simulate producer-consumer problem using
//semaphores

#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

void producer() // Function to produce an item and add it to the buffer


{
--mutex; // Decrease mutex value by 1
++full; // Increase the number of full slots by 1
--empty; // Decrease the number of empty slots by 1
x++; // Item produced
printf("\nProducer produces"item %d", x);

++mutex; // Increase mutex value by 1


}

void consumer() // Function to consume an item and remove it from buffer


{
--mutex; // Decrease mutex value by 1
--full; // Decrease the number of full slots by 1
++empty; // Increase the number of empty slots by 1
printf("\nConsumer consumes item %d", x);
x--;
++mutex; // Increase mutex value by 1
}

int main() // Driver Code


{
int n, i;
printf("\n1. Press 1 for Producer \n2. Press 2 for Consumer \n3. Press 3 for Exit");

for (i = 1; i > 0; i++)


{
printf("\nEnter your choice:");
scanf("%d", &n);

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();
}

else // Otherwise, print buffer is full


{
printf("Buffer is full!");
}
break;

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;

case 3: // Exit Condition


exit(0);
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

Producer produces item 1


1. Press 1 for Producer
2. Press 2 for Consumer
3. Press 3 for Exit
Enter your choice:2

Consumer consumes item 1


1. Press 1 for Producer
2. Press 2 for Consumer
3. Press 3 for Exit
Enter your choice:1

Producer produces item 1


1. Press 1 for Producer
2. Press 2 for Consumer
3. Press 3 for Exit
Enter your choice:1

Producer produces item 2


1. Press 1 for Producer
2. Press 2 for Consumer
3. Press 3 for Exit
Enter your choice:1

Producer produces item 3


1. Press 1 for Producer
2. Press 2 for Consumer
3. Press 3 for Exit
Enter your choice:2

Consumer consumes item 3


1. Press 1 for Producer
2. Press 2 for Consumer
3. Press 3 for Exit
Enter your choice:2

Consumer consumes item 2


1. Press 1 for Producer
2. Press 2 for Consumer
3. Press 3 for Exit
Enter your choice:3
/*4. Develop a C program which demonstrates interprocess communication
between a reader process and a writer process. Use mkfifo, open, read, write
and close APIs in your program.*/

#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";

// Open the named pipe for writing


fd = open("svit", O_WRONLY);
if (fd == -1) {
perror("open");
exit(-1);
}

// Write the message to the named pipe


write(fd, message, strlen(message) + 1);
printf("Message sent: %s\n", message);

// Close the named pipe


close(fd);
}
void reader_process() {
int fd;
char buffer[100];

// Open the named pipe for reading


fd = open("svit", O_RDONLY);
if (fd == -1) {
perror("open");
exit(-1);
}

// Read the message from the named pipe


read(fd, buffer, sizeof(buffer));
printf("Message received: %s\n", buffer);

// Close the named pipe


close(fd);
}
void main() {
pid_t pid;

// Create the named pipe


mkfifo("svit", 0666);

// Fork a child process


pid = fork();

if (pid == -1) {
perror("fork");
exit(-1);
}
else if (pid == 0) {
// Child process (writer)
writer_process();
}
else {
// Parent process (reader)
reader_process();

// Wait for the child process to finish


wait(NULL);

// Remove the named pipe


}
}
Ouput:

Message received: Hello,welcome to OS Lab


Message sent: Hello,welcome to OS Lab

You might also like