OS Digital Assignment 2
Ch.Sree Ram Varma
19BCE0034
12. Write a C program to kill a process by specifying its name rather
than its PID.
Aim:To kill a process by specifying its name rather than it PID
Commands used:ps -u root (Display all the running process
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
int main()
{
int result;
char child[BUFSIZ];
char die[BUFSIZ];
result=system("ps ax | grep one");
if(result==-1)
return 0;
while(result!=0);
{
strcpy(die,"killall -9 firefox-esr\n");
system(die);
sleep(5);
}
return 0;
}
Command used to get the list of processes:
ps -u root
Process selected to kill : firefox-esr
Output:
Firefox-esr is open in the back ground
After running the executing the C program the firefox application running in the
background is closed.
13. Create a file with few lines, Write a C program to read the file and
delete the spaces more than one in the file
Aim: To create a file with new lines , also to write a C program to read
the file and also to delete the spaces more than one in the file (use
UNIX file API’s).
Code:
#include <stdio.h>
#include <stdlib.h>
#define DATA_SIZE 1000
int main()
{
char text[1000], blank[1000];
int c = 0, d = 0;
/* Variable to store user content */
char data[DATA_SIZE];
/* File pointer to hold reference to our file */
FILE * fPtr;
/*
* Open file in w (write) mode.
* "data/file1.txt" is complete path to create file
*/
fPtr = fopen("file1.txt", "w");
/* fopen() return NULL if last operation was unsuccessful */
if(fPtr == NULL)
{
/* File not created hence exit */
printf("Unable to create file.\n");
exit(EXIT_FAILURE);
}
/* Input contents from user to store in file */
printf("Enter contents to store in file : \n");
fgets(data, DATA_SIZE, stdin);
while (data[c] != '\0') {
if (data[c] == ' ') {
int temp = c + 1;
if (data[temp] != '\0') {
while (data[temp] == ' ' && data[temp] != '\0') {
if (data[temp] == ' ') {
c++;
}
temp++;
}
}
}
blank[d] = data[c];
c++;
d++;
}
blank[d] = '\0';
/* Write data to file */
fputs(data, fPtr);
/* Close file to save file data */
fclose(fPtr);
/* Success message */
printf("File created and saved successfully. :) \n");
printf("Text after removing blanks\n%s\n", blank);
return 0;
}
Output:
Linux output:
14. Write a program
a. To create parent & child process and print their id.
b. To create a zombie process.
c. To create orphan process.
a) Code for creating parent and child process and print their id:
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int main(void)
{
int abc;
abc = fork();
if (abc>0)
{
printf("\nI am parent");
printf("\n My PID is %d ",getpid());
}
else if (abc==0)
{
sleep(5);
printf("\nI am child");
printf("\n My PID is %d",getpid());
printf("\n My parent PID is %d",getppid());
}
return 0;
}
Output:
b) Code for creating Zombie process:
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<wait.h>
#include<stdlib.h>
int main()
{
int pid=fork();
if(pid>0)
{
wait(NULL);
sleep(10);
printf("parentprocess ID:%d\n",getpid());
}
else if(pid==0)
{
printf("Child process ID:%d\n",getpid());
exit(0);
}
else
{
//error occoured
}
return 0;
}
Output:
c) Orphan process
Code:
#include<stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
// Create a child process
int pid = fork();
if (pid > 0)
{
printf("in parent process ID:%d\n",getpid());
exit(0);
}
else if (pid == 0)
{
sleep(30);
printf("Child process ID:%d\n",getpid());
}
return 0;
}
Output:
15. Write a program
a. To make the process to sleep for few seconds.
b. To create a background process.
Shell Code:
#!/usr/bin/env bash
# Execute a process in the background
echo "$(date) - Running first process in the background..."
for i in {1..1000}; do
echo "$(date) - I am running in the background";
sleep 1;
done &> background-process-output.txt &
# Wait for 5 seconds
echo "$(date) - Sleeping..."
sleep 5
# Execute a second process in the foreground
echo "$(date) - Running second process in the foreground..."
for i in {1..1000}; do
echo "$(date) - I am running in the foreground";
sleep 1;
done
Output:
16. Write a program to create a thread and let the thread check whether
the given number is prime or not.
Aim:to write a c program to create a thread and let the thread check whether
the given number is prime or not.
Code:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void *prime(void *num)
{
int *x= (int *)num;
int i=2;
while(i < (*x))
{
if((*x)%i==0)
{
break;
}
i++;
}
if( i<(*x))
{
printf("the number is not prime\n");
}
else {printf("the number is prime\n");}
return NULL;
}
int main()
{
int x = 0;
printf("Enter the number ");
scanf("%d", &x);
pthread_t thread;
pthread_create(&thread, NULL, prime, &x);
pthread_join(thread, NULL);
return 0;
}
Output:
17. Design the following CPU Scheduling Algorithms to provide
the performance analysis among them.
a. FCFS
b. PRIORITY
c. ROUND ROBIN
d. SJF
a. FCFS
AIM:
C program for First Come First Served (fcfs) scheduling Algorithm
Alogrithm:
First we are given with the n number of processes i.e.P1,P2,P3,..,Pn and
corresponding
burst times.We have to find the average waiting time and average
turnaround time.
CODE:
#include<stdio.h>
int main()
{
int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
printf("Enter total number of processes(maximum 20):");
scanf("%d",&n);
printf("\nEnter Process Burst Time\n");
for(i=0;i<n;i++)
{
printf("P[%d]:",i+1);
scanf("%d",&bt[i]);
}
wt[0]=0; //waiting time for first process is 0
//calculating waiting time
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
}
printf("\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time");
//calculating turnaround time
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
avwt+=wt[i];
avtat+=tat[i];
printf("\nP[%d]\t\t%d\t\t%d\t\t%d",i+1,bt[i],wt[i],tat[i]);
}
avwt/=i;
avtat/=i;
printf("\n\nAverage Waiting Time:%d",avwt);
printf("\nAverage Turnaround Time:%d",avtat);
return 0;
}
OUTPUT:
b. PRIORITY Scheduling algorithm
Aim: C program for Priority Scheduling Algorithm
Algorithm:
First we are given with the n number of processes and corresponding burst
times and every process is assigned a priority number .Based on this
Priority
Number,the process are executed.The process having the highest priority(1)
is
executed first and then priority 2,3 and so on.
CODE:
#include<stdio.h>
int main()
{
int
bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_
tat;
printf("Enter Total Number of Process:");
scanf("%d",&n);
printf("\nEnter Burst Time and Priority\n");
for(i=0;i<n;i++)
{
printf("\nP[%d]\n",i+1);
printf("Burst Time:");
scanf("%d",&bt[i]);
printf("Priority:");
scanf("%d",&pr[i]);
p[i]=i+1; //contains process number
}
//sorting burst time, priority and process number in ascending order
using
selection sort
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(pr[j]<pr[pos])
pos=j;
}
temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0; //waiting time for first process is zero
//calculate waiting time
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=total/n; //average waiting time
total=0;
printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround time
total+=tat[i];
printf("\nP[%d]\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=total/n; //average turnaround time
printf("\n\nAverage Waiting Time=%d",avg_wt);
printf("\nAverage Turnaround Time=%d\n",avg_tat);
return 0;
}
OUTPUT:
c. ROUND ROBIN
AIM:
C Program for Round Robin Scheduling Algorithm
Algorithm:
First we are given with the n number of processes and corresponding arrival
time ,burst times .In this each process is provided a fix time to
execute.Once
the process is executed for a given time period .In this we are applying
Time
Quantum as 3ms.
CODE:
#include<stdio.h>
int main()
{
int count,j,n,time,remain,flag=0,time_quantum;
int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10];
printf("Enter Total Process:\t ");
scanf("%d",&n);
remain=n;
for(count=0;count<n;count++)
{
printf("Enter Arrival Time and Burst Time for Process Process Number %d
:",count+1);
scanf("%d",&at[count]);
scanf("%d",&bt[count]);
rt[count]=bt[count];
}
printf("Enter Time Quantum:\t");
scanf("%d",&time_quantum);
printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n");
for(time=0,count=0;remain!=0;)
{
if(rt[count]<=time_quantum && rt[count]>0)
{
time+=rt[count];
rt[count]=0;
flag=1;
}
else if(rt[count]>0)
{
rt[count]-=time_quantum;
time+=time_quantum;
}
if(rt[count]==0 && flag==1)
{
remain--;
printf("P[%d]\t|\t%d\t|\t%d\n",count+1,time-at[count],time-at[count]-
bt[count]);
wait_time+=time-at[count]-bt[count];
turnaround_time+=time-at[count];
flag=0;
}
if(count==n-1)
count=0;
else if(at[count+1]<=time)
count++;
else
count=0;
}
printf("\nAverage Waiting Time= %f\n",wait_time*1.0/n);
printf("Avg Turnaround Time = %f",turnaround_time*1.0/n);
return 0;
}
OUTPUT:
c. SJF
Aim:
C program for Shortest Job First (SJF) Scheduling Algorithm
Algorithm:
First we are given with the n number of processes and corresponding burst
times.But in SJF scheduling algorithm,the processor selects the waiting
process
with the smallest execution time to execute next.
Code:
#include<stdio.h>
void main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,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]);
p[i]=i+1; //contains process number
}
//sorting burst time in ascending order using selection sort
for(i=0;i<n;i++)
{
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=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0; //waiting time for first process will be zero
//calculate waiting time
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=(float)total/n; //average waiting time
total=0;
printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround time
total+=tat[i];
printf("\np%d\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=(float)total/n; //average turnaround time
printf("\n\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%f\n",avg_tat);
}
Output: