0% found this document useful (0 votes)
176 views49 pages

Os Lab File 2017se114

The document details 9 tasks completed in a Linux lab covering file permissions, processes, and CPU scheduling algorithms using commands like ls, rm, date, ps, kill, du, df, cal, and gantt chart output to simulate first-come, first-served scheduling by calculating waiting times and turnaround times for processes.

Uploaded by

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

Os Lab File 2017se114

The document details 9 tasks completed in a Linux lab covering file permissions, processes, and CPU scheduling algorithms using commands like ls, rm, date, ps, kill, du, df, cal, and gantt chart output to simulate first-come, first-served scheduling by calculating waiting times and turnaround times for processes.

Uploaded by

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

LAB FILE ADDITIONAL SUMMER COURSE 2021

Subject: Operating System

Submitted By: Muhammad Ahmed

Roll No: 2017-SE-114

Submitted To: Ms. Falak Saleem


LAB # 01
Introduction to Linux Operating System

Lab Task:

1. Create multiple directories and subdirectories.

2. Search different options of rmdir command using man command.


LAB # 02
File Access Permission in LINUX
Task # 01:

ssuet@ ssuet-/user:~$ cat > file1


English
ssuet@ ssuet-/user:~$ cat > file2
Urdu
ssuet@ ssuet-/user:~$ cat > file3
Mathematics
ssuet@ ssuet-/user:~$ cat >file4
Islamyat
ssuet@ ssuet-/user:~$ cat > file5
Science
ssuet@ ssuet-/user:~$

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:

ssuet@ ssuet-/user:~$ date


00:52:45 4 ‫ بدھ دسمبر‬PKT 2019
Task # 04:
ssuet@ ssuet-/user:~$ ps
PID TTY TIME CMD
1500 pts/0 00:00:00 bash
1924 pts/0 00:00:00 ps

Task # 05:

ssuet@ ssuet-/user:~$ finger


Login Name Tty Idle Login Time Office Office Phone
ssuet Ssuet *:0 Dec 4 18:32 (:0)

Task # 06:

ssuet@ ssuet-/user:~$ cal


2019 ‫دسمبر‬
‫ات پي من بد جم جم ھف‬
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31

Task # 07:

ssuet@ ssuet-/user:~$ free


total used free shared buff/cache available
Mem: 8029288 1003740 6026480 201056 999068 6574168
Swap: 2097148 0 2097148
‫‪Task # 08:‬‬

‫‪ssuet@ ssuet-/user:~$ cat > yearfile‬‬


‫‪cal 2019‬‬
‫‪ssuet@ ssuet-/user:~$ cal 2019 > yearfile‬‬
‫‪ssuet@ ssuet-/user:~$ cat yearfile‬‬

‫‪2019‬‬

‫مارچ‬ ‫فروری‬ ‫جنوری‬


‫ات پي من بد جم جم ھف ات پي من بد جم جم ھف ات پي من بد جم جم ھف‬
‫‪1 2 3 4 5‬‬ ‫‪1 2‬‬ ‫‪1 2‬‬
‫‪6 7 8 9 10 11 12 3 4 5 6 7 8 9 3 4 5 6 7 8 9‬‬
‫‪13 14 15 16 17 18 19 10 11 12 13 14 15 16 10 11 12 13 14 15 16‬‬
‫‪20 21 22 23 24 25 26 17 18 19 20 21 22 23 17 18 19 20 21 22 23‬‬
‫‪27 28 29 30 31‬‬ ‫‪24 25 26 27 28‬‬ ‫‪24 25 26 27 28 29 30‬‬
‫‪31‬‬

‫جون‬ ‫مئی‬ ‫اپریل‬


‫ات پي من بد جم جم ھف ات پي من بد جم جم ھف ات پي من بد جم جم ھف‬
‫‪1 2 3 4 5 6‬‬ ‫‪1 2 3 4‬‬ ‫‪1‬‬
‫‪7 8 9 10 11 12 13 5 6 7 8 9 10 11 2 3 4 5 6 7 8‬‬
‫‪14 15 16 17 18 19 20 12 13 14 15 16 17 18 9 10 11 12 13 14 15‬‬
‫‪21 22 23 24 25 26 27 19 20 21 22 23 24 25 16 17 18 19 20 21 22‬‬
‫‪28 29 30‬‬ ‫‪26 27 28 29 30 31‬‬ ‫‪23 24 25 26 27 28 29‬‬
‫‪30‬‬

‫ستمبر‬ ‫اگست‬ ‫جوالئی‬


‫ات پي من بد جم جم ھف ات پي من بد جم جم ھف ات پي من بد جم جم ھف‬
‫‪1 2 3 4 5 6‬‬ ‫‪1 2 3 1 2 3 4 5 6 7‬‬
‫‪7 8 9 10 11 12 13 4 5 6 7 8 9 10 8 9 10 11 12 13 14‬‬
‫‪14 15 16 17 18 19 20 11 12 13 14 15 16 17 15 16 17 18 19 20 21‬‬
‫‪21 22 23 24 25 26 27 18 19 20 21 22 23 24 22 23 24 25 26 27 28‬‬
‫‪28 29 30 31‬‬ ‫‪25 26 27 28 29 30 31 29 30‬‬

‫دسمبر‬ ‫نومبر‬ ‫اکتوبر‬


‫ات پي من بد جم جم ھف ات پي من بد جم جم ھف ات پي من بد جم جم ھف‬
‫‪1 2 3 4 5‬‬ ‫‪1 2 1 2 3 4 5 6 7‬‬
‫‪6 7 8 9 10 11 12 3 4 5 6 7 8 9 8 9 10 11 12 13 14‬‬
‫‪13 14 15 16 17 18 19 10 11 12 13 14 15 16 15 16 17 18 19 20 21‬‬
‫‪20 21 22 23 24 25 26 17 18 19 20 21 22 23 22 23 24 25 26 27 28‬‬
‫‪27 28 29 30 31‬‬ ‫‪24 25 26 27 28 29 30 29 30 31‬‬
Task # 09:

ssuet@ ssuet-/user:~$ cal > yearfile


ssuet@ ssuet-/user:~$ date >> yearfile
ssuet@ ssuet-/user:~$ cat yearfile
2019 ‫دسمبر‬
‫ات پي من بد جم جم ھف‬
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31

01:06:06 4 ‫ بدھ دسمبر‬PKT 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

boots up, and it runs until the system shuts down.

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) */

void *runner(void *param); /* the thread */

main(int argc , char *argv[])


{
pthread_t tid; /* the thread identifier */
pthread_attr_t attr; /* set of attributes for the thread */
if(argc !=2)
{
printf(“usage :a.out <integer value> \n”);

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 */

pthread_create(&tid,&attr,runner,argv[1]); /* create the thread */

/*pthread_create() takes 4 arguments.


The first argument is a pointer to thread_id which is set by this function.
The second argument specifies attributes. If the value is NULL, then default attributes shall be used.
The third argument is name of function to be executed for the thread to be created.
The fourth argument is used to pass arguments to the function, runner.

When pthread_create() is successful, the ID of the created thread is stored in the location referred to

as tid.
*/

pthread_join(tid,NULL); /* wait for termination of another thread*/

/* Wait till threads are complete before main continues.*/


printf ("sum = %d\n", sum);
}
/** The thread will begin control in this function */
void *runner(void *param)
{
int upper=atoi(param);
/*The atoi() function in C takes a string (which represents an integer) as an argument and returns its
value of type int. So basically the function is used to convert a string argument to an integer.*/

int i;
sum = 0;
if(upper>0)
{
for(i=1;i<upper;i++)
{
sum= sum+i;
}
pthread_exit(0);
}
}

COMPILE AND RUN

gcc f4.9.c –lpthread


./f4.9
4
Sum =10
LAB # 05

CPU Scheduling Algorithms-1


Exercise

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>

void gantt_chart(int n,int burst_time[20],

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|");

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


for(j=0; j<burst_time[i] - 1; j++) printf(" ");
printf("P%d", n+1);
for(j=0; j<burst_time[i] - 1; j++) printf(" ");
printf("|");
}
printf("\n ");
for(i=0; i<n; i++) {
for(j=0; j<burst_time[i]; j++)
printf("--");
printf(" ");
}
printf("\n");
printf("0");
for(i=0; i<n; i++)
{
Forj=0;j<burst_time[i;j
++)
printf("");
if(turnaround_time[i]
>9)
printf("\b");
printf("%d",turnaround_time[i]);
}
printf("\n");
}
2. Simulate SJF scheduling in C language. Also attach the print out of code and snapshot of output
window.
#include<stdio.h>

void print_gantt_chart( int n,int burst_time[20],int turnaround_time[20],int


waiting_time[20]);

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;

printf("Enter number of process:");

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]);

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;

printf("\n\nAverage Waiting Time= %f",avg_wait_time);

printf("\nAverage Turnaround Time= %f\n",avg_turnaarround_time);

printf("Gantt Chart: \n");

print_gantt_chart(
process_count,burst_time,turnaround_time,waiting_time);

void print_gantt_chart( int n,int burst_time[20],int turnaround_time[20],int


waiting_time[20])

int i, j;

int last = burst_time[n-1] + ( n == 1 ? 0 : waiting_time[n-1]);

// printing top bar

printf(" ");

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

for(j=0; j<burst_time[i]; j++) printf("--");

printf(" ");
2
3
}

printf("\n|");

// middle position

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

for(j=0; j<burst_time[i]-1; j++) printf(" ");

printf("p%d", i+1);

for(j=0; j<burst_time[i]-1; j++) printf(" ");

printf("|");

printf("\n ");

// bottom bar

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

for(j=0; j<burst_time[i]; j++) printf("--");

printf(" ");

printf("\n");

// printing waiting time

int minus = 0;

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

if(waiting_time[i]>9) printf(" ");

printf("%d", waiting_time[i]);

if(waiting_time[i+1]>9){

minus = 1;

24
}

if(i+1 == n ) if (last>9) minus = 1;

for(j=0; j<burst_time[i]-minus; j++)

printf(" ");

25
}

if(last>9) printf(" ");

printf("%d\n", last);

26
LAB # 06

CPU Scheduling Algorithms-2

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("Enter number of process:");


scanf("%d",&n);

printf("\nEnter BurstTime:\n");
for(i=0;i<n;i++)

printf("p%d:",i+1);

scanf("%d",&bt[i]);

p[i]=i+1; //contains process number

//sorting burst time in ascending order using


selection sort for(i=0;i<n;i++) 27

{
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[0]=0; //waiting time for first process will be zero

//calculate waiting time


for(i=1;i<n;i++)

wt[i]=0; for(j=0;j<i;j++)

wt[i]+=bt[j];

total+=wt[i];

avg_wt=(float)total/n; //average waiting time total=0;

printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time/tResponse Time");


for(i=0;i<n;i++)
28
{

tat[i]=bt[i]+wt[i];
//calculate turnaround time total+=tat[i];

printf("\np%d\t\t %d\t\t %d\t\t\t%d\t\t%d",p[i],bt[i],wt[i],tat[i],wt[i]);

avg_tat=(float)total/n;

//average turnaround time

printf("\n\nAverage Waiting Time=%f",avg_wt);

printf("\nAverageTurnaroundTime=%f\n",avg_tat);

printf("\n\nAverage Response Time :%f",avg_wt);

29
Lab task 02
Write the algorithm of SJF (Preemptive) algorithm and also implement it and show the output.

PROGRAM FOR PREEMPTIVE:

#include <stdio.h>

int main()

int arrival_time[10], burst_time[10], temp[10]; int i, smallest, count = 0, time,

double wait_time = 0, turnaround_time = 0, end;

float average_waiting_time, average_turnaround_time;

printf("nEnter the Total Number of Processes:t");

scanf("%d", &limit);

printf("nEnter Details of %d Processesn", limit);

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

printf(" nEnter Arrival Time:t");

scanf("%d",&arrival_time[i]);

printf(" Enter Burst Time:t");

scanf("%d",&burst_ time[i]); temp[i] = burst_ti me[i];

burst_time[9] = 9999;

for(time = 0; count != limit; time++) 30


{

smallest = 9;

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

if(arrival_time[i] <= time && burst_time[i] < burst_time[smallest] && burst_time[i] > 0)

smallest = i;

burst_time[s malles t]--;

if(burs t_time [small est] == 0)

count++;

end = time + 1;

wait_time = wait_time + end - arrival_time[smallest]temp[smallest];

turnaround_time = turnaround_time + end - arrival_time[smallest];

average_waiting_time = wait_time / limit;

average_turnaround_time= turnaround_time / limit

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.

2. What did you learn after running the Program 3?

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:

Question: What did you learn after running the Program 1?

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:

Write a script that determines whether number is odd or even.


37
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:

Introduction to Perl Scripting in Linux Operating System.

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.

A. Assign scalar $a to a starting value of 5. Print value of $a .


B. Add 6 to the previous result. Print the new result.
C. Multiply the previous result by 2. Print the new result.
D. Auto increment the previous result. Print the new result. E. Subtract 9 from the previous result.
Print the new result.

F. Divide the previous result by 7. Print the new result.

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:

Operating System (SWE-204) SSUET/QR/114

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

You might also like