Os Lab File 2017se114
Os Lab File 2017se114
Lab Task:
Task # 02:
ssuet@ ssuet-/user:~$ ls
abc file2 ini ler file5 newfile2 Videos
Desktop file3 khan file1 Music Pictures whofile
Documents file4 kk file2 myfile Public yearfile
Downloads file5 lab file3 new Templates
examples.desktop filename lab1 file4 newfile unsorted
ssuet@ ssuet-/user:~$ rm file1
ssuet@ ssuet-/user:~$ ls
abc examples.desktop file5 kk file2 Music newfile2 unsorted
Desktop file2 filename lab file3 myfile Pictures Videos
Documents file3 ini lab1 file4 new Public whofile
Downloads file4 khan ler file5 newfile Templates yearfile
Task # 03:
Task # 05:
Task # 06:
Task # 07:
2019
Task # 10:
ssuet@ ssuet-/user:~$ df ./
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda6 54717364 8527484 43380692 17% /
Task # 11:
ssuet@ ssuet-/user:~$ df -k ./
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda6 54717364 8527484 43380692 17% /
Task # 12:
ssuet@ ssuet-/user:~$ du -s *
0 abc
1788 Desktop
28 Documents
416 Downloads
12 examples.desktop
4 file2
4 file3
4 file4
12 file5
4 filename
736 ini
0 khan
4 kk
4 lab
4 lab1
4 ler
4 file1
4 file2
4 file3
LAB # 03
Process Management
TASK #01:
a. Ps
Ps stands for process state. This is the easiest way to find out which processes are currently
running in your system.
b. kill
the kill command is used to terminate the process properly. In order to terminate any
process, you should know the Process id of that particular process that you want to kill. .
c. killall
killall is a tool for terminating running processes on your system based on name.
d. ps -l
e. init
Init is the parent of all Linux processes. It is the first process to start when a computer
TASK #02:
Forcefully kill a child process and attach the results.
LAB # 04
P-Threads
Example Program:
Program 1:
#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
int sum; /* this data is shared by the thread(s) */
exit(0);
}
if(atoi (argv[1])<0)
{
printf(“%d must be >=0\n” , atoi(argv[1]));
exit(0);
}
pthread_attr_init(&attr); /* get the default attributes */
When pthread_create() is successful, the ID of the created thread is stored in the location referred to
as tid.
*/
int i;
sum = 0;
if(upper>0)
{
for(i=1;i<upper;i++)
{
sum= sum+i;
}
pthread_exit(0);
}
}
1. Simulate FCFS scheduling using the algorithm defined above in C Language. Also attach the print
out of code and snapshot of output window.
#include<stdio.h>
#include
<stdlib.h>
int turnaround_time[20]);
void main()
{
Int process_count,
burst_time[20],
waiting_time[20],
turnaround_time[20],
avg wait time=0,
avg_turnaarround_time=0,i,j;
waiting_time[0]=0;
printf("Enter number of processes : ");
scanf("%d",&process_count);
printf("\nEnter Burst Time\n");
for(i=0;i<process_count;i++)
{
printf("P[%d]:",i+1);
scanf("%d",&burst_time[i]);
}
for(i=1;i<process_count;i++)
{
waiting_time[i]=0;
for(j=0;j<i;j++)
{
waiting_time[i]+=burst_time[j];
}
}
printf("\nProcess\tTurnaround Time\t\tBurst Time\tWaiting Time");
for(i=0;i<process_count;i++)
{
turnaround_time[i]=burst_time[i]+waiting_time[i];
avg_wait_time+=waiting_time[i];
avg_turnaarround_time+=turnaround_time[i];
printf("\nP[%d]\t%d\t\t\t%d\t\t%d",i+1,turnaround_time[i],burst_tim
e[i],waiting_time[i]);
avg_wait_time/=i;
avg_turnaarround_time/=i;
printf("\n\nAverage Waiting Time:%d",avg_wait_time);
printf("\nAverage Turnaround
Time:%d\n",avg_turnaarround_time); printf("Gantt Chart:
\n");
gantt_chart( process_count,burst_time,turnaround_time);
}
void gantt_chart( int n,int burst_time[20],int turnaround_time[20])
{
int i, j;
printf(" ");
for(i=0; i<n; i++)
{
for(j=0; j< burst_time[i]; j++)
printf("--");
printf(" ");
}
printf("\n|");
void main()
int
burst_time[20],p[20],waiting_time[20],turnaround_time[20],i,j,process_count,t
otal=0,cursor,temp;
float avg_wait_time,avg_turnaarround_time;
scanf("%d",&process_count);
for(i=0;i<process_count;i++)
printf("p%d:",i+1);
scanf("%d",&burst_time[i]);
p[i]=i+1;
for(i=0;i<process_count;i++)
{
{
if(burst_time[j]<burst_time[cursor])
cursor=j;
temp=burst_time[i];
burst_time[i]=burst_time[cursor];
burst_time[cursor]=temp;
temp=p[i];
p[i]=p[cursor];
p[cursor]=temp;
waiting_time[0]=0;
for(i=1;i<process_count;i++)
waiting_time[i]=0;
for(j=0;j<i;j++)
waiting_time[i]+=burst_time[j];
total+=waiting_time[i];
}
avg_wait_time=(float)total/process_count;
total=
printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
for(i=0;i<process_count;i++)
turnaround_time[i]=burst_time[i]+waiting_time[i];
total+=turnaround_time[i];
printf("\np%d\t\t %d\t\t
%d\t\t\t%d",p[i],burst_time[i],waiting_time[i],turnaround_time[i]);
avg_turnaarround_time=(float)total/process_count;
print_gantt_chart(
process_count,burst_time,turnaround_time,waiting_time);
int i, j;
printf(" ");
printf(" ");
2
3
}
printf("\n|");
// middle position
printf("p%d", i+1);
printf("|");
printf("\n ");
// bottom bar
printf(" ");
printf("\n");
int minus = 0;
printf("%d", waiting_time[i]);
if(waiting_time[i+1]>9){
minus = 1;
24
}
printf(" ");
25
}
printf("%d\n", last);
26
LAB # 06
Lab task 01
1. Perform the implementation and show the output of FCFS and SJF(Non-Preemptive), algorithms.
#include<stdio.h>
void main()
bt[20],avg,p[20],wt[20],tat[20],i,j,n,total=0,pos,te
mp; float avg_wt,avg_tat;
printf("\nEnter BurstTime:\n");
for(i=0;i<n;i++)
printf("p%d:",i+1);
scanf("%d",&bt[i]);
{
pos=i; for(j=i+1;j<n;j++)
if(bt[j]<bt[pos]) pos=j;
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp
temp=p[i];
p[i]=p[pos]
p[pos]=temp;
wt[i]=0; for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
tat[i]=bt[i]+wt[i];
//calculate turnaround time total+=tat[i];
avg_tat=(float)total/n;
printf("\nAverageTurnaroundTime=%f\n",avg_tat);
29
Lab task 02
Write the algorithm of SJF (Preemptive) algorithm and also implement it and show the output.
#include <stdio.h>
int main()
scanf("%d", &limit);
scanf("%d",&arrival_time[i]);
burst_time[9] = 9999;
smallest = 9;
if(arrival_time[i] <= time && burst_time[i] < burst_time[smallest] && burst_time[i] > 0)
smallest = i;
count++;
end = time + 1;
printf("nnAverageWaitingTime:t%lfn", average_waiting_time);
printf("AverageTurnaroundTime:t%lfn", average_turnaround_time);
return 0; 31
32
LAB # 07
NAMED PIPES
Exercises
1. What did you learn after running the above Program 1 and Program 2?
Answer:
First as we try to run program1 and it will not run as we are trying to read a file that we did not write, then
we do the same with program 2 and it will also not run for same reason and same goes for program 3. After
that we return to program 2 and try to write program by executing the output of execution file and write
msg1, msg2 and msg3, after that program will go on hold.
After that we try again to run program1 and it will again won’t run. Now we will again come back to
program 2 and will write cd /tmp to create a folder after that we write cat > testfile to hold read program
and when we get write program it will directly read it. After that we press CTRL Z to exit program and write
cd and give program msg1, msg2 and msg3. Now we go to program 3 to read those messages.
While pipes are powerful inter-process communication they are not without drawbacks.
Firstly, pipes can only be used on processes that have a common ancestry like a parent and child process.
And, secondly, they are not permanent. A process creates them and the termination of the process leads to
their destruction.
To overcome these deficiencies a variation on these pipes has been implemented known as NAMED PIPES.
These named pipes basically function in the same way as pipes: acting as one way communication channels.
But unlike pipes, a named pipe is a permanent fixture. UNIX treats it just likes a file, giving it a size, owner
and access permission. It can be opened, closed or deleted like any other file.
33
LAB # 08
INTERPROCESS COMMUNICATION
Exercise:
Answer:
We use the if else condition to check the pipe connection and if there isn’t a pipe connection then
system will generate a error message. Then we have used fork method to divide the parts in two
tasks, firstly we will take the input of Fibonacci Series and then we will calculate and print the
Fibonacci Series. Then we will write the input through pipes till where we need the Fibonacci
series. Basically, we have used parent and child process through pfd and pfd means read end and
we have done inter communication process between fork and pipes and that’s how we generated
Fibonacci Series.
34
LAB # 09
INTERPROCESS COMMUNICATION
Exercises
1. What was the mistake in the Program 1, and how it is rectified in Program 2?
Ans: nsem is the variable which shows the number of sub semaphores. In program 1 the mistake
which we have made is that we have taken nsem=0 which means there are no semaphores and that’s
why it doesn’t fulfill the requirement and semaphores will not create.
So in program 2 we have taken nsem=1 and because of that semaphores will create and the output
will be zero.
2. Do ipcs –s at the prompt to see a listing of the semaphores and an entry for the
semaphore will be displayed.
35
4
.
3. Run the programs 3 and 4, find out what is the problem in 3 and how it is rectified in 4.
Ans: In program 3 we have passed GETPID argument which return the value of single semaphore.
In program 4 we used SETVAL which set the value for single semaphore to get the value of
semaphore which is created or processed last.
36
LAB # 10
SHELL SCRIPTING
Q1: Write a script which displays Good Morning if hour is less than 12.00, Good Afternoon if
hour is less than 5.00p.m, and Good Evening if hour is greater than 5.00p.m.
Output:
38
Write a script which takes two numbers as input and asks user choice for multiplication, division,
addition, and subtraction. Like (calculator), and then calculate the output. Apply suitable checks; if
user enters wrong input the program again asks for correct input.
Output:
39
Write a script to print no’s as 5, 4, 3, 2, 1 using while loop.
Output:
40
Q5: Write script to print given number in reverse order, for e.g. If no is 123 it must print as 321.
OUTPUT
Q6: Write script to print given numbers sum of all digits, for e.g. If no is 123 it's sum of all digit
will be 1+2+3 = 6.
41
Output:
Q7: Write Script to find out biggest number from given three numbers. No’s are supplies as
command line argument. Print error if sufficient arguments are not supplied.
Output:
42
43
LAB # 11
PERL SCRIPTING
Objective:
Exercise:
Q1: Construct an array @family holds a list of family member names. The first hash %shoe_color contains favorite shoe
color per person. The second hash %shoe_size contains shoe size per person name. Evaluate and print the favorite shoe
color and shoe size per each family member. For shoe sizes 10 and above, add the word ‘Large’ to the output line.
Output line should be in the format: “John wears large brown shoes size 12”
Code:
Output:
44
Operating System (SWE-204) SSUET/QR/114
Q2: Loop through and print out all even numbers from the @NUMBERS array in the same order they are received. Don't
print any numbers that come after 237 in the array
Code:
Output:
Q3: Follow these instructions and print the result after each step.
Code:
Operating
System (SWE-
204)
SSUET/QR/11
45 4
Output:
Q4: Declare
two scalar
variables and
assign
numeric
values to
them. Then
perform
mathematical
operations on
those
variables and
print the
results.
(Addition,
Subtraction, Multiplication and division).
Code:
Output:
46
LAB # 12
OPERATIONS ON FILES
Exercises 1.
Write few lines about what did you learn after running Programs 3 and Program 4.
Program 3:
#include<fcntl.h>
#include<unistd.h>
main( )
{ int
fd,i;
fd = open(“locktest”,O_APPEND | O_CREAT | O_RDWR,0777);
lockf(fd,F_LOCK,0); for(i=0; i<=2000; i++) write(fd, “A”,1);
}
Program 4
#include<fcntl.h>
#include<unistd.h>
main( )
{ int
fd,i;
fd = open(“locktest”,O_APPEND | O_CREAT | O_RDWR,0777);
lockf(fd,F_LOCK,0); for(i=0; i<=2000; i++) write(fd, “B”,1);
}
47
PROGRAM 3:
PROGRAM 4:
48
LOCKTEST FILE:
OUTPUT:
49
ANSWER:
We have used lockf function which lock the file until the next statement didn’t
execute for example for loop so that’s what we have learned in program 3 and 4