COMP 3000 Lab 2 2010: Difference between revisions

From Soma-notes
Created page with '==Part A (Mandatory)== ===The Shell=== The shell or command line provides a text interface for running programs. While not as visually pleasing as a graphical interface, the s…'
 
No edit summary
 
(20 intermediate revisions by 6 users not shown)
Line 1: Line 1:
==Part A (Mandatory)==
The following lab requires access to a relatively standard Linux environment.  Use a virtual machine version of Ubuntu or log in to one of the SCS Linux machines.  Alternately, use your own Linux installation (installed natively or in a virtual machine).


===The Shell===
=Part A (Mandatory)=
 
==The Shell==


The shell or command line provides a text interface for running programs.  While not as visually pleasing as a graphical interface, the shell provides a more clear representation of the functionality provided by the operating system.
The shell or command line provides a text interface for running programs.  While not as visually pleasing as a graphical interface, the shell provides a more clear representation of the functionality provided by the operating system.
Line 7: Line 9:
To run a program contained in the current directory in the shell, you need to prefix the name of the command with a <tt>./</tt>.  This "./" tells the shell that the location of the command you wish to run is the current directory.  By default, the shell will not search for executable commands in the current working directory.  To run most system commands, the name of the command can be typed without a path specification.
To run a program contained in the current directory in the shell, you need to prefix the name of the command with a <tt>./</tt>.  This "./" tells the shell that the location of the command you wish to run is the current directory.  By default, the shell will not search for executable commands in the current working directory.  To run most system commands, the name of the command can be typed without a path specification.


====Help====
===Help===


#When working in the shell, help is available on most programs in the system, especially those that are command line based.  This system of help is available by using the <tt>man</tt> command.  If one wanted to get help on the echo command, the associated command would be <tt>man echo</tt>.  What does <tt>man</tt> stand for?
#When working in the shell, help is available on most programs in the system, especially those that are command line based.  This system of help is available by using the <tt>man</tt> command.  If one wanted to get help on the echo command, the associated command would be <tt>man echo</tt>.  What does <tt>man</tt> stand for?
#The <tt>man</tt> command can also be used to get help on standard C functions.  Briefly (in one line), what does the C function <tt>brk</tt> do?
#The <tt>man</tt> command can also be used to get help on standard C functions.  Briefly (in one line), what does the C function <tt>brk</tt> do?
# What does the <tt>more</tt> command do? 
# How do you quit the <tt>less</tt> command?
# How do you quit the <tt>less</tt> command?


====Shell Basics====
===Shell Basics===


# The <tt>which</tt> command can be used to figure out what directory an executable program resides in.  Using this command, what directory contains the <tt>top</tt> command?
# The <tt>which</tt> command can be used to figure out what directory an executable program resides in.  Using this command, what directory contains the <tt>top</tt> command?
# <tt>bash</tt> is your default shell.  Which of the following is another shell that can be used instead of BASH? <tt>mv</tt>, <tt>cat</tt>, <tt>bc</tt>, <tt>tcsh</tt>, or <tt>arch</tt>
# <tt>bash</tt> is your default shell.  Which of the following is another shell that can be used instead of BASH?<tt> mv, cat, bc, tcsh, or arch </tt>
# The <tt>ls</tt> command can be used to get a listing of the files in a directory.  What options are passed to <tt>ls</tt> to see all of the files within a directory (including hidden files)?
# The <tt>ls</tt> command can be used to get a listing of the files in a directory.  What options are passed to <tt>ls</tt> to see all of the files within a directory (including hidden files)?
# The <tt>ps</tt> command is used to get a list of processes which are running on the system.  Start a new shell from bash by running <tt>bash</tt>.  Does the new shell replace or run in parallel with the old BASH shell?  What about <tt>exec bash</tt>?  You can use the <tt>ps</tt> command to determine if bash is still running.
# The <tt>ps</tt> command is used to get a list of processes which are running on the system.  Start a new shell from bash by running <tt>bash</tt>.  Does the new shell replace or run in parallel with the old BASH shell?  What about <tt>exec bash</tt>?  You can use the <tt>ps</tt> command to determine if bash is still running.
# Because each process is started by some other process, there is a process tree structure on the system.  From within your bash terminal, determine what process started BASH by using the <tt>pstree</tt> command.
# Because each process is started by some other process, there is a process tree structure on the system.  From within your bash terminal, determine what process started BASH by using the <tt>pstree</tt> command. What graphical application can give you the same information?
/usr/bin/top
tcsh
ls -a
bash: open a new one.
exec bash: replace the old one
init--gnome-terminal--bash


===Processes===
==Processes==


Each application running on a system is assigned a unique process
Each application running on a system is assigned a unique process identifier.  The <tt>ps</tt> command shows the process identifiers for running processes.  Each process running on the system is kept separated from other processes by the operating system.  This information will be useful for subsequent questions.
identifier.  The <tt>ps</tt> command shows the process identifiers for
running processes.  Each process running on the system is kept
separated from other processes by the operating system.  This
information will be useful for subsequent questions.


===Permissions===
==Permissions==


Your permission to access a file in Unix is determined by who you are
Your permission to access a file in Unix is determined by who you are logged in as.  All files on the Unix file system (including directories and other special files) have three different sets of permissions. The first set permissions denotes the allowed file operations for the owner of the file.  The second set of permissions denotes the allowed file operations for a group of users.  The third set of permissions denotes the allowed file operations for everyone else.  A
logged in as.  All files on the Unix file system (including directories
and other special files) have three different sets of permissions.
The first set permissions denotes the allowed file operations for
the owner of the file.  The second set of permissions denotes the
allowed file operations for a group of users.  The third set of
permissions denotes the allowed file operations for everyone else.  A
file is always owned by someone and is always associated with a group.
file is always owned by someone and is always associated with a group.


\begin{enumerate}
The <tt>ls</tt> command with the <tt>-l</tt> option can be used to show both the permissions of a file as well as the owner and group associated with the file.  Permissions are listed first, followed by the owner and the group.
 
# The <tt>ls</tt> command with the <tt>-l</tt> option can be used to
show both the permissions of a file as well as the owner and group
associated with the file.  Permissions are listed first, followed by
the owner and the group.
 
Who is the owner of the <tt>/etc</tt> directory?
 


#Who is the owner of the <tt>/etc</tt> directory?
A: root
# What group is associated with the <tt>/etc/shadow</tt> file?
# What group is associated with the <tt>/etc/shadow</tt> file?
A: shadow
# Each user is a member of some number of groups.  You can determine what groups you are part of by using the <tt>groups</tt> command. Based on the groups listed, would you (the ``student'' user in the lab) be a member of the group associated with the <tt>/etc/shadow</tt> file?
A: no


# Each user is a member of some number of groups.  You can determine
==Environment==
what groups you are part of by using the <tt>groups</tt> command.
Based on the groups listed, would you (the ``student'' user in the
lab) be a member of the group associated with the <tt>/etc/shadow</tt>
file?
 
===Environment===


The environment on both Linux and Windows contains variable - value
The environment on both Linux and Windows contains variable - value pairs which are useful to applications running on the system.  In Linux, these environment variables can be printed on the command line by referring to the variable name prefixed with a $ sign (eg: to output the value in the HELLO environment variable, one could write <tt>echo $HELLO</tt>).
pairs which are useful to applications running on the system.  In
Linux, these environment variables can be printed on the command line
by referring to the variable name prefixed with a $ sign (eg: to
output the value in the HELLO environment variable, one could write
<tt>echo $HELLO</tt>).


# On the command line, run the following two sets of commands.  Notice
# On the command line, run the following two sets of commands.  Notice that the bash command will start a new shell separate from the shell that the HELLO environment variable was set in.
that the bash command will start a new shell separate from the shell
that the HELLO environment variable was set in.


  HELLO="Hi There"
  HELLO="Hi There"
Line 81: Line 62:
  echo $HELLO
  echo $HELLO
  exit
  exit


What does the export command seem to do?
What does the export command seem to do?
declare a global variable


===Dynamic Libraries===
==Dynamic Libraries==


Most applications on the system do not contain all the code that they
Most applications on the system do not contain all the code that they need right within the executable.  Instead, dynamic libraries are loaded into the program address space when the program loads.  As an example, the standard C library, which contains such functions as <tt>printf</tt> is loaded in at run-time.
need right within the executable.  Instead, dynamic libraries are
loaded into the program address space when the program loads.  As an
example, the standard C library, which contains such functions as
<tt>printf</tt> is loaded in at run-time.


# Using <tt>ldd</tt>, what dynamic library dependencies does the
# Using <tt>ldd</tt>, what dynamic library dependencies does the <tt>top</tt> command have?
<tt>top</tt> command have?
# In addition to the libraries listed as dependencies for the application <tt>top</tt> by <tt>ldd</tt>, there may be other libraries that the application loads dynamically at run-time.  Retrieve the process number <em>PID</em> for the <tt>bash</tt> process (using <tt>ps</tt>) and examine the map file located at <tt>/proc/</tt><em>PID</em><tt>/maps</tt>.  What other dynamic libraries have been loaded into the application while it has been running?


# In addition to the libraries listed as dependencies for the
Linux-gate.so.1, libproc, libncurses, libc, libdl, /lib/ld-linux
application <tt>top</tt> by <tt>ldd</tt>, there may be other libraries
that the application loads dynamically at run-time. Retrieve the
process number <em>PID</em> for the <tt>bash</tt> process (using
<tt>ps</tt>) and examine the mmap file located at
<tt>/proc/</tt><em>PID</em><tt>/maps</tt>.  What other dynamic libraries
have been loaded into the application while it has been running?


=Part B (Optional)=


==Part B (Optional)==
==Processes==
 
===Processes===
 
# What is the difference between the <tt>fork</tt> and <tt>exec</tt>
function in a Unix environment?


# What is the difference between the <tt>fork</tt> and <tt>exec</tt> function in a UNIX environment?
# What is a zombie process?
# What is a zombie process?
# Give an example C program which creates a zombie process.  Note that <tt>BASH</tt> by default will collect and destroy zombie processes and so you will need to avoid bash destroying the zombie process during debugging.  This can be done by delaying the parent exit (using sleep is one good way to do this).
# Perform the modifications to your program above to avoid creating a zombie process.  List the new program.


# Give an example C program which creates a zombie process.  Note that
==Permissions==
<tt>BASH</tt> by default will collect and destroy zombie processes and
so you will need to avoid bash destroying the zombie process during
debugging.  This can be done by delaying the parent exit (using sleep
is one good way to do this).
 
# Perform the modifications to your program above to avoid creating a
zombie process.  List the new program.
 
===Permissions===
 
# Permissions on Unix are grouped into three basic file operations.
What are these file operations?


# Permissions on Unix are grouped into three basic file operations. What are these file operations?
# What does it mean to have execute permission on a directory?
# What does it mean to have execute permission on a directory?
# What are the 6 basic file permissions within Windows?
# What are the 6 basic file permissions within Windows?
# What is the difference between the write and modify file permission in Windows?
# Because all files are stored in a directory, under UNIX permission to delete, rename, and move files is determined by the users access rights on the directory the file is contained in.  What attribute on the directory prevents those who can modify a directory from deleting files (hint: help on the <tt>chmod</tt> command may prove useful).
# In Windows, a file can be associated with more than one group and each group can have different access permissions.  On Unix, new groups can be created by the system administrator which are supersets of other groups.  Given this, is it possible to develop an access permission scenario which would be impossible to implement in Unix but possible to implement in Windows?  If yes, give an example.  If no, explain why.


# What is the difference between the write and modify file permission
==Environment==
in Windows?


# What does the PATH environment variable do?
# What environment variable tells X applications where to find the X server which it should communicate with to display the output?


# Because all files are stored in a directory, under Unix permission
==Dynamic Libraries==
to delete, rename, and move files is determined by the users access
rights on the directory the file is contained in.  What attribute on
the directory prevents those who can modify a directory from deleting
files (hint: help on the <tt>chmod</tt> command may prove useful).


# In Windows, a file can be associated with more than one group and
Dynamic libraries allow one copy of executable code to be used by
each group can have different access permissions.  On Unix, new groups
can be created by the system administrator which are supersets of
other groups.  Given this, is it possible to develop an access
permission scenario which would be impossible to implement in Unix but
possible to implement in Windows?  If yes, give an example.  If no,
explain why.
 
===Dynamic Libraries===
 
# Dynamic libraries allow one copy of executable code to be used by
many different processes on the system, without requiring that
many different processes on the system, without requiring that
multiple copies of the code be stored on disk in different files.
multiple copies of the code be stored on disk in different files.
What are some problems that can arise when different programs use the
What are some problems that can arise when different programs use the
same common DLLs (hint: ``DLL Hell'')?
same common DLLs (hint: ``DLL Hell'')?
===Environment===
# What does the PATH environment variable do?
# What environment variable tells X applications where to find the X
server which it should communicate with to display the output?

Latest revision as of 15:44, 8 November 2010

The following lab requires access to a relatively standard Linux environment. Use a virtual machine version of Ubuntu or log in to one of the SCS Linux machines. Alternately, use your own Linux installation (installed natively or in a virtual machine).

Part A (Mandatory)

The Shell

The shell or command line provides a text interface for running programs. While not as visually pleasing as a graphical interface, the shell provides a more clear representation of the functionality provided by the operating system.

To run a program contained in the current directory in the shell, you need to prefix the name of the command with a ./. This "./" tells the shell that the location of the command you wish to run is the current directory. By default, the shell will not search for executable commands in the current working directory. To run most system commands, the name of the command can be typed without a path specification.

Help

  1. When working in the shell, help is available on most programs in the system, especially those that are command line based. This system of help is available by using the man command. If one wanted to get help on the echo command, the associated command would be man echo. What does man stand for?
  2. The man command can also be used to get help on standard C functions. Briefly (in one line), what does the C function brk do?
  3. How do you quit the less command?

Shell Basics

  1. The which command can be used to figure out what directory an executable program resides in. Using this command, what directory contains the top command?
  2. bash is your default shell. Which of the following is another shell that can be used instead of BASH? mv, cat, bc, tcsh, or arch
  3. The ls command can be used to get a listing of the files in a directory. What options are passed to ls to see all of the files within a directory (including hidden files)?
  4. The ps command is used to get a list of processes which are running on the system. Start a new shell from bash by running bash. Does the new shell replace or run in parallel with the old BASH shell? What about exec bash? You can use the ps command to determine if bash is still running.
  5. Because each process is started by some other process, there is a process tree structure on the system. From within your bash terminal, determine what process started BASH by using the pstree command. What graphical application can give you the same information?

/usr/bin/top tcsh ls -a bash: open a new one. exec bash: replace the old one init--gnome-terminal--bash

Processes

Each application running on a system is assigned a unique process identifier. The ps command shows the process identifiers for running processes. Each process running on the system is kept separated from other processes by the operating system. This information will be useful for subsequent questions.

Permissions

Your permission to access a file in Unix is determined by who you are logged in as. All files on the Unix file system (including directories and other special files) have three different sets of permissions. The first set permissions denotes the allowed file operations for the owner of the file. The second set of permissions denotes the allowed file operations for a group of users. The third set of permissions denotes the allowed file operations for everyone else. A file is always owned by someone and is always associated with a group.

The ls command with the -l option can be used to show both the permissions of a file as well as the owner and group associated with the file. Permissions are listed first, followed by the owner and the group.

  1. Who is the owner of the /etc directory?

A: root

  1. What group is associated with the /etc/shadow file?

A: shadow

  1. Each user is a member of some number of groups. You can determine what groups you are part of by using the groups command. Based on the groups listed, would you (the ``student user in the lab) be a member of the group associated with the /etc/shadow file?

A: no

Environment

The environment on both Linux and Windows contains variable - value pairs which are useful to applications running on the system. In Linux, these environment variables can be printed on the command line by referring to the variable name prefixed with a $ sign (eg: to output the value in the HELLO environment variable, one could write echo $HELLO).

  1. On the command line, run the following two sets of commands. Notice that the bash command will start a new shell separate from the shell that the HELLO environment variable was set in.
HELLO="Hi There"
bash
echo $HELLO
exit
export HELLO="Hi There"
bash
echo $HELLO
exit

What does the export command seem to do? declare a global variable

Dynamic Libraries

Most applications on the system do not contain all the code that they need right within the executable. Instead, dynamic libraries are loaded into the program address space when the program loads. As an example, the standard C library, which contains such functions as printf is loaded in at run-time.

  1. Using ldd, what dynamic library dependencies does the top command have?
  2. In addition to the libraries listed as dependencies for the application top by ldd, there may be other libraries that the application loads dynamically at run-time. Retrieve the process number PID for the bash process (using ps) and examine the map file located at /proc/PID/maps. What other dynamic libraries have been loaded into the application while it has been running?

Linux-gate.so.1, libproc, libncurses, libc, libdl, /lib/ld-linux

Part B (Optional)

Processes

  1. What is the difference between the fork and exec function in a UNIX environment?
  2. What is a zombie process?
  3. Give an example C program which creates a zombie process. Note that BASH by default will collect and destroy zombie processes and so you will need to avoid bash destroying the zombie process during debugging. This can be done by delaying the parent exit (using sleep is one good way to do this).
  4. Perform the modifications to your program above to avoid creating a zombie process. List the new program.

Permissions

  1. Permissions on Unix are grouped into three basic file operations. What are these file operations?
  2. What does it mean to have execute permission on a directory?
  3. What are the 6 basic file permissions within Windows?
  4. What is the difference between the write and modify file permission in Windows?
  5. Because all files are stored in a directory, under UNIX permission to delete, rename, and move files is determined by the users access rights on the directory the file is contained in. What attribute on the directory prevents those who can modify a directory from deleting files (hint: help on the chmod command may prove useful).
  6. In Windows, a file can be associated with more than one group and each group can have different access permissions. On Unix, new groups can be created by the system administrator which are supersets of other groups. Given this, is it possible to develop an access permission scenario which would be impossible to implement in Unix but possible to implement in Windows? If yes, give an example. If no, explain why.

Environment

  1. What does the PATH environment variable do?
  2. What environment variable tells X applications where to find the X server which it should communicate with to display the output?

Dynamic Libraries

Dynamic libraries allow one copy of executable code to be used by many different processes on the system, without requiring that multiple copies of the code be stored on disk in different files. What are some problems that can arise when different programs use the same common DLLs (hint: ``DLL Hell)?