COMP 3000 Lab 2 2010 Soln: Difference between revisions

From Soma-notes
Praman (talk | contribs)
Created page with "=Part A (Mandatory)= ===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…"
 
Praman (talk | contribs)
 
(9 intermediate revisions by the same user not shown)
Line 4: Line 4:


#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?<br /><b>A:Manual</b>
#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?<br /><b>A:Manual</b>
 
#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?<br /><b>A: Change data segment size</b>
#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?
# How do you quit the <tt>less</tt> command?<br /><b>A: Type the q key</b>
# 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?<br /><b>A: /usr/X11R6/bin</b>
# <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>
# <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><br /><b>A: tcsh</b>
# 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)?<br /><b>A: -a</b>
# 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.<br /><b>A: bash runs in parallel, exec bash replaces it.</b>
# 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?
# 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?<br /><b>A: gnome-terminal or maybe xterm (note that other answers are possible)</b>
/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 <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==
Line 32: Line 21:
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?<br /><b>A: root</b>
A: root
# What group is associated with the <tt>/etc/shadow</tt> file?<br /><b>A: shadow</b>
# What group is associated with the <tt>/etc/shadow</tt> file?
# 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? <br /><b>A: No</b>
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


==Environment==
==Environment==
Line 55: Line 41:
  exit
  exit


What does the export command seem to do?
What does the export command seem to do?<br /><b>A: Allow the second (child) bash process to see the environment variable.</b>
declare a global variable


==Dynamic Libraries==
==Dynamic Libraries==
Line 62: Line 47:
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.
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.


# Using <tt>ldd</tt>, what dynamic library dependencies does the <tt>top</tt> command have?
# Using <tt>ldd</tt>, what dynamic library dependencies does the <tt>top</tt> command have?<br /><b>A: linux-gate, libproc, libncurses, libc, libdl, ld-linux</b>
# 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 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?<br /><b>A: ld, nss compat, nsl, nss nis, nss files</b>
 
Linux-gate.so.1, libproc, libncurses, libc, libdl, /lib/ld-linux


=Part B (Optional)=
=Part B (Optional)=
Line 71: Line 54:
==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?<br /><b>A: fork creates a new process, exec replaces the running program in the current process with a new program.</b>
# What is a zombie process?
# What is a zombie process?<br /><b>A: A process which has finished running but has not had it’s return value read by the person who started it yet.</b>
# 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).
# 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).<br/><b>A:</b><br/>int main() {<br/>if( fork() == 0 ) {<br/>return 0;<br/>}else {<br/>sleep( 1 );<br/>}<br/>}<br/>
# Perform the modifications to your program above to avoid creating a zombie process.  List the new program.
# Perform the modifications to your program above to avoid creating a zombie process.  List the new program.<br /><b>A.</b><br />int main() {<br />if( fork() == 0 ) {<br />return 0;<br />} else {<br />wait( NULL );<br />sleep( 1 );<br />}<br />}<br />


==Permissions==
==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?<br /><b>A: Read,Write, Execute</b>
# What does it mean to have execute permission on a directory?
# What does it mean to have execute permission on a directory?<br /><b>A: Can change into the directory.</b>
# What are the 6 basic file permissions within Windows?
# What are the 6 basic file permissions within Windows?<br /><b>A:<br/>• Read: Allows files or folders to be opened as read-only and to be copied.<br/>• Write: Allows the creation of files and folders; allows data to be added to or removed from files.<br/>• List Folder Contents: As per Read but also allows navigation of sub-folders.<br/>• Read and Execute: As per Read but also allows users to run executable files.<br/>• Modify: All the above as well as the permission to delete the file or folder.<br/>• Full Control: Full Control – including the ability to change ACLs.</b>
# What is the difference between the write and modify file permission in Windows?
# What is the difference between the write and modify file permission in Windows?<br /><b>A:Modify allows you to delete the file or folder.</b>
# 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).
# 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).<br /><b>A: The Sticky bit.</b>
# 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.
# 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 super-sets 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.<br /><b>A: Yes. File 1 (G1-R), File 2 (G2-R), File 3 (G1-R, G2-W).</b>


==Environment==
==Environment==


# What does the PATH environment variable do?
# What does the PATH environment variable do?<br /><b>A: Dictates where to look for executable programs.</b>
# What environment variable tells X applications where to find the X server which it should communicate with to display the output?
# What environment variable tells X applications where to find the X server which it should communicate with to display the output?<br /><b>A: DISPLAY</b>


==Dynamic Libraries==
==Dynamic Libraries==
Line 96: Line 79:
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'')?<br /><b>A: Different Versions of the DLL. No standard location for the DLL. Hard to determine
when no application is installed which uses the DLL.</b>

Latest revision as of 17:53, 18 November 2010

Part A (Mandatory)

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?
    A:Manual
  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?
    A: Change data segment size
  3. How do you quit the less command?
    A: Type the q key

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?
    A: /usr/X11R6/bin
  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
    A: tcsh
  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)?
    A: -a
  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.
    A: bash runs in parallel, exec bash replaces it.
  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?
    A: gnome-terminal or maybe xterm (note that other answers are possible)

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
  2. What group is associated with the /etc/shadow file?
    A: shadow
  3. 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?
A: Allow the second (child) bash process to see the environment 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?
    A: linux-gate, libproc, libncurses, libc, libdl, ld-linux
  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?
    A: ld, nss compat, nsl, nss nis, nss files

Part B (Optional)

Processes

  1. What is the difference between the fork and exec function in a UNIX environment?
    A: fork creates a new process, exec replaces the running program in the current process with a new program.
  2. What is a zombie process?
    A: A process which has finished running but has not had it’s return value read by the person who started it yet.
  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).
    A:
    int main() {
    if( fork() == 0 ) {
    return 0;
    }else {
    sleep( 1 );
    }
    }
  4. Perform the modifications to your program above to avoid creating a zombie process. List the new program.
    A.
    int main() {
    if( fork() == 0 ) {
    return 0;
    } else {
    wait( NULL );
    sleep( 1 );
    }
    }

Permissions

  1. Permissions on Unix are grouped into three basic file operations. What are these file operations?
    A: Read,Write, Execute
  2. What does it mean to have execute permission on a directory?
    A: Can change into the directory.
  3. What are the 6 basic file permissions within Windows?
    A:
    • Read: Allows files or folders to be opened as read-only and to be copied.
    • Write: Allows the creation of files and folders; allows data to be added to or removed from files.
    • List Folder Contents: As per Read but also allows navigation of sub-folders.
    • Read and Execute: As per Read but also allows users to run executable files.
    • Modify: All the above as well as the permission to delete the file or folder.
    • Full Control: Full Control – including the ability to change ACLs.
  4. What is the difference between the write and modify file permission in Windows?
    A:Modify allows you to delete the file or folder.
  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).
    A: The Sticky bit.
  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 super-sets 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.
    A: Yes. File 1 (G1-R), File 2 (G2-R), File 3 (G1-R, G2-W).

Environment

  1. What does the PATH environment variable do?
    A: Dictates where to look for executable programs.
  2. What environment variable tells X applications where to find the X server which it should communicate with to display the output?
    A: DISPLAY

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)?
A: Different Versions of the DLL. No standard location for the DLL. Hard to determine
when no application is installed which uses the DLL.