OS File
OS File
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;
}
#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;}
DIR=$1
if [ ! -d "$DIR" ]; then
echo "Directory $DIR does not exist."
fi
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"
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>
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
*--------------------------------------------------------------------*/
/*---------------------------------------------------------------------
* 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
*--------------------------------------------------------------------*/
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
*--------------------------------------------------------------------*/
/*---------------------------------------------------------------------
* 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
*--------------------------------------------------------------------*/
semoparray[0].sem_num=semnum;
semoparray[0].sem_op=-1;
semoparray[0].sem_flg=0;
/*---------------------------------------------------------------------
* 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
*--------------------------------------------------------------------*/
semoparray[0].sem_num=semnum;
semoparray[0].sem_op=1;
semoparray[0].sem_flg=0;
#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;
}
#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);
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!");
}
return 0;
}
LRU
#include<stdio.h>
#include<stdlib.h>
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;
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;
}