0% found this document useful (0 votes)
25 views13 pages

Screenshot 2024-06-12 at 8.01.22 AM

Uploaded by

narayana.guttula
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)
25 views13 pages

Screenshot 2024-06-12 at 8.01.22 AM

Uploaded by

narayana.guttula
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/ 13

UNIX Lab-Introduction to Unix

Experiment 1:
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.
1. man - view manual pages for UNIX commands
Syntax: man [-s section] item
$ man cat
2. who - displays the list of users logged presently
Syntax: who [option]… [file][arg1]
$ who
3. cat - concatenate files and show contents to the standard output
Syntax: cat [option]…[file]
$ cat >
file1
hello
4. cd - change directory
Syntax: cd [option] directory
$ cd dir1
5. cp - copy files
Syntax: cp [option] source destination
$ cp file1 file2
6. ps - Reports process status
Syntax: Ps [options]
$ ps
7. ls - List files & directories
Syntax: ls [option] [file]
$ ls
8. mv - Rename or move files & directories to another location
Syntax: mv [option] source destination
$ mv file1 file2
9. rm - removes files & directories
Syntax: rm [option]…[file]
$ rm file1
10. mkdir - makes new directory
Syntax: mkdir [option] directory
$ mkdir dir1
11. rmdir - removes directory

Syntax: rmdir [option] directory


$ rmdir dir1
12. echo - displays a line of text to the standard output
Syntax: $ echo "Hello, World!"
$ echo $x
13. more - basic pagination when viewing text files or parsing UNIX commands output
Syntax: more [-options] [-num] [+/pattern] [+linenum] [file_name]
$ more +/reset sample.txt
$ more +30 sample.txt
14. date - Show current date & time
Syntax: date [+format]
$ date +%d/%m/%y
15. time - shows current
time
$ time
16. kill - terminates a specified process
Syntax: Kill PID
$ kill 4901
17. history - displays history commands in the current session
Syntax: history [options]
$ history
18. chmod - change file / directory access permissions
Symbolic
mode
Syntax: chmod [ugoa][[+-=][mode]] file
• The first optional parameter indicates who – this can be (u)ser, (g)roup, (o)thers or (a)ll
• The second optional parameter indicates opcode – this can be for adding (+), removing (-
) or assigning (=) a permission.
• The third optional parameter indicates the mode – this can be (r)ead, (w)rite, or e(x)ecute.
$ chmod o-w file1
Numeric mode
Syntax: chmod [mode] file
• The mode is a combination of three digits – the first digit indicates the permission for
the user, the second digit for the group, and the third digit for others.
• Each digit is computed by adding the associated permissions. Read permission is
‘4’, write permission is ‘2’ and execute permission is ‘1’.
$ chmod 777 file1
19. chown - change file / directory ownership
Syntax: chown [owner] [file]

$ chown user2 file1


20. finger - displays user info if login is known
Syntax: finger username
21. pwd - confirm current directory
Syntax: pwd [option]
$ pwd
22. cal - displays calendar
Syntax: cal [[month] year]
$ cal 6 2019
23. logout - logs off UNIX
Syntax: Logout [options]
$ logout
24. shutdown - brings the system down in a secure way
Syntax: shutdown [options]
$ shutdown

Experiment 2:
Study of Bash shell, Bourne shell and C shell in Unix/Linux operating system.
What Is a Shell?
A shell is a program that provides an interface between a user and an operating system (OS)
kernel.
An OS starts a shell for each user when the user logs in or opens a terminal or console
window. A kernel is a program that:
• Controls all computer operations.
• Coordinates all executing utilities
• Ensuresthat executing utilities do not interfere with each other or consume all system
resources.
• Schedules and manages all system processes.
By interfacing with a kernel, a shell provides a way for a user to execute utilities and programs.

Types of Shell

1. Bourne Shell (sh)


• The Bourne shell is one of the original shells, developed for UNIX computers by
Stephen Bourne at AT&T Bell Labs in 1977.
• It offers features such as input and output redirection, shell scripting with string
and integer variables, and condition testing and looping.
• Command full-path name is /bin/sh and /sbin/sh.
• Default prompt for a non-root user is $.
• Default prompt for a root user is #.
2. C Shell (csh)
• The C shell was designed to allow users to write shell script programs using
syntax very similar to that of the C programming language.
• The C shell is a UNIX shell created by Bill Joy.
• The C shell is a command processor typically run in a text window, allowing the
user to type commands.
• Command full-path name is /bin/csh.
• Default prompt for a non-root user is hostname %.
• Default prompt for a root user is hostname #.
3. Bourne Again Shell (bash)
• The default Linux shell.
• Backward-compatible with the original sh UNIX shell.
• Bash is largely compatible with sh and incorporates useful features from the
Korn shell ksh and the C shell csh.
• Command full-path name is /bin/bash.
• Default prompt for a non-root user is bash-x.xx$. (Where x.xx indicates the shell
version number. For example, bash-3.50$)
• Root user default prompt is bash-x.xx#. (Where x.xx indicates the shell version
number. For example, bash-3.50$#)

Experiment 3:

Study of Unix/Linux files system (tree


structure). Unix/Linux files system (tree
structure)
▪ UNIX file system is a logical method of organizing and storing large amounts of
information in a way that makes it easy to manage.
▪ All data in UNIX is organized into files. All files are organized into directories. These
directories are organized into a tree-like structure called the file system.
▪ A file is a smallest unit in which the information is stored.
▪ Files in Unix System are organized into multi-level hierarchy structure known as a
directory tree. At the very top of the file system is a directory called “root” which is
represented by a “/”. All other files are “descendants” of root.
1. / – Root
▪ Every single file and directory starts from the root directory.
▪ Only root user has write privilege under this directory.
▪ Please note that /root is root user’s home directory, which is not same as /.
2. /bin – User Binaries
▪ Contains binary executables.
▪ Common linux commands you need to use in single-user modes are located under this
directory.
▪ Commands used by all the users of the system are located here.
▪ For example: ps, ls, ping, grep, cp.
3. /sbin – System Binaries
▪ Just like /bin, /sbin also contains binary executables.
▪ But, the linux commands located under this directory are used typically by system
aministrator, for system maintenance purpose.
▪ For example: iptables, reboot, fdisk, ifconfig, swapon
4. /etc – Configuration Files
▪ Contains configuration files required by all programs.
▪ This also contains startup and shutdown shell scripts used to start/stop individual
programs.
▪ For example: /etc/resolv.conf, /etc/logrotate.conf

5. /dev – Device Files


▪ Contains device files.
▪ These include terminal devices, usb, or any device attached to the system.
▪ For example: /dev/tty1, /dev/usbmon0
6. /proc – Process Information
▪ Contains information about system process.
▪ This is a pseudo filesystem contains information about running process. For example:
/proc/{pid} directory contains information about the process with that particular pid.

▪ This is a virtual filesystem with text information about system resources. For example:
/proc/uptime
7. /var – Variable Files
▪ var stands for variable files.
▪ Content of the files that are expected to grow can be found under this directory.
▪ This includes — system log files (/var/log); packages and database files (/var/lib);
emails (/var/mail); print queues (/var/spool); lock files (/var/lock); temp files needed
across reboots (/var/tmp);
8. /tmp – Temporary Files
▪ Directory that contains temporary files created by system and users.
▪ Files under this directory are deleted when system is rebooted.
9. /usr – User Programs
▪ Contains binaries, libraries, documentation, and source-code for second level programs.
▪ /usr/bin contains binary files for user programs. If you can’t find a user binary under
/bin, look under /usr/bin. For example: at, awk, cc, less, scp
▪ /usr/sbin contains binary files for system administrators. If you can’t find a system
binary under /sbin, look under /usr/sbin. For example: atd, cron, sshd, useradd, userdel
▪ /usr/lib contains libraries for /usr/bin and /usr/sbin
▪ /usr/local contains users programs that you install from source. For example, when you
install apache from source, it goes under /usr/local/apache2
10. /home – Home Directories
▪ Home directories for all users to store their personal files.
▪ For example: /home/john, /home/nikita
11. /boot – Boot Loader Files
▪ Contains boot loader related files.
▪ Kernel initrd, vmlinux, grub files are located under /boot
▪ For example: initrd.img-2.6.32-24-generic, vmlinuz-2.6.32-24-generic
12. /lib – System Libraries
▪ Contains library files that supports the binaries located under /bin and /sbin
▪ Library filenames are either ld* or lib*.so.*
▪ For example: ld-2.11.1.so, libncurses.so.5.7
13. /opt – Optional add-on Applications
▪ opt stands for optional.
▪ Contains add-on applications from individual vendors.
▪ Add-on applications should be installed under either /opt/ or /opt/ sub-directory.
14. /mnt – Mount Directory
▪ Temporary mount directory where sysadmins can mount filesystems.
15. /media – Removable Media Devices

▪ Temporary mount directory for removable devices.


▪ For examples, /media/cdrom for CD-ROM;
/media/floppy for floppy drives;
/media/cdrecorder for CD writer
16. /srv – Service Data
▪ srv standsfor service.
▪ Contains server specific services related data.
▪ For example, /srv/cvs contains CVS related data.

Experiment 4:
Write a C program to emulate the UNIX ls –l Command.
Program:
#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”);
exit (0);
}
}
Output:
guest-glcbIs@ubuntu:~$gcc –o lsc.out lsc.c
guest-glcbIs@ubuntu:~$./lsc.out
total 100
-rwxrwx—x 1 guest-glcbls guest-glcbls 140 2012-07-06 14:55 f1
drwxrwxr-x 4 guest-glcbls guest-glcbls 140 2012-07-06 14:40 dir1
child complete

Experiment 5:
Write a C program that illustrates how to execute two commands concurrently with a
command pipe. Ex: - ls –l | sort
Program:

#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);
}
}

Output:
[student@gcet ~]$ vi pipes2.c
[student@gcet ~]$ cc pipes2.c
[student@gcet ~]$ ./a.out
Parent reading from pipe
Total 24
-rwxrwxr-x l student student 5563Aug 3 10:39 a.out
-rw-rw-r—l
Student student 340 jul 27 10:45 pipe2.c
-rw-rw-r—l student student
Pipes2.c
-rw-rw-r—l student student 401 34127 10:27 pipe2.c
student
Experiment 6:
Multiprogramming Memory management Implementation of
fork(),wait(),exec() and exit(),system calls

FORK():

PROGRAM:

/* C Program to implement Fork() system calls */

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
void forkexample()
{
if (fork() == 0)
printf("Hello from Child!\n");

else
printf("Hello from Parent!\n");
}
int main()
{
forkexample();
return 0;
}

OUTPUT:

Hello from Child!


Hello from Parent!
EXEC():

/* C Program to implement Exec() system calls */

PROGRAM:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/wait.h>
int main()
{
pid_t pid;
int ret = 1;
int status;
pid = fork();
if (pid == -1)
{

printf("can't fork, error occured\n");


exit(EXIT_FAILURE);
}
else if (pid == 0)
{
printf("child process, pid = %u\n",getpid());
execv("ls",argv_list);
exit(0);
}
else{
printf("parent process, pid = %u\n",getppid());
if (waitpid(pid, &status, 0) > 0)
{
if (WIFEXITED(status) && !WEXITSTATUS(status))
printf("program execution successfull\n");
else if (WIFEXITED(status) && WEXITSTATUS(status))
{
if (WEXITSTATUS(status) == 127)
{
printf("execv failed\n");
}
else
printf("program terminated normally,"
" but returned a non-zero status\n");
}
else
printf("program didn't terminate normally\n");
}
else
{
printf("waitpid() failed\n");
}
exit(0);
}
return 0;
}
OUTPUT:

parent process, pid = 11523


child process, pid = 14188
Program execution successfull
WAIT() :

PROGRAM:

/* C Program to implement Wait() system calls */

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

int main()
{
pid_t cpid;
if (fork()== 0)
exit(0);
else
cpid = wait(NULL);
printf("Parent pid = %d\n", getpid());
printf("Child pid = %d\n", cpid);

return 0;
}

EXIT():

PROGRAM:

/* C Program to implement Exit() system calls */

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
printf("START");
exit(0);
printf("End of program");
}

OUTPUT:

START

Operating System Lab

1. Simulate the Following CPU Scheduling Algorithms

A) FCFS B) SJF C) Priority D) Round Robin

FCFS:
#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);
getch();

OUTPUT:
Enter number of processors:
3
Enter the Burst Time of the process: 124 Enter the Burst Time of the process: 23 Enter the Burst Time of the
process: 33
FIRST COME FIRST SERVE SCHEDULING ALGORITHM

Process ID Waiting Time Turn Around Time

1 0 24

2 24 27

3 27 30
Average Waiting Time: 17.00 Average Turnaround Time: 27.00

SJF (Shortest Job First) Program:


#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;

}
}
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();
}

OUTPUT:
Enter number of Processes: 4 Enter Process 1 ID: 1
Enter Process 1 Time: 6
Enter Process 2 ID: 2
Enter Process 2 Time: 8
Enter Process 3 ID: 3
Enter Process 3 Time: 7
Enter Process 4 ID: 4
Enter Process 4 Time: 3
P_I D P_TIM E W_TIM E

4 3 0

1 6 3

3 7 9

2 8 16
Total Waiting Time: 28
Average Waiting Time: 7.000000

.priority Program:

#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];
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();
}

OUTPUT:
-----------PRIORITY SCHEDULING--------------
Enter the No of Process: 5
Enter the Burst time of Pid_0: 10
Enter the Priority of Pid_0: 3
Enter the Burst time of Pid_1: 1
Enter the Priority of Pid_1: 1
Enter the Burst time of Pid_2:
2 Enter the Priority of Pid_2:
4 Enter the Burst time of Pid_3: 1
Enter the Priority of Pid_3: 5
Enter the Burst time of Pid_4: 5
Enter the Priority of Pid_4: 2
--------------------------------------------------------------------------------------------------

Pid Priority Burst time WaitingTime TurnArroundTime


--------------------------------------------------------------------------------------------------

1 1 1 0 1

4 2 5 1 6

0 3 10 6 16

2 4 2 16 18

3 5 1 18 19
--------------------------------------------------------------------------------------------------
Avg.Waiting Time: 8.200000
Avg.Turn around Time: 12.000000

a) Round Robin

Program: #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) 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();
}

OUTPUT:
Enter number of processes: 3
Enter burst time for sequences: 24 3 3
Enter time quantum: 4

Process_no Burst time Wait time Turnaround time

1 24 6 30

2 3 4 7

3 3 7 10
Avg wait time is 5.666667
Avg turn around time is 15.666667

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy