Вы находитесь на странице: 1из 25

Sinhgad Technical Education Society's

RMD Sinhgad School of Engineering,Warje

Lab Manual on Operating System and Administration Laboratory


Prepared by

Mukund P. Deshmukh,
Assistant Professor, Department of Computer Engineering, RMD Sinhgad School of Engineering, Warje, Pune.

List of Assignments
Sr.No.
1 2

Title of Assignment
Implementation of Create/ rename/ delete a le using Unix/Linux commands Write a function to display the list of devices connected to your system including the physical names and its instance number. Write a function using mount and unmount command to mount device and un-mount it.

3 4

Adding users and access rights Implement the commands for creation and deletion of directory. Write a program to change current working directory and display the node details for each le in the new directory.

5 6

Process related commands list the processes for the current shell, Display information about processes, Display the global priority of a process, change the priority of a process with default arguments. Use Operating system Commands to obtain the following results To print the name of operating system To print the login name To print the host name

7 8 9 10 11 12 13

Write a C/C++ script to display all logged in users. Java Program to display the list of devices connected to your system including the physical names and its instance number. C/C++ Program to Parent creating the child process by use of fork. Java Program to Identify the available memory in the system. Python script to display looged user info Write a shell program to convert all lowercase letter in a le to uppercase letter. Write bash script to check given name is le or directory.

List of Figures
1 2 3 4 5 6 7 8 9 10 11 12 Write a C/C++ script to display all logged in users . . . . . . . . . . . . . . . . . . . . . 13 14 14 15 16 16 17 17 19 20 21 22 Java program to display the list of devices . . . . . . . . . . . . . . . . . . . . . . . . . . . Java program to display the list of devices . . . . . . . . . . . . . . . . . . . . . . . . . . . Fork Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Simple Fork Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Parent Child Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Fork Example1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Parent Child Process Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Java Program to Identify the available memory in the system . . . . . . . . . . . . . . . . Python script to display looged user info . . . . . . . . . . . . . . . . . . . . . . . . . . . . A shell program to convert all lowercase letter in a le to uppercase bash script to check given name is le or directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Assignment 1
Title
Implementation of Create/ rename/ delete a le using Unix/Linux commands

About Linux
Linux is

Open Source

operating system. The term open source means availability of the source code.

The idea behind keeping the source code open is to share the knowledge. Moreover people believe that

Knowledge belongs to the world and must be shared.

About UNIX Basics


UNIX is an operating system which was rst developed in the 1960s, and has been under constant development ever since. By operating system, we mean the suite of programs which make the computer work. It is a stable, multi-user, multi-tasking system for servers, desktops and laptops. UNIX systems also have a graphical user interface (GUI) similar to Microsoft Windows which provides an easy to use environment. There are many dierent versions of UNIX, although they share common similarities. The most popular varieties of UNIX are Sun Solaris, GNU/Linux, and MacOS X. The UNIX operating system is made up of three parts; the kernel, the shell and the programs.

The Kernel
The kernel is the core of the UNIX operating system. Basically, the kernel is a large program that is loaded into memory when the machine is turned on, and it controls the allocation of hardware resources from that point forward. The kernel knows what hardware resources are available (like the processor(s), the on-board memory, the disk drives, network interfaces, etc.), and it has the necessary programs to talk to all the devices connected to it. As an illustration of the way that the shell and the kernel work together, suppose a user types rm myle (which has the eect of removing the le myle). The shell searches the le store for the le containing the program rm, and then requests the kernel, through system calls, to execute the program rm on myle. When the process rm myle has nished running, the shell then returns the UNIX prompt to the user, indicating that it is waiting for further commands.

The Shell
The shell acts as an interface between the user and the kernel. The shell is a command line interpreter (CLI). It interprets the commands the user types in and arranges for them to be carried out. The commands are themselves programs: when they terminate, the shell gives the user another prompt .The adept user can customizehis /herown shell and users can use dierent shells on the same machine. UNIX system oers verity of shells like

Bourne shell C Shell

Korn shell Bash shell which is very powerful and recommended for use, Linux default shell

History - The shell keeps a list of the commands you have typed in. If you need to repeat a command, use the cursor keys to scroll up and down the list or type history for a list of previous commands.

Shell Commands
cal:

General purpose commands


year]
This command is used to see calender for any specic month or a complete year.

cal [ [month]

e.g. rmd@ubuntu:

$cal may 2013

date:This

command is used to display the current date.

rmd@ubuntu:

$date

Fri Jan 18 10:22:35 MMT 2013

echo:

Print message on the terminal.

e.g. rmd@ubuntu:

$echo This is the message that I want to print on the terminal

This is the message that I want to print on the terminal

bc:

A text based calculator. $bc

2*10+20-9+4/2 [Input] 33 [Output] [ctrl+d] [Quit]

xcalc

: This is graphical based calculator. This command records your session and stores it in a le.

script:
$script

$Script started, le is typescript $echo this is a sample script this is a sample script $[ctrl+d] Script done, le is typescript

By default if you don't specify any le name the contents will be stored in le name typescipt.
passwd:This who:
command allows you to change your password.

This command tells you the users currently logged on to the system. command displays the documentation for a command.

man:This

usage: man <command name>

Linux File System Commands


ls:
This command lists the contents of the directory. see the le attributes type ls -l on your terminal.

ls -l:To

rmd@ubuntu: $ls -l total 60 -rwxrwxr-x 1 rmd rmd 7162 Jan 16 15:18 a.out drwxr-xr-x 3 rmd rmd 4096 Jan 18 09:34 Desktop drwxr-xr-x 2 rmd rmd 4096 Jan 18 10:51 Documents drwxr-xr-x 3 rmd rmd 4096 Jan 18 09:39 Downloads

-rw-rr 1 rmd rmd 8445 Jan 10 21:02 examples.desktop drwxr-xr-x 2 rmd rmd 4096 Jan 10 21:07 Music drwxr-xr-x 2 rmd rmd 4096 Jan 10 21:07 Pictures drwxr-xr-x 2 rmd rmd 4096 Jan 10 21:07 Public -rw-rw-r 1 rmd rmd 85 Jan 16 15:18 sample.c drwxr-xr-x 2 rmd rmd 4096 Jan 10 21:07 Templates -rw-rw-r 1 rmd rmd 605 Jan 18 10:34 typescript drwxr-xr-x 2 rmd rmd 4096 Jan 10 21:07 Videos
The le sample.c has the following permissions -rw-rw-r It has 10 characters, rst character is d if its directory and - if its le. Next 9 characters are divided into three groups with a set of 3 characters each. First 3 characters - Owner of the le or directory Next 3 characters - Group Last 3 characters - Others r - Read i.e. File or directory is readable. w - Write i.e. File or directory is writable. x - Execute i.e. File or directory is executable. -rw-rr means it has read, write but not execute permissions for the owner of the le, only read permissions for the group and only read permissions for others.

The third column of the command ls -l tells about the owner of the le, next column tells to which group it belongs. The le sample.c has the owner as rmd and also belongs to a group called rmd.

File Handling Commands


cat:
This command is used to display the contents of a small le on terminal. usage: cat

f ilename f ilename

tac:This wc:

command is used to display the contents of a small le in reverse order on terminal.

usage: tac

This command is used to count lines, words and characters, depending on the option used.

usage: wc [options] [le name] $wc sample1.txt 65 2776 17333 sample1.txt Which means sample1.txt le has 65 lines, 2776 words, and 17333 characters. you can just print number of lines, number of words or number of charcters by using following options: -l : Number of lines. -w : Number of words. -c : Number of characters.

cmp: This command f ile1 f ile2


screen.

is used to compare two les whether they are identical or not. usage: cmp

The two les are compared byte by byte and the location of the rst mismatch is printed on the If two les are identical, then it doesnot print anything on the screen.

File System Commands


rm
: This command is used to delete the les. Usage: rm lename

cp

: This command is used to copy one le into another le.

Usage: cp sourceFilename DestinationFilename

mv:

This command is used to rename the les.

Usage: mv oldlename newlename

mkdir: rmdir:

This command is used to create new directory.

Usage: mkdir directoryName This command is used to remove directory.

e.g. $rm Testing.java This command deletes the le Testing.java $cp Testing.java Copy.java This command creates the copy of Testing.java $mv Testing.java Test.java This command renames the le Testing.java to Test.java $mkdir newDir This command creates directory newDir. $rmdir newDir This command deletes directory newDir.

Compressing and Archiving Files


gzip:
This command is used to compress the le, and gunzip is used to de-compress it. usage: gzip

f ilename

It provides the extension .gz and removes the original le. The compression ratio depends on the type, size and nature of the le. usage: gunzip

f ilenamewith.gz

tar

:This command is used to create archive that contains a group or le or entire directory struc-

ture. It is generally used for backups. usage: tar [options] archive -x Extract les from archive -t Display les in archive -f arch Name the archive arch $tar -cvf tarlename.tar lename or directory name

outputf ile.tar

f ile1ordir

. . . The following are the options: -c Create an

zip:

This command can be used for archiving as well as compressing the contents of the directory les to be zipped or directory $zip sample1.zip sample1.txt

or the le. usage: zip [options] output.zip This will create sample1.zip le.

unzip:To

un-compress the le use unzip command. $unzip compression.zip

This will uncompress the compression.zip le.

Assignment 2
Title
Write a function to display the list of devices connected to your system including the physical names and its instance number. Write a function using mount and unmount command to mount device and un-mount it.

What exactly is mounting is ?

Assignment 3
Title
Adding users and access rights

Assignment 4
Title
Implement the commands for creation and deletion of directory. Write a program to change current working directory and display the node details for each le in the new directory.

Assignment 5
Title
Process related commands list the processes for the current shell, Display information about processes, Display the global priority of a process, change the priority of a process with default arguments.

Many of the commands here perform a single function and can be combined  that's the Unix philosophy of designing programs. Other programs, like htop, provide a friendly interface on top of the commands. top The top command is the traditional way to view your system's resource usage and see the processes that are taking up the most system resources. Top displays a list of processes, with the ones using the most CPU at the top. To exit top or htop, use the Ctrl-C keyboard shortcut. This keyboard shortcut usually kills the currently running process in the terminal. htop The htop command is an improved top. It's not installed by default on most Linux distributions  here's the command you'll need to install it on Ubuntu: sudo apt-get install htop htop displays the same information with an easier-to-understand layout. It also lets you select processes with the arrow keys and perform actions, such as killing them or changing their priority, with the F keys. We've covered htop in more detail in the past. ps The ps command lists running processes. The following command lists all processes running on your system: ps -A This may be too many processes to read at one time, so you can pipe the output through the less command to scroll through them at your own pace: ps -A | less Press q to exit when you're done. You could also pipe the output through grep to search for a specic process without using any other commands. The following command would search for the Firefox process: ps -A | grep refox pstree The pstree command is another way of visualizing processes. It displays them in tree format. So, for example, your X server and graphical environment would appear under the display manager that spawned them. kill The kill command can kill a process, given its process ID. You can get this information from the ps -A, top or pgrep commands. kill PID Technically speaking, the kill command can send any signal to a process. You can use kill -KILL or kill -9 instead to kill a stubborn process. pgrep Given a search term, pgrep returns the process IDs that match it. For example, you could use the following command to nd Firefox's PID: pgrep refox You can also combine this command with kill to kill a specic process. Using pkill or killall is simpler, though. pkill and killall The pkill and killall commands can kill a process, given its name. Use either command to kill Firefox: pkill refox killall refox We've covered pkill in more depth in the past. renice The renice command changes the nice value of an already running process. The nice value determines what priority the process runs with. A value of -19 is very high priority, while a value of 19 is very low priority. A value of 0 is the default priority. The renice command requires a process's PID. The following command makes a process run with very low priority: renice 19 PID You can use the pgrep trick above with renice, too.

If you're making a process run at a higher priority, you'll require root permissions. On Ubuntu, use sudo for that: sudo renice -19 $xkill The xkill command is a way of easily killing graphical programs. Run it and your cursor will turn into an x sign. Click a program's window to kill that program. If you don't want to kill a program, you can back out of xkill by right-clicking instead. You don't have to run this command from a terminal  you can also press Alt-F2, type xkill and press Enter to use it from a graphical desktop.

Assignment 6
Title
Use Operating system Commands to obtain the following results To print the name of operating system To print the login name To print the host name
Suppose you have a query related to some thing on Linux and you ask an expert sitting at a remote end. The expert cross questions and enquires about the Linux kernel release that you are using. Well. sometimes you get stuck as you don't know where to search for kernel release in Linux. What if you knew a Linux command that can provide most of your system related information quickly? So, In this article, we will discuss the Linux 'uname' command (through examples) that serves exactly the same purpose. The syntax The syntax of uname command is : uname [option].... The above syntax means that the uname command can be run with or without options. Linux uname command examples 1. Print operating system name The option -o or operating-system can be used if operating system name is to produced in the output. Here is an example : $uname -o GNU/Linux or $uname operating-system GNU/Linux So we see that the operating system that I am using is GNU/Linux. 2. Print machine hardware name The option -m or machine can be used if machine hardware name is to be produced in the output. Here is an example : $uname -m x86 64 or $uname machine x86 64 So we see that the machine hardware name was produced in the output above. 3. Print kernel version The option -v or kernel-version can be used if kernel version is to be produced in the output.

10

Here is an example : $uname -v 32-Ubuntu SMP Fri Apr 16 08:09:38 UTC 2010 or $uname kernel-version 32-Ubuntu SMP Fri Apr 16 08:09:38 UTC 2010 So we see that the kernel version details were produced in the output above. 4. Print kernel release The option -r or kernel-release can be used if kernel release information is to be produced in the output. Here is an example : $uname -r 2.6.32-21-generic or $uname kernel-release 2.6.32-21-generic So we see that the kernel release related information was produced in the output. 5. Print the network node host name The option -n or nodename can be used if host name related information is to be produced in the output. Here is an example : $uname -n admin-laptop or $uname nodename admin-laptop So we see that the host name related information was produced in the output. 6. Print the kernel name The option -s or kernel-name can be used if kernel name is to produced in the output. Here is an example : $uname -s Linux or $uname kernel-name Linux So we see that the kernel name 'Linux' was produced in the output. Note that the uname command when executed without any option also produces kernel name in output. Here is an example : $uname Linux So we see that kernel name is the default output of the uname command. 7. Print maximum possible system information The option -a or all can be used if all possible information is to be produced in the output. Here is an example : or $$$uname all Linux admin-laptop 2.6.32-21-generic Ubuntu SMP Fri Apr 16 08:09:38 UTC 2010 x86 64 GNU/Linux So we see that all the system information was produced in the output. $uname -a Linux admin-laptop 2.6.32-21-generic Ubuntu SMP Fri Apr 16

08:09:38 UTC 2010 x86 64 GNU/Linux

8. Print help and version related information about uname command The options help and version can be used to produce help and version related information for uname command.

Conceptual Overview

Assignment 7
Title
Write a C/C++ script to display all logged in users.

Figure 1: Write a C/C++ script to display all logged in users

13

Assignment 8
Title
Java Program to display the list of devices connected to your system including the physical names and its instance number.

Figure 2: Java program to display the list of devices

Figure 3: Java program to display the list of devices

14

Assignment 9
Title
C/C++ Program to Parent creating the child process by use of fork.

Concepts
fork()
It is a system call that creates a new process under the UNIX operating system. It takes no arguments. The purpose of fork() is to create a new process, which becomes the child process of the caller. After a new child process is created, both processes will execute the next instruction following the fork() system call. Therefore, we have to distinguish the parent from the child. This can be done by testing the returned value of fork():

If fork() returns a negative value, the creation of a child process was unsuccessful. fork() returns a zero to the newly created child process. fork() returns a positive value, the process ID of the child process, to the parent. The returned process ID is of type pid t dened in sys/types.h. Normally, the process ID is an integer. Moreover, a process can use function getpid() to retrieve the process ID assigned to this process.

Therefore, after the system call to fork(), a simple test can tell which process is the child. Note that Unix will make an exact copy of the parent's address space and give it to the child. Therefore, the parent and child processes have separate address spaces. Let us take an example: If the call to fork() is executed

Figure 4: Fork Example

successfully, Unix will

Make two identical copies of address spaces, one for the parent and the other for the child. Both processes will start their execution at the next statement following the fork() call.

After Forking

If we run this program, we might see the following on the screen:

Before Forking

15

After Forking
process.

Here printf() statement after fork() system call executed by parent as well as child Since both processes

Both processes start their execution right after the system call fork().

have identical but separate address spaces, those variables initialized before the fork() call have the same values in both address spaces. Since every process has its own address space, any modications will be independent of the others. In other words, if the parent changes the value of its variable, the modication will only aect the variable in the parent process's address space. Other address spaces created by fork() calls will not be aected even though they have identical variable names. Consider one simpler example, which distinguishes the parent from the child.

Figure 5: Simple Fork Example

In this program, both processes print lines that indicate (1) whether the line is printed by the child or by the parent process, and (2) the value of variable i. When the main program executes fork(), an identical copy of its address space, including the program and all data, is created. System call fork() returns the child process ID to the parent and returns 0 to the child process. The following gure shows that in both address spaces there is a variable pid. The one in the parent receives the child's process ID 3456 and the one in the child receives 0.

Figure 6: Parent Child Process

Now both programs (i.e., the parent and child) will execute independent of each other starting at the next statement:

Figure 7: Fork Example1

In the parent, since pid is non-zero, it calls function parentprocess(). On the other hand, the child has a zero pid and calls childprocess() as shown below:

Figure 8: Parent Child Process Execution

Due to the fact that the CPU scheduler will assign a time quantum to each process, the parent or the child process will run for some time before the control is switched to the other and the running process will print some lines before you can see any line printed by the other process. the system. E.g. $ps ef By default, the ps program shows only processes that maintain a connection with a terminal, a console, a serial line, or a pseudo terminal. Other processes run without needing to communicate with a user on a terminal. These are typically system processes that Linux uses to manage shared resources. We can use ps to see all such processes using the -e option and to get full information with -f.The following is the C program for parent child process. Main(int argc,char*argv[]) ps command: The ps command shows the processes we're running, the process another user is running, or all the processes on

If(fork()==0) execl(Filecopy,copy,argv[1],argv[2],0); wait((int*)0); printf(File Copy is Completed Successfully);

References
1. Beginning Linux Programming by Neil Mathew and Richard Stones, Wrox Publications. 2. Unix Concepts and Applications By Sumitabha Das, Tata McGraw Hill

Assignment 10
Title
Java Program to Identify the available memory in the system.

Figure 9: Java Program to Identify the available memory in the system

19

Assignment 11
Title
Python script to display looged user info

Figure 10: Python script to display looged user info

20

Assignment 12
Title
Write a shell program to convert all lowercase letter in a le to uppercase

Figure 11: A shell program to convert all lowercase letter in a le to uppercase

21

Assignment 13
Title
Write bash script to check given name is le or directory.

Figure 12: bash script to check given name is le or directory

22

Вам также может понравиться