0% found this document useful (0 votes)
5 views

Operating System

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)
5 views

Operating System

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/ 21

Name: Suraj Dasgupta

Roll No: 35000120049


RAMKRISHNA MAHATO GOVT. ENGINEERING COLLEGE, PURULIA
SESSIONAL TRANING PLAN FOR 5TH SEMESTER
SESSIONAL: OPERATING SYSTEM LAB
DEPARTMENT: C.S.E.

Assignment No: PCC-CS 692/1

1)Experiment Name: Factorial of a number

Objective: Write a ‘Shell’ program to find the factorial of any number entered
through the keyboard
Priciple:
Read n;
fact=1;
i=1;
while(i<=n)
Do
fact=fact*i;
i=i+1;
Done
Print fact

Program:

echo "Enter a Number"


read num

fact=1

while [ $num -gt 1 ]


do
fact=$((fact * num))
num=$((num - 1))
done

echo $fact

Screenshots of I/P and O/P

Assignment No: PCC-CS 692/2


2)Experiment Name: Determine of leap year

Objective: Write a ‘Shell’ program to determine any year entered through the
keyboard leap year or not

Priciple:
Read leap;
if (leap%400==0 || leap%100==0||leap%4==0)
Print leap year;
else
Print not a leap year;
fi

Program:
echo "Enter a Year"
read leap
if [ `expr $leap % 400` -eq 0 ]
then
echo leap year
elif [ `expr $leap % 100` -eq 0 ]
then
echo not a leap year
elif [ `expr $leap % 4` -eq 0 ]
then
echo leap year
else
echo not a leap year
fi

Screenshots of I/P and O/P

Assignment No: PCC-CS 692/3


3)Experiment Name: Digit summation of a given number

Objective: Write a ‘Shell’ program to find the Digit summation of a given number
any number entered through the keyboard

Priciple:
Read n;
sum=0;
i=1;
while(n>0)
Do
a=n%10;
sum=sum+a;
n/=10;
Done
Print sum
Program:
echo "Enter a number"
read num
sum=0

while [ $num -gt 0 ]


do
mod=$((num % 10))
sum=$((sum + mod))
num=$((num / 10))
done

echo $sum

Screenshots of I/P and O/P

Assignment No: PCC-CS 692/4


4)Experiment Name: Fibonacci series

Objective: Write a ‘Shell’ program to find the Fibonacci series


of a given number any number entered through the keyboard
Priciple:
Read n;
a=0;
b=1;
show=0;
Print a;
Print b;
for(int i=2;i<n;i++){
show=a+b;
a=b;
b=show;
Print show;
Program:
echo "How many number of terms to be generated ?"
read n
function fib
{
x=0
y=1
i=2
echo "Fibonacci Series up to $n terms :"
echo "$x"
echo "$y"
while [ $i -lt $n ]
do
i=`expr $i + 1 `
z=`expr $x + $y `
echo "$z"
x=$y
y=$z
done
}
r=`fib $n`
echo "$r"

Screenshots of I/P and O/P


Assignment No: PCC-CS 692/5
5)Experiment Name: Summation of natural numbers

Objective: Write a ‘Shell’ program to find Summation of natural numbers


entered through the keyboard
Priciple:
Read n;
sum=0;
for(int i=0;i<=n;i++){
sum+=i;
}
Print sum;
Program:
echo "Enter the number of N"

read n

sum=0

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

do

sum=$((sum + i))

done

echo -e "The sum of first N number is \t $sum"

Screenshots of I/P and O/P


Assignment No: PCC-CS 692/6
6)Experiment Name: Input string is valid or not
Objective: Write a ‘Shell’ program to check Input string is valid or not

Program:
echo "Enter the String : "
read string

len=`echo -n $string | wc -c`

if [ $len -lt 10 ]
then
echo "String is too short."
fi
Screenshots of I/P and O/P

Assignment No: PCC-CS 692/7


7)Experiment Name: Last modification time of a file in current directory

Objective: Write a ‘Shell’ program to find the Last modification time of a file in
current directory

Program:
echo -n "Enter a filename to see last modification time : "
read fileName

if [ ! -f $fileName ]
then
echo "$fileName not a file"
exit 1
fi
echo "$fileName was last modified on $(stat -c %x $fileName)"

Screenshots of I/P and O/P

Assignment No: PCC-CS 692/8


8)Experiment Name: Find out the name,grade,maximum marks holder & total
marks from a file
Objective: Write a ‘Shell’ program to Find out the name,grade,maximum marks
holder & total marks from a file

Program:
echo "Enter filename"
read fileName
max=0
while read -r line; do
name=`echo $line|cut -d " " -f1`
m1=`echo $line|cut -d " " -f2`
m2=`echo $line|cut -d " " -f3`
m3=`echo $line|cut -d " " -f4`
total=`expr $m1 + $m2 + $m3`
avg=`expr $total / 3`
if [ $avg -ge 80 ]
then
grade='A'
else
grade='B'
fi
echo $name $avg $grade
if [ $total -gt $max ]
then
max=$total
maxname=$name
fi
done<$fileName
echo "maximum marksholder = $maxname and marks= $max"

Screenshots of I/P and O/P

Assignment No: PCC-CS 692/9


9)Experiment Name: Calling a program from the other program

Program:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main()
{
char child1[] = "sh prog1.sh";
char child2[BUFSIZ];
/*
* Execute by passing the name directly
*/
//fork();
system ("sh prog1.sh");
/*
* Execute by passing an array name
*/
system (child1);
/*
* Build a buffer, and execute the commands within it
*/
strcpy (child2, "sh prog1.sh");
strcat (child2, " -aparm -b");
printf ("Executing %s\n", child2);
system (child2);
return 0;
}
Another Program :

echo "This is to check wheather second program runs or not "

Screenshots of I/P and O/P

Assignment No: PCC-CS 692/10


10)Experiment Name: Print a message when child dies

Program:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>

int i;

static void signal_handler(int signo)


{
if (signo == SIGUSR1)
{
printf("child %d received sigusr1\n", i + 1);
}
}
int main(void)
{
char buff[50];
int status;
if (signal(SIGUSR1, signal_handler) == SIGUSR1)
{
perror("Signal handling error");
}

pid_t cpid, count;

for (i = 0; i < 5; i++)


{
cpid = fork();
if (cpid == 0)
{
sleep(1);
// exit(0);
}
else if (cpid > 0)
{
kill(cpid, SIGUSR1);
int status;
int n = waitpid(cpid, &status, 0);
if (n != cpid)
{
perror("error");
}
if (WIFSIGNALED(status))
{ /* get WTERMSIG(status) */
printf("killed by signal=%d\n", WTERMSIG(status));
}
}
else
{
perror("Fork error: ");
exit(1);
}
}
}
Screenshots of I/P and O/P

Assignment No: PCC-CS 692/11


11)Experiment Name: Know the PID & PPID of child & parent Create an
Orphan process

Program:
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>

int main()
{
pid_t child_pid;
printf("Main process id is : %d\n", getpid());
child_pid = fork();
if (child_pid != 0){
printf("This is the parent process with id : %d\n", (int)getpid());
printf("the child pid is : %d\n", child_pid);
}
else{
printf("this is the child process , with id %d\n", (int)getpid());
}
return 0;
}

Screenshots of I/P and O/P

Assignment No: PCC-CS 692/12


12)Experiment Name:Implement IPC using Pipe system call

Program:
#include<stdio.h>
#include <unistd.h>
#define MSGSIZE 16

char* msg1 = "hello, world #1";


char* msg2 = "hello, world #2";
char* msg3 = "hello, world #3";

int main()
{
char inbuf[MSGSIZE];
int p[2], i;

if (pipe(p) < 0)
exit(1);

/* continued */
/* write pipe */
write(p[1], msg1, MSGSIZE);
write(p[1], msg2, MSGSIZE);
write(p[1], msg3, MSGSIZE);

for (i = 0; i < 4; i++) {


/* read pipe */
read(p[0], inbuf, MSGSIZE);
printf("% s\n", inbuf);
}
return 0;
}

Screenshots of I/P and O/P

Assignment No: PCC-CS 692/13


13)Experiment Name:Write a C program to simulate First Come First Serve
scheduling algorithm.

Program:
#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 calculating waiting time of each process


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]);

//calculating and printing turnaround time of each process


printf("%d\t\t", bt[i]+wt[i]);
printf("\n");

//for calculating total waiting time


twt += wt[i];

//for calculating total turnaround time


tat += (wt[i]+bt[i]);
}
float att,awt;

//for calculating average waiting time


awt = twt/n;

//for calculating average turnaround time


att = tat/n;
printf("Avg. waiting time= %f\n",awt);
printf("Avg. turnaround time= %f",att);
}

Screenshots of I/P and O/P

Assignment No: PCC-CS 692/14


14)Experiment Name: Write a C program to simulate priority scheduling
algorithm

Program:
#include <stdio.h>

//Function to swap two variables


void swap(int *a,int *b)
{
int temp=*a;
*a=*b;
*b=temp;
}
int main()
{
int n;
printf("Enter Number of Processes: ");
scanf("%d",&n);

// b is array for burst time, p for priority and index for process id
int b[n],p[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",&b[i],&p[i]);
index[i]=i+1;
}
for(int i=0; i<n; i++)
{
int a=p[i],m=i;

//Finding out highest priority element and placing it at its desired position
for(int j=i; j<n; j++)
{
if(p[j] > a)
{
a=p[j];
m=j;
}
}

//Swapping processes
swap(&p[i], &p[m]);
swap(&b[i], &b[m]);
swap(&index[i],&index[m]);
}

// T stores the starting time of process


int t=0;
//Printing scheduled process
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+b[i]);
t+=b[i];
}
printf("\n");
printf("Process Id Burst Time Wait Time TurnAround Time\n");
int wait_time=0;
for(int i=0; i<n; i++)
{
printf("P%d %d %d %d\n",index[i],b[i],wait_time,wait_time + b[i]);
wait_time += b[i];
}
return 0;
}

Screenshots of I/P and O/P


Assignment No: PCC-CS 692/15
15)Experiment Name:Write a C program to simulate Round Robin scheduling
algorithm

Program:
#include<stdio.h>
#include<conio.h>

void main()
{
// initlialize the variable name
int i, NOP, sum=0,count=0, y, quant, wt=0, tat=0, at[10], bt[10], temp[10];
float avg_wt, avg_tat;
printf(" Total number of process in the system: ");
scanf("%d", &NOP);
y = NOP; // Assign the number of process to variable y

// Use for loop to enter the details of the process like Arrival time and the Burst Time
for(i=0; i<NOP; i++)
{
printf("\n Enter the Arrival and Burst time of the Process[%d]\n", i+1);
printf(" Arrival time is: \t"); // Accept arrival time
scanf("%d", &at[i]);
printf(" \nBurst time is: \t"); // Accept the Burst time
scanf("%d", &bt[i]);
temp[i] = bt[i]; // store the burst time in temp array
}
// Accept the Time qunat
printf("Enter the Time Quantum for the process: \t");
scanf("%d", &quant);
// Display the process No, burst time, Turn Around Time and the waiting time
printf("\n Process No \t\t Burst Time \t\t TAT \t\t Waiting Time ");
for(sum=0, i = 0; y!=0; )
{
if(temp[i] <= quant && temp[i] > 0) // define the conditions
{
sum = sum + temp[i];
temp[i] = 0;
count=1;
}
else if(temp[i] > 0)
{
temp[i] = temp[i] - quant;
sum = sum + quant;
}
if(temp[i]==0 && count==1)
{
y--; //decrement the process no.
printf("\nProcess No[%d] \t\t %d\t\t\t\t %d\t\t\t %d", i+1, bt[i], sum-at[i], sum-at[i]-
bt[i]);
wt = wt+sum-at[i]-bt[i];
tat = tat+sum-at[i];
count =0;
}
if(i==NOP-1)
{
i=0;
}
else if(at[i+1]<=sum)
{
i++;
}
else
{
i=0;
}
}
// represents the average waiting time and Turn Around time
avg_wt = wt * 1.0/NOP;
avg_tat = tat * 1.0/NOP;
printf("\n Average Turn Around Time: \t%f", avg_wt);
printf("\n Average Waiting Time: \t%f", avg_tat);
getch();
}

Screenshots of I/P and O/P

You might also like