Lab Programs OS
Lab Programs OS
struct pcb{
int pid, at, bt, ct, tt, wt;
};
void main(){
int n;
printf("Enter the num. of processes>> ");
scanf("%d", &n);
printf("\n\n");
while (completed < n){
int min = 99999, index = -1;
if (index != -1){
if ( prev == curtime - 1 && pid[j-1] == p[index].pid ){
++ct[j-1];
prev = curtime;
}
else{
pid[j] = p[index].pid;
ct[j] = curtime + 1;
++len;
++j;
prev = curtime;
}
--btRemain[index];
++curtime;
if ( btRemain[index] == 0 ){
// Process execution completed
p[index].ct = curtime;
p[index].tt = p[index].ct - p[index].at;
p[index].wt = p[index].tt - p[index].bt;
totwt += p[index].wt;
tottt += p[index].tt;
++completed;
isCompleted[index] = 1;
}
}
else{
if ( prev == curtime - 1 && pid[j-1] == -1){
++ct[j-1];
prev = curtime;
}
else{
pid[j] = -1;
ct[j] = curtime + 1;
++len;
++j;
prev = curtime;
}
++curtime;
}
}
printf("\nPID\tAT\tBT\tCT\tTT\tWT\n");
for(int i = 0; i<n; ++i)
printf("%d\t%d\t%d\t%d\t%d\t%d\n", p[i].pid, p[i].at, p[i].bt, p[i].ct,
p[i].tt, p[i].wt);
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-
2a. Write a Shell program to generate prime number in the given range
Program:
struct pcb{
int pid, at, bt, pr, ct, tt, wt;
};
void main(){
int n;
printf("Enter num. of processes >> ");
scanf("%d", &n);
if ( index != -1 ){
if ( prev == curtime -1 && pid[j-1] == p[index].pid){
++ct[j-1];
prev = curtime;
}
else{
ct[j] = curtime + 1;
pid[j] = p[index].pid;
++len;
++j;
prev = curtime;
}
--btRemain[index];
++curtime;
if ( btRemain[index] == 0 ){
// If process finished execution
p[index].ct = curtime;
p[index].tt = p[index].ct - p[index].at;
p[index].wt = p[index].tt - p[index].bt;
totwt += p[index].wt;
tottt += p[index].tt;
++completed;
isCompleted[index] = 1;
}
}
else{
if ( prev == curtime -1 && pid[j-1] == -1){
++ct[j-1];
prev = curtime;
}
else{
ct[j] = curtime + 1;
pid[j] = -1;
++len;
++j;
prev = curtime;
}
++curtime;
}
}
printf("PID\tAT\tBT\tPRI\tCT\tTT\tWT\n");
for(int i = 0; i<n; ++i)
printf("%d\t%d\t%d\t%d\t%d\t%d\t%d\n", p[i].pid, p[i].at, p[i].bt, p[i].pr,
p[i].ct, p[i].tt, p[i].wt);
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
3a. Write a Shell program to find the largest of n numbers,storing numbers in an
array
Program:
#Shell script to find the largest number in an array
#!/bin/bash
echo "Enter the elements of the array "
read -a A
echo "The elements of the array are ${A[*]}"
len=${#A[*]}
echo "The length of the array is $len"
large=${A[0]}
for((i=1;i<$len;i++))
do
if [ ${A[i]} -gt $large ]
then
large=${A[i]}
fi
done
echo "The largest element in the array is $large"
struct pcb{
int pid, at, bt, ct, tt, wt;
};
int n;
int Q[20], f = -1, r = -1;
int deq(){
if (f==r){
int a = f;
f = r = -1;
return Q[a];
}
else{
int a = f;
f = (f+1)%n;
return Q[a];
}
}
int isEmpty(){
if ( f==r && r == -1)
return 1;
else
return 0;
}
void main(){
printf("Enter num. of processes >> ");
scanf("%d", &n);
int t = bt[j+1];
bt[j+1] = bt[j];
bt[j] = t;
}
}
}
if ( p[0].at != 0 ){
pid[j] = -1;
ct[j] = p[0].at;
++j;
++len;
}
while ( completed != n ){
if ( !isEmpty() ){
index = deq();
if ( p[index].bt > qt ){
p[index].bt -= qt;
curtime += qt;
}
else{
curtime += p[index].bt;
p[index].bt = 0;
p[index].ct = curtime;
p[index].tt = p[index].ct - p[index].at;
p[index].wt = p[index].tt - bt[index];
tottt += p[index].tt;
totwt += p[index].wt;
++completed;
}
}
else{
++curtime;
}
printf("\nPID\tAT\tBT\tCT\tTT\tWT\n");
for(int i = 0; i<n; ++i)
printf("%d\t%d\t%d\t%d\t%d\t%d\n", p[i].pid, p[i].at, bt[i], p[i].ct,
p[i].tt, p[i].wt);
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-
4a. Write a Shell program to find input of two matrices, add the matrices and
display their sum
Program:
#Shell Script to read two matrices,find their sum and display the same
#!/bin/bash
declare -A X
declare -A Y
declare -A Z
echo "Enter the order of matrix"
read order
echo "Enter the elements of 1st Matrix"
for((i=0;i<$order;i++))
do
for((j=0;j<$order;j++))
do
read X[$i,$j]
done
done
echo "Enter the elements of 2nd Matrix"
for((i=0;i<$order;i++))
do
for((j=0;j<$order;j++))
do
read Y[$i,$j]
done
done
echo "First Matrix:"
for((i=0;i<$order;i++))
do
for((j=0;j<$order;j++))
do
echo -ne "${X[$i,$j]} "
done
echo -ne "\n"
done
echo "Second Matrix"
for((i=0;i<$order;i++))
do
for((j=0;j<$order;j++))
do
echo -ne "${X[$i,$j]} "
done
echo -ne "\n"
done
echo "The sum of Matrices is"
for((i=0;i<$order;i++))
do
for((j=0;j<$order;j++))
do
Z[$i,$j]=$((X[$i,$j]+Y[$i,$j]))
echo -ne "${Z[$i,$j]} "
done
echo -e "\n"
done
struct pcb{
int pid, at, bt, ct, wt, tt, pr;
};
void main(){
int n;
printf("Enter num. of processes>> ");
scanf("%d", &n);
struct pcb p[n];
if (index != -1){
p[index].ct = curtime + p[index].bt;
p[index].tt = p[index].ct - p[index].at;
p[index].wt = p[index].tt - p[index].bt;
pid[j] = p[index].pid;
ct[j] = p[index].ct;
++j;
totwt += p[index].wt;
tottt += p[index].tt;
++completed;
if (completed == 1) temp = curtime;
curtime = p[index].ct;
isCompleted[index] = 1;
}
else{
if ( prev == curtime - 1){
++ct[j-1];
prev = curtime;
}
else{
pid[j]= -1;
ct[j] = curtime + 1;
++len;
++j;
prev = curtime;
}
++curtime;
}
}
printf("PID\tAT\tBT\tPRI\tCT\tTT\tWT\n");
for(int i = 0; i<n; ++i)
printf("%d\t%d\t%d\t%d\t%d\t%d\t%d\n", p[i].pid, p[i].at, p[i].bt, p[i].pr,
p[i].ct, p[i].tt, p[i].wt );
struct pcb{
int pid, at, bt, ct, tt, wt;
};
void main(){
int n;
printf("Enter num of processes >> ");
scanf("%d", &n);
printf("PID AT BT\n");
for(int i= 0; i<n; ++i)
scanf("%d%d%d", &p[i].pid, &p[i].at, &p[i].bt);
while ( completed != n ){
index = -1, min = 99999;
if ( index != -1 ){
p[index].ct = p[index].bt + curtime;
p[index].tt = p[index].ct - p[index].at;
p[index].wt = p[index].tt - p[index].bt;
tottt += p[index].tt;
totwt += p[index].wt;
ct[j] = p[index].ct;
pid[j] = p[index].pid;
++j;
curtime = p[index].ct;
++completed;
isCompleted[index] = 1;
}
else{
if (prev == curtime - 1){
++ct[j-1];
prev = curtime;
}
else{
pid[j] = -1;
ct[j] = curtime + 1;
++len;
++j;
prev = curtime;
}
++curtime;
}
}
printf("PID\tAT\tBT\tCT\tTT\tWT\n");
for(int i = 0; i<n; ++i)
printf("%d\t%d\t%d\t%d\t%d\t%d\n", p[i].pid, p[i].at, p[i].bt, p[i].ct,
p[i].tt, p[i].wt);
}
}
//Orphan process
/*C program to show Orphan process*/
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
void main()
{
pid_t my,child,parent;
child=fork();
if(child<0)
{
printf("\nFork unsuccuessfull");
}
if(child==0)
{
printf("\n[CHILD]This is the Child process");
my=getpid();
parent=getppid();
printf("\n[CHILD]My pid is %d",my);
printf("\n[CHILD]My parent's pid is %d",parent);
sleep(10);
printf("\n[CHILD]My parent has been terminated and now with the termination
of my process I am adopted by Init process with pid %d",parent);
}
else
{
printf("\n[PARENT]This is Parent Process");
my=getpid();
parent=getppid();
printf("\n[PARENT]My pid is %d",my);
printf("\n[PARENT]My parent's pid is %d",parent);
printf("\n[PARENT]Parent process finished its task and hence is
terminating");
exit(0);
}
}
struct pcb {
int pid, at, bt, wt, tt, ct;
};
void main(){
int n;
printf("Enter the number of processes >> ");
scanf("%d",&n);
struct pcb p[n];
printf("PID AT BT\n");
for(int i = 0 ; i<n ; ++i)
scanf("%d%d%d", &p[i].pid, &p[i].at, &p[i].bt);
// If two process has same bt, select process that has arrived first
if (p[i].bt == min){
if (p[i].at < p[index].at){
index = i;
min = p[i].bt;
}
}
}
}
if ( index != -1 ){
p[index].ct = curtime + p[index].bt;
p[index].tt = p[index].ct - p[index].at;
p[index].wt = p[index].tt - p[index].bt;
tottt += p[index].tt;
totwt += p[index].wt;
pid[j] = p[index].pid;
ct[j] = p[index].ct;
++j;
isCompleted[index] = 1;
++completed;
if (completed == 1) temp = curtime;
curtime = p[index].ct;
}
else{
// CPU idle
if ( prev == curtime - 1 ){
++ct[j-1];
prev = curtime;
}
else{
pid[j] = -1;
ct[j] = curtime + 1;
++j;
prev = curtime;
++len;
}
++curtime;
}
}
printf("\n\nPID\tAT\tBT\tCT\tTT\tWT\n");
for(int i = 0; i<n ; ++i)
printf("%d\t%d\t%d\t%d\t%d\t%d\n", p[i].pid, p[i].at, p[i].bt, p[i].ct,
p[i].tt, p[i].wt);
printf("\nGantt chart\n");
for(int i = 0;i<len; ++i)
printf("-------");
printf("\n");
for(int i = 0;i<len; ++i){
if ( pid[i] != -1)
printf(" P%d |", pid[i]);
else
printf(" IDLE |");
}
printf("\n");
for(int i = 0;i<len; ++i)
printf("-------");
printf("\n");
printf("0 ");
for(int i = 0;i<len; ++i)
printf("%d ", ct[i]);
printf("\n");
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-
7a. Write a program to simulate grep command using system calls
Program:
/*C program to simulate grep command*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
char fn[10],pat[10],temp[20];
FILE *fp;
printf("\nEnter the file name: ");
scanf("%s",fn);
printf("\nEnter the pattern to be searched: ");
scanf("%s",pat);
fp=fopen(fn,"r");
if(fp==NULL)
{
printf("\nFile does not exist");
}
else
{
while (!feof(fp))
{
fgets(temp,100,fp);
if(strstr(temp,pat))
{
printf("%s",pat);
}
}
}
}
7b.Write a program to generate and print Fibonacci series with the following
requirements:
- Parent program should create a child and distribute the task of generating
Fibonacci no to
its child.
- The code for generating Fibonacci no. should reside in different program.
- Child should write the generated Fibonacci sequence to a shared memory.
- Parent process has to print by retrieving the Fibonacci sequence from the shared
memory.
i) Implement the above using shmget and shmat
Note: Shared object should be removed at the end in the program
Program:
//Parent.c
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
int i;
pid_t pid;
int k, n1, n2, n3;
const int SIZE = 4096;
int shmid;
void *ptr;
shmid = shmget((key_t)1122, 4096, 0666 | IPC_CREAT);
ptr = shmat(shmid, NULL, 0666);
if (argc > 1) {
sscanf(argv[1], "%d", &i);
if (i < 1) {
printf("\nError input");
exit(0);
}
} else {
return (0);
}
pid = fork();
if (pid == 0) {
execlp("./fib", "fib", argv[1], NULL);
} else if (pid > 0) {
wait(NULL);
printf("\nPARENT: Child Completed ");
printf("\nParent Printing:");
printf("%s", (char *)ptr);
shmctl(shmid, IPC_RMID, NULL);
}
return (0);
}
//Child.c
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
int k = 2, n1, n2, n3;
void *ptr;
int shmid = shmget((key_t)1122, 4096, 0666);
ptr = shmat(shmid, NULL, 0666);
printf("CHILD:\n");
int i = atoi(argv[1]);
n1 = 0;
n2 = 1;
sprintf(ptr, "%d ", n1);
ptr += strlen(ptr);
printf("%d ", n1);
sprintf(ptr, "%d ", n2);
ptr += strlen(ptr);
printf("%d ", n2);
while (k != i) {
n3 = n1 + n2;
sprintf(ptr, "%d ", n3);
printf("%d ", n3);
n1 = n2;
n2 = n3;
ptr += strlen(ptr);
k++;
}
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-
8a. Write a program to simulate ls command using system calls
Program:
/*C program to simulate ls command using system calls*/
#include<stdio.h>
#include<stdlib.h>
#include<dirent.h>
#include<sys/types.h>
void main(int argc,char *argv[])
{
DIR *dp;
struct dirent *dirp;
if(argc<2)
{
printf("\nKindly pass the directory onto the file");
exit(0);
}
if((dp==opendir(argv[1]))==NULL)
{
printf("\nCannot open the file %s passed as the arument");
exit(0);
}
while ((dirp==readdir(dp))!=NULL)
{
printf("%s\n",dirp->d_name);
}
closedir(dp);
}
8b. Write a program to generate and print Fibonacci series with the following
requirements:
- Parent program should create a child and distribute the task of generating
Fibonacci no to
its child.
- The code for generating Fibonacci no. should reside in different program.
- Child should write the generated Fibonacci sequence to a shared memory.
- Parent process has to print by retrieving the Fibonacci sequence from the shared
memory.
i) Implement the above using shm_open and mmap
Note: Shared object should be removed at the end in the program
Program:
-----------------------------------------------------------------------------------
---------------------------------------------------------------------------------
9a. Write a Shell program to check whether a given number is palindrome or not
#Shell Script to check whether a given number is palindrome or not
#!/bin/bash
echo "Enter the number"
read num
num1=$num
zero=0
while [ $num1 != $zero ]
do
rem=$((num1%10))
rev=$((rev*10))
rev=$((rev+rem))
num1=$((num1/10))
done
if [ $num == $rev ]
then
echo "$num is a palindrome"
else
echo "$num is not a palindrome"
fi
9b. Write a program to generate and print N ODD numbers with the following
requirements:
- Parent program should create a child and distribute the task of generating odd
numbers to
its child.
- The code for generating odd numbers should reside in different program.
- Child should write the generated odd numbers to a shared memory.
- Parent process has to print the odd numbers by retrieving from the shared memory.
i) Implement the above using shmget and shmat
Note: Shared object should be removed at the end in the program
Program:
//Parent.c
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/mman.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
int i, k, n1 = 0, n2 = 1, n3;
pid_t pid;
const int SIZE = 4096;
int shmid;
void *ptr;
shmid = shmget((key_t)1122, 4096, 0666 | IPC_CREAT);
ptr = shmat(shmid, NULL, 0666);
if (argc > 1) {
sscanf(argv[1], "%d", &i);
if (i < 1) {
printf("Error input: %d\n", i);
return 0;
}
} else {
return 1;
}
pid = fork();
if (pid == 0) {
execlp("./fib", "fib", argv[1], NULL);
} else if (pid > 0) {
wait(NULL);
printf("\nParent: Child completed\n");
printf("Parent printing: \n");
printf("%s", (char *)ptr);
shmctl(shmid, IPC_RMID, NULL);
}
return 0;
}
//Child.c
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/mman.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
-----------------------------------------------------------------------------------
----------------------------------------------------------------------------------
10a. Shell program to simulate a calculator
Program:
#Shell Script to simulate a calculator
#!/bin/bash
echo "Enter the first number"
read n1
echo "Enter the second number"
read n2
echo "Enter the operator"
read op
case $op in
'+')
echo "The sum of $n1 and $n2 is $((n1+n2))"
;;
'/')
echo "The quotient of $n1 and $n2 is $((n1/n2))"
;;
'-')
echo "The difference of $n1 and $n2 is $((n1-n2))"
;;
'*')
echo "The product of $n1 and $n2 is $((n1*n2))"
;;
'%')
echo "The remainder of $n1 and $n2 is $((n1%n2))"
;;
*)
echo "Invalid Opeartor entered"
;;
esac
10b.
-----------------------------------------------------------------------------------
----------------------------------------------------------------------------------
11a. A. Write a program to perform the following tasks using system calls:
i) Parent process should create a child process
ii) Both parent child processes should display their pid and parent’s pid; parent
process
should also its child’s pid
iii) Load a new program into child process
iv) The parent process should terminate after the child process terminate
Program:
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/shm.h>
#include<sys/ipc.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
#include<sys/mman.h>
void main()
{
void *psm;
char buf[10];
int shmid;
shmid=shm_open("OS",O_CREAT | O_RDWR,0666);
ftruncate(shmid,1024);
psm=mmap(0,1024,PROT_WRITE,MAP_SHARED,shmid,0);
printf("\nKey/ID/fd of the shared memory: %d",shmid);
printf("\nShared memory is attached at: %d",psm);
printf("\nEnter some data to write shared memory: ");
read(0,buf,10);
sprintf(psm,"%s",buf);
}
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include<sys/ipc.h> //Unix commands
#include<sys/shm.h> //Unix commands
#include<sys/mman.h> //unix commands
#include<sys/stat.h>
#include<unistd.h>
void main()
{
void *psm;
int shmid;
shmid=shm_open("OS",O_RDONLY,0666);
ftruncate(shmid,1024);
psm=mmap(0,1024,PROT_READ,MAP_SHARED,shmid,0);
printf("\nKey/ID/fs of the shared memory: %d",shmid);
printf("\nShared memory for consumer is attached at: %d",psm);
printf("%s",(char *)psm);
printf("\n");
shm_unlink("OS");
}
-----------------------------------------------------------------------------------
----------------------------------------------------------------------------------
12a. Write a Shell program to find the largest of three numbers
Program:
#Shell script to find the largest of three numbers
#!/bin/bash
echo "Enter the first number"
read n1
echo "Enter the second number"
read n2
echo "Enter the third number"
read n3
large=$n1
if [ $n2 -gt $large ]
then
large=$n2
fi
if [ $n3 -gt $large ]
then
large=$n3
fi
echo "The largest among $n1,$n2 and $n3 is $large"
12b. Write a program that implement solution to readers writers problem using mutex
and semaphores
Program:
/*C program to implement readers writers problem using Semaphores and Mutex*/
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<semaphore.h>
sem_t wrt;
pthread_mutex_t mutex;
int count=1,num_reader=0;
void *writer(void *Wno)
{
sem_wait(&wrt);
count*=2;
printf("\nWriter %d is modified the count value to %d",((int *)Wno),count);
sem_post(&wrt);
}
void *reader(void *Rno)
{
pthread_mutex_lock(&mutex);
num_reader++;
if(num_reader==1)
sem_wait(&wrt);
pthread_mutex_unlock(&mutex);
printf("\nReader %d read the count as %d",((int *)Rno),count);
pthread_mutex_lock(&mutex);
num_reader--;
if(num_reader==0)
{
sem_post(&wrt);
}
pthread_mutex_unlock(&mutex);
}
void main()
{
int i;
pthread_t read[10],write[10];
pthread_mutex_init(&mutex,NULL);
sem_init(&wrt,0,1);
int a[10]={1,2,3,4,5,6,7,8,9,10};
for(i=0;i<10;i++)
pthread_create(&read[i],NULL,(void *)reader,(void *)&a[i]);
for(i=0;i<10;i++)
pthread_create(&write[i],NULL,(void *)writer,(void *)&a[i]);
for(i=0;i<10;i++)
pthread_join(read[i],NULL);
for(i=0;i<10;i++)
pthread_join(write[i],NULL);
sem_destroy(&wrt);
}