0% found this document useful (0 votes)
9 views4 pages

Assignment-05 Anil

study materials

Uploaded by

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

Assignment-05 Anil

study materials

Uploaded by

john
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1. Given p1,p2,p3 processes along with their burst times.

Write a program to find the


waiting time for each processes along with average waiting time and turn around time
using FFCS algorithm. (5 marks)
Process Duration Order Arrival
P1 20 1 0
P2 4 2 2
P3 5 4 4

#include <stdio.h>
#define N 3 /* number of processes */
int main(void)
{
char *pid[N] = {"P1", "P2", "P3"}; /* process IDs */
int burst[N] = {20, 4, 5 }; /* burst (CPU) times */
int arrive[N]= { 0, 2, 4 }; /* arrival times */
int wait[N] = {0}; /* waiting time */
int tat[N] = {0}; /* turnaround */
int clock = 0; /* current time */
double sum_wait = 0, sum_tat = 0;
for (int i = 0; i < N; ++i) {
/* CPU is idle until the next job arrives */
if (clock < arrive[i])
clock = arrive[i];
wait[i] = clock - arrive[i]; /* time spent in ready queue */
clock += burst[i]; /* run the job */
tat[i] = wait[i] + burst[i]; /* finish time – arrival */
sum_wait += wait[i];
sum_tat += tat[i];
}
puts("PID Burst Arrival Wait Turn-around");
for (int i = 0; i < N; ++i)
printf("%-3s %5d %7d %4d %11d\n",
pid[i], burst[i], arrive[i], wait[i], tat[i]);
printf("\nAverage waiting time = %.2f\n", sum_wait / N);
printf("Average turnaround time = %.2f\n", sum_tat / N);
return 0;
}
The Average waiting time = 12.67
Average turnaround time = 22.33
2. Given two processes p1,p2 along with duration, order and arrival times.
Process Duration Order Arrival
P1 9 1 0
P2 4 2 2
Write a program to implement SJF algorithm for the processes given in the table and
find the average waiting time and turn around time. (5 marks)

#include <stdio.h>
int main(void)
{
char *pid[2] = {"P1", "P2"}; /* IDs */
int bt [2] = { 9, 4 }; /* burst times */
int at [2] = { 0, 2 }; /* arrival times */

int wait[2] = {0}, tat[2] = {0};


int done[2] = {0}; /* 0 = not done */
int time = 0, finished = 0;

while (finished < 2) {


int idx = -1; /* pick next job */
for (int i = 0; i < 2; ++i)
if (!done[i] && at[i] <= time &&
(idx == -1 || bt[i] < bt[idx]))
idx = i;

if (idx == -1) { ++time; continue; } /* CPU idle */

wait[idx] = time - at[idx]; /* waiting time */


time += bt[idx]; /* run process */
tat[idx] = wait[idx] + bt[idx]; /* turn-around */
done[idx] = 1; ++finished;
}

printf("PID Wait Turn-around\n");


for (int i = 0; i < 2; ++i)
printf("%s %2d %2d\n", pid[i], wait[i], tat[i]);
printf("\nAverage wait = %.2f\n",
(wait[0] + wait[1]) / 2.0);
printf("Average TAT = %.2f\n",
(tat[0] + tat[1]) / 2.0);
return 0;
}

The Average Wait = 3.50


The Average TAT = 10.00

3. Write a program to create a child process using the system call fork(), and display the
process id of the child process and the parent process using the system calls getpid()
and getppid().
Note:
Processes are expected to do some functionalities (like Greeting a person, adding
some numbers etc.) (5 marks)

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

int main(void)
{
pid_t pid = fork();

if (pid < 0) {
perror("fork");
return 1;
}

if (pid == 0) {
printf("\n[Child] PID=%d PPID=%d\n", getpid(), getppid());
printf("[Child] 5 + 7 = %d\n", 5 + 7);
return 0;
}

printf("\n[Parent] PID=%d ChildPID=%d\n", getpid(), pid);


printf("[Parent] 6 * 8 = %d\n", 6 * 8);

wait(NULL);
printf("[Parent] child completed.\n");
return 0;
}
4. Assume that you are giving your university name, your first, and last names in the
command line. Write a shell script that takes the arguments from the command line
and displays your first name, last name and university name as follows:
Ex:
Your first name: Hepsiba
Your last name: Vivenkanandan
Your university name: AU

Then write these details into a file and to count the number of words and lines in a
given file.
Note: store the file path in a variable and use the variable to fetch the file. (5 marks)

first_name=$1
last_name=$2
university=$3

echo "Your first name: $first_name"


echo "Your last name: $last_name"
echo "Your university name: $university"

file_path="details.txt"
printf "%s\n%s\n%s\n" \
"Your first name: $first_name" \
"Your last name: $last_name" \
"Your university name: $university" > "$file_path"

echo
echo "In '$file_path':"
wc -w -l "$file_path"

You might also like