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

OS File

Uploaded by

souvik.it222073
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

OS File

Uploaded by

souvik.it222073
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 19

Assignment 1

Q.11.write a shell script to determine whether the character entered is capital or small case letter, a
digit or a special symbol.(using if-else)

Shell Script:
Output:

Q.1 FCFS

#include<stdio.h>

void calculate(int burstTime[], int arrivalTime[], int completionTime[], int waitingTime[], int
turnaroundTime[], int n) {
int startTime = 0;
for (int i = 0; i < n; i++) {
if (startTime < arrivalTime[i]) {
startTime = arrivalTime[i];
}
completionTime[i] = startTime + burstTime[i];
turnaroundTime[i] = completionTime[i] - arrivalTime[i];
waitingTime[i] = turnaroundTime[i] - burstTime[i];
startTime = completionTime[i];
}
}

void printTimes(int process[], int burstTime[], int arrivalTime[], int completionTime[], int
waitingTime[], int turnaroundTime[], int n) {
printf("Process NO. Burst Time Arrival Time Completion Time Waiting Time Turnaround
Time\n");
for (int i = 0; i < n; i++) {
printf(" %d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n", process[i], burstTime[i], arrivalTime[i],
completionTime[i], waitingTime[i], turnaroundTime[i]);
}
}

int main() {
int process[] = {1, 2, 3, 4, 5};
int burstTime[] = {4, 3, 2, 1, 3};
int arrivalTime[] = {3, 5, 0, 5, 4};
int n = sizeof(process)/sizeof(int);
int completionTime[n];
int waitingTime[n];
int turnaroundTime[n];
calculate(burstTime, arrivalTime, completionTime, waitingTime, turnaroundTime, n);
printTimes(process, burstTime, arrivalTime, completionTime, waitingTime, turnaroundTime, n);

return 0;
}

Q.2 SJF
#include <stdio.h>

int main() {
int process[] = {1, 2, 3, 4, 5};
int burstTime[] = {4, 3, 2, 1, 3};
int arrivalTime[] = {3, 5, 0, 5, 4};
int n = sizeof(process) / sizeof(process[0]);
int waitingTime[5], turnaroundTime[5];
int totalWaitingTime = 0, totalTurnaroundTime = 0;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (burstTime[j] > burstTime[j + 1]) {
int temp = burstTime[j];
burstTime[j] = burstTime[j + 1];
burstTime[j + 1] = temp;

temp = process[j];
process[j] = process[j + 1];
process[j + 1] = temp;

temp = arrivalTime[j];
arrivalTime[j] = arrivalTime[j + 1];
arrivalTime[j + 1] = temp;
}}}
waitingTime[0] = 0;
for (int i = 1; i < n; i++) {
waitingTime[i] = burstTime[i - 1] + waitingTime[i - 1];
totalWaitingTime += waitingTime[i];
}
for (int i = 0; i < n; i++) {
turnaroundTime[i] = burstTime[i] + waitingTime[i];
totalTurnaroundTime += turnaroundTime[i];
}
printf("Process\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; i++) {
printf("%d\t%d\t\t%d\t\t%d\t\t%d\n", process[i], arrivalTime[i], burstTime[i], waitingTime[i],
turnaroundTime[i]);
}
printf("Average Waiting Time = %.2f\n", (float)totalWaitingTime / n);
printf("Average Turnaround Time = %.2f\n", (float)totalTurnaroundTime / n);
return 0;
}

Q.3 Pririty scheduling

#include <stdio.h>
int main() {
int process[] = {1, 2, 3, 4, 5};
int arrivalTime[] = {0, 5, 12, 2, 9};
int burstTime[] = {11, 28, 2, 10, 16};
int priority[] = {2, 0, 3, 1, 4};
int n = sizeof(process) / sizeof(process[0]);
int waitingTime[5], turnaroundTime[5];
int totalWaitingTime = 0, totalTurnaroundTime = 0;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (priority[j] > priority[j + 1]) {
int temp = priority[j];
priority[j] = priority[j + 1];
priority[j + 1] = temp;

temp = burstTime[j];
burstTime[j] = burstTime[j + 1];
burstTime[j + 1] = temp;

temp = arrivalTime[j];
arrivalTime[j] = arrivalTime[j + 1];
arrivalTime[j + 1] = temp;

temp = process[j];
process[j] = process[j + 1];
process[j + 1] = temp;
}}}
waitingTime[0] = 0;
for (int i = 1; i < n; i++) {
waitingTime[i] = waitingTime[i - 1] + burstTime[i - 1];
totalWaitingTime += waitingTime[i];
}
for (int i = 0; i < n; i++) {
turnaroundTime[i] = burstTime[i] + waitingTime[i];
totalTurnaroundTime += turnaroundTime[i];
}
printf("Process\tArrival Time\tBurst Time\tPriority\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; i++) {
printf("%d\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n", process[i], arrivalTime[i], burstTime[i], priority[i],
waitingTime[i], turnaroundTime[i]);
}
printf("Average Waiting Time = %.2f\n", (float)totalWaitingTime / n);
printf("Average Turnaround Time = %.2f\n", (float)totalTurnaroundTime / n);
return 0;}

Q.1 File renaming

DIR=$1

if [ ! -d "$DIR" ]; then
echo "Directory $DIR does not exist."
fi

for FILE in "$DIR"/*; do


if [ -f "$FILE" ]; then
BASENAME=$(basename "$FILE")
mv "$FILE" "$DIR/${BASENAME}.new"
fi
done

echo "Renaming complete."

Q.2 reverse a number

n=$1
num=0
while [ $n -gt 0 ]
do
num=$(expr $num \* 10)
k=$(expr $n % 10)
num=$(expr $num + $k)
n=$(expr $n / 10)
done
echo "Reversed number is $num"

Q.3 check password strength

echo "enter the password"


read password
len="${#password}"
if test $len -ge 8 ; then
echo "$password" | grep -q [0-9]
if test $? -eq 0 ; then
echo "$password" | grep -q [A-Z]
if test $? -eq 0 ; then
echo "$password" | grep -q [a-z]
if test $? -eq 0 ; then
echo "Strong password"
else
echo "weak password include lower case char"
fi
else
echo "weak password include capital char"
fi
else
echo "please include the numbers in password it is weak password"
fi
else
echo "password lenght should be greater than or equal 8 hence weak password"
fi

Semaphore
In a ticket counter there are many ticket-window (say 4) Ticket sellers are selling tickets to
customer, ticket sellers are only allowed to sell ticket until they are all gone(say 100). Before
attempting to sell a ticket, the thread must acquire the lock by waiting on the semaphore and then
release the lock when through by signalling the semaphore.There is a global variable numTickets
which tracks the number of tickets remaining to sell. Implement system program to demonstrate this
scenario.

#ifndef SEMA_H
#define SEMA_H

#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>

#define SEMKEY 150


#define PERM 0666

union semun
{
int val;
struct semid_ds *buf;
ushort *array;
};

/*---------------------------------------------------------------------
* Function Name: semGet
* Objective: creates a semaphore array
* In Parameters: 1. semaphore key (key_t [int])
* 2. number of members in the semaphore array (int)
* Return Values: semaphore id (int)
* -1 -> failure
* Other Variables Changed or Used: None
*
* Warning (To be taken into account when modifying): None
*--------------------------------------------------------------------*/

int semGet(key_t semkey, int mem_no)


{
int semid;

if((semid=semget(semkey, mem_no, IPC_CREAT|PERM))==-1)


{
//printf("\ncan't get semaphore (key = %d)",SEMKEY);
//fflush(stdout);
}
else
{
//printf("\ngot semaphore successfully (id = %d)",semid);
//fflush(stdout);
}
return(semid);
}
/*---------------------------------------------------------------------
* Function Name: removeSem
* Objective: removes a semaphore array
* In Parameters: 1. semaphore id (int)
* Return Values: -1 -> failure
* 1 -> success
* Other Variables Changed or Used: None
*
* Warning (To be taken into account when modifying): None
*--------------------------------------------------------------------*/

int removeSem(int semid)


{
int retval;

if((retval=semctl(semid, 0, IPC_RMID, 0))==-1)


{
//printf("\ncan't remove semaphore");
//fflush(stdout);
}
else
{
//printf("\nsemaphore (id = %d) removed successfully",semid);
//fflush(stdout);
}
return(retval);
}

/*---------------------------------------------------------------------
* Function Name: setVal
* Objective: sets value to a semaphore
* In Parameters: 1. semaphore id (int)
* 2. semaphore number within the array (int)
* 3. the value (int)
* Return Values: 1 -> success
* -1 -> failure
* Other Variables Changed or Used: None
*
* Warning (To be taken into account when modifying): None
*--------------------------------------------------------------------*/

int setVal(int semid, int semnum, int semval)


{
int retval;
union semun arg;

arg.val=semval;
if((retval=semctl(semid, semnum, SETVAL, arg))==-1)
{
/*
printf("\ncan't set %dth memeber of semaphore set (id = %d)
to %d",semnum, semid, semval);
fflush(stdout);
*/
}
else
{
/*
printf("\nset %dth memeber of semaphore set (id = %d) to %d",
semnum, semid, semval);
fflush(stdout);
*/
}
return(retval);
}

/*---------------------------------------------------------------------
* Function Name: getVal
* Last Modified:
* Objective: read the value of a semaphore
* In Parameters: 1. semaphore id
* 2. semaphore number within the semaphore array
* Return Values: semaphore value (int)
* Other Variables Changed or Used: None
*
* Warning (To be taken into account when modifying): None
*--------------------------------------------------------------------*/

int getVal(int semid, int semnum)


{
int retval;
union semun arg;
if((retval=semctl(semid, semnum, GETVAL, 0))==-1)
{
/*
printf("\ncan't get value of %dth member of semaphore set
(id = %d)", semnum, semid);
fflush(stdout);
*/
}
else
{
/*
printf("\nvalue of %dth member of semaphore set (
id = %d) = %d", semnum, semid, retval);
fflush(stdout);
*/
}
return(retval);
}

/*---------------------------------------------------------------------
* Function Name: lock
* Objective: decrement a semaphore value, the calling process blocks
* if the semaphore value cannot be decremented
* In Parameters: 1. semaphore id (int)
* 2. semaphore number (int)
* Return Values: None
* Other Variables Changed or Used: None
*
* Warning (To be taken into account when modifying): None
*--------------------------------------------------------------------*/

void lock(int semid, int semnum)


{
struct sembuf semoparray[1];

semoparray[0].sem_num=semnum;
semoparray[0].sem_op=-1;
semoparray[0].sem_flg=0;

semop(semid, semoparray, 1);


}

/*---------------------------------------------------------------------
* Function Name: unlock
* Objective: increase the value of a semaphore
* In Parameters: 1. semaphore id
* 2. semaphore number within the semaphore array
* Return Values: None
* Other Variables Changed or Used: None
*
* Warning (To be taken into account when modifying): None
*--------------------------------------------------------------------*/

void unlock(int semid, int semnum)


{
struct sembuf semoparray[1];

semoparray[0].sem_num=semnum;
semoparray[0].sem_op=1;
semoparray[0].sem_flg=0;

semop(semid, semoparray, 1);


}

#endif
#include"mysema.h"

main()
{
int semid;

semid=semGet(100, 1);
printf("Process Master: Got semaphore %d\n", semid);
setVal(semid, 0, 1);
printf("Process Master: Set value 1 to semaphore %d\n", semid);

instance1

#include"mysema.h"

main()
{
int semid;

semid=semGet(100, 1);
printf("Process A: Got semaphore %d\n", semid);

while(1)
{
printf("Process A: Trying to enter into <CS>\n");
lock(semid, 0);
printf("Process A: Sucessfully entered into <CS>\n");
printf("Process A: I am in critical section\n");
sleep(8);
printf("Process A: Trying to exit from <CS>\n");
unlock(semid, 0);
printf("Process A: Successfully exited from <CS>\n");

printf("Process A: I am in <RS>\n");
sleep(4);

instance2

#include"mysema.h"

main()
{
int semid;

semid=semGet(100, 1);
printf("Process B: Got semaphore %d\n", semid);

while(1)
{
printf("Process B: Trying to enter into <CS>\n");
lock(semid, 0);
printf("Process B: Sucessfully entered into <CS>\n");
printf("Process B: I am in critical section\n");
sleep(15);
printf("Process B: Trying to exit from <CS>\n");
unlock(semid, 0);
printf("Process B: Successfully exited from <CS>\n");
printf("Process B: I am in <RS>\n");
sleep(5);
}

instance 3

#include"mysema.h"

main()
{
int semid;

semid=semGet(100, 1);
printf("Process A: Got semaphore %d\n", semid);

while(1)
{
printf("Process C: Trying to enter into <CS>\n");
lock(semid, 0);
printf("Process C: Sucessfully entered into <CS>\n");
printf("Process C: I am in critical section\n");
sleep(22);
printf("Process C: Trying to exit from <CS>\n");
unlock(semid, 0);
printf("Process C: Successfully exited from <CS>\n");

printf("Process c: I am in <RS>\n");
sleep(4);

instance4

#include"mysema.h"

main()
{
int semid;
semid=semGet(100, 1);
printf("Process A: Got semaphore %d\n", semid);

while(1)
{
printf("Process D: Trying to enter into <CS>\n");
lock(semid, 0);
printf("Process D: Sucessfully entered into <CS>\n");
printf("Process D: I am in critical section\n");
sleep(30);
printf("Process D: Trying to exit from <CS>\n");
unlock(semid, 0);
printf("Process D: Successfully exited from <CS>\n");

printf("Process D: I am in <RS>\n");
sleep(4);

1. Write a system program to display your process id, parent process id, group id, effective
group id, user id and effective user id.

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

int main(){
pid_t pid = getpid();
pid_t ppid = getppid();
gid_t gid = getgid();
gid_t egid = getegid();
uid_t uid = getuid();
uid_t euid = geteuid();
printf("1. Process ID: %d\n", pid);
printf("2. Parent Process ID: %d\n", ppid);
printf("3. Group ID: %d\n", gid);
printf("4. Effective Group ID: %d\n", egid);
printf("5. User ID: %d\n", uid);
printf("6. Effective User ID: %d\n", euid);
return 0;
}

2. Write a system program to create a child process, show message from both parent and child
process.

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

int main() {
pid_t pid = fork();
if(pid < 0){
printf("Fork() failed");
return 1;
}
else if(pid == 0)
printf("Child Process: My PID is %d and my Parent's PID is %d\n", getpid(), getppid());
else
printf("Parent Process: My PID is %d and I created a child with PID %d\n", getpid(), pid);
return 0;
}
3. Write a system program to demonstrate zombie process.

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

int main(){
pid_t pid = fork();
if (pid > 0){
printf("Parent: child process created with PID %d\n", pid);
sleep(1);
printf("Parent: waiting for child to terminate...\n");
wait(NULL);
printf("Parent: child process terminated, zombie cleaned up.\n");
}
else if(pid == 0){
printf("Child: exiting...\n");
exit(0);
}
else
printf("Fork() has failed!");
return 0;
}

4. Write a system program to demonstrate orphan process.

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

int main(){
pid_t pid = fork();
if(pid > 0){
printf("Parent: My PID is %d\n", getpid());
sleep(1);
printf("Parent: exiting...\n");
}
else if(pid == 0){
printf("Child: My PID= %d and Parent's PID= %d\n", getpid(), getppid());
sleep(1);
printf("Child: Parent is terminated and adopted by init (Orphan Process)!\n");
exit(0);
}
else
printf("Fork() has failed!");
return 0;
}

FIFO

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

int main() {
int pageFrames = 3, pageQueueLength, i, j, k;
int pageQueue[100], frames[10], pageFaults = 0, hits = 0;
int predefinedQueue[] = {1,3,0,3,5,6};
pageQueueLength = sizeof(predefinedQueue)/ sizeof(int);

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


pageQueue[i] = predefinedQueue[i];

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


frames[i] = -1;

for (i = 0,k=0; i < pageQueueLength; i++) {


int pageFound = 0;
for (j = 0; j < pageFrames; j++) {
if (frames[j] == pageQueue[i]) {
pageFound = 1;
hits++;
break;
}
}

if (pageFound == 0) {
frames[k] = pageQueue[i];
k = (k + 1) % pageFrames;
pageFaults++;
printf("\nPage Fault! Frames: ");
for (j = 0; j < pageFrames; j++) {
if (frames[j] != -1)
printf("%d ", frames[j]);
else
printf("- ");
}
}
else
printf("\nPage Hit! Frames Unchanged!");
}

float hitRatio = (float)hits / pageQueueLength;


float missRatio = (float)pageFaults / pageQueueLength;
printf("\n\nTotal Page Faults: %d\n", pageFaults);
printf("Page Fault Ratio: %f\n", missRatio);
printf("Total Hits: %d\n", hits);
printf("Hit Ratio: %f\n", hitRatio);

return 0;
}

LRU

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

int findLRU(int time[], int n){


int i=0, min = time[0], pos=0;
for(i=1; i<n; i++){
if (time[i] < min){
min = time[i];
pos = i;
}
}
return pos;
}

int main(){
int pageFrames = 4;
int predefinedQueue[]={5,0,1,3,2,4,1,0,5};
int pageQueueLength = sizeof(predefinedQueue)/sizeof(int);
int frames[10],time[10], pageFaults=0, hits=0, counter = 0, i, j, flag1, flag2;
for (i=0; i<pageFrames; ++i)
frames[i] = -1;

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


flag1 = 0;
flag2 = 0;
for(j=0; j<pageFrames; ++j){
if(frames[j]==predefinedQueue[i]){
counter++;
time[j]= counter;
hits++;
flag1 = 1;
flag2 = 1;
break;
}
}

if (flag1 == 0) {
for (j = 0; j < pageFrames; ++j) {
if (frames[j] == -1) {
counter++;
pageFaults++;
frames[j] = predefinedQueue[i];
time[j] = counter;
flag2 = 1;
break;
}
}
}

if (flag2 == 0) {
int pos = findLRU(time, pageFrames);
counter++;
pageFaults++;
frames[pos] = predefinedQueue[i];
time[pos] = counter;
}

printf("\nFrames: ");
for (j = 0; j < pageFrames; ++j) {
if (frames[j] != -1)
printf("%d ", frames[j]);
else
printf("- ");
}
}
printf("\n\nTotal Page Faults: %d\n", pageFaults);
printf("Fault Ratio: %f\n", (float)pageFaults/pageQueueLength);
printf("Total Hits: %d\n", hits);
printf("Hit Ratio: %f\n", (float)hits/pageQueueLength);
return 0;
}

You might also like