0% found this document useful (0 votes)
21 views71 pages

Os Lab Manual

The document is a lab manual for the Operating Systems course at Sree Venkateswara College of Engineering, detailing the vision and mission of the institution and department, along with program educational objectives and outcomes. It includes a comprehensive list of exercises for Unix/Linux operating system commands and programming tasks, aimed at enhancing practical skills in computer science. The manual serves as a guide for students to learn and apply various operating system concepts and commands effectively.

Uploaded by

harshaeduru1
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)
21 views71 pages

Os Lab Manual

The document is a lab manual for the Operating Systems course at Sree Venkateswara College of Engineering, detailing the vision and mission of the institution and department, along with program educational objectives and outcomes. It includes a comprehensive list of exercises for Unix/Linux operating system commands and programming tasks, aimed at enhancing practical skills in computer science. The manual serves as a guide for students to learn and apply various operating system concepts and commands effectively.

Uploaded by

harshaeduru1
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/ 71

SREE VENKATESWARA

COLLEGE OF ENGINEERING
(AUTONOMOUS)
N.RAJUPALEM, NELLORE (DIST)

AFFILIATED TO JNTU, ANANTAPUR.

OPERATING SYSTEM
LAB MANUAL
(R-20 REGULATION)
II BTECH CSE –I SEM

DEPARTMENT
OF
COMPUTER SCIENCE AND ENGINEERING
NAME OF THE STUDENT

ROLL.NO

YEAR

BRANCH
SREE VENKATESWARA COLLEGE OF ENGINEERING
NAAC ‘A’ Grade Accredited Institution
An ISO 9001: 2015 Certified Institution
(Approved by AICTE, New Delhi and Affiliated to JNTU, Anantapur)
Northrajupalem (Vi), Kodavaluru (M) , S.P.S.R Nellore (Dt)-524316

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Vision of the Institute

❖ Be a best institute to originate and foster skilled engineers and create


changemaker’s for the development of nation with a phenomenal wisdom
with morals, values and societal responsibilities.

Mission of the Institute

❖ M1: To impart graduate with quality education and use innovative


teaching-learning process to excel in their careers.
❖ M2: To build an ecosystem and play a vital role in strengthening
innovative research and entrepreneurship skills among graduates.
❖ M3: To produce engineers with moral values and social responsibility,
and address societal needs for better nation.
SREE VENKATESWARA COLLEGE OF ENGINEERING
NAAC ‘A’ Grade Accredited Institution
An ISO 9001: 2015 Certified Institution
(Approved by AICTE, New Delhi and Affiliated to JNTU, Anantapur)
Northrajupalem (Vi), Kodavaluru (M) , S.P.S.R Nellore (Dt)-524316

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Vision of the Department

❖ To produce the globally competent professionals in the field of computer


science and engineering with socio economic and ethical values.

Mission of the Department

❖ M1: To provide state-of-the-art computer science and engineering


facilities through innovative teaching and learning practices.
❖ M2: To prepare the graduates for higher learning, emphasizing research
and entrepreneurship.
❖ M3: To inculcate ethical values, leadership qualities, and professional
behaviour to improve the living standards of society.
SREE VENKATESWARA COLLEGE OF ENGINEERING
NAAC ‘A’ Grade Accredited Institution
An ISO 9001:: 2015 Certified Institution
(Approved by AICTE, New Delhi and Affiliated to JNTU, Anantapur)
Northrajupalem (Vi), Kodavaluru (M) , S.P.S.R Nellore (Dt)-524316

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


Program Educational Objectives

PEO-1: To provide the graduates with effectively applying mathematics,


science, and engineering methodologies for analyzing, designing, and
implementing software solutions for real-world problems.

PEO-2: To become recognized professional engineers with a demonstrated


commitment to lifelong learning, continuous learning, and self-improvement to
change in computer science engineering.

PEO-3: To train the graduates to have basic interpersonal skills and a sense of
social responsibility that covers them a way to become good team members and
leaders.
SREE VENKATESWARA COLLEGE OF ENGINEERING
NAAC ‘A’ Grade Accredited Institution
An ISO 9001:: 2015 Certified Institution
(Approved by AICTE, New Delhi and Affiliated to JNTU, Anantapur)
Northrajupalem (Vi), Kodavaluru (M) , S.P.S.R Nellore (Dt)-524316

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


Program Outcomes (PO’s)

PO_1 Engineering knowledge: Apply the knowledge of mathematics, science, engineering


fundamentals, and an engineering specialization to the solution of complex engineering problems.
PO_2 Problem analysis: Identify, formulate, review research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of mathematics,
natural sciences, and engineering sciences.
PO_3 Design/development of solutions: Design solutions for complex engineering problems and
design system components or processes that meet the specified needs with appropriate
consideration for the public health and safety, and the cultural, societal, and environmental
considerations.
PO_4 Conduct investigations of complex problems: Use research-based knowledge and research
methods including design of experiments, analysis and interpretation of data, and synthesis of the
information to provide valid conclusions.
PO_5 Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
engineering and IT tools including prediction and modeling to complex engineering activities
with an understanding of the limitations.
PO_6 The engineer and society: Apply reasoning informed by the contextual knowledge to assess
societal, health, safety, legal and cultural issues and the consequent responsibilities relevant to the
professional engineering practice.
PO_7 Environment and sustainability: Understand the impact of the professional engineering
solutions in societal and environmental contexts, and demonstrate the knowledge of, and need
for sustainable development.
PO_8 Ethics: Apply ethical principles and commit to professional ethics and responsibilities and norms
of the engineering practice.
PO_9 Individual and team work: Function effectively as an individual, and as a member or leader in
diverse teams, and in multidisciplinary settings.
PO_10 Communication: Communicate effectively on complex engineering activities with the
engineering community and with society at large, such as, being able to comprehend and write
effective reports and design documentation, make effective presentations, and give and receive
clear instructions.
PO_11 Project management and finance: Demonstrate knowledge and understanding of the
engineering and management principles and apply these to one’s own work, as a member and
leader in a team, to manage projects and in multidisciplinary environments.
PO_12 Life-long learning: Recognize the need for, and have the preparation and ability to engage in
independent and life-long learning in the broadest context of technological change.
SREE VENKATESWARA COLLEGE OF ENGINEERING
NAAC ‘A’ Grade Accredited Institution
An ISO 9001:: 2015 Certified Institution
(Approved by AICTE, New Delhi and Affiliated to JNTU, Anantapur)
Northrajupalem (Vi), Kodavaluru (M) , S.P.S.R Nellore (Dt)-524316

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Program Specific Outcomes (PSO’s)

PSO-1: Domain-specific knowledge: Acquire knowledge of hardware


functionality, design, and development of software components required to
process the information.

PSO-2: Problem-solving skills: Analyze data, identify data structures, design


suitable algorithms, develop and maintain software for real-world problems.
INDEX
S.No. Date Name of the Program Page Remarks
No.
UNIX OPERATING SYSTEM LAB
LIST OF EXERCISES

1) a) Study of Unix/Linux general purpose utility command list: man,who,cat, cd, cp, ps, ls, mv, rm, mkdir, rmdir,
echo, more, date, time, kill, history, chmod, chown, finger, pwd, cal, logout, shutdown.
b) Study of vi editor
c) Study of Bash shell, Bourne shell and C shell in Unix/Linux operating system
d) Study of Unix/Linux file system (tree structure)
e) Study of .bashrc, /etc/bashrc and Environment variables.
2) Write a C program that makes a copy of a file using standard I/O, and system calls
3) Write a C program to emulate the UNIX ls –l command.
4) Write a C program that illustrates how to execute two commands concurrently with a command pipe.
Ex: - ls –l | sort
5) Simulate the following CPU scheduling algorithms:
(a) Round Robin (b) SJF (c) FCFS (d) Priority
6) Multiprogramming-Memory management-Implementation of fork (), wait (), exec() and exit (), System calls
7) Simulate the following:
a) Multiprogramming with a fixed number of tasks (MFT)
b) Multiprogramming with a variable number of tasks (MVT)
8) Simulate Bankers Algorithm for Dead Lock Avoidance
9) Simulate Bankers Algorithm for Dead Lock Prevention.
10) Simulate the following page replacement algorithms:
a) FIFO b) LRU c) LFU
11) Simulate the following File allocation strategies
(a) Sequenced (b) Indexed (c) Linked
12) Write a C program that illustrates two processes communicating using shared memory
13) Write a C program to simulate producer and consumer problem usingsemaphores
14) Write C program to create a thread using pthreads library and let it run its function.
15) Write a C program to illustrate concurrent execution of threads using pthreads library.

Additional exercises
1. Study of Unix/Linux Basic Filters command list : grep , sed , awk
2. Study of Unix/Linux Basic Text processing utility command list : head, tail ,paste ,cmp, diff ,comm,
join
UNIX OPERATING SYSTEM LAB MANUAL

1. Study of Unix/Linux general purpose utility command list


man,who,cat, mkdir, cd, cp, ps, ls, mv, rm, rmdir, echo, more, date, time, kill,
history,
chmod, chown, finger, pwd, cal, logout, shutdown.
1) The man command:
man - an interface to the on-line reference manuals
man is the system's manual pager. Each page argument given to man is normally
the name of a program, utility or function.
Example:
$man pwd
Output:
PWD
NAME
pwd – print working directory name
SYNOPSIS
pwd
DESCRIPTION
pwd prints the pathname of the working (current) directory

2) The who command:


who - show who is logged on
Print information about users who are currently logged in.
Example:
$who
root console july 2 10:30
aaa tty01 july 2 10:40
bbb tty02 july 2 10:55
$

3) The cat command:


cat - concatenate files and print on the standard output
Concatenate FILE(s), or standard input, to standard output.

DHANEKULA INSTITUTE OF ENGINEERING AND TECHNOLOGY Page 1


UNIX OPERATING SYSTEM LAB MANUAL

Example:
$cat > newfile
Hi
Hell
Welcome
$
The input operation is terminated by using <ctrl+d> command

4) The mkdir command:


mkdir - make directories
It creates the directories if they do not already exist.
Example:
$mkdir user1
$

5) The cd command:
cd - change directories
It is a command-line OS shell command used to change the current working
directory
Example:
$cd user1
user1]$

6) The cp command:
cp - copy files and directories
This command is used to create a duplicate of a file, a set of files or a directory
The cp command copies both text and binary files
Syntax:
$cp source-file/directory destination-file/directory
Examples:
i) $cp file1 file2
ii) $cp dir1/file1 dir1/file2

7) The ps command:
ps - report a snapshot of the current processes.

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 2


UNIX OPERATING SYSTEM LAB MANUAL

The ps command is used to display the attributes of processes that are running
currently.
Example:
$ps
PID TTY TIME CMD
476 tty03 00:00:01 login
659 tty03 00:00:01 sh
684 tty03 00:00:00 ps
$

8) The ls command:
ls - list directory contents
This command lists the contents in a directory.
Syntax:
$ls
Example:
$ls

9) The mv command:
mv - move (rename) files
This command is used to move either an individual file, a list of files or a directory
Syntax:
$mv source-file/directory destination-file/directory
Example:
$mv dir1/file1 dir2
The mv command is also used to rename a file or directory
Syntax:
$mv old-file-name new-file-name
Example:
$mv file1 file7

10) The rm command:


rm - remove files or directories
This command is used remove a file or a directory.

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 3


UNIX OPERATING SYSTEM LAB MANUAL

Syntax:
$rm filename
Example:
$rm file1

11) The rmdir command:


rmdir - remove empty directories
This command is used to delete a directory
Syntax:
$rmdir directory-name
Example:
$rmdir user1

12) The echo command:


echo - display a line of text
This command is used to display messages.
It takes zero, one or more arguments.
Example:
$echo “Hello World”
Hello World
$

13) The more command:


more - file perusal filter for crt viewing
This command is used to display a file.
- It allows us to set the output page size and pauses at the end of each page to allow
us to read the file.
Syntax:
$more filename
Example:
$more story1

14) The date command:


date - print or set the system date and time

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 4


UNIX OPERATING SYSTEM LAB MANUAL

Using this command, the user can display the current date along with the time
nearest to the second
Example:
$date
Sat Jan 10 11:58:00 IST 2004
$

15) The time command:


time - time a simple command or give resource usage
This command is used to know the resource usage
It runs a program or command with given arguments, generates a timing statistics
about the program run and directs this statistics report to the standard output.
Example:
$time
real 0m0.000s
user 0m0.000s
sys 0m0.000s
$

16) The kill command:


kill - terminate a process
To kill a background process a kill command is used.
This command is given with the PID of the process to be killed as its argument
Example:
$kill 555
Here, 555 is the PID of the process

17) The history command:


history – Used to display the history of previous executed commands
With no options, display the command history list with line numbers
Example:
$history
1 pwd
2 date
3 cal

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 5


UNIX OPERATING SYSTEM LAB MANUAL

4 history

18) The chmod command:


chmod - change file mode bits
The chmod command is used to change permissions of a file after its creation
Only the Owner or Super User can change file permissions
Syntax:
$chmod assignment-expression filename
Example:
$chmod u+x sample
$ls -l sample
- r w x r - - r - - sample

19) The chown command:


chown - change file owner and group
Only the owner can change the major attributes of a file
Sometimes it is necessary to change the ownership of a file.
Example:
$ls -l sample
-rwxr--r-x 1 dhoni July 19 11:55 sample
$chown virat sample
$ls -l sample
-rwxr--r-x 1 virat July 19 11:55 sample

20) The finger command:


finger - user information lookup program
The finger displays information about the system users.
Example:
$finger
Login Name TTY Idle When Where
root super user tty01 1:24 Mon 14:00
501 ABD tty02 0:20 Mon 15:40 abd.cric.com
502 Federer tty03 2:12 Mon 15:40 fedrr.batm.com

21) The pwd command:

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 6


UNIX OPERATING SYSTEM LAB MANUAL

pwd – print name of current/working directory.


This command is used to determine the location of the current directory in the
directory structure
Syntax:
$pwd
Example:
$pwd

22) The cal command:


cal - displays a calendar
This command is used to print the calendar of a specific month or a specific year
Example:
$cal 03 2018

23) The logout command:


logout - Exit a login shell.
Example:
$logout

24) The shutdown command:


shutdown - bring the system down
shutdown arranges for the system to be brought down in a safe way.
Example:
$shutdown

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 7


UNIX OPERATING SYSTEM LAB MANUAL

Exp.No: 1(b)
Study of vi editor.

Vi Editor:
- An editor allows the users to see a portion of a file on the screen and to modify
characters and lines by simply typing at the cursor position.
- The vi editor represents,
- Vi stands for visual
- It is a full screen editor and allows the user to view and edit the entire
document at the same time.
- Vi is case sensitive.
- It has powerful undo features.

Modes of Vi editor:
i) Command mode:
- In this mode all the keys pressed by the user are interpreted to be editor
commands.
- No text is displayed on the screen even if corresponding keys is pressed on the
keyboard.
ii) Insert mode:
- This mode permits to insert a new text, editing and replacement of existing text.
- When vi editor is in insert mode the letters typed at the keyboard are echoed on
the screen.
iii) Escape mode:
- Commands typed at the command line.
Starting with vi editor:
Syntax:
$vi filename

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 8


UNIX OPERATING SYSTEM LAB MANUAL

Editing the file:


- Open the file using $ vi filename
- To add text at the end of the file, position the cursor at the last character ofthe
file.
- Switch from command mode to text input mode by pressing ‘a’.
- Here ‘a’ stands for append.
- Inserting text in the middle of the file is possible by pressing ‘i’.
- The editor accepts and inserts the typed character until Esc key is pressed.

Saving text:
:w – save the file and remains in edit mode
:wq – save the file and quits from edit mode
:q – quit without changes from edit mode

Quitting vi:
Press zz or ‘:wq’ in command mode.

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 9


UNIX OPERATING SYSTEM LAB MANUAL

Exp.No: 1(c)
Study of Bash shell, Bourne shell and C shell in Unix/Linux operating system.
Types of Shells:
- The Shells are 4 types
a) The Bourne Shell (sh)
b) The C Shell (csh)
c) The Korn Shell (ksh)
d) The Bourne-Again Shell (bash)
a) The Bourne Shell (sh):
- This is the most common shell available on Unix Systems.
- The first major shell developed by Stephen Bourne at AT&T Bell labs.
- This shell is widely used.
- This shell is distributed as the standard shell on almost all Unix Systems.
b) The C Shell (csh):
- It is developed by Bill Joy at UCB as part of the BSD release.
- Its syntax and usage is very similar to the C programming language.
- This shell is not available on all machines.
- Shell scripts written in the C shell are not compatible with the Bourne Shell.
- A version of it called tcsh is available free of cost under Linux.
c) The Korn Shell (ksh):
- This shell was developed by David Korn at AT&T Bell labs.
- Basically it is built on the Bourne Shell.
- It also incorporates certain features of the C shell.
- At present it is one of the widely used shells.
- It can run Bourne shell scripts without any modification.
- One of its versions, the Public Domain Korn Shell (pdksh), comes with Linux
free of cost.
d) The Bourne-Again Shell:
- It was developed by B Fox and C Ramey at Free Software Foundation.
- Certain Linux operating system variants come with this shell as its default shell.
- This is clearly a free ware shell.

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 10


UNIX OPERATING SYSTEM LAB MANUAL

Exp.No: 1(d)
Study of Unix/Linux file system (tree structure).
Unix/Linux file system (tree structure):

- It is also called as UNIX File System, because it is used to organize UNIX files.
- The different directories in the Directory Hierarchy are,
Directory Meaning
root (/) Root of the file system
/bin Essential programs in executable form (binaries)
/tmp Temporary files; cleaned when system is restarted
/dev Device files
/etc System miscellany
/sbin System binaries
/usr User file system
/var Variables
/usr/lib Libraries for C, FORTRAN. Etc
/usr/bin User binaries

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 11


UNIX OPERATING SYSTEM LAB MANUAL

Exp.No: 1(e)
Study of .bashrc, /etc/bashrc and Environment variables.

.bashrc
.bashrc file is automatically executed when new terminal(shell) is opened.
Purpose of bashrc file:
• You can export environment variables(So there is no need to export
environment variable every time)
• You can define aliases
• You can provide the path for cross compiler
• You can add your own script which can start automaticallywhenever
new shell is opened.
• You can change the history length

/etc/bashrc
• Like .bash_profile you will also commonly see a .bashrc file in your home
directory. This file is meant for setting command aliases and functions used
by bash shell users.
• Just like the /etc/profile is the system wide version of .bash_profile. The
/etc/bashrc for Red Hat and /etc/bash.bashrc in Ubuntu is the system wide
version of .bashrc.
Interestingly enough in the Red Hat implementation the /etc/bashrc also executes
the shell scripts within /etc/profile.d but only if the users shell is a Interactive Shell
(aka Login Shell)

Environment Variables:

variable purpose
PATH The PATH variable holds a list of directories in a certain order. In this
list colon (:) separate different directories
HOME When a user logs in, he or she will be automatically placed in the home
directory. This directory is decided by the system administrator at the
time of opening an account for a user.This directory is stored in the file
/etc/passwd
IFS This variable holds tokens used by the shell commands to parse a string
into substrings such as a word or a record into its individual fields. The

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 12


UNIX OPERATING SYSTEM LAB MANUAL

default tokens are the three white space tokens Space, Tab, New line
MAIL This variable holds the absolute pathname of the file where user’s mail
is kept. Usually the name of this file is the user’s login name
SHELL This variable contains the name of the users shell program in the form of
absolute pathname. System administrator sets the default shell If
required, user can change it
TERM This variable holds the information regarding the type of the terminal
being used. If TERM is not set properly, utilities like vi editor will not
work

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 13


UNIX OPERATING SYSTEM LAB MANUAL

2. Aim:
Write a C Program that makes a copy of a file using standard I/O and system
calls.
Source Code:
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
void typefile (char *filename)
{
int fd, nread;
char buf[1024];
fd = open (filename, O_RDONLY);
if (fd == -1) {
perror (filename);
return;
}
while ((nread = read (fd, buf, sizeof (buf))) > 0)
write (1, buf, nread);
close (fd);
}
int main (int argc, char **argv)
{
int argno;
for (argno = 1; argno<argc; argno++)
typefile (argv[argno]);
}

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 14


UNIX OPERATING SYSTEM LAB MANUAL

Output:

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 15


UNIX OPERATING SYSTEM LAB MANUAL

3. Aim:
Write a C program to emulate the Unix ls -l command.
Source Code:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
int main()
{
int pid; //process id
pid = fork(); //create another process
if ( pid< 0 )
{ //fail
printf(“\nFork failed\n”);
exit (-1);
}
else if ( pid == 0 )
{ //child
execlp ( “/bin/ls”, “ls”, “-l”, NULL ); //execute ls
}
else
{ //parent
wait (NULL); //wait for child
printf(“\nchild complete\n”);
}

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 16


UNIX OPERATING SYSTEM LAB MANUAL

Output:

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 17


UNIX OPERATING SYSTEM LAB MANUAL

4. Aim:
Write a program that illustrates how to execute two commands concurrently with
a command pipe.
Source Code:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
int main()
{
int pfds[2];
char buf[30];
if(pipe(pfds)==-1)
{
perror("pipe failed");
exit(1);
}
if(!fork())
{
close(1);
dup(pfds[1]);
system (“ls –l”);
}
else
{
printf("parent reading from pipe \n");
while(read(pfds[0],buf,80))
printf("%s \n" ,buf);
}
}

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 18


UNIX OPERATING SYSTEM LAB MANUAL

Output:

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 19


UNIX OPERATING SYSTEM LAB MANUAL

5. Aim:
Write a C program to implement the various process scheduling mechanisms such
as
A)Round Robin
Scheduling. Source
Code: #include<stdio.h>
#include<conio.h>
main()
{
int st[10],bt[10],wt[10],tat[10],n,tq;
int i,count=0,swt=0,stat=0,temp,sq=0;
float awt=0.0,atat=0.0;
clrscr();
printf("Enter number of processes:");
scanf("%d",&n);
printf("Enter burst time for sequences:");
for(i=0;i<n;i++)
{
scanf("%d",&bt[i]);
st[i]=bt[i];
}
printf("Enter time quantum:");
scanf("%d",&tq);
while(1)
{
for(i=0,count=0;i<n;i++)
{
temp=tq;
if(st[i]==0)
{
count++;
continue;
}
if(st[i]>tq)

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 20


UNIX OPERATING SYSTEM LAB MANUAL

st[i]=st[i]-tq;
else
if(st[i]>=0)
{
temp=st[i];
st[i]=0;
}
sq=sq+temp;
tat[i]=sq;
}
if(n==count)
break;
}
for(i=0;i<n;i++)
{
wt[i]=tat[i]-bt[i];
swt=swt+wt[i];
stat=stat+tat[i];
}
awt=(float)swt/n;
atat=(float)stat/n;
printf("Process_no \t Burst time \t Wait time \t Turn around time\n");
for(i=0;i<n;i++)
printf("%d \t%d \t%d \t%d\n",i+1,bt[i],wt[i],tat[i]);
printf("Avg wait time is %f \n Avg turn around time is %f",awt,atat);
getch();
}

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 21


UNIX OPERATING SYSTEM LAB MANUAL

Output:

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 22


UNIX OPERATING SYSTEM LAB MANUAL

(b)
Aim:
Write a C program to implement the various process scheduling mechanisms such
as
SJF Scheduling.
Source Code:
#include<conio.h>
#include<stdio.h>
void main()
{
int i, j, n, process[10], total=0, wtime[10], ptime[10], temp, ptemp;
float avg=0;
clrscr();
printf("\nEnter number of Processes:");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\nEnter Process %d ID:",i+1);
scanf("%d", &process[i]);
printf("\nEnter Process %d Time:",i+1);
scanf("%d",&ptime[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(ptime[i]>ptime[j])
{
temp = ptime[i];
ptime[i] = ptime[j];
ptime[j] = temp;
ptemp = process[i];
process[i] = process[j];
process[j] = ptemp;
}
}
}

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 23


UNIX OPERATING SYSTEM LAB MANUAL

wtime[0]=0;
for(i=1;i<n;i++)
{
wtime[i]=wtime[i-1]+ptime[i-1];
total=total+wtime[i];
}
avg=(float)total/n;
printf("\nP_ID\t P_TIME\t W_TIME\n");
for(i=0;i<n;i++)
printf("%d\t %d\t %d\n",process[i],ptime[i],wtime[i]);
printf("\nTotal Waiting Time: %d \nAverage Waiting Time: %f", total, avg);
getch();
}

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 24


UNIX OPERATING SYSTEM LAB MANUAL

Output:

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 24


UNIX OPERATING SYSTEM LAB MANUAL

(c)
Aim:
Write a C program to implement the various process scheduling mechanisms.
Source Code:
#include<stdio.h>
void main()
{
int i,n,sum,wt,tat,twt,ttat;
int t[10];
float awt,atat;
clrscr();
printf("Enter number of processors:\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter the Burst Time of the process %d",i+1);
scanf("\n %d",&t[i]);
}
printf("\n\n FIRST COME FIRST SERVE SCHEDULING ALGORITHM \n");
printf("\n Process ID \t Waiting Time \t Turn Around Time \n");
printf("1 \t\t 0 \t\t %d \n",t[0]);
sum=0;
twt=0;
ttat=t[0];
for(i=1;i<n;i++)
{
sum+=t[i-1];
wt=sum;
tat=sum+t[i];
twt=twt+wt;
ttat=ttat+tat;
printf("\n %d \t\t %d \t\t %d",i+1,wt,tat);
printf("\n\n");
}
awt=(float)twt/n;
atat=(float)ttat/n;
printf("\n Average Waiting Time %4.2f",awt);

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 25


UNIX OPERATING SYSTEM LAB MANUAL

printf("\n Average Turnaround Time %4.2f",atat);


getch();
}

Output:

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 26


UNIX OPERATING SYSTEM LAB MANUAL

(d)
Aim:
Write a C program to implement the various process scheduling mechanisms such
as
Priority Scheduling.
Source Code:
#include <stdio.h>
#include <conio.h>
void main()
{
int i,j,n,tat[10],wt[10],bt[10],pid[10],pr[10],t,twt=0,ttat=0;
float awt,atat;
clrscr();
printf("\n-----------PRIORITY SCHEDULING ------------- \n");
printf("Enter the No of Process: ");
scanf("%d", &n);
for (i=0;i<n;i++)
{
pid[i] = i;
printf("Enter the Burst time of Pid %d : ",i);
scanf("%d",&bt[i]);
printf("Enter the Priority of Pid %d : ",i);
scanf ("%d",&pr[i]);
}
for (i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
if (pr[i] > pr[j] )
{
t = pr[i];
pr[i] = pr[j];
pr[j] = t;
t = bt[i];
bt[i] = bt[j];
bt[j] = t;
t = pid[i];
pid[i] = pid[j];

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 27


UNIX OPERATING SYSTEM LAB MANUAL

pid[j] = t;
}
}
tat[0] = bt[0];
wt[0] = 0;
for (i=1;i<n;i++)
{
wt[i] = wt[i-1] + bt[i-1];
tat[i] = wt[i] + bt[i];
}
printf("\n \n");
printf("Pid\t Priority\tBurst time\t WaitingTime\tTurnArroundTime\n");
printf("\n -\n");
for(i=0;i<n;i++)
{
printf("\n%d\t\t%d\t%d\t\t%d\t\t%d",pid[i],pr[i],bt[i],wt[i],tat[i]);
}
for(i=0;i<n;i++)
{
ttat = ttat+tat[i];
twt = twt + wt[i];
}
awt = (float)twt / n;
atat = (float)ttat / n;
printf("\n\nAvg.Waiting Time: %f\nAvg.Turn Around Time: %f\n",awt,atat);
getch();
}

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 28


UNIX OPERATING SYSTEM LAB MANUAL

Output:

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 29


UNIX OPERATING SYSTEM LAB MANUAL

6. Aim:
Write a program for Multiprogramming-Memory management- Implementation of
fork (), wait (), exec() and exit (), System calls
Source Code:
#include<stdio.h>
#include<unistd.h>
main( )
{
int i, pid;
pid=fork( );
if(pid== -1)
{
perror(“fork failed”);
exit(0);
}
else if(pid==0)
{
printf(“\n Childprocess starts‟);
for(i=0; i<5; i++)
{
printf(“\n Child process %d is called”, i);
}
printf(“\n Child process ends”);
}
else
{
wait(0);
printf(“\n Parent process ends”);
}
exit(0);
}

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 30


UNIX OPERATING SYSTEM LAB MANUAL

Output:

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page31


UNIX OPERATING SYSTEM LAB MANUAL

7. Aim:
aWrite a program to implement Dynamic allocation of memories in MVT.
Source Code:
#include<stdio.h>
#include<conio.h>
main()
{
int i,m,n,tot,s[20];
clrscr();
printf("Enter total memory size:");
scanf("%d",&tot);
printf("Enter no. of pages:");
scanf("%d",&n);
printf("Enter memory for OS:");
scanf("%d",&m);
for(i=0;i<n;i++)
{
printf("Enter size of page%d:",i+1);
scanf("%d",&s[i]);
}
tot=tot-m;
for(i=0;i<n;i++)
{
if(tot>=s[i])
{
printf("Allocate page %d\n",i+1);
tot=tot-s[i];
}
else
printf("process p%d is blocked\n",i+1);
}
printf("External Fragmentation is=%d",tot);
getch();
}

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 32


UNIX OPERATING SYSTEM LAB MANUAL

Output:

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 33


UNIX OPERATING SYSTEM LAB MANUAL

(b)
Aim:
Write a program to implement Dynamic allocation of memories in MVT.
Source Code:
#include<stdio.h>
#include<conio.h>
main()
{
int ms,i,ps[20],n,size,p[20],s,intr=0;
clrscr();
printf("Enter size of memory:");
scanf("%d",&ms);
printf("Enter memory for OS:");
scanf("%d",&s);
ms-=s;
printf("Enter no.of partitions to be divided:");
scanf("%d",&n);
size=ms/n;
for(i=0;i<n;i++)
{
printf("Enter process and process size");
scanf("%d%d",&p[i],&ps[i]);
if(ps[i]<=size)
{
intr=intr+size-ps[i];
printf("process%d is allocated\n",p[i]);
}
else
printf("process%d is blocked",p[i]);
}
printf("total fragmentation is %d",intr);
getch();
}

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 34


UNIX OPERATING SYSTEM LAB MANUAL

Output:

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 35


UNIX OPERATING SYSTEM LAB MANUAL

8&9Aim:
To implement deadlock avoidance & Prevention by using Banker’s Algorithm.
Deadlock avoidance & Dead Lock Prevention
Source Code:
#include<stdio.h>
#include<conio.h>
struct da
{
int max[10],a1[10],need[10],before[10],after[10];
}p[10];
void main()
{
int i,j,k,l,r,n,tot[10],av[10],cn=0,cz=0,temp=0,c=0;
clrscr();
printf("\n ENTER THE NO. OF PROCESSES:");
scanf("%d",&n);
printf("\n ENTER THE NO. OF RESOURCES:");
scanf("%d",&r);
for(i=0;i<n;i++)
{
printf("PROCESS %d \n",i+1);
for(j=0;j<r;j++)
{
printf("MAXIMUM VALUE FOR RESOURCE %d:",j+1);
scanf("%d",&p[i].max[j]);
}
for(j=0;j<r;j++)
{
printf("ALLOCATED FROM RESOURCE %d:",j+1);
scanf("%d",&p[i].a1[j]);
p[i].need[j]=p[i].max[j]-p[i].a1[j];
}
}
for(i=0;i<r;i++)
{
printf("ENTER TOTAL VALUE OF RESOURCE %d:",i+1);

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 36


UNIX OPERATING SYSTEM LAB MANUAL

scanf("%d",&tot[i]);
}
for(i=0;i<r;i++)
{
for(j=0;j<n;j++)
temp=temp+p[j].a1[i];
av[i]=tot[i]-temp;
temp=0;
}
printf("\n\t RESOURCES ALLOCATED NEEDED TOTAL AVAIL");
for(i=0;i<n;i++)
{
printf("\n P%d \t",i+1);
for(j=0;j<r;j++)
printf("%d",p[i].max[j]);
printf("\t");
for(j=0;j<r;j++)
printf("%d",p[i].a1[j]);
printf("\t");
for(j=0;j<r;j++)
printf("%d",p[i].need[j]);
printf("\t");
for(j=0;j<r;j++)
{
if(i==0)
printf("%d",tot[j]);
}
printf(" ");
for(j=0;j<r;j++)
{
if(i==0)
printf("%d",av[j]);
}
}
printf("\n\n\t AVAIL BEFORE\T AVAIL AFTER ");
for(l=0;l<n;l++)
{

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page37


UNIX OPERATING SYSTEM LAB MANUAL

for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
if(p[i].need[j] >av[j])
cn++;
if(p[i].max[j]==0)
cz++;
}
if(cn==0 && cz!=r)
{
for(j=0;j<r;j++)
{
p[i].before[j]=av[j]-p[i].need[j];
p[i].after[j]=p[i].before[j]+p[i].max[j];
av[j]=p[i].after[j];
p[i].max[j]=0;
}
printf("\n P %d \t",i+1);
for(j=0;j<r;j++)
printf("%d",p[i].before[j]);
printf("\t");
for(j=0;j<r;j++)
printf("%d",p[i].after[j]);
cn=0;
cz=0;
c++;
break;
}
else
{
cn=0;cz=0;
}
}
}
if(c==n)
printf("\n THE ABOVE SEQUENCE IS A SAFE SEQUENCE");

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page38


UNIX OPERATING SYSTEM LAB MANUAL

else
printf("\n DEADLOCK OCCURED");
getch();
}

Output:

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page39


UNIX OPERATING SYSTEM LAB MANUAL

10Aim:
To implement page replacement algorithms a)FIFO (First In First Out)
Source code:
#include<stdio.h>
#include<conio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
void main()
{
clrscr();
printf("\n \t\t\t FIFI PAGE REPLACEMENT ALGORITHM");
printf("\n Enter no.of frames ... ");
scanf("%d",&nof);
printf("Enter number of reference string..\n");
scanf("%d",&nor);
printf("\n Enter the reference string..");
for(i=0;i<nor;i++)
scanf("%d",&ref[i]);
printf("\nThe given reference string:");
for(i=0;i<nor;i++)
printf("%4d",ref[i]);
for(i=1;i<=nof;i++)
frm[i]=-1;
printf("\n");
for(i=0;i<nor;i++)
{
flag=0;
printf("\n\t Reference np%d->\t",ref[i]);
for(j=0;j<nof;j++)
{
if(frm[j]==ref[i])
{
flag=1;
break;
}
}
if(flag==0)

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page40


UNIX OPERATING SYSTEM LAB MANUAL

{
pf++;
victim++;
victim=victim%nof;
frm[victim]=ref[i];
for(j=0;j<nof;j++)
printf("%4d",frm[j]);
}
}
printf("\n\n\t\t No.of pages faults...%d",pf);
getch();
}
Output:

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page41


UNIX OPERATING SYSTEM LAB MANUAL

(b)
Aim:
To implement page replacement algorithm LRU (Least Recently Used)
Source code:
#include<stdio.h>
#include<conio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
int recent[10],lrucal[50],count=0;
int lruvictim();
void main()
{
clrscr();
printf("\n\t\t\t LRU PAGE REPLACEMENT ALGORITHM");
printf("\n Enter no.of Frames ... ");
scanf("%d",&nof);
printf(" Enter no.of reference string..");
scanf("%d",&nor);
printf("\n Enter reference string..");
for(i=0;i<nor;i++)
scanf("%d",&ref[i]);
printf("\n\n\t\t LRU PAGE REPLACEMENT ALGORITHM ");
printf("\n\t The given reference string:");
printf("\n ..................................................");
for(i=0;i<nor;i++)
printf("%4d",ref[i]);
for(i=1;i<=nof;i++)
{
frm[i]=-1;
lrucal[i]=0;
}
for(i=0;i<10;i++)
recent[i]=0;
printf("\n");
for(i=0;i<nor;i++)
{
flag=0;
printf("\n\t Reference NO %d->\t",ref[i]);
SREE VENKATESHWARA COLLEGE OF ENGINEERING Page42
UNIX OPERATING SYSTEM LAB MANUAL

for(j=0;j<nof;j++)
{
if(frm[j]==ref[i])
{
flag=1;
break;
}
}
if(flag==0)
{
count++;
if(count<=nof)
victim++;
else
victim=lruvictim();
pf++;
frm[victim]=ref[i];
for(j=0;j<nof;j++)
printf("%4d",frm[j]);
}
recent[ref[i]]=i;
}
printf("\n\n\t No.of page faults...%d",pf);
getch();
}
int lruvictim()
{
int i,j,temp1,temp2;
for(i=0;i<nof;i++)
{
temp1=frm[i];
lrucal[i]=recent[temp1];
}
temp2=lrucal[0];
for(j=1;j<nof;j++)
{
if(temp2>lrucal[j])

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page43


UNIX OPERATING SYSTEM LAB MANUAL

temp2=lrucal[j];
}
for(i=0;i<nof;i++)
if(ref[temp2]==frm[i])
return i;
return 0;
}

Output:

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page44


UNIX OPERATING SYSTEM LAB MANUAL

(c)
Aim:
To implement page replacement algorithms like LFU (Least Frequently Used)
Source Code:
#include<stdio.h>
int main()
{
int total_frames, total_pages, hit = 0;
int pages[25], frame[10], arr[25], time[25];
int m, n, page, flag, k, minimum_time, temp;
printf("Enter Total Number of Pages:\t");
scanf("%d", &total_pages);
printf("Enter Total Number of Frames:\t");
scanf("%d", &total_frames);
for(m = 0; m < total_frames; m++)
{
frame[m] = -1;
}
for(m = 0; m < 25; m++)
{
arr[m] = 0;
}
printf("Enter Values of Reference String\n");
for(m = 0; m < total_pages; m++)
{
printf("Enter Value No.[%d]:\t", m + 1);
scanf("%d", &pages[m]);
}
printf("\n");
for(m = 0; m < total_pages; m++)
{
arr[pages[m]]++;
time[pages[m]] = m;
flag = 1;
k = frame[0];
for(n = 0; n < total_frames; n++)
{

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page45


UNIX OPERATING SYSTEM LAB MANUAL

if(frame[n] == -1 || frame[n] == pages[m])


{
if(frame[n] != -1)
{
hit++;
}
flag = 0;
frame[n] = pages[m];
break;
}
if(arr[k] > arr[frame[n]])
{
k = frame[n];
}
}
if(flag)
{
minimum_time = 25;
for(n = 0; n < total_frames; n++)
{
if(arr[frame[n]] == arr[k] && time[frame[n]] < minimum_time)
{
temp = n;
minimum_time = time[frame[n]];
}
}
arr[frame[temp]] = 0;
frame[temp] = pages[m];
}
for(n = 0; n < total_frames; n++)
{
printf("%d\t", frame[n]);
}
printf("\n");
}
printf("Page Hit:\t%d\n", hit);
return 0;

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page46


UNIX OPERATING SYSTEM LAB MANUAL

Output:

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page47


UNIX OPERATING SYSTEM LAB MANUAL

11. Aim:
Write a C Program to implement
a)Sequential File Allocation method.
Source Code:
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,j,b[20],sb[20],t[20],x,c[20][20];
clrscr();
printf("Enter no.of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter no. of blocks occupied by file%d",i+1);
scanf("%d",&b[i]);
printf("Enter the starting block of file%d",i+1);
scanf("%d",&sb[i]);
t[i]=sb[i];
for(j=0;j<b[i];j++)
c[i][j]=sb[i]++;
}
printf("Filename\tStart block\tlength\n");
for(i=0;i<n;i++)
printf("%d\t %d \t%d\n",i+1,t[i],b[i]);
printf("Enter file name:");
scanf("%d",&x);
printf("File name is:%d",x);
printf("length is:%d",b[x-1]);
printf("blocks occupied:");
for(i=0;i<b[x-1];i++)
printf("%4d",c[x-1][i]);
getch();
}

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page48


UNIX OPERATING SYSTEM LAB MANUAL

Output:

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page49


UNIX OPERATING SYSTEM LAB MANUAL

(b)
Aim:
Write a C Program to implement Indexed File Allocation method.
Source Code:
#include<stdio.h>
#include<conio.h>
main()
{
int n,m[20],i,j,sb[20],s[20],b[20][20],x;
clrscr();
printf("Enter no. of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{ printf("Enter starting block and size of file%d:",i+1);
scanf("%d%d",&sb[i],&s[i]);
printf("Enter blocks occupied by file%d:",i+1);
scanf("%d",&m[i]);
printf("enter blocks of file%d:",i+1);
for(j=0;j<m[i];j++)
scanf("%d",&b[i][j]);
} printf("\nFile\t index\tlength\n");
for(i=0;i<n;i++)
{
printf("%d\t%d\t%d\n",i+1,sb[i],m[i]);
}printf("\nEnter file name:");
scanf("%d",&x);
printf("file name is:%d\n",x);
i=x-1;
printf("Index is:%d",sb[i]);
printf("Block occupied are:");
for(j=0;j<m[i];j++)
printf("%3d",b[i][j]);
getch();
}

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page50


UNIX OPERATING SYSTEM LAB MANUAL

Output:

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page51


UNIX OPERATING SYSTEM LAB MANUAL

(c)
Aim:
Write a C Program to implement Linked File Allocation method.
Source Code:
#include<stdio.h>
#include<conio.h>
struct file
{
char fname[10];
int start,size,block[10];
}f[10];
main()
{
int i,j,n;
clrscr();
printf("Enter no. of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter file name:");
scanf("%s",&f[i].fname);
printf("Enter starting block:");
scanf("%d",&f[i].start);
f[i].block[0]=f[i].start;
printf("Enter no.of blocks:");
scanf("%d",&f[i].size);
printf("Enter block numbers:");
for(j=1;j<=f[i].size;j++)
{
scanf("%d",&f[i].block[j]);
}
}
printf("File\tstart\tsize\tblock\n");
for(i=0;i<n;i++)
{
printf("%s\t%d\t%d\t",f[i].fname,f[i].start,f[i].size);
for(j=1;j<=f[i].size-1;j++)

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page52


UNIX OPERATING SYSTEM LAB MANUAL

printf("%d--->",f[i].block[j]);
printf("%d",f[i].block[j]);
printf("\n");
}
getch();
}

Output:

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page53


UNIX OPERATING SYSTEM LAB MANUAL

12. Aim:
Write a C program that illustrates two processes communicating using shared
memory.
Source Code:
#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
Struct country
{
char name[30];
char capital_city [30];
char currency[30];
int population;
};
int main(intargc,char*argv[])
{
int shm_id;
char*shm_addr;
int*countries_num;
struct country*countries;
structshmid_dsshm_desc;
shm_id=shmget(100,2048,IPC_CREAT|IPC_EXCL\0600);
if(shm_id==-1){
perror(“main:shmget:”);
exit(1);
}
shm_addr=shmat(shm_id,NULL,0);
if(!shm_addr){
ierror(“main:shmat:”);
exit(1);
}
SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 56
UNIX OPERATING SYSTEM LAB MANUAL

countries_num=(int*)shm_addr;
*countries_num=0;
countries=(struct country*)((void*)shm_addrsizeof(int));
strcpy(countries[0],name,”U.S.A”);
strcpy(countries[0],capital_city,”WASHINGTON”);
strcpy(countries[0],currency,”U.S.DOLLAR”);
countries[0].population=250000000;
( countries_num) ;
strcpy(countries[1].name,”israel”);
strcpy(countries[1].capital_city,”jerushalem”);
strcpy(countries[1].currency,”NEW ISRAEL SHEKED”);
countries[1].population=6000000;
(*countries_num) ;
strcpy(countries[2].name,”France”);
strcpy(countries[2].capital_city,”paris”);
strcpy(countries[2].currency,”Frank”);
countries[2].population=60000000;
(*countries_num) ;
for(i=0;i<(*countries_num);i )
{
printf(“country%d:\n”,i 1);
printf(“name:%d:\n”,i 1);
printf(“currency:%s:\n”,countries[i].currency);
printf(“population:%d:\n”,countries[i].population);
}
if(shmdt(shm_addr)==-1){
perror(“main:shmdt:”);
}
if(shmctl(shm_id,IPC_RMID,&SHM_DESC)==-1)
{
perror(“main:shmctl:”);
}
return 0;
SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 57
UNIX OPERATING SYSTEM LAB MANUAL

Output:

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 58


UNIX OPERATING SYSTEM LAB MANUAL

13. Aim:
Write a C program to simulate producer and consumer problem using semaphores
Source Code:
#include<stdio.h>
int mutex=1,full=0,empty=3,x=0;
main()
{
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n1.PRODUCER\n2.CONSUMER\n3.EXIT\n");
while(1)
{
printf("\nENTER YOUR CHOICE\n");
scanf("%d",&n);
switch(n)
{
case 1:
if((mutex==1)&&(empty!=0))
producer();
else
printf("BUFFER IS FULL");
break;
case 2:
if((mutex==1)&&(full!=0))
consumer();
else
printf("BUFFER IS EMPTY");
break;
case 3:
exit(0);
break;
}
SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 59
UNIX OPERATING SYSTEM LAB MANUAL

}
}
int wait(int s)
{
return(--s);
}
int signal(int s)
{
return(++s);
}
void producer()
{
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf("\nproducer produces the item%d",x);
mutex=signal(mutex);
}
void consumer()
{
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\n consumer consumes item%d",x);
x--;
mutex=signal(mutex);
}

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 60


UNIX OPERATING SYSTEM LAB MANUAL

Output:

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 61


UNIX OPERATING SYSTEM LAB MANUAL

14. Aim:
Write C program to create a thread using pthreads library and let it run its function
Source Code:

#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
void *mythread(void *vargp)
{
sleep(1);
printf("welcome to PThreads Library\n");
return NULL;
}
int main()
{
pthread_tid;
printf("before thread\n");
pthread_create(&tid,NULL,mythread,NULL);
pthread_join(tid,NULL);
exit(0);
}
Output:

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 62


UNIX OPERATING SYSTEM LAB MANUAL

15. Aim:
Write a C program to illustrate concurrent execution of threads using pthreads
Library
Source Code:
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
void *mythread1(void *vargp)
{
int i;
printf("thread1\n");
for(i=1;i<=10;i++)
printf("i=%d\n",i);
printf("exit from thread1\n");
return NULL;
}
void *mythread2(void *vargp)
{
int j;
printf("thread2 \n");
for(j=1;j<=10;j++)
printf("j=%d\n",j);
printf("Exit from thread2\n");
return NULL;
}
int main()
{
pthread_tid;
printf("before thread\n");
pthread_create(&tid,NULL,mythread1,NULL);
pthread_create(&tid,NULL,mythread2,NULL);
pthread_join(tid,NULL);
pthread_join(tid,NULL)
}

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 63


UNIX OPERATING SYSTEM LAB MANUAL
Output:

SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 64

You might also like