COMP 3000 Lab 1 2011: Difference between revisions
No edit summary |
|||
(28 intermediate revisions by 2 users not shown) | |||
Line 23: | Line 23: | ||
===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? (1 point) | ||
#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? (1 point) | ||
# How do you quit the <tt>less</tt> command? | # How do you quit the <tt>less</tt> command? (1 point) | ||
===Shell Basics=== | ===Shell Basics=== | ||
Line 83: | Line 83: | ||
==Processes== | ==Processes== | ||
# What is the difference between the <tt>fork</tt> and <tt>exec</tt> function in a UNIX environment? ( 1 point ) | # What is the difference between the <tt>fork</tt> and <tt>exec</tt> function in a UNIX environment? ( 1 point ) | ||
# What is a zombie process? ( 1 point ) | |||
# Give an example C program which creates a zombie process. Note that the shell by default will collect and destroy zombie processes and so you will need to avoid the shell destroying the zombie process during debugging. This can be done by delaying the parent exit (using sleep is one good way to do this). ( 2 points ) | |||
# Perform the modifications to your program above to avoid creating a zombie process. List the new program. ( 3 points ) | |||
==Permissions== | |||
# Permissions on Unix are grouped into three basic file operations. What are these file operations? ( 1 point ) | |||
# What does it mean to have execute permission on a directory? ( 1 point ) | |||
# What are the 6 basic file permissions within Windows? ( 1 point ) | |||
# What is the difference between the write and modify file permission in Windows? ( 1 point ) | |||
# 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). ( 1 point ) | |||
# In Windows, a file can be associated with more than one group and each group can have different access permissions. On Unix, each file can only belong to one group and groups cannot contain other groups (groups can only contain users). Having said that, new groups can be created by the system administrator which are supersets of other groups (e.g., to create group C with members of A and B, just add all of A's and B's members to C). 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. ( 1 point ) | |||
==Environment== | |||
# What does the PATH environment variable do? PWD? ( 1 point ) | |||
# What environment variable tells X applications where to find the X server which it should communicate with to display the output? ( 1 point ) | |||
==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'')? ( 1 point ) | |||
=Answers= | |||
==Part A== | |||
===Help=== | |||
# Manual | |||
# The break or brk() function sets the break or lowest address of a process's data segment (uninitialized data) to addr (immediately above bss). Memory is allocated by brk in page size pieces; if addr is not evenly divisible by the system page size, it is increased to the next page boundary. It returns a pointer to the new end of memory if successful; otherwise -1 with errno set to indicate why allocation failed. | |||
# using the letter 'q'. | |||
# | ===Shell Basics=== | ||
# /usr/bin/top | |||
# bash | |||
# ls -a | |||
# runs in parallel, exec csh replaces the shell without creating a new process. | |||
# csh or sshd (for putty users) and gnome-terminal for graphical representation system monitor with dependencies. | |||
===Permissions=== | |||
# root | |||
# shadow | |||
# no | |||
===Environment=== | |||
# Set sets shell variables specific to that particular shell instance, and the setenv sets environment variables for all subsequently called shell instances. | |||
===Dynamic Libraries=== | |||
# (Your answers might vary a bit) <br> linux-gate.so.1 => (0x00b26000) <br> libproc-3.2.8.so => /lib/libproc-3.2.8.so (0x00541000) <br> libncurses.so.5 => /lib/libncurses.so.5 (0x002a9000) <br> libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0x00c63000) <br> libdl.so.2 => /lib/i386-linux-gnu/libdl.so.2 (0x00e6c000) <br> /lib/ld-linux.so.2 (0x00a0c000) <br> | |||
# (Your answers might vary a bit) <br> /lib/libcrypt-2.11.1.so <br> /lib/ld-2.11.1.so <br> /usr/lib/gconv/gconv-modules.cache | |||
# | ==Part B== | ||
===Processes=== | |||
# Fork creates a new process with the same image resulting in two processes running identical code, where as exec replaces the current binary with a new binary and continues exection in the same process. | |||
# A zombie process is a process that has terminated its execution, but whose parent hasn't collected its exit status. | |||
# zombie.c | |||
#include <sys/wait.h> | #include <sys/wait.h> | ||
#include <stdlib.h> | #include <stdlib.h> | ||
Line 99: | Line 150: | ||
int main() | int main() | ||
{ | { | ||
int child = fork(); | int child = fork(); | ||
if(child == 0) { | |||
sleep(1); | |||
return 0; | |||
} | |||
else { | |||
sleep(10); //Make it a zombie...look at ps | |||
wait(NULL); //Enough of zombie fun, collect it now. | |||
return 0; | |||
} | |||
} | } | ||
4. nozombie.c | |||
/* Most of you are just waiting in the parent process with wait(). | |||
* But that means you cannot do anything else in that process - wait() is a blocking system call. | |||
* A better idea would be to use signal handler, where the parent registers for a signal if a child dies (SIGCHILD) | |||
* and then use wait() to collect the exit code. | |||
*/ | |||
#include <sys/wait.h> | #include <sys/wait.h> | ||
#include <stdlib.h> | #include <stdlib.h> | ||
Line 127: | Line 183: | ||
fflush(stdout); | fflush(stdout); | ||
//Waits for a specific child, and returns info about how child died | //Waits for a specific child, and returns info about how child died | ||
waitpid(child,&status,0); | waitpid(child,&status,0); | ||
} | } | ||
Line 145: | Line 201: | ||
} | } | ||
else { | else { | ||
sigaction(SIGCHLD, &act, NULL); | sigaction(SIGCHLD, &act, NULL); | ||
sleep(3); | sleep(3); | ||
return 0; | return 0; | ||
} | } | ||
} | } | ||
==Permissions== | ===Permissions=== | ||
# read, write, execute | |||
# | # You can 'cd' to that directory and list contents in it. | ||
# Read, Write, Read and Execute, List Folder Contents, Modify, Full Control | |||
# | # Write doesn't allow deletion of files or subfolders where as Modify does. | ||
# In Linux, when the sticky bit is set on a directory, files in that directory may only be unlinked or renamed by root or their owner. | |||
# | # Yes. For example, consider a file f and three groups each containing a single user assigned to f with read, write and read & execute permissions. In unix, this means that three different users should have three different permissions for a single file - which is impossible. Owner and others are fixed, and group can have a single octal permission. | ||
# | |||
# | |||
# | |||
===Environment=== | |||
# PATH contains a colon-separated list of directories in which the shell searches for binary files that have the same name as the command entered. PWD gives the current working directory. | |||
# $DISPLAY | |||
===Dynamic Libraries=== | |||
# Incompatible versions of DLLs might be required by two different binaries. A new application might replace a DLL with a version it works with, possibly breaking other applications. | |||
Latest revision as of 21:11, 28 September 2011
In this lab you will be learning the basics of command-line interaction with UNIX/Linux. This lab is due by 8 PM on Friday, September 23, 2011. Please submit a text or PDF document via WebCT.
Part A (in Tutorial)
Getting Started
For this lab, you need to get access to a Linux or UNIX machine. We suggest you use the existing SCS Linux boxes; however, you may do the following on any machine you have access to.
To access the SCS Linux boxes, log in to your SCS Windows account. Double-click on the "SCS Linux" icon on the left of your desktop and login to a less-loaded lambda machine (i.e., one with fewer users).
Alternatively, try accessing the lambda machines using putty, e.g., use host "lambda01.scs.carleton.ca", port 22.
Make sure you have only one instance of X-Win32 running; multiple instances will hang.
To get a terminal window, look under the Applications/Accessories submenu (at the top left).
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
- 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? (1 point)
- 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? (1 point)
- How do you quit the less command? (1 point)
Shell Basics
Note that csh is the default shell on the SCS systems. Other Linux distributions normally default to bash. Whenever there is a reference to csh in this document, you may substitute bash, zsh, or any other standard UNIX shell. Please, just be clear which shell you've chosen to use.
- 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? (1 point)
- Which of the following is another shell that can be used instead of csh? mv, cat, bc, bash, or arch (1 point)
- 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)? (1 point)
- The ps command is used to get a list of processes which are running on the system. Start a new shell by typing "csh". Does the new shell replace or run in parallel with the old shell? What about exec csh? You can use the ps command to determine if the shell is still running. (1 point)
- Because each process is started by some other process, there is a process tree structure on the system. From within your terminal, determine what process started csh by using the pstree command. What graphical application can give you the same information? (1 point)
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 of 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.
- Who is the owner of the /etc directory? (1 point)
- What group is associated with the /etc/shadow file? (1 point)
- 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 be a member of the group associated with the /etc/shadow file? (1 point)
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).
- On the command line, run the following two sets of commands in the bash or csh shell. Notice that the shell command will start a new shell separate from the shell that the HELLO environment variable was set in.
HELLO="Hi There" or set HELLO="Hi There" bash csh echo $HELLO echo $HELLO exit exit
export HELLO="Hi There" or setenv HELLO "Hi There" bash csh echo $HELLO echo $HELLO exit exit
Note: Do not put spaces on either side of the equal signs!
What does the export command seem to do? (Or, what is the difference between set and setenv?) (2 points)
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.
- Using ldd, what dynamic library dependencies does the top command have? Note that you must specify the full path to top. (1 point)
- 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 csh 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? (2 points)
Part B (take home)
Processes
- What is the difference between the fork and exec function in a UNIX environment? ( 1 point )
- What is a zombie process? ( 1 point )
- Give an example C program which creates a zombie process. Note that the shell by default will collect and destroy zombie processes and so you will need to avoid the shell destroying the zombie process during debugging. This can be done by delaying the parent exit (using sleep is one good way to do this). ( 2 points )
- Perform the modifications to your program above to avoid creating a zombie process. List the new program. ( 3 points )
Permissions
- Permissions on Unix are grouped into three basic file operations. What are these file operations? ( 1 point )
- What does it mean to have execute permission on a directory? ( 1 point )
- What are the 6 basic file permissions within Windows? ( 1 point )
- What is the difference between the write and modify file permission in Windows? ( 1 point )
- 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). ( 1 point )
- In Windows, a file can be associated with more than one group and each group can have different access permissions. On Unix, each file can only belong to one group and groups cannot contain other groups (groups can only contain users). Having said that, new groups can be created by the system administrator which are supersets of other groups (e.g., to create group C with members of A and B, just add all of A's and B's members to C). 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. ( 1 point )
Environment
- What does the PATH environment variable do? PWD? ( 1 point )
- What environment variable tells X applications where to find the X server which it should communicate with to display the output? ( 1 point )
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)? ( 1 point )
Answers
Part A
Help
- Manual
- The break or brk() function sets the break or lowest address of a process's data segment (uninitialized data) to addr (immediately above bss). Memory is allocated by brk in page size pieces; if addr is not evenly divisible by the system page size, it is increased to the next page boundary. It returns a pointer to the new end of memory if successful; otherwise -1 with errno set to indicate why allocation failed.
- using the letter 'q'.
Shell Basics
- /usr/bin/top
- bash
- ls -a
- runs in parallel, exec csh replaces the shell without creating a new process.
- csh or sshd (for putty users) and gnome-terminal for graphical representation system monitor with dependencies.
Permissions
- root
- shadow
- no
Environment
- Set sets shell variables specific to that particular shell instance, and the setenv sets environment variables for all subsequently called shell instances.
Dynamic Libraries
- (Your answers might vary a bit)
linux-gate.so.1 => (0x00b26000)
libproc-3.2.8.so => /lib/libproc-3.2.8.so (0x00541000)
libncurses.so.5 => /lib/libncurses.so.5 (0x002a9000)
libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0x00c63000)
libdl.so.2 => /lib/i386-linux-gnu/libdl.so.2 (0x00e6c000)
/lib/ld-linux.so.2 (0x00a0c000) - (Your answers might vary a bit)
/lib/libcrypt-2.11.1.so
/lib/ld-2.11.1.so
/usr/lib/gconv/gconv-modules.cache
Part B
Processes
- Fork creates a new process with the same image resulting in two processes running identical code, where as exec replaces the current binary with a new binary and continues exection in the same process.
- A zombie process is a process that has terminated its execution, but whose parent hasn't collected its exit status.
- zombie.c
#include <sys/wait.h> #include <stdlib.h> #include <unistd.h> #include <stdio.h> #include <signal.h> int main() { int child = fork(); if(child == 0) { sleep(1); return 0; } else { sleep(10); //Make it a zombie...look at ps wait(NULL); //Enough of zombie fun, collect it now. return 0; } }
4. nozombie.c
/* Most of you are just waiting in the parent process with wait(). * But that means you cannot do anything else in that process - wait() is a blocking system call. * A better idea would be to use signal handler, where the parent registers for a signal if a child dies (SIGCHILD) * and then use wait() to collect the exit code. */ #include <sys/wait.h> #include <stdlib.h> #include <unistd.h> #include <stdio.h> #include <signal.h> int child; void sigchild_handler(int signum) { int status; printf("Signal handler called.\n"); fflush(stdout); //Waits for a specific child, and returns info about how child died waitpid(child,&status,0); } int main() { struct sigaction act; act.sa_handler = sigchild_handler; act.sa_flags = 0; sigemptyset(&act.sa_mask); child = fork(); if(child == 0) { sleep(1); return 0; } else { sigaction(SIGCHLD, &act, NULL); sleep(3); return 0; } }
Permissions
- read, write, execute
- You can 'cd' to that directory and list contents in it.
- Read, Write, Read and Execute, List Folder Contents, Modify, Full Control
- Write doesn't allow deletion of files or subfolders where as Modify does.
- In Linux, when the sticky bit is set on a directory, files in that directory may only be unlinked or renamed by root or their owner.
- Yes. For example, consider a file f and three groups each containing a single user assigned to f with read, write and read & execute permissions. In unix, this means that three different users should have three different permissions for a single file - which is impossible. Owner and others are fixed, and group can have a single octal permission.
Environment
- PATH contains a colon-separated list of directories in which the shell searches for binary files that have the same name as the command entered. PWD gives the current working directory.
- $DISPLAY
Dynamic Libraries
- Incompatible versions of DLLs might be required by two different binaries. A new application might replace a DLL with a version it works with, possibly breaking other applications.