<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://homeostasis.scs.carleton.ca/wiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Saran</id>
	<title>Soma-notes - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://homeostasis.scs.carleton.ca/wiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Saran"/>
	<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php/Special:Contributions/Saran"/>
	<updated>2026-05-02T08:54:23Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.42.1</generator>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_7_2011&amp;diff=15571</id>
		<title>COMP 3000 Lab 7 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_7_2011&amp;diff=15571"/>
		<updated>2011-12-12T21:28:57Z</updated>

		<summary type="html">&lt;p&gt;Saran: /* Part B */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by Sunday, November 27th at 11:30 PM.  &lt;br /&gt;
* Please answer using a single text file (with a .txt extension). Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.) &#039;&#039;&#039;Anything other than a single text file will receive -1 points&#039;&#039;&#039;&lt;br /&gt;
* Show all your work.  If you find an answer by browsing code, explain your search path (e.g., I searched for X, which led me to source file Y, where I found function Z.)   Also, list any websites or individuals you consult.&lt;br /&gt;
* Do all of the following on a Linux system.  Ubuntu Linux is guaranteed to work, but other Linux systems should work.  Note that *BSD systems, including MacOS X, will produce different answers for several of the following questions.&lt;br /&gt;
* Note that the tools from Lab 2 in particular may be of use here...&lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
# [3] Run &amp;lt;tt&amp;gt;ifconfig&amp;lt;/tt&amp;gt;.  For all configured interfaces except loopback, there should be an &amp;quot;inet&amp;quot; address, a &amp;quot;HWaddr&amp;quot; (hardware) address, and perhaps even an &amp;quot;inet6&amp;quot; address.  Each of these addresses identifies the computer within a certain context.  In what context are each of these addresses used?  Specifically, for what protocol is the address used, and what other computers can access this one using this address?  (Ignore NAT issues.)&lt;br /&gt;
# [2] What does MTU refer to in the output of &amp;lt;tt&amp;gt;ifconfig&amp;lt;/tt&amp;gt;?  Specifically, what does the acronym mean, and what does this value determine?&lt;br /&gt;
# [1] How could you use netcat (&amp;lt;tt&amp;gt;nc&amp;lt;/tt&amp;gt;) to listen on port 9200 and write received data to the file /tmp/foo?&lt;br /&gt;
# [1] What does &amp;lt;tt&amp;gt;tracepath slashdot.org&amp;lt;/tt&amp;gt; return?&lt;br /&gt;
# [1] How can you get &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt; to return a list of all current TCP connections and open ports, listed using numeric IP addresses (rather than DNS hostnames)?&lt;br /&gt;
# [1] How could I use &amp;lt;tt&amp;gt;lsof&amp;lt;/tt&amp;gt; to find all of the processes accessing the directory &amp;lt;tt&amp;gt;/home&amp;lt;/tt&amp;gt;?&lt;br /&gt;
# [2] In &amp;lt;tt&amp;gt;/proc/&amp;lt;PID&amp;gt;/maps&amp;lt;/tt&amp;gt;, each entry has four permission bits: r,w,x, &amp;amp; p.  What do each of them indicate?&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# [2] Why does &amp;lt;tt&amp;gt;tracepath&amp;lt;/tt&amp;gt; return &amp;quot;no reply&amp;quot; sometimes?&lt;br /&gt;
# [3] &amp;lt;tt&amp;gt;nc&amp;lt;/tt&amp;gt; uses very few system calls (three on Ubuntu 11.10) when actually receiving data on the network.  (The other system calls are for setup and for ending the connection.)  What are those few system calls, and what do each of them do?&lt;br /&gt;
# [2] What file does &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt; access to find out about current TCP connections on Linux?  And, what is the difference between this file and the output of &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt;, in syntax and semantics?  Be specific.&lt;br /&gt;
# [2] What does &amp;lt;tt&amp;gt;lsof&amp;lt;/tt&amp;gt; sometimes get a &amp;quot;permission denied&amp;quot; error when run as a regular user?  Specifically, what files is it trying to access, and why are these protected?&lt;br /&gt;
# [2] What kernel data structure stores the permissions listed in /proc/&amp;lt;PID&amp;gt;/maps?&lt;br /&gt;
&lt;br /&gt;
==Answers==&lt;br /&gt;
===Part A===&lt;br /&gt;
# inet address is the IPv4 address of the host. The [http://en.wikipedia.org/wiki/Reserved_IP_addresses#Reserved_IPv4_addresses scope] is usually either a subnet or global, the former meaning other computers on the same subnet can talk, and the latter meaning any computer with an access to the Internet can talk to this one. HWaddr is the MAC address of the network card. Computers on the same local network (ethernet/wifi etc) connected via a switch or a hub or some such device can talk to it without having to use the Network layer. inet6 address, or the IPv6 address is a newer replacement for the aging IPv4 address. If its [http://en.wikipedia.org/wiki/IPv6_address#IPv6_address_scopes scope] is only link, that means it doesn&#039;t have a globally routable address. Routers drop packets that originate with a [http://en.wikipedia.org/wiki/Link-local_address link-local] address. A global scope means a globally routable ipv6 address.&lt;br /&gt;
# MTU, or the Maximum Transmission Unit, is the maximum frame size of whatever layer 2 protocol is under use, ( e.g Ethernet ). Some of you&#039;ve answered that MTU is the maximum IP packet size - that is [http://superuser.com/questions/220429/what-layer-does-ifconfig-operate-at incorrect]. You can verify it if you&#039;d like by changing mtu of your eth0, running Wireshark, and looking at frame length ( which in my case seems to be upper-bounded by MTU + 14 bytes ). I&#039;m unsure about what those additional 14 bytes are. Although, technically, you can limit the size of all egress IP packets from your NIC, typically IP layer dynamically figures out its MTUs - because they vary. See [http://en.wikipedia.org/wiki/Maximum_transmission_unit#Path_MTU_Discovery here] and [http://linux-ip.net/html/routing-icmp.html#routing-icmp-mtu here] for more info. &lt;br /&gt;
# nc -l 9200 &amp;gt; /tmp/foo&lt;br /&gt;
# From a lambda machine, I got this : &lt;br /&gt;
 1:  134.117.27.129    0.118ms pmtu 1500&lt;br /&gt;
 1:  134.117.27.1      5.051ms&lt;br /&gt;
 1:  134.117.27.1      5.660ms&lt;br /&gt;
 2:  10.50.254.10      5.377ms &lt;br /&gt;
 3:  10.30.33.1        5.561ms &lt;br /&gt;
 4:  10.30.53.1        5.711ms &lt;br /&gt;
 5:  134.117.254.242 114.264ms asymm  4 &lt;br /&gt;
 6:  134.117.254.243   4.572ms asymm  4 &lt;br /&gt;
 7:  10.30.56.1        4.442ms asymm  6 &lt;br /&gt;
 8:  no reply&lt;br /&gt;
 9:  154.54.40.137     7.099ms asymm  8 &lt;br /&gt;
 10:  154.54.42.85     19.135ms asymm  9 &lt;br /&gt;
 11:  154.54.44.218    19.609ms asymm 10 &lt;br /&gt;
 12:  no reply&lt;br /&gt;
 13:  204.70.198.17    20.179ms asymm 12 &lt;br /&gt;
 14:  204.70.196.241   30.987ms asymm 11 &lt;br /&gt;
 15:  no reply&lt;br /&gt;
 16:  no reply&lt;br /&gt;
 17:  64.27.160.194    35.202ms asymm 12 &lt;br /&gt;
 18:  216.34.181.45    31.924ms reached&lt;br /&gt;
5. netstat -ant&amp;lt;br&amp;gt;&lt;br /&gt;
6. lsof +D /home&amp;lt;br&amp;gt;&lt;br /&gt;
7. Read, Write, Execute (self-explanatory) and Private. A page can either be shared ( meaning - &amp;quot;Updates to the mapping are visible to other processes that map this file, and are carried through to the underlying file.&amp;quot; ), or private (Updates to the mapping are not visible to other processes mapping the same file, and are not carried through to the underlying file - using copy-on-write).&lt;br /&gt;
&lt;br /&gt;
===Part B ===&lt;br /&gt;
# Tracepath/Traceroute either use ICMP echo requests, or simply send a UDP packet to a big port number, and wait for ICMP destination unreachable packet. In either case, if a server doesn&#039;t receive an ICMP packet as response, tracepath says &amp;quot;no reply&amp;quot;. See [http://en.wikipedia.org/wiki/Traceroute here] or [http://www.inetdaemon.com/tutorials/troubleshooting/tools/traceroute/definition.shtml here]. Tracepath is essentially the same as traceroute. &lt;br /&gt;
# I see the following system calls : poll, read and write. Poll waits for the file descriptor status to change, until it is ready to perform I/O. read reads a fixed number of bytes into a buffer from a file descriptor, and write writes a given number of bytes to a file descriptor. The question asks for system calls used in the &amp;quot;steady state&amp;quot;, not for setting up etc. Use strace to check. &lt;br /&gt;
# /proc/net/tcp. Syntactically, the ip addresses and port numbers in netstat are human-readable and not in hex. The state values have been translated from numbers to words. Further, if used with flags, ( like --program ), netstat retrieves the application name related to the connection, by looking up inode info from /proc/net/tcp. &lt;br /&gt;
# When lsof recurses through a directory, and encounters a file that it cannot open, it says &amp;quot;permission denied&amp;quot;. This is because ordinary users don&#039;t have access to some protected files ( for e.g /etc/shadow )&lt;br /&gt;
# In [http://lxr.linux.no/linux+v3.1.4/include/linux/mm_types.h#L187 include/linux/mm_types.h], the following are used to store permissions. ( Some of you&#039;ve answered &amp;quot;Virtual Memory Area&amp;quot; - which is very vague. You need to specify the exact structure and the fields used in that ). &lt;br /&gt;
 pgprot_t vm_page_prot;          /* Access permissions of this VMA. */&lt;br /&gt;
 unsigned long vm_flags;         /* Flags, see mm.h. */&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_7_2011&amp;diff=15495</id>
		<title>COMP 3000 Lab 7 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_7_2011&amp;diff=15495"/>
		<updated>2011-12-07T22:28:27Z</updated>

		<summary type="html">&lt;p&gt;Saran: /* Part B */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by Sunday, November 27th at 11:30 PM.  &lt;br /&gt;
* Please answer using a single text file (with a .txt extension). Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.) &#039;&#039;&#039;Anything other than a single text file will receive -1 points&#039;&#039;&#039;&lt;br /&gt;
* Show all your work.  If you find an answer by browsing code, explain your search path (e.g., I searched for X, which led me to source file Y, where I found function Z.)   Also, list any websites or individuals you consult.&lt;br /&gt;
* Do all of the following on a Linux system.  Ubuntu Linux is guaranteed to work, but other Linux systems should work.  Note that *BSD systems, including MacOS X, will produce different answers for several of the following questions.&lt;br /&gt;
* Note that the tools from Lab 2 in particular may be of use here...&lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
# [3] Run &amp;lt;tt&amp;gt;ifconfig&amp;lt;/tt&amp;gt;.  For all configured interfaces except loopback, there should be an &amp;quot;inet&amp;quot; address, a &amp;quot;HWaddr&amp;quot; (hardware) address, and perhaps even an &amp;quot;inet6&amp;quot; address.  Each of these addresses identifies the computer within a certain context.  In what context are each of these addresses used?  Specifically, for what protocol is the address used, and what other computers can access this one using this address?  (Ignore NAT issues.)&lt;br /&gt;
# [2] What does MTU refer to in the output of &amp;lt;tt&amp;gt;ifconfig&amp;lt;/tt&amp;gt;?  Specifically, what does the acronym mean, and what does this value determine?&lt;br /&gt;
# [1] How could you use netcat (&amp;lt;tt&amp;gt;nc&amp;lt;/tt&amp;gt;) to listen on port 9200 and write received data to the file /tmp/foo?&lt;br /&gt;
# [1] What does &amp;lt;tt&amp;gt;tracepath slashdot.org&amp;lt;/tt&amp;gt; return?&lt;br /&gt;
# [1] How can you get &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt; to return a list of all current TCP connections and open ports, listed using numeric IP addresses (rather than DNS hostnames)?&lt;br /&gt;
# [1] How could I use &amp;lt;tt&amp;gt;lsof&amp;lt;/tt&amp;gt; to find all of the processes accessing the directory &amp;lt;tt&amp;gt;/home&amp;lt;/tt&amp;gt;?&lt;br /&gt;
# [2] In &amp;lt;tt&amp;gt;/proc/&amp;lt;PID&amp;gt;/maps&amp;lt;/tt&amp;gt;, each entry has four permission bits: r,w,x, &amp;amp; p.  What do each of them indicate?&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# [2] Why does &amp;lt;tt&amp;gt;tracepath&amp;lt;/tt&amp;gt; return &amp;quot;no reply&amp;quot; sometimes?&lt;br /&gt;
# [3] &amp;lt;tt&amp;gt;nc&amp;lt;/tt&amp;gt; uses very few system calls (three on Ubuntu 11.10) when actually receiving data on the network.  (The other system calls are for setup and for ending the connection.)  What are those few system calls, and what do each of them do?&lt;br /&gt;
# [2] What file does &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt; access to find out about current TCP connections on Linux?  And, what is the difference between this file and the output of &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt;, in syntax and semantics?  Be specific.&lt;br /&gt;
# [2] What does &amp;lt;tt&amp;gt;lsof&amp;lt;/tt&amp;gt; sometimes get a &amp;quot;permission denied&amp;quot; error when run as a regular user?  Specifically, what files is it trying to access, and why are these protected?&lt;br /&gt;
# [2] What kernel data structure stores the permissions listed in /proc/&amp;lt;PID&amp;gt;/maps?&lt;br /&gt;
&lt;br /&gt;
==Answers==&lt;br /&gt;
===Part A===&lt;br /&gt;
# inet address is the IPv4 address of the host. The [http://en.wikipedia.org/wiki/Reserved_IP_addresses#Reserved_IPv4_addresses scope] is usually either a subnet or global, the former meaning other computers on the same subnet can talk, and the latter meaning any computer with an access to the Internet can talk to this one. HWaddr is the MAC address of the network card. Computers on the same local network (ethernet/wifi etc) connected via a switch or a hub or some such device can talk to it without having to use the Network layer. inet6 address, or the IPv6 address is a newer replacement for the aging IPv4 address. If its [http://en.wikipedia.org/wiki/IPv6_address#IPv6_address_scopes scope] is only link, that means it doesn&#039;t have a globally routable address. Routers drop packets that originate with a [http://en.wikipedia.org/wiki/Link-local_address link-local] address. A global scope means a globally routable ipv6 address.&lt;br /&gt;
# MTU, or the Maximum Transmission Unit, is the maximum frame size of whatever layer 2 protocol is under use, ( e.g Ethernet ). Some of you&#039;ve answered that MTU is the maximum IP packet size - that is [http://superuser.com/questions/220429/what-layer-does-ifconfig-operate-at incorrect]. You can verify it if you&#039;d like by changing mtu of your eth0, running Wireshark, and looking at frame length ( which in my case seems to be upper-bounded by MTU + 14 bytes ). I&#039;m unsure about what those additional 14 bytes are. Although, technically, you can limit the size of all egress IP packets from your NIC, typically IP layer dynamically figures out its MTUs - because they vary. See [http://en.wikipedia.org/wiki/Maximum_transmission_unit#Path_MTU_Discovery here] and [http://linux-ip.net/html/routing-icmp.html#routing-icmp-mtu here] for more info. &lt;br /&gt;
# nc -l 9200 &amp;gt; /tmp/foo&lt;br /&gt;
# From a lambda machine, I got this : &lt;br /&gt;
 1:  134.117.27.129    0.118ms pmtu 1500&lt;br /&gt;
 1:  134.117.27.1      5.051ms&lt;br /&gt;
 1:  134.117.27.1      5.660ms&lt;br /&gt;
 2:  10.50.254.10      5.377ms &lt;br /&gt;
 3:  10.30.33.1        5.561ms &lt;br /&gt;
 4:  10.30.53.1        5.711ms &lt;br /&gt;
 5:  134.117.254.242 114.264ms asymm  4 &lt;br /&gt;
 6:  134.117.254.243   4.572ms asymm  4 &lt;br /&gt;
 7:  10.30.56.1        4.442ms asymm  6 &lt;br /&gt;
 8:  no reply&lt;br /&gt;
 9:  154.54.40.137     7.099ms asymm  8 &lt;br /&gt;
 10:  154.54.42.85     19.135ms asymm  9 &lt;br /&gt;
 11:  154.54.44.218    19.609ms asymm 10 &lt;br /&gt;
 12:  no reply&lt;br /&gt;
 13:  204.70.198.17    20.179ms asymm 12 &lt;br /&gt;
 14:  204.70.196.241   30.987ms asymm 11 &lt;br /&gt;
 15:  no reply&lt;br /&gt;
 16:  no reply&lt;br /&gt;
 17:  64.27.160.194    35.202ms asymm 12 &lt;br /&gt;
 18:  216.34.181.45    31.924ms reached&lt;br /&gt;
5. netstat -ant&amp;lt;br&amp;gt;&lt;br /&gt;
6. lsof +D /home&amp;lt;br&amp;gt;&lt;br /&gt;
7. Read, Write, Execute (self-explanatory) and Private. A page can either be shared ( meaning - &amp;quot;Updates to the mapping are visible to other processes that map this file, and are carried through to the underlying file.&amp;quot; ), or private (Updates to the mapping are not visible to other processes mapping the same file, and are not carried through to the underlying file - using copy-on-write).&lt;br /&gt;
&lt;br /&gt;
===Part B ===&lt;br /&gt;
# Tracepath/Traceroute either use ICMP echo requests, or simply send a UDP packet to a big port number, and wait for ICMP destination unreachable packet. In either case, if a server doesn&#039;t receive an ICMP packet as response, tracepath says &amp;quot;no reply&amp;quot;. See [http://en.wikipedia.org/wiki/Traceroute here] or [http://www.inetdaemon.com/tutorials/troubleshooting/tools/traceroute/definition.shtml here]. Tracepath is essentially the same as traceroute. &lt;br /&gt;
# I see the following system calls : poll, read and write. Poll waits for the file descriptor status to change, until it is ready to perform I/O. read reads a fixed number of bytes into a buffer from a file descriptor, and write writes a given number of bytes to a file descriptor. &lt;br /&gt;
# /proc/net/tcp. Syntactically, the ip addresses and port numbers in netstat are human-readable and not in hex. The state values have been translated from numbers to words. Further, if used with flags, ( like --program ), netstat retrieves the application name related to the connection, by looking up inode info from /proc/net/tcp. &lt;br /&gt;
# When lsof recurses through a directory, and encounters a file that it cannot open, it says &amp;quot;permission denied&amp;quot;. This is because ordinary users don&#039;t have access to some protected files ( for e.g /etc/shadow )&lt;br /&gt;
# In [http://lxr.linux.no/linux+v3.1.4/include/linux/mm_types.h#L187 include/linux/mm_types.h], the following are used to store permissions. ( Some of you&#039;ve answered &amp;quot;Virtual Memory Area&amp;quot; - which is very vague. You need to specify the exact structure and the fields used in that ). &lt;br /&gt;
 pgprot_t vm_page_prot;          /* Access permissions of this VMA. */&lt;br /&gt;
 unsigned long vm_flags;         /* Flags, see mm.h. */&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_7_2011&amp;diff=15489</id>
		<title>COMP 3000 Lab 7 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_7_2011&amp;diff=15489"/>
		<updated>2011-12-07T21:27:09Z</updated>

		<summary type="html">&lt;p&gt;Saran: /* Part B */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by Sunday, November 27th at 11:30 PM.  &lt;br /&gt;
* Please answer using a single text file (with a .txt extension). Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.) &#039;&#039;&#039;Anything other than a single text file will receive -1 points&#039;&#039;&#039;&lt;br /&gt;
* Show all your work.  If you find an answer by browsing code, explain your search path (e.g., I searched for X, which led me to source file Y, where I found function Z.)   Also, list any websites or individuals you consult.&lt;br /&gt;
* Do all of the following on a Linux system.  Ubuntu Linux is guaranteed to work, but other Linux systems should work.  Note that *BSD systems, including MacOS X, will produce different answers for several of the following questions.&lt;br /&gt;
* Note that the tools from Lab 2 in particular may be of use here...&lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
# [3] Run &amp;lt;tt&amp;gt;ifconfig&amp;lt;/tt&amp;gt;.  For all configured interfaces except loopback, there should be an &amp;quot;inet&amp;quot; address, a &amp;quot;HWaddr&amp;quot; (hardware) address, and perhaps even an &amp;quot;inet6&amp;quot; address.  Each of these addresses identifies the computer within a certain context.  In what context are each of these addresses used?  Specifically, for what protocol is the address used, and what other computers can access this one using this address?  (Ignore NAT issues.)&lt;br /&gt;
# [2] What does MTU refer to in the output of &amp;lt;tt&amp;gt;ifconfig&amp;lt;/tt&amp;gt;?  Specifically, what does the acronym mean, and what does this value determine?&lt;br /&gt;
# [1] How could you use netcat (&amp;lt;tt&amp;gt;nc&amp;lt;/tt&amp;gt;) to listen on port 9200 and write received data to the file /tmp/foo?&lt;br /&gt;
# [1] What does &amp;lt;tt&amp;gt;tracepath slashdot.org&amp;lt;/tt&amp;gt; return?&lt;br /&gt;
# [1] How can you get &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt; to return a list of all current TCP connections and open ports, listed using numeric IP addresses (rather than DNS hostnames)?&lt;br /&gt;
# [1] How could I use &amp;lt;tt&amp;gt;lsof&amp;lt;/tt&amp;gt; to find all of the processes accessing the directory &amp;lt;tt&amp;gt;/home&amp;lt;/tt&amp;gt;?&lt;br /&gt;
# [2] In &amp;lt;tt&amp;gt;/proc/&amp;lt;PID&amp;gt;/maps&amp;lt;/tt&amp;gt;, each entry has four permission bits: r,w,x, &amp;amp; p.  What do each of them indicate?&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# [2] Why does &amp;lt;tt&amp;gt;tracepath&amp;lt;/tt&amp;gt; return &amp;quot;no reply&amp;quot; sometimes?&lt;br /&gt;
# [3] &amp;lt;tt&amp;gt;nc&amp;lt;/tt&amp;gt; uses very few system calls (three on Ubuntu 11.10) when actually receiving data on the network.  (The other system calls are for setup and for ending the connection.)  What are those few system calls, and what do each of them do?&lt;br /&gt;
# [2] What file does &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt; access to find out about current TCP connections on Linux?  And, what is the difference between this file and the output of &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt;, in syntax and semantics?  Be specific.&lt;br /&gt;
# [2] What does &amp;lt;tt&amp;gt;lsof&amp;lt;/tt&amp;gt; sometimes get a &amp;quot;permission denied&amp;quot; error when run as a regular user?  Specifically, what files is it trying to access, and why are these protected?&lt;br /&gt;
# [2] What kernel data structure stores the permissions listed in /proc/&amp;lt;PID&amp;gt;/maps?&lt;br /&gt;
&lt;br /&gt;
==Answers==&lt;br /&gt;
===Part A===&lt;br /&gt;
# inet address is the IPv4 address of the host. The [http://en.wikipedia.org/wiki/Reserved_IP_addresses#Reserved_IPv4_addresses scope] is usually either a subnet or global, the former meaning other computers on the same subnet can talk, and the latter meaning any computer with an access to the Internet can talk to this one. HWaddr is the MAC address of the network card. Computers on the same local network (ethernet/wifi etc) connected via a switch or a hub or some such device can talk to it without having to use the Network layer. inet6 address, or the IPv6 address is a newer replacement for the aging IPv4 address. If its [http://en.wikipedia.org/wiki/IPv6_address#IPv6_address_scopes scope] is only link, that means it doesn&#039;t have a globally routable address. Routers drop packets that originate with a [http://en.wikipedia.org/wiki/Link-local_address link-local] address. A global scope means a globally routable ipv6 address.&lt;br /&gt;
# MTU, or the Maximum Transmission Unit, is the maximum frame size of whatever layer 2 protocol is under use, ( e.g Ethernet ). Some of you&#039;ve answered that MTU is the maximum IP packet size - that is [http://superuser.com/questions/220429/what-layer-does-ifconfig-operate-at incorrect]. You can verify it if you&#039;d like by changing mtu of your eth0, running Wireshark, and looking at frame length ( which in my case seems to be upper-bounded by MTU + 14 bytes ). I&#039;m unsure about what those additional 14 bytes are. Although, technically, you can limit the size of all egress IP packets from your NIC, typically IP layer dynamically figures out its MTUs - because they vary. See [http://en.wikipedia.org/wiki/Maximum_transmission_unit#Path_MTU_Discovery here] and [http://linux-ip.net/html/routing-icmp.html#routing-icmp-mtu here] for more info. &lt;br /&gt;
# nc -l 9200 &amp;gt; /tmp/foo&lt;br /&gt;
# From a lambda machine, I got this : &lt;br /&gt;
 1:  134.117.27.129    0.118ms pmtu 1500&lt;br /&gt;
 1:  134.117.27.1      5.051ms&lt;br /&gt;
 1:  134.117.27.1      5.660ms&lt;br /&gt;
 2:  10.50.254.10      5.377ms &lt;br /&gt;
 3:  10.30.33.1        5.561ms &lt;br /&gt;
 4:  10.30.53.1        5.711ms &lt;br /&gt;
 5:  134.117.254.242 114.264ms asymm  4 &lt;br /&gt;
 6:  134.117.254.243   4.572ms asymm  4 &lt;br /&gt;
 7:  10.30.56.1        4.442ms asymm  6 &lt;br /&gt;
 8:  no reply&lt;br /&gt;
 9:  154.54.40.137     7.099ms asymm  8 &lt;br /&gt;
 10:  154.54.42.85     19.135ms asymm  9 &lt;br /&gt;
 11:  154.54.44.218    19.609ms asymm 10 &lt;br /&gt;
 12:  no reply&lt;br /&gt;
 13:  204.70.198.17    20.179ms asymm 12 &lt;br /&gt;
 14:  204.70.196.241   30.987ms asymm 11 &lt;br /&gt;
 15:  no reply&lt;br /&gt;
 16:  no reply&lt;br /&gt;
 17:  64.27.160.194    35.202ms asymm 12 &lt;br /&gt;
 18:  216.34.181.45    31.924ms reached&lt;br /&gt;
5. netstat -ant&amp;lt;br&amp;gt;&lt;br /&gt;
6. lsof +D /home&amp;lt;br&amp;gt;&lt;br /&gt;
7. Read, Write, Execute (self-explanatory) and Private. A page can either be shared ( meaning - &amp;quot;Updates to the mapping are visible to other processes that map this file, and are carried through to the underlying file.&amp;quot; ), or private (Updates to the mapping are not visible to other processes mapping the same file, and are not carried through to the underlying file - using copy-on-write).&lt;br /&gt;
&lt;br /&gt;
===Part B ===&lt;br /&gt;
# Tracepath/Traceroute either use ICMP echo requests, or simply send a UDP packet to a big port number, and wait for ICMP destination unreachable packet. In either case, if a server doesn&#039;t receive an ICMP packet as response, tracepath says &amp;quot;no reply&amp;quot;. &lt;br /&gt;
# I see the following system calls : poll, read and write. Poll waits for the file descriptor status to change, until it is ready to perform I/O. read reads a fixed number of bytes into a buffer from a file descriptor, and write writes a given number of bytes to a file descriptor. &lt;br /&gt;
# /proc/net/tcp. Syntactically, the ip addresses and port numbers in netstat are human-readable and not in hex. The state values have been translated from numbers to words. Further, if used with flags, ( like --program ), netstat retrieves the application name related to the connection, by looking up inode info from /proc/net/tcp. &lt;br /&gt;
# When lsof recurses through a directory, and encounters a file that it cannot open, it says &amp;quot;permission denied&amp;quot;. This is because ordinary users don&#039;t have access to some protected files ( for e.g /etc/shadow )&lt;br /&gt;
# In [http://lxr.linux.no/linux+v3.1.4/include/linux/mm_types.h#L187 include/linux/mm_types.h], the following are used to store permissions. ( Some of you&#039;ve answered &amp;quot;Virtual Memory Area&amp;quot; - which is very vague. You need to specify the exact structure and the fields used in that ). &lt;br /&gt;
 pgprot_t vm_page_prot;          /* Access permissions of this VMA. */&lt;br /&gt;
 unsigned long vm_flags;         /* Flags, see mm.h. */&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_7_2011&amp;diff=15488</id>
		<title>COMP 3000 Lab 7 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_7_2011&amp;diff=15488"/>
		<updated>2011-12-07T21:07:53Z</updated>

		<summary type="html">&lt;p&gt;Saran: /* Part A */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by Sunday, November 27th at 11:30 PM.  &lt;br /&gt;
* Please answer using a single text file (with a .txt extension). Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.) &#039;&#039;&#039;Anything other than a single text file will receive -1 points&#039;&#039;&#039;&lt;br /&gt;
* Show all your work.  If you find an answer by browsing code, explain your search path (e.g., I searched for X, which led me to source file Y, where I found function Z.)   Also, list any websites or individuals you consult.&lt;br /&gt;
* Do all of the following on a Linux system.  Ubuntu Linux is guaranteed to work, but other Linux systems should work.  Note that *BSD systems, including MacOS X, will produce different answers for several of the following questions.&lt;br /&gt;
* Note that the tools from Lab 2 in particular may be of use here...&lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
# [3] Run &amp;lt;tt&amp;gt;ifconfig&amp;lt;/tt&amp;gt;.  For all configured interfaces except loopback, there should be an &amp;quot;inet&amp;quot; address, a &amp;quot;HWaddr&amp;quot; (hardware) address, and perhaps even an &amp;quot;inet6&amp;quot; address.  Each of these addresses identifies the computer within a certain context.  In what context are each of these addresses used?  Specifically, for what protocol is the address used, and what other computers can access this one using this address?  (Ignore NAT issues.)&lt;br /&gt;
# [2] What does MTU refer to in the output of &amp;lt;tt&amp;gt;ifconfig&amp;lt;/tt&amp;gt;?  Specifically, what does the acronym mean, and what does this value determine?&lt;br /&gt;
# [1] How could you use netcat (&amp;lt;tt&amp;gt;nc&amp;lt;/tt&amp;gt;) to listen on port 9200 and write received data to the file /tmp/foo?&lt;br /&gt;
# [1] What does &amp;lt;tt&amp;gt;tracepath slashdot.org&amp;lt;/tt&amp;gt; return?&lt;br /&gt;
# [1] How can you get &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt; to return a list of all current TCP connections and open ports, listed using numeric IP addresses (rather than DNS hostnames)?&lt;br /&gt;
# [1] How could I use &amp;lt;tt&amp;gt;lsof&amp;lt;/tt&amp;gt; to find all of the processes accessing the directory &amp;lt;tt&amp;gt;/home&amp;lt;/tt&amp;gt;?&lt;br /&gt;
# [2] In &amp;lt;tt&amp;gt;/proc/&amp;lt;PID&amp;gt;/maps&amp;lt;/tt&amp;gt;, each entry has four permission bits: r,w,x, &amp;amp; p.  What do each of them indicate?&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# [2] Why does &amp;lt;tt&amp;gt;tracepath&amp;lt;/tt&amp;gt; return &amp;quot;no reply&amp;quot; sometimes?&lt;br /&gt;
# [3] &amp;lt;tt&amp;gt;nc&amp;lt;/tt&amp;gt; uses very few system calls (three on Ubuntu 11.10) when actually receiving data on the network.  (The other system calls are for setup and for ending the connection.)  What are those few system calls, and what do each of them do?&lt;br /&gt;
# [2] What file does &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt; access to find out about current TCP connections on Linux?  And, what is the difference between this file and the output of &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt;, in syntax and semantics?  Be specific.&lt;br /&gt;
# [2] What does &amp;lt;tt&amp;gt;lsof&amp;lt;/tt&amp;gt; sometimes get a &amp;quot;permission denied&amp;quot; error when run as a regular user?  Specifically, what files is it trying to access, and why are these protected?&lt;br /&gt;
# [2] What kernel data structure stores the permissions listed in /proc/&amp;lt;PID&amp;gt;/maps?&lt;br /&gt;
&lt;br /&gt;
==Answers==&lt;br /&gt;
===Part A===&lt;br /&gt;
# inet address is the IPv4 address of the host. The [http://en.wikipedia.org/wiki/Reserved_IP_addresses#Reserved_IPv4_addresses scope] is usually either a subnet or global, the former meaning other computers on the same subnet can talk, and the latter meaning any computer with an access to the Internet can talk to this one. HWaddr is the MAC address of the network card. Computers on the same local network (ethernet/wifi etc) connected via a switch or a hub or some such device can talk to it without having to use the Network layer. inet6 address, or the IPv6 address is a newer replacement for the aging IPv4 address. If its [http://en.wikipedia.org/wiki/IPv6_address#IPv6_address_scopes scope] is only link, that means it doesn&#039;t have a globally routable address. Routers drop packets that originate with a [http://en.wikipedia.org/wiki/Link-local_address link-local] address. A global scope means a globally routable ipv6 address.&lt;br /&gt;
# MTU, or the Maximum Transmission Unit, is the maximum frame size of whatever layer 2 protocol is under use, ( e.g Ethernet ). Some of you&#039;ve answered that MTU is the maximum IP packet size - that is [http://superuser.com/questions/220429/what-layer-does-ifconfig-operate-at incorrect]. You can verify it if you&#039;d like by changing mtu of your eth0, running Wireshark, and looking at frame length ( which in my case seems to be upper-bounded by MTU + 14 bytes ). I&#039;m unsure about what those additional 14 bytes are. Although, technically, you can limit the size of all egress IP packets from your NIC, typically IP layer dynamically figures out its MTUs - because they vary. See [http://en.wikipedia.org/wiki/Maximum_transmission_unit#Path_MTU_Discovery here] and [http://linux-ip.net/html/routing-icmp.html#routing-icmp-mtu here] for more info. &lt;br /&gt;
# nc -l 9200 &amp;gt; /tmp/foo&lt;br /&gt;
# From a lambda machine, I got this : &lt;br /&gt;
 1:  134.117.27.129    0.118ms pmtu 1500&lt;br /&gt;
 1:  134.117.27.1      5.051ms&lt;br /&gt;
 1:  134.117.27.1      5.660ms&lt;br /&gt;
 2:  10.50.254.10      5.377ms &lt;br /&gt;
 3:  10.30.33.1        5.561ms &lt;br /&gt;
 4:  10.30.53.1        5.711ms &lt;br /&gt;
 5:  134.117.254.242 114.264ms asymm  4 &lt;br /&gt;
 6:  134.117.254.243   4.572ms asymm  4 &lt;br /&gt;
 7:  10.30.56.1        4.442ms asymm  6 &lt;br /&gt;
 8:  no reply&lt;br /&gt;
 9:  154.54.40.137     7.099ms asymm  8 &lt;br /&gt;
 10:  154.54.42.85     19.135ms asymm  9 &lt;br /&gt;
 11:  154.54.44.218    19.609ms asymm 10 &lt;br /&gt;
 12:  no reply&lt;br /&gt;
 13:  204.70.198.17    20.179ms asymm 12 &lt;br /&gt;
 14:  204.70.196.241   30.987ms asymm 11 &lt;br /&gt;
 15:  no reply&lt;br /&gt;
 16:  no reply&lt;br /&gt;
 17:  64.27.160.194    35.202ms asymm 12 &lt;br /&gt;
 18:  216.34.181.45    31.924ms reached&lt;br /&gt;
5. netstat -ant&amp;lt;br&amp;gt;&lt;br /&gt;
6. lsof +D /home&amp;lt;br&amp;gt;&lt;br /&gt;
7. Read, Write, Execute (self-explanatory) and Private. A page can either be shared ( meaning - &amp;quot;Updates to the mapping are visible to other processes that map this file, and are carried through to the underlying file.&amp;quot; ), or private (Updates to the mapping are not visible to other processes mapping the same file, and are not carried through to the underlying file - using copy-on-write).&lt;br /&gt;
&lt;br /&gt;
===Part B ===&lt;br /&gt;
# Tracepath/Traceroute either use ICMP echo requests, or simply send a UDP packet to a big port number, and wait for ICMP destination unreachable packet. In either case, if a server doesn&#039;t receive an ICMP packet as response, tracepath says &amp;quot;no reply&amp;quot;. &lt;br /&gt;
# I see the following system calls : poll, read and write. Poll waits for the file descriptor status to change, until it is ready to perform I/O. read reads a fixed number of bytes into a buffer from a file descriptor, and write writes a given number of bytes to a file descriptor. &lt;br /&gt;
# /proc/net/tcp. Syntactically, the ip addresses and port numbers in netstat are human-readable and not in hex. The state values have been translated from numbers to words. Further, if used with flags, ( like --program ), netstat retrieves the application name related to the connection, by looking up inode info from /proc/net/tcp. &lt;br /&gt;
# When lsof recurses through a directory, and encounters a file that it cannot open, it says &amp;quot;permission denied&amp;quot;. This is because ordinary users don&#039;t have access to some protected files ( for e.g /etc/shadow )&lt;br /&gt;
# In [http://lxr.linux.no/linux+v3.1.4/include/linux/mm_types.h#L187 include/linux/mm_types.h], the following are used to store permissions. &lt;br /&gt;
 pgprot_t vm_page_prot;          /* Access permissions of this VMA. */&lt;br /&gt;
 unsigned long vm_flags;         /* Flags, see mm.h. */&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_7_2011&amp;diff=15486</id>
		<title>COMP 3000 Lab 7 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_7_2011&amp;diff=15486"/>
		<updated>2011-12-07T20:46:20Z</updated>

		<summary type="html">&lt;p&gt;Saran: /* Part A */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by Sunday, November 27th at 11:30 PM.  &lt;br /&gt;
* Please answer using a single text file (with a .txt extension). Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.) &#039;&#039;&#039;Anything other than a single text file will receive -1 points&#039;&#039;&#039;&lt;br /&gt;
* Show all your work.  If you find an answer by browsing code, explain your search path (e.g., I searched for X, which led me to source file Y, where I found function Z.)   Also, list any websites or individuals you consult.&lt;br /&gt;
* Do all of the following on a Linux system.  Ubuntu Linux is guaranteed to work, but other Linux systems should work.  Note that *BSD systems, including MacOS X, will produce different answers for several of the following questions.&lt;br /&gt;
* Note that the tools from Lab 2 in particular may be of use here...&lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
# [3] Run &amp;lt;tt&amp;gt;ifconfig&amp;lt;/tt&amp;gt;.  For all configured interfaces except loopback, there should be an &amp;quot;inet&amp;quot; address, a &amp;quot;HWaddr&amp;quot; (hardware) address, and perhaps even an &amp;quot;inet6&amp;quot; address.  Each of these addresses identifies the computer within a certain context.  In what context are each of these addresses used?  Specifically, for what protocol is the address used, and what other computers can access this one using this address?  (Ignore NAT issues.)&lt;br /&gt;
# [2] What does MTU refer to in the output of &amp;lt;tt&amp;gt;ifconfig&amp;lt;/tt&amp;gt;?  Specifically, what does the acronym mean, and what does this value determine?&lt;br /&gt;
# [1] How could you use netcat (&amp;lt;tt&amp;gt;nc&amp;lt;/tt&amp;gt;) to listen on port 9200 and write received data to the file /tmp/foo?&lt;br /&gt;
# [1] What does &amp;lt;tt&amp;gt;tracepath slashdot.org&amp;lt;/tt&amp;gt; return?&lt;br /&gt;
# [1] How can you get &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt; to return a list of all current TCP connections and open ports, listed using numeric IP addresses (rather than DNS hostnames)?&lt;br /&gt;
# [1] How could I use &amp;lt;tt&amp;gt;lsof&amp;lt;/tt&amp;gt; to find all of the processes accessing the directory &amp;lt;tt&amp;gt;/home&amp;lt;/tt&amp;gt;?&lt;br /&gt;
# [2] In &amp;lt;tt&amp;gt;/proc/&amp;lt;PID&amp;gt;/maps&amp;lt;/tt&amp;gt;, each entry has four permission bits: r,w,x, &amp;amp; p.  What do each of them indicate?&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# [2] Why does &amp;lt;tt&amp;gt;tracepath&amp;lt;/tt&amp;gt; return &amp;quot;no reply&amp;quot; sometimes?&lt;br /&gt;
# [3] &amp;lt;tt&amp;gt;nc&amp;lt;/tt&amp;gt; uses very few system calls (three on Ubuntu 11.10) when actually receiving data on the network.  (The other system calls are for setup and for ending the connection.)  What are those few system calls, and what do each of them do?&lt;br /&gt;
# [2] What file does &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt; access to find out about current TCP connections on Linux?  And, what is the difference between this file and the output of &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt;, in syntax and semantics?  Be specific.&lt;br /&gt;
# [2] What does &amp;lt;tt&amp;gt;lsof&amp;lt;/tt&amp;gt; sometimes get a &amp;quot;permission denied&amp;quot; error when run as a regular user?  Specifically, what files is it trying to access, and why are these protected?&lt;br /&gt;
# [2] What kernel data structure stores the permissions listed in /proc/&amp;lt;PID&amp;gt;/maps?&lt;br /&gt;
&lt;br /&gt;
==Answers==&lt;br /&gt;
===Part A===&lt;br /&gt;
# inet address is the IPv4 address of the host. The [http://en.wikipedia.org/wiki/Reserved_IP_addresses#Reserved_IPv4_addresses scope] is usually either a subnet or global, the former meaning other computers on the same subnet can talk, and the latter meaning any computer with an access to the Internet can talk to this one. HWaddr is the MAC address of the network card. Computers on the same local network (ethernet/wifi etc) connected via a switch or a hub or some such device can talk to it without having to use the Network layer. inet6 address, or the IPv6 address is a newer replacement for the aging IPv4 address. If it&#039;s [http://en.wikipedia.org/wiki/IPv6_address#IPv6_address_scopes scope] is only link, that means it doesn&#039;t have a globally routable address. Routers drop packets that originate with a [http://en.wikipedia.org/wiki/Link-local_address link-local] address. A global scope means a globally routable ipv6 address.&lt;br /&gt;
# MTU, or the Maximum Transmission Unit, is the maximum frame size of whatever layer 2 protocol is under use, ( e.g Ethernet ). Some of you&#039;ve answered that MTU is the maximum IP packet size - that is [http://superuser.com/questions/220429/what-layer-does-ifconfig-operate-at incorrect]. You can verify it if you&#039;d like by changing mtu of your eth0, running Wireshark, and looking at frame length ( which in my case seems to be upper-bounded by MTU + 14 bytes ). I&#039;m unsure about what those additional 14 bytes are. Although, technically, you can limit the size of all egress IP packets from your NIC, typically IP layer dynamically figures out its MTUs - because they vary. See [http://en.wikipedia.org/wiki/Maximum_transmission_unit#Path_MTU_Discovery here] and [http://linux-ip.net/html/routing-icmp.html#routing-icmp-mtu here] for more info. &lt;br /&gt;
# nc -l 9200 &amp;gt; /tmp/foo&lt;br /&gt;
# From a lambda machine, I got this : &lt;br /&gt;
 1:  134.117.27.129    0.118ms pmtu 1500&lt;br /&gt;
 1:  134.117.27.1      5.051ms&lt;br /&gt;
 1:  134.117.27.1      5.660ms&lt;br /&gt;
 2:  10.50.254.10      5.377ms &lt;br /&gt;
 3:  10.30.33.1        5.561ms &lt;br /&gt;
 4:  10.30.53.1        5.711ms &lt;br /&gt;
 5:  134.117.254.242 114.264ms asymm  4 &lt;br /&gt;
 6:  134.117.254.243   4.572ms asymm  4 &lt;br /&gt;
 7:  10.30.56.1        4.442ms asymm  6 &lt;br /&gt;
 8:  no reply&lt;br /&gt;
 9:  154.54.40.137     7.099ms asymm  8 &lt;br /&gt;
 10:  154.54.42.85     19.135ms asymm  9 &lt;br /&gt;
 11:  154.54.44.218    19.609ms asymm 10 &lt;br /&gt;
 12:  no reply&lt;br /&gt;
 13:  204.70.198.17    20.179ms asymm 12 &lt;br /&gt;
 14:  204.70.196.241   30.987ms asymm 11 &lt;br /&gt;
 15:  no reply&lt;br /&gt;
 16:  no reply&lt;br /&gt;
 17:  64.27.160.194    35.202ms asymm 12 &lt;br /&gt;
 18:  216.34.181.45    31.924ms reached&lt;br /&gt;
5. netstat -ant&amp;lt;br&amp;gt;&lt;br /&gt;
6. lsof +D /home&amp;lt;br&amp;gt;&lt;br /&gt;
7. Read, Write, Execute (self-explanatory) and Private. A page can either be shared ( meaning - &amp;quot;Updates to the mapping are visible to other processes that map this file, and are carried through to the underlying file.&amp;quot; ), or private (Updates to the mapping are not visible to other processes mapping the same file, and are not carried through to the underlying file - using copy-on-write).&lt;br /&gt;
&lt;br /&gt;
===Part B ===&lt;br /&gt;
# Tracepath/Traceroute either use ICMP echo requests, or simply send a UDP packet to a big port number, and wait for ICMP destination unreachable packet. In either case, if a server doesn&#039;t receive an ICMP packet as response, tracepath says &amp;quot;no reply&amp;quot;. &lt;br /&gt;
# I see the following system calls : poll, read and write. Poll waits for the file descriptor status to change, until it is ready to perform I/O. read reads a fixed number of bytes into a buffer from a file descriptor, and write writes a given number of bytes to a file descriptor. &lt;br /&gt;
# /proc/net/tcp. Syntactically, the ip addresses and port numbers in netstat are human-readable and not in hex. The state values have been translated from numbers to words. Further, if used with flags, ( like --program ), netstat retrieves the application name related to the connection, by looking up inode info from /proc/net/tcp. &lt;br /&gt;
# When lsof recurses through a directory, and encounters a file that it cannot open, it says &amp;quot;permission denied&amp;quot;. This is because ordinary users don&#039;t have access to some protected files ( for e.g /etc/shadow )&lt;br /&gt;
# In [http://lxr.linux.no/linux+v3.1.4/include/linux/mm_types.h#L187 include/linux/mm_types.h], the following are used to store permissions. &lt;br /&gt;
 pgprot_t vm_page_prot;          /* Access permissions of this VMA. */&lt;br /&gt;
 unsigned long vm_flags;         /* Flags, see mm.h. */&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_7_2011&amp;diff=15485</id>
		<title>COMP 3000 Lab 7 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_7_2011&amp;diff=15485"/>
		<updated>2011-12-07T20:46:04Z</updated>

		<summary type="html">&lt;p&gt;Saran: /* Part A */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by Sunday, November 27th at 11:30 PM.  &lt;br /&gt;
* Please answer using a single text file (with a .txt extension). Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.) &#039;&#039;&#039;Anything other than a single text file will receive -1 points&#039;&#039;&#039;&lt;br /&gt;
* Show all your work.  If you find an answer by browsing code, explain your search path (e.g., I searched for X, which led me to source file Y, where I found function Z.)   Also, list any websites or individuals you consult.&lt;br /&gt;
* Do all of the following on a Linux system.  Ubuntu Linux is guaranteed to work, but other Linux systems should work.  Note that *BSD systems, including MacOS X, will produce different answers for several of the following questions.&lt;br /&gt;
* Note that the tools from Lab 2 in particular may be of use here...&lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
# [3] Run &amp;lt;tt&amp;gt;ifconfig&amp;lt;/tt&amp;gt;.  For all configured interfaces except loopback, there should be an &amp;quot;inet&amp;quot; address, a &amp;quot;HWaddr&amp;quot; (hardware) address, and perhaps even an &amp;quot;inet6&amp;quot; address.  Each of these addresses identifies the computer within a certain context.  In what context are each of these addresses used?  Specifically, for what protocol is the address used, and what other computers can access this one using this address?  (Ignore NAT issues.)&lt;br /&gt;
# [2] What does MTU refer to in the output of &amp;lt;tt&amp;gt;ifconfig&amp;lt;/tt&amp;gt;?  Specifically, what does the acronym mean, and what does this value determine?&lt;br /&gt;
# [1] How could you use netcat (&amp;lt;tt&amp;gt;nc&amp;lt;/tt&amp;gt;) to listen on port 9200 and write received data to the file /tmp/foo?&lt;br /&gt;
# [1] What does &amp;lt;tt&amp;gt;tracepath slashdot.org&amp;lt;/tt&amp;gt; return?&lt;br /&gt;
# [1] How can you get &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt; to return a list of all current TCP connections and open ports, listed using numeric IP addresses (rather than DNS hostnames)?&lt;br /&gt;
# [1] How could I use &amp;lt;tt&amp;gt;lsof&amp;lt;/tt&amp;gt; to find all of the processes accessing the directory &amp;lt;tt&amp;gt;/home&amp;lt;/tt&amp;gt;?&lt;br /&gt;
# [2] In &amp;lt;tt&amp;gt;/proc/&amp;lt;PID&amp;gt;/maps&amp;lt;/tt&amp;gt;, each entry has four permission bits: r,w,x, &amp;amp; p.  What do each of them indicate?&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# [2] Why does &amp;lt;tt&amp;gt;tracepath&amp;lt;/tt&amp;gt; return &amp;quot;no reply&amp;quot; sometimes?&lt;br /&gt;
# [3] &amp;lt;tt&amp;gt;nc&amp;lt;/tt&amp;gt; uses very few system calls (three on Ubuntu 11.10) when actually receiving data on the network.  (The other system calls are for setup and for ending the connection.)  What are those few system calls, and what do each of them do?&lt;br /&gt;
# [2] What file does &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt; access to find out about current TCP connections on Linux?  And, what is the difference between this file and the output of &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt;, in syntax and semantics?  Be specific.&lt;br /&gt;
# [2] What does &amp;lt;tt&amp;gt;lsof&amp;lt;/tt&amp;gt; sometimes get a &amp;quot;permission denied&amp;quot; error when run as a regular user?  Specifically, what files is it trying to access, and why are these protected?&lt;br /&gt;
# [2] What kernel data structure stores the permissions listed in /proc/&amp;lt;PID&amp;gt;/maps?&lt;br /&gt;
&lt;br /&gt;
==Answers==&lt;br /&gt;
===Part A===&lt;br /&gt;
# inet address is the IPv4 address of the host. The [http://en.wikipedia.org/wiki/Reserved_IP_addresses#Reserved_IPv4_addresses scope] is usually either a subnet or global, the former meaning other computers on the same subnet can talk, and the latter meaning any computer with an access to the Internet can talk to this one. HWaddr is the MAC address of the network card. Computers on the same local network (ethernet/wifi etc) connected via a switch or a hub or some such device can talk to it without having to use the Network layer. inet6 address, or the IPv6 address is a newer replacement for the aging IPv4 address. If it&#039;s [http://en.wikipedia.org/wiki/IPv6_address#IPv6_address_scopes scope] is only link, that means it doesn&#039;t have a globally routable address. Routers drop packets that originate with a [http://en.wikipedia.org/wiki/Link-local_address link-local] address. A global scope means a globally routable ipv6 address.&lt;br /&gt;
# MTU, or the Maximum Transmission Unit, is the maximum frame size of whatever layer 2 protocol is under use, ( e.g Ethernet ). Some of you&#039;ve answered that MTU is the maximum IP packet size - that is [incorrect http://superuser.com/questions/220429/what-layer-does-ifconfig-operate-at ]. You can verify it if you&#039;d like by changing mtu of your eth0, running Wireshark, and looking at frame length ( which in my case seems to be upper-bounded by MTU + 14 bytes ). I&#039;m unsure about what those additional 14 bytes are. Although, technically, you can limit the size of all egress IP packets from your NIC, typically IP layer dynamically figures out its MTUs - because they vary. See [http://en.wikipedia.org/wiki/Maximum_transmission_unit#Path_MTU_Discovery here] and [http://linux-ip.net/html/routing-icmp.html#routing-icmp-mtu here] for more info. &lt;br /&gt;
# nc -l 9200 &amp;gt; /tmp/foo&lt;br /&gt;
# From a lambda machine, I got this : &lt;br /&gt;
 1:  134.117.27.129    0.118ms pmtu 1500&lt;br /&gt;
 1:  134.117.27.1      5.051ms&lt;br /&gt;
 1:  134.117.27.1      5.660ms&lt;br /&gt;
 2:  10.50.254.10      5.377ms &lt;br /&gt;
 3:  10.30.33.1        5.561ms &lt;br /&gt;
 4:  10.30.53.1        5.711ms &lt;br /&gt;
 5:  134.117.254.242 114.264ms asymm  4 &lt;br /&gt;
 6:  134.117.254.243   4.572ms asymm  4 &lt;br /&gt;
 7:  10.30.56.1        4.442ms asymm  6 &lt;br /&gt;
 8:  no reply&lt;br /&gt;
 9:  154.54.40.137     7.099ms asymm  8 &lt;br /&gt;
 10:  154.54.42.85     19.135ms asymm  9 &lt;br /&gt;
 11:  154.54.44.218    19.609ms asymm 10 &lt;br /&gt;
 12:  no reply&lt;br /&gt;
 13:  204.70.198.17    20.179ms asymm 12 &lt;br /&gt;
 14:  204.70.196.241   30.987ms asymm 11 &lt;br /&gt;
 15:  no reply&lt;br /&gt;
 16:  no reply&lt;br /&gt;
 17:  64.27.160.194    35.202ms asymm 12 &lt;br /&gt;
 18:  216.34.181.45    31.924ms reached&lt;br /&gt;
5. netstat -ant&amp;lt;br&amp;gt;&lt;br /&gt;
6. lsof +D /home&amp;lt;br&amp;gt;&lt;br /&gt;
7. Read, Write, Execute (self-explanatory) and Private. A page can either be shared ( meaning - &amp;quot;Updates to the mapping are visible to other processes that map this file, and are carried through to the underlying file.&amp;quot; ), or private (Updates to the mapping are not visible to other processes mapping the same file, and are not carried through to the underlying file - using copy-on-write).&lt;br /&gt;
&lt;br /&gt;
===Part B ===&lt;br /&gt;
# Tracepath/Traceroute either use ICMP echo requests, or simply send a UDP packet to a big port number, and wait for ICMP destination unreachable packet. In either case, if a server doesn&#039;t receive an ICMP packet as response, tracepath says &amp;quot;no reply&amp;quot;. &lt;br /&gt;
# I see the following system calls : poll, read and write. Poll waits for the file descriptor status to change, until it is ready to perform I/O. read reads a fixed number of bytes into a buffer from a file descriptor, and write writes a given number of bytes to a file descriptor. &lt;br /&gt;
# /proc/net/tcp. Syntactically, the ip addresses and port numbers in netstat are human-readable and not in hex. The state values have been translated from numbers to words. Further, if used with flags, ( like --program ), netstat retrieves the application name related to the connection, by looking up inode info from /proc/net/tcp. &lt;br /&gt;
# When lsof recurses through a directory, and encounters a file that it cannot open, it says &amp;quot;permission denied&amp;quot;. This is because ordinary users don&#039;t have access to some protected files ( for e.g /etc/shadow )&lt;br /&gt;
# In [http://lxr.linux.no/linux+v3.1.4/include/linux/mm_types.h#L187 include/linux/mm_types.h], the following are used to store permissions. &lt;br /&gt;
 pgprot_t vm_page_prot;          /* Access permissions of this VMA. */&lt;br /&gt;
 unsigned long vm_flags;         /* Flags, see mm.h. */&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_7_2011&amp;diff=15484</id>
		<title>COMP 3000 Lab 7 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_7_2011&amp;diff=15484"/>
		<updated>2011-12-07T20:17:54Z</updated>

		<summary type="html">&lt;p&gt;Saran: /* Part B */  How did the last question of part B change???&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by Sunday, November 27th at 11:30 PM.  &lt;br /&gt;
* Please answer using a single text file (with a .txt extension). Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.) &#039;&#039;&#039;Anything other than a single text file will receive -1 points&#039;&#039;&#039;&lt;br /&gt;
* Show all your work.  If you find an answer by browsing code, explain your search path (e.g., I searched for X, which led me to source file Y, where I found function Z.)   Also, list any websites or individuals you consult.&lt;br /&gt;
* Do all of the following on a Linux system.  Ubuntu Linux is guaranteed to work, but other Linux systems should work.  Note that *BSD systems, including MacOS X, will produce different answers for several of the following questions.&lt;br /&gt;
* Note that the tools from Lab 2 in particular may be of use here...&lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
# [3] Run &amp;lt;tt&amp;gt;ifconfig&amp;lt;/tt&amp;gt;.  For all configured interfaces except loopback, there should be an &amp;quot;inet&amp;quot; address, a &amp;quot;HWaddr&amp;quot; (hardware) address, and perhaps even an &amp;quot;inet6&amp;quot; address.  Each of these addresses identifies the computer within a certain context.  In what context are each of these addresses used?  Specifically, for what protocol is the address used, and what other computers can access this one using this address?  (Ignore NAT issues.)&lt;br /&gt;
# [2] What does MTU refer to in the output of &amp;lt;tt&amp;gt;ifconfig&amp;lt;/tt&amp;gt;?  Specifically, what does the acronym mean, and what does this value determine?&lt;br /&gt;
# [1] How could you use netcat (&amp;lt;tt&amp;gt;nc&amp;lt;/tt&amp;gt;) to listen on port 9200 and write received data to the file /tmp/foo?&lt;br /&gt;
# [1] What does &amp;lt;tt&amp;gt;tracepath slashdot.org&amp;lt;/tt&amp;gt; return?&lt;br /&gt;
# [1] How can you get &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt; to return a list of all current TCP connections and open ports, listed using numeric IP addresses (rather than DNS hostnames)?&lt;br /&gt;
# [1] How could I use &amp;lt;tt&amp;gt;lsof&amp;lt;/tt&amp;gt; to find all of the processes accessing the directory &amp;lt;tt&amp;gt;/home&amp;lt;/tt&amp;gt;?&lt;br /&gt;
# [2] In &amp;lt;tt&amp;gt;/proc/&amp;lt;PID&amp;gt;/maps&amp;lt;/tt&amp;gt;, each entry has four permission bits: r,w,x, &amp;amp; p.  What do each of them indicate?&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# [2] Why does &amp;lt;tt&amp;gt;tracepath&amp;lt;/tt&amp;gt; return &amp;quot;no reply&amp;quot; sometimes?&lt;br /&gt;
# [3] &amp;lt;tt&amp;gt;nc&amp;lt;/tt&amp;gt; uses very few system calls (three on Ubuntu 11.10) when actually receiving data on the network.  (The other system calls are for setup and for ending the connection.)  What are those few system calls, and what do each of them do?&lt;br /&gt;
# [2] What file does &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt; access to find out about current TCP connections on Linux?  And, what is the difference between this file and the output of &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt;, in syntax and semantics?  Be specific.&lt;br /&gt;
# [2] What does &amp;lt;tt&amp;gt;lsof&amp;lt;/tt&amp;gt; sometimes get a &amp;quot;permission denied&amp;quot; error when run as a regular user?  Specifically, what files is it trying to access, and why are these protected?&lt;br /&gt;
# [2] What kernel data structure stores the permissions listed in /proc/&amp;lt;PID&amp;gt;/maps?&lt;br /&gt;
&lt;br /&gt;
==Answers==&lt;br /&gt;
===Part A===&lt;br /&gt;
# inet address is the IPv4 address of the host. The [http://en.wikipedia.org/wiki/Reserved_IP_addresses#Reserved_IPv4_addresses scope] is usually either a subnet or global, the former meaning other computers on the same subnet can talk, and the latter meaning any computer with an access to the Internet can talk to this one. HWaddr is the MAC address of the network card. Computers on the same local network (ethernet/wifi etc) connected via a switch or a hub or some such device can talk to it without having to use the Network layer. inet6 address, or the IPv6 address is a newer replacement for the aging IPv4 address. If it&#039;s [http://en.wikipedia.org/wiki/IPv6_address#IPv6_address_scopes scope] is only link, that means it doesn&#039;t have a globally routable address. Routers drop packets that originate with a [http://en.wikipedia.org/wiki/Link-local_address link-local] address. A global scope means a globally routable ipv6 address.&lt;br /&gt;
# MTU, or the Maximum Transmission Unit, is the maximum frame size of whatever layer 2 protocol is under use, ( e.g Ethernet ). &lt;br /&gt;
# nc -l 9200 &amp;gt; /tmp/foo&lt;br /&gt;
# From a lambda machine, I got this : &lt;br /&gt;
 1:  134.117.27.129    0.118ms pmtu 1500&lt;br /&gt;
 1:  134.117.27.1      5.051ms&lt;br /&gt;
 1:  134.117.27.1      5.660ms&lt;br /&gt;
 2:  10.50.254.10      5.377ms &lt;br /&gt;
 3:  10.30.33.1        5.561ms &lt;br /&gt;
 4:  10.30.53.1        5.711ms &lt;br /&gt;
 5:  134.117.254.242 114.264ms asymm  4 &lt;br /&gt;
 6:  134.117.254.243   4.572ms asymm  4 &lt;br /&gt;
 7:  10.30.56.1        4.442ms asymm  6 &lt;br /&gt;
 8:  no reply&lt;br /&gt;
 9:  154.54.40.137     7.099ms asymm  8 &lt;br /&gt;
 10:  154.54.42.85     19.135ms asymm  9 &lt;br /&gt;
 11:  154.54.44.218    19.609ms asymm 10 &lt;br /&gt;
 12:  no reply&lt;br /&gt;
 13:  204.70.198.17    20.179ms asymm 12 &lt;br /&gt;
 14:  204.70.196.241   30.987ms asymm 11 &lt;br /&gt;
 15:  no reply&lt;br /&gt;
 16:  no reply&lt;br /&gt;
 17:  64.27.160.194    35.202ms asymm 12 &lt;br /&gt;
 18:  216.34.181.45    31.924ms reached&lt;br /&gt;
5. netstat -ant&amp;lt;br&amp;gt;&lt;br /&gt;
6. lsof +D /home&amp;lt;br&amp;gt;&lt;br /&gt;
7. Read, Write, Execute (self-explanatory) and Private. A page can either be shared ( meaning - &amp;quot;Updates to the mapping are visible to other processes that map this file, and are carried through to the underlying file.&amp;quot; ), or private (Updates to the mapping are not visible to other processes mapping the same file, and are not carried through to the underlying file - using copy-on-write).&lt;br /&gt;
&lt;br /&gt;
===Part B ===&lt;br /&gt;
# Tracepath/Traceroute either use ICMP echo requests, or simply send a UDP packet to a big port number, and wait for ICMP destination unreachable packet. In either case, if a server doesn&#039;t receive an ICMP packet as response, tracepath says &amp;quot;no reply&amp;quot;. &lt;br /&gt;
# I see the following system calls : poll, read and write. Poll waits for the file descriptor status to change, until it is ready to perform I/O. read reads a fixed number of bytes into a buffer from a file descriptor, and write writes a given number of bytes to a file descriptor. &lt;br /&gt;
# /proc/net/tcp. Syntactically, the ip addresses and port numbers in netstat are human-readable and not in hex. The state values have been translated from numbers to words. Further, if used with flags, ( like --program ), netstat retrieves the application name related to the connection, by looking up inode info from /proc/net/tcp. &lt;br /&gt;
# When lsof recurses through a directory, and encounters a file that it cannot open, it says &amp;quot;permission denied&amp;quot;. This is because ordinary users don&#039;t have access to some protected files ( for e.g /etc/shadow )&lt;br /&gt;
# In [http://lxr.linux.no/linux+v3.1.4/include/linux/mm_types.h#L187 include/linux/mm_types.h], the following are used to store permissions. &lt;br /&gt;
 pgprot_t vm_page_prot;          /* Access permissions of this VMA. */&lt;br /&gt;
 unsigned long vm_flags;         /* Flags, see mm.h. */&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_7_2011&amp;diff=15483</id>
		<title>COMP 3000 Lab 7 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_7_2011&amp;diff=15483"/>
		<updated>2011-12-07T19:57:16Z</updated>

		<summary type="html">&lt;p&gt;Saran: /* Part A */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by Sunday, November 27th at 11:30 PM.  &lt;br /&gt;
* Please answer using a single text file (with a .txt extension). Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.) &#039;&#039;&#039;Anything other than a single text file will receive -1 points&#039;&#039;&#039;&lt;br /&gt;
* Show all your work.  If you find an answer by browsing code, explain your search path (e.g., I searched for X, which led me to source file Y, where I found function Z.)   Also, list any websites or individuals you consult.&lt;br /&gt;
* Do all of the following on a Linux system.  Ubuntu Linux is guaranteed to work, but other Linux systems should work.  Note that *BSD systems, including MacOS X, will produce different answers for several of the following questions.&lt;br /&gt;
* Note that the tools from Lab 2 in particular may be of use here...&lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
# [3] Run &amp;lt;tt&amp;gt;ifconfig&amp;lt;/tt&amp;gt;.  For all configured interfaces except loopback, there should be an &amp;quot;inet&amp;quot; address, a &amp;quot;HWaddr&amp;quot; (hardware) address, and perhaps even an &amp;quot;inet6&amp;quot; address.  Each of these addresses identifies the computer within a certain context.  In what context are each of these addresses used?  Specifically, for what protocol is the address used, and what other computers can access this one using this address?  (Ignore NAT issues.)&lt;br /&gt;
# [2] What does MTU refer to in the output of &amp;lt;tt&amp;gt;ifconfig&amp;lt;/tt&amp;gt;?  Specifically, what does the acronym mean, and what does this value determine?&lt;br /&gt;
# [1] How could you use netcat (&amp;lt;tt&amp;gt;nc&amp;lt;/tt&amp;gt;) to listen on port 9200 and write received data to the file /tmp/foo?&lt;br /&gt;
# [1] What does &amp;lt;tt&amp;gt;tracepath slashdot.org&amp;lt;/tt&amp;gt; return?&lt;br /&gt;
# [1] How can you get &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt; to return a list of all current TCP connections and open ports, listed using numeric IP addresses (rather than DNS hostnames)?&lt;br /&gt;
# [1] How could I use &amp;lt;tt&amp;gt;lsof&amp;lt;/tt&amp;gt; to find all of the processes accessing the directory &amp;lt;tt&amp;gt;/home&amp;lt;/tt&amp;gt;?&lt;br /&gt;
# [2] In &amp;lt;tt&amp;gt;/proc/&amp;lt;PID&amp;gt;/maps&amp;lt;/tt&amp;gt;, each entry has four permission bits: r,w,x, &amp;amp; p.  What do each of them indicate?&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# [2] Why does &amp;lt;tt&amp;gt;tracepath&amp;lt;/tt&amp;gt; return &amp;quot;no reply&amp;quot; sometimes?&lt;br /&gt;
# [3] &amp;lt;tt&amp;gt;nc&amp;lt;/tt&amp;gt; uses very few system calls (three on Ubuntu 11.10) when actually receiving data on the network.  (The other system calls are for setup and for ending the connection.)  What are those few system calls, and what do each of them do?&lt;br /&gt;
# [2] What file does &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt; access to find out about current TCP connections on Linux?  And, what is the difference between this file and the output of &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt;, in syntax and semantics?  Be specific.&lt;br /&gt;
# [2] What does &amp;lt;tt&amp;gt;lsof&amp;lt;/tt&amp;gt; sometimes get a &amp;quot;permission denied&amp;quot; error when run as a regular user?  Specifically, what files is it trying to access, and why are these protected?&lt;br /&gt;
# [2] Can you tell from &amp;lt;tt&amp;gt;/proc/&amp;lt;PID&amp;gt;/maps&amp;lt;/tt&amp;gt; which memory segments are shared for read-only access?  What about read-write access?  Explain briefly.&lt;br /&gt;
&lt;br /&gt;
==Answers==&lt;br /&gt;
===Part A===&lt;br /&gt;
# inet address is the IPv4 address of the host. The [http://en.wikipedia.org/wiki/Reserved_IP_addresses#Reserved_IPv4_addresses scope] is usually either a subnet or global, the former meaning other computers on the same subnet can talk, and the latter meaning any computer with an access to the Internet can talk to this one. HWaddr is the MAC address of the network card. Computers on the same local network (ethernet/wifi etc) connected via a switch or a hub or some such device can talk to it without having to use the Network layer. inet6 address, or the IPv6 address is a newer replacement for the aging IPv4 address. If it&#039;s [http://en.wikipedia.org/wiki/IPv6_address#IPv6_address_scopes scope] is only link, that means it doesn&#039;t have a globally routable address. Routers drop packets that originate with a [http://en.wikipedia.org/wiki/Link-local_address link-local] address. A global scope means a globally routable ipv6 address.&lt;br /&gt;
# MTU, or the Maximum Transmission Unit, is the maximum frame size of whatever layer 2 protocol is under use, ( e.g Ethernet ). &lt;br /&gt;
# nc -l 9200 &amp;gt; /tmp/foo&lt;br /&gt;
# From a lambda machine, I got this : &lt;br /&gt;
 1:  134.117.27.129    0.118ms pmtu 1500&lt;br /&gt;
 1:  134.117.27.1      5.051ms&lt;br /&gt;
 1:  134.117.27.1      5.660ms&lt;br /&gt;
 2:  10.50.254.10      5.377ms &lt;br /&gt;
 3:  10.30.33.1        5.561ms &lt;br /&gt;
 4:  10.30.53.1        5.711ms &lt;br /&gt;
 5:  134.117.254.242 114.264ms asymm  4 &lt;br /&gt;
 6:  134.117.254.243   4.572ms asymm  4 &lt;br /&gt;
 7:  10.30.56.1        4.442ms asymm  6 &lt;br /&gt;
 8:  no reply&lt;br /&gt;
 9:  154.54.40.137     7.099ms asymm  8 &lt;br /&gt;
 10:  154.54.42.85     19.135ms asymm  9 &lt;br /&gt;
 11:  154.54.44.218    19.609ms asymm 10 &lt;br /&gt;
 12:  no reply&lt;br /&gt;
 13:  204.70.198.17    20.179ms asymm 12 &lt;br /&gt;
 14:  204.70.196.241   30.987ms asymm 11 &lt;br /&gt;
 15:  no reply&lt;br /&gt;
 16:  no reply&lt;br /&gt;
 17:  64.27.160.194    35.202ms asymm 12 &lt;br /&gt;
 18:  216.34.181.45    31.924ms reached&lt;br /&gt;
5. netstat -ant&amp;lt;br&amp;gt;&lt;br /&gt;
6. lsof +D /home&amp;lt;br&amp;gt;&lt;br /&gt;
7. Read, Write, Execute (self-explanatory) and Private. A page can either be shared ( meaning - &amp;quot;Updates to the mapping are visible to other processes that map this file, and are carried through to the underlying file.&amp;quot; ), or private (Updates to the mapping are not visible to other processes mapping the same file, and are not carried through to the underlying file - using copy-on-write).&lt;br /&gt;
&lt;br /&gt;
===Part B ===&lt;br /&gt;
# Tracepath/Traceroute either use ICMP echo requests, or simply send a UDP packet to a big port number, and wait for ICMP destination unreachable packet. In either case, if a server doesn&#039;t receive an ICMP packet as response, tracepath says &amp;quot;no reply&amp;quot;. &lt;br /&gt;
# I see the following system calls : poll, read and write. Poll waits for the file descriptor status to change, until it is ready to perform I/O. read reads a fixed number of bytes into a buffer from a file descriptor, and write writes a given number of bytes to a file descriptor. &lt;br /&gt;
# /proc/net/tcp. Syntactically, the ip addresses and port numbers in netstat are human-readable and not in hex. The state values have been translated from numbers to words. Further, if used with flags, ( like --program ), netstat retrieves the application name related to the connection, by looking up inode info from /proc/net/tcp. &lt;br /&gt;
# When lsof recurses through a directory, and encounters a file that it cannot open, it says &amp;quot;permission denied&amp;quot;. This is because ordinary users don&#039;t have access to some protected files ( for e.g /etc/shadow )&lt;br /&gt;
# In [http://lxr.linux.no/linux+v3.1.4/include/linux/mm_types.h#L187 include/linux/mm_types.h], the following are used to store permissions. &lt;br /&gt;
 pgprot_t vm_page_prot;          /* Access permissions of this VMA. */&lt;br /&gt;
 unsigned long vm_flags;         /* Flags, see mm.h. */&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_7_2011&amp;diff=15219</id>
		<title>COMP 3000 Lab 7 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_7_2011&amp;diff=15219"/>
		<updated>2011-12-05T04:39:28Z</updated>

		<summary type="html">&lt;p&gt;Saran: /* Part B */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by Sunday, November 27th at 11:30 PM.  &lt;br /&gt;
* Please answer using a single text file (with a .txt extension). Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.) &#039;&#039;&#039;Anything other than a single text file will receive -1 points&#039;&#039;&#039;&lt;br /&gt;
* Show all your work.  If you find an answer by browsing code, explain your search path (e.g., I searched for X, which led me to source file Y, where I found function Z.)   Also, list any websites or individuals you consult.&lt;br /&gt;
* Do all of the following on a Linux system.  Ubuntu Linux is guaranteed to work, but other Linux systems should work.  Note that *BSD systems, including MacOS X, will produce different answers for several of the following questions.&lt;br /&gt;
* Note that the tools from Lab 2 in particular may be of use here...&lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
# [3] Run &amp;lt;tt&amp;gt;ifconfig&amp;lt;/tt&amp;gt;.  For all configured interfaces except loopback, there should be an &amp;quot;inet&amp;quot; address, a &amp;quot;HWaddr&amp;quot; (hardware) address, and perhaps even an &amp;quot;inet6&amp;quot; address.  Each of these addresses identifies the computer within a certain context.  In what context are each of these addresses used?  Specifically, for what protocol is the address used, and what other computers can access this one using this address?  (Ignore NAT issues.)&lt;br /&gt;
# [2] What does MTU refer to in the output of &amp;lt;tt&amp;gt;ifconfig&amp;lt;/tt&amp;gt;?  Specifically, what does the acronym mean, and what does this value determine?&lt;br /&gt;
# How could you use netcat (&amp;lt;tt&amp;gt;nc&amp;lt;/tt&amp;gt;) to listen on port 9200 and write received data to the file /tmp/foo?&lt;br /&gt;
# What does &amp;lt;tt&amp;gt;tracepath slashdot.org&amp;lt;/tt&amp;gt; return?&lt;br /&gt;
# How can you get &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt; to return a list of all current TCP connections and open ports, listed using numeric IP addresses (rather than DNS hostnames)?&lt;br /&gt;
# How could I use &amp;lt;tt&amp;gt;lsof&amp;lt;/tt&amp;gt; to find all of the processes accessing the directory &amp;lt;tt&amp;gt;/home&amp;lt;/tt&amp;gt;?&lt;br /&gt;
# [2] In &amp;lt;tt&amp;gt;/proc/&amp;lt;PID&amp;gt;/maps&amp;lt;/tt&amp;gt;, each entry has four permission bits: r,w,x, &amp;amp; p.  What do each of them indicate?&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# [2] Why does &amp;lt;tt&amp;gt;tracepath&amp;lt;/tt&amp;gt; return &amp;quot;no reply&amp;quot; sometimes?&lt;br /&gt;
# [3] &amp;lt;tt&amp;gt;nc&amp;lt;/tt&amp;gt; uses very few system calls (three on Ubuntu 11.10) when actually receiving data on the network.  (The other system calls are for setup and for ending the connection.)  What are those few system calls, and what do each of them do?&lt;br /&gt;
# [2] What file does &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt; access to find out about current TCP connections on Linux?  And, what is the difference between this file and the output of &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt;, in syntax and semantics?  Be specific.&lt;br /&gt;
# [2] What does &amp;lt;tt&amp;gt;lsof&amp;lt;/tt&amp;gt; sometimes get a &amp;quot;permission denied&amp;quot; error when run as a regular user?  Specifically, what files is it trying to access, and why are these protected?&lt;br /&gt;
# [2] Can you tell from &amp;lt;tt&amp;gt;/proc/&amp;lt;PID&amp;gt;/maps&amp;lt;/tt&amp;gt; which memory segments are shared for read-only access?  What about read-write access?  Explain briefly.&lt;br /&gt;
&lt;br /&gt;
==Answers==&lt;br /&gt;
===Part A===&lt;br /&gt;
# inet address is the IPv4 address of the host. The [http://en.wikipedia.org/wiki/Reserved_IP_addresses#Reserved_IPv4_addresses scope] is usually either a subnet or global, the former meaning other computers on the same subnet can talk, and the latter meaning any computer with an access to the Internet can talk to this one. HWaddr is the MAC address of the network card. Computers on the same local network (ethernet/wifi etc) connected via a switch or a hub or some such device can talk to it without having to use the Network layer. inet6 address, or the IPv6 address is a newer replacement for the aging IPv4 address. If it&#039;s [http://en.wikipedia.org/wiki/IPv6_address#IPv6_address_scopes scope] is only link, that means it doesn&#039;t have a globally routable address. Routers drop packets that originate with a [http://en.wikipedia.org/wiki/Link-local_address link-local] address. A global scope means a globally routable ipv6 address.&lt;br /&gt;
# MTU, or the Maximum Transmission Unit, is the maximum frame size of whatever layer 2 protocol is under use, ( e.g Ethernet ). &lt;br /&gt;
# nc -l 9200 &amp;gt; /tmp/foo&lt;br /&gt;
# From a lambda machine, I got this : &lt;br /&gt;
 1:  134.117.27.129    0.118ms pmtu 1500&lt;br /&gt;
 1:  134.117.27.1      5.051ms&lt;br /&gt;
 1:  134.117.27.1      5.660ms&lt;br /&gt;
 2:  10.50.254.10      5.377ms &lt;br /&gt;
 3:  10.30.33.1        5.561ms &lt;br /&gt;
 4:  10.30.53.1        5.711ms &lt;br /&gt;
 5:  134.117.254.242 114.264ms asymm  4 &lt;br /&gt;
 6:  134.117.254.243   4.572ms asymm  4 &lt;br /&gt;
 7:  10.30.56.1        4.442ms asymm  6 &lt;br /&gt;
 8:  no reply&lt;br /&gt;
 9:  154.54.40.137     7.099ms asymm  8 &lt;br /&gt;
 10:  154.54.42.85     19.135ms asymm  9 &lt;br /&gt;
 11:  154.54.44.218    19.609ms asymm 10 &lt;br /&gt;
 12:  no reply&lt;br /&gt;
 13:  204.70.198.17    20.179ms asymm 12 &lt;br /&gt;
 14:  204.70.196.241   30.987ms asymm 11 &lt;br /&gt;
 15:  no reply&lt;br /&gt;
 16:  no reply&lt;br /&gt;
 17:  64.27.160.194    35.202ms asymm 12 &lt;br /&gt;
 18:  216.34.181.45    31.924ms reached&lt;br /&gt;
5. netstat -ant&amp;lt;br&amp;gt;&lt;br /&gt;
6. lsof +D /home&amp;lt;br&amp;gt;&lt;br /&gt;
7. Read, Write, Execute (self-explanatory) and Private. A page can either be shared ( meaning - &amp;quot;Updates to the mapping are visible to other processes that map this file, and are carried through to the underlying file.&amp;quot; ), or private (Updates to the mapping are not visible to other processes mapping the same file, and are not carried through to the underlying file - using copy-on-write).&lt;br /&gt;
&lt;br /&gt;
===Part B ===&lt;br /&gt;
# Tracepath/Traceroute either use ICMP echo requests, or simply send a UDP packet to a big port number, and wait for ICMP destination unreachable packet. In either case, if a server doesn&#039;t receive an ICMP packet as response, tracepath says &amp;quot;no reply&amp;quot;. &lt;br /&gt;
# I see the following system calls : poll, read and write. Poll waits for the file descriptor status to change, until it is ready to perform I/O. read reads a fixed number of bytes into a buffer from a file descriptor, and write writes a given number of bytes to a file descriptor. &lt;br /&gt;
# /proc/net/tcp. Syntactically, the ip addresses and port numbers in netstat are human-readable and not in hex. The state values have been translated from numbers to words. Further, if used with flags, ( like --program ), netstat retrieves the application name related to the connection, by looking up inode info from /proc/net/tcp. &lt;br /&gt;
# When lsof recurses through a directory, and encounters a file that it cannot open, it says &amp;quot;permission denied&amp;quot;. This is because ordinary users don&#039;t have access to some protected files ( for e.g /etc/shadow )&lt;br /&gt;
# In [http://lxr.linux.no/linux+v3.1.4/include/linux/mm_types.h#L187 include/linux/mm_types.h], the following are used to store permissions. &lt;br /&gt;
 pgprot_t vm_page_prot;          /* Access permissions of this VMA. */&lt;br /&gt;
 unsigned long vm_flags;         /* Flags, see mm.h. */&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_7_2011&amp;diff=15218</id>
		<title>COMP 3000 Lab 7 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_7_2011&amp;diff=15218"/>
		<updated>2011-12-05T04:38:53Z</updated>

		<summary type="html">&lt;p&gt;Saran: /* Part B */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by Sunday, November 27th at 11:30 PM.  &lt;br /&gt;
* Please answer using a single text file (with a .txt extension). Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.) &#039;&#039;&#039;Anything other than a single text file will receive -1 points&#039;&#039;&#039;&lt;br /&gt;
* Show all your work.  If you find an answer by browsing code, explain your search path (e.g., I searched for X, which led me to source file Y, where I found function Z.)   Also, list any websites or individuals you consult.&lt;br /&gt;
* Do all of the following on a Linux system.  Ubuntu Linux is guaranteed to work, but other Linux systems should work.  Note that *BSD systems, including MacOS X, will produce different answers for several of the following questions.&lt;br /&gt;
* Note that the tools from Lab 2 in particular may be of use here...&lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
# [3] Run &amp;lt;tt&amp;gt;ifconfig&amp;lt;/tt&amp;gt;.  For all configured interfaces except loopback, there should be an &amp;quot;inet&amp;quot; address, a &amp;quot;HWaddr&amp;quot; (hardware) address, and perhaps even an &amp;quot;inet6&amp;quot; address.  Each of these addresses identifies the computer within a certain context.  In what context are each of these addresses used?  Specifically, for what protocol is the address used, and what other computers can access this one using this address?  (Ignore NAT issues.)&lt;br /&gt;
# [2] What does MTU refer to in the output of &amp;lt;tt&amp;gt;ifconfig&amp;lt;/tt&amp;gt;?  Specifically, what does the acronym mean, and what does this value determine?&lt;br /&gt;
# How could you use netcat (&amp;lt;tt&amp;gt;nc&amp;lt;/tt&amp;gt;) to listen on port 9200 and write received data to the file /tmp/foo?&lt;br /&gt;
# What does &amp;lt;tt&amp;gt;tracepath slashdot.org&amp;lt;/tt&amp;gt; return?&lt;br /&gt;
# How can you get &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt; to return a list of all current TCP connections and open ports, listed using numeric IP addresses (rather than DNS hostnames)?&lt;br /&gt;
# How could I use &amp;lt;tt&amp;gt;lsof&amp;lt;/tt&amp;gt; to find all of the processes accessing the directory &amp;lt;tt&amp;gt;/home&amp;lt;/tt&amp;gt;?&lt;br /&gt;
# [2] In &amp;lt;tt&amp;gt;/proc/&amp;lt;PID&amp;gt;/maps&amp;lt;/tt&amp;gt;, each entry has four permission bits: r,w,x, &amp;amp; p.  What do each of them indicate?&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# [2] Why does &amp;lt;tt&amp;gt;tracepath&amp;lt;/tt&amp;gt; return &amp;quot;no reply&amp;quot; sometimes?&lt;br /&gt;
# [3] &amp;lt;tt&amp;gt;nc&amp;lt;/tt&amp;gt; uses very few system calls (three on Ubuntu 11.10) when actually receiving data on the network.  (The other system calls are for setup and for ending the connection.)  What are those few system calls, and what do each of them do?&lt;br /&gt;
# [2] What file does &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt; access to find out about current TCP connections on Linux?  And, what is the difference between this file and the output of &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt;, in syntax and semantics?  Be specific.&lt;br /&gt;
# [2] What does &amp;lt;tt&amp;gt;lsof&amp;lt;/tt&amp;gt; sometimes get a &amp;quot;permission denied&amp;quot; error when run as a regular user?  Specifically, what files is it trying to access, and why are these protected?&lt;br /&gt;
# [2] Can you tell from &amp;lt;tt&amp;gt;/proc/&amp;lt;PID&amp;gt;/maps&amp;lt;/tt&amp;gt; which memory segments are shared for read-only access?  What about read-write access?  Explain briefly.&lt;br /&gt;
&lt;br /&gt;
==Answers==&lt;br /&gt;
===Part A===&lt;br /&gt;
# inet address is the IPv4 address of the host. The [http://en.wikipedia.org/wiki/Reserved_IP_addresses#Reserved_IPv4_addresses scope] is usually either a subnet or global, the former meaning other computers on the same subnet can talk, and the latter meaning any computer with an access to the Internet can talk to this one. HWaddr is the MAC address of the network card. Computers on the same local network (ethernet/wifi etc) connected via a switch or a hub or some such device can talk to it without having to use the Network layer. inet6 address, or the IPv6 address is a newer replacement for the aging IPv4 address. If it&#039;s [http://en.wikipedia.org/wiki/IPv6_address#IPv6_address_scopes scope] is only link, that means it doesn&#039;t have a globally routable address. Routers drop packets that originate with a [http://en.wikipedia.org/wiki/Link-local_address link-local] address. A global scope means a globally routable ipv6 address.&lt;br /&gt;
# MTU, or the Maximum Transmission Unit, is the maximum frame size of whatever layer 2 protocol is under use, ( e.g Ethernet ). &lt;br /&gt;
# nc -l 9200 &amp;gt; /tmp/foo&lt;br /&gt;
# From a lambda machine, I got this : &lt;br /&gt;
 1:  134.117.27.129    0.118ms pmtu 1500&lt;br /&gt;
 1:  134.117.27.1      5.051ms&lt;br /&gt;
 1:  134.117.27.1      5.660ms&lt;br /&gt;
 2:  10.50.254.10      5.377ms &lt;br /&gt;
 3:  10.30.33.1        5.561ms &lt;br /&gt;
 4:  10.30.53.1        5.711ms &lt;br /&gt;
 5:  134.117.254.242 114.264ms asymm  4 &lt;br /&gt;
 6:  134.117.254.243   4.572ms asymm  4 &lt;br /&gt;
 7:  10.30.56.1        4.442ms asymm  6 &lt;br /&gt;
 8:  no reply&lt;br /&gt;
 9:  154.54.40.137     7.099ms asymm  8 &lt;br /&gt;
 10:  154.54.42.85     19.135ms asymm  9 &lt;br /&gt;
 11:  154.54.44.218    19.609ms asymm 10 &lt;br /&gt;
 12:  no reply&lt;br /&gt;
 13:  204.70.198.17    20.179ms asymm 12 &lt;br /&gt;
 14:  204.70.196.241   30.987ms asymm 11 &lt;br /&gt;
 15:  no reply&lt;br /&gt;
 16:  no reply&lt;br /&gt;
 17:  64.27.160.194    35.202ms asymm 12 &lt;br /&gt;
 18:  216.34.181.45    31.924ms reached&lt;br /&gt;
5. netstat -ant&amp;lt;br&amp;gt;&lt;br /&gt;
6. lsof +D /home&amp;lt;br&amp;gt;&lt;br /&gt;
7. Read, Write, Execute (self-explanatory) and Private. A page can either be shared ( meaning - &amp;quot;Updates to the mapping are visible to other processes that map this file, and are carried through to the underlying file.&amp;quot; ), or private (Updates to the mapping are not visible to other processes mapping the same file, and are not carried through to the underlying file - using copy-on-write).&lt;br /&gt;
&lt;br /&gt;
===Part B ===&lt;br /&gt;
# Tracepath/Traceroute either use ICMP echo requests, or simply send a UDP packet to a big port number, and wait for ICMP destination unreachable packet. In either case, if a server doesn&#039;t receive an ICMP packet as response, tracepath says &amp;quot;no reply&amp;quot;. &lt;br /&gt;
# I see the following system calls : poll, read and write. Poll waits for the file descriptor status to change, until it is ready to perform I/O. read reads a fixed number of bytes into a buffer from a file descriptor, and write writes a given number of bytes to a file descriptor. &lt;br /&gt;
# /proc/net/tcp. Syntactically, the ip addresses and port numbers in netstat are human-readable and not in hex. The state values have been translated from numbers to words. Further, if used with flags, ( like --program ), netstat retrieves the application name related to the connection, by looking up inode info from /proc/net/tcp. &lt;br /&gt;
# When lsof recurses through a directory, and encounters a file that it cannot open, it says &amp;quot;permission denied&amp;quot;. This is because ordinary users don&#039;t have access to some protected files ( for e.g /etc/shadow )&lt;br /&gt;
# In include/linux/mm_types.h, the following are used to store permissions. &lt;br /&gt;
 pgprot_t vm_page_prot;          /* Access permissions of this VMA. */&lt;br /&gt;
 unsigned long vm_flags;         /* Flags, see mm.h. */&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_7_2011&amp;diff=15212</id>
		<title>COMP 3000 Lab 7 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_7_2011&amp;diff=15212"/>
		<updated>2011-12-05T04:30:06Z</updated>

		<summary type="html">&lt;p&gt;Saran: /* Part A */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by Sunday, November 27th at 11:30 PM.  &lt;br /&gt;
* Please answer using a single text file (with a .txt extension). Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.) &#039;&#039;&#039;Anything other than a single text file will receive -1 points&#039;&#039;&#039;&lt;br /&gt;
* Show all your work.  If you find an answer by browsing code, explain your search path (e.g., I searched for X, which led me to source file Y, where I found function Z.)   Also, list any websites or individuals you consult.&lt;br /&gt;
* Do all of the following on a Linux system.  Ubuntu Linux is guaranteed to work, but other Linux systems should work.  Note that *BSD systems, including MacOS X, will produce different answers for several of the following questions.&lt;br /&gt;
* Note that the tools from Lab 2 in particular may be of use here...&lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
# [3] Run &amp;lt;tt&amp;gt;ifconfig&amp;lt;/tt&amp;gt;.  For all configured interfaces except loopback, there should be an &amp;quot;inet&amp;quot; address, a &amp;quot;HWaddr&amp;quot; (hardware) address, and perhaps even an &amp;quot;inet6&amp;quot; address.  Each of these addresses identifies the computer within a certain context.  In what context are each of these addresses used?  Specifically, for what protocol is the address used, and what other computers can access this one using this address?  (Ignore NAT issues.)&lt;br /&gt;
# [2] What does MTU refer to in the output of &amp;lt;tt&amp;gt;ifconfig&amp;lt;/tt&amp;gt;?  Specifically, what does the acronym mean, and what does this value determine?&lt;br /&gt;
# How could you use netcat (&amp;lt;tt&amp;gt;nc&amp;lt;/tt&amp;gt;) to listen on port 9200 and write received data to the file /tmp/foo?&lt;br /&gt;
# What does &amp;lt;tt&amp;gt;tracepath slashdot.org&amp;lt;/tt&amp;gt; return?&lt;br /&gt;
# How can you get &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt; to return a list of all current TCP connections and open ports, listed using numeric IP addresses (rather than DNS hostnames)?&lt;br /&gt;
# How could I use &amp;lt;tt&amp;gt;lsof&amp;lt;/tt&amp;gt; to find all of the processes accessing the directory &amp;lt;tt&amp;gt;/home&amp;lt;/tt&amp;gt;?&lt;br /&gt;
# [2] In &amp;lt;tt&amp;gt;/proc/&amp;lt;PID&amp;gt;/maps&amp;lt;/tt&amp;gt;, each entry has four permission bits: r,w,x, &amp;amp; p.  What do each of them indicate?&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# [2] Why does &amp;lt;tt&amp;gt;tracepath&amp;lt;/tt&amp;gt; return &amp;quot;no reply&amp;quot; sometimes?&lt;br /&gt;
# [3] &amp;lt;tt&amp;gt;nc&amp;lt;/tt&amp;gt; uses very few system calls (three on Ubuntu 11.10) when actually receiving data on the network.  (The other system calls are for setup and for ending the connection.)  What are those few system calls, and what do each of them do?&lt;br /&gt;
# [2] What file does &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt; access to find out about current TCP connections on Linux?  And, what is the difference between this file and the output of &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt;, in syntax and semantics?  Be specific.&lt;br /&gt;
# [2] What does &amp;lt;tt&amp;gt;lsof&amp;lt;/tt&amp;gt; sometimes get a &amp;quot;permission denied&amp;quot; error when run as a regular user?  Specifically, what files is it trying to access, and why are these protected?&lt;br /&gt;
# [2] Can you tell from &amp;lt;tt&amp;gt;/proc/&amp;lt;PID&amp;gt;/maps&amp;lt;/tt&amp;gt; which memory segments are shared for read-only access?  What about read-write access?  Explain briefly.&lt;br /&gt;
&lt;br /&gt;
==Answers==&lt;br /&gt;
===Part A===&lt;br /&gt;
# inet address is the IPv4 address of the host. The [http://en.wikipedia.org/wiki/Reserved_IP_addresses#Reserved_IPv4_addresses scope] is usually either a subnet or global, the former meaning other computers on the same subnet can talk, and the latter meaning any computer with an access to the Internet can talk to this one. HWaddr is the MAC address of the network card. Computers on the same local network (ethernet/wifi etc) connected via a switch or a hub or some such device can talk to it without having to use the Network layer. inet6 address, or the IPv6 address is a newer replacement for the aging IPv4 address. If it&#039;s [http://en.wikipedia.org/wiki/IPv6_address#IPv6_address_scopes scope] is only link, that means it doesn&#039;t have a globally routable address. Routers drop packets that originate with a [http://en.wikipedia.org/wiki/Link-local_address link-local] address. A global scope means a globally routable ipv6 address.&lt;br /&gt;
# MTU, or the Maximum Transmission Unit, is the maximum frame size of whatever layer 2 protocol is under use, ( e.g Ethernet ). &lt;br /&gt;
# nc -l 9200 &amp;gt; /tmp/foo&lt;br /&gt;
# From a lambda machine, I got this : &lt;br /&gt;
 1:  134.117.27.129    0.118ms pmtu 1500&lt;br /&gt;
 1:  134.117.27.1      5.051ms&lt;br /&gt;
 1:  134.117.27.1      5.660ms&lt;br /&gt;
 2:  10.50.254.10      5.377ms &lt;br /&gt;
 3:  10.30.33.1        5.561ms &lt;br /&gt;
 4:  10.30.53.1        5.711ms &lt;br /&gt;
 5:  134.117.254.242 114.264ms asymm  4 &lt;br /&gt;
 6:  134.117.254.243   4.572ms asymm  4 &lt;br /&gt;
 7:  10.30.56.1        4.442ms asymm  6 &lt;br /&gt;
 8:  no reply&lt;br /&gt;
 9:  154.54.40.137     7.099ms asymm  8 &lt;br /&gt;
 10:  154.54.42.85     19.135ms asymm  9 &lt;br /&gt;
 11:  154.54.44.218    19.609ms asymm 10 &lt;br /&gt;
 12:  no reply&lt;br /&gt;
 13:  204.70.198.17    20.179ms asymm 12 &lt;br /&gt;
 14:  204.70.196.241   30.987ms asymm 11 &lt;br /&gt;
 15:  no reply&lt;br /&gt;
 16:  no reply&lt;br /&gt;
 17:  64.27.160.194    35.202ms asymm 12 &lt;br /&gt;
 18:  216.34.181.45    31.924ms reached&lt;br /&gt;
5. netstat -ant&amp;lt;br&amp;gt;&lt;br /&gt;
6. lsof +D /home&amp;lt;br&amp;gt;&lt;br /&gt;
7. Read, Write, Execute (self-explanatory) and Private. A page can either be shared ( meaning - &amp;quot;Updates to the mapping are visible to other processes that map this file, and are carried through to the underlying file.&amp;quot; ), or private (Updates to the mapping are not visible to other processes mapping the same file, and are not carried through to the underlying file - using copy-on-write).&lt;br /&gt;
&lt;br /&gt;
===Part B ===&lt;br /&gt;
# Tracepath/Traceroute either use ICMP echo requests, or simply send a UDP packet to a big port number, and wait for ICMP destination unreachable packet. In either case, if a server doesn&#039;t receive an ICMP packet as response, tracepath says &amp;quot;no reply&amp;quot;. &lt;br /&gt;
# I see the following system calls : poll, read and write. Poll waits for the file descriptor status to change, until it is ready to perform I/O. read reads a fixed number of bytes into a buffer from a file descriptor, and write writes a given number of bytes to a file descriptor. &lt;br /&gt;
# /proc/net/tcp. Syntactically, the ip addresses and port numbers in netstat are human-readable and not in hex. The state values have been translated from numbers to words. Further, if used with flags, ( like --program ), netstat retrieves the application name related to the connection, by looking up inode info from /proc/net/tcp. &lt;br /&gt;
# When lsof recurses through a directory, and encounters a file that it cannot open, it says &amp;quot;permission denied&amp;quot;. This is because ordinary users don&#039;t have access to some protected files ( for e.g /etc/shadow )&lt;br /&gt;
#&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_7_2011&amp;diff=15211</id>
		<title>COMP 3000 Lab 7 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_7_2011&amp;diff=15211"/>
		<updated>2011-12-05T04:29:49Z</updated>

		<summary type="html">&lt;p&gt;Saran: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by Sunday, November 27th at 11:30 PM.  &lt;br /&gt;
* Please answer using a single text file (with a .txt extension). Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.) &#039;&#039;&#039;Anything other than a single text file will receive -1 points&#039;&#039;&#039;&lt;br /&gt;
* Show all your work.  If you find an answer by browsing code, explain your search path (e.g., I searched for X, which led me to source file Y, where I found function Z.)   Also, list any websites or individuals you consult.&lt;br /&gt;
* Do all of the following on a Linux system.  Ubuntu Linux is guaranteed to work, but other Linux systems should work.  Note that *BSD systems, including MacOS X, will produce different answers for several of the following questions.&lt;br /&gt;
* Note that the tools from Lab 2 in particular may be of use here...&lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
# [3] Run &amp;lt;tt&amp;gt;ifconfig&amp;lt;/tt&amp;gt;.  For all configured interfaces except loopback, there should be an &amp;quot;inet&amp;quot; address, a &amp;quot;HWaddr&amp;quot; (hardware) address, and perhaps even an &amp;quot;inet6&amp;quot; address.  Each of these addresses identifies the computer within a certain context.  In what context are each of these addresses used?  Specifically, for what protocol is the address used, and what other computers can access this one using this address?  (Ignore NAT issues.)&lt;br /&gt;
# [2] What does MTU refer to in the output of &amp;lt;tt&amp;gt;ifconfig&amp;lt;/tt&amp;gt;?  Specifically, what does the acronym mean, and what does this value determine?&lt;br /&gt;
# How could you use netcat (&amp;lt;tt&amp;gt;nc&amp;lt;/tt&amp;gt;) to listen on port 9200 and write received data to the file /tmp/foo?&lt;br /&gt;
# What does &amp;lt;tt&amp;gt;tracepath slashdot.org&amp;lt;/tt&amp;gt; return?&lt;br /&gt;
# How can you get &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt; to return a list of all current TCP connections and open ports, listed using numeric IP addresses (rather than DNS hostnames)?&lt;br /&gt;
# How could I use &amp;lt;tt&amp;gt;lsof&amp;lt;/tt&amp;gt; to find all of the processes accessing the directory &amp;lt;tt&amp;gt;/home&amp;lt;/tt&amp;gt;?&lt;br /&gt;
# [2] In &amp;lt;tt&amp;gt;/proc/&amp;lt;PID&amp;gt;/maps&amp;lt;/tt&amp;gt;, each entry has four permission bits: r,w,x, &amp;amp; p.  What do each of them indicate?&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# [2] Why does &amp;lt;tt&amp;gt;tracepath&amp;lt;/tt&amp;gt; return &amp;quot;no reply&amp;quot; sometimes?&lt;br /&gt;
# [3] &amp;lt;tt&amp;gt;nc&amp;lt;/tt&amp;gt; uses very few system calls (three on Ubuntu 11.10) when actually receiving data on the network.  (The other system calls are for setup and for ending the connection.)  What are those few system calls, and what do each of them do?&lt;br /&gt;
# [2] What file does &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt; access to find out about current TCP connections on Linux?  And, what is the difference between this file and the output of &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt;, in syntax and semantics?  Be specific.&lt;br /&gt;
# [2] What does &amp;lt;tt&amp;gt;lsof&amp;lt;/tt&amp;gt; sometimes get a &amp;quot;permission denied&amp;quot; error when run as a regular user?  Specifically, what files is it trying to access, and why are these protected?&lt;br /&gt;
# [2] Can you tell from &amp;lt;tt&amp;gt;/proc/&amp;lt;PID&amp;gt;/maps&amp;lt;/tt&amp;gt; which memory segments are shared for read-only access?  What about read-write access?  Explain briefly.&lt;br /&gt;
&lt;br /&gt;
==Answers==&lt;br /&gt;
===Part A===&lt;br /&gt;
# inet address is the IPv4 address of the host. The [http://en.wikipedia.org/wiki/Reserved_IP_addresses#Reserved_IPv4_addresses scope] is usually either a subnet or global, the former meaning other computers on the same subnet can talk, and the latter meaning any computer with an access to the Internet can talk to this one. HWaddr is the MAC address of the network card. Computers on the same local network (ethernet/wifi etc) connected via a switch or a hub or some such device can talk to it without having to use the Network layer. inet6 address, or the IPv6 address is a newer replacement for the aging IPv4 address. If it&#039;s [http://en.wikipedia.org/wiki/IPv6_address#IPv6_address_scopes scope] is only link, that means it doesn&#039;t have a globally routable address. Routers drop packets that originate with a [http://en.wikipedia.org/wiki/Link-local_address link-local] address. A global scope means a globally routable ipv6 address.&lt;br /&gt;
# MTU, or the Maximum Transmission Unit, is the maximum frame size of whatever layer 2 protocol is under use, ( e.g Ethernet ). &lt;br /&gt;
# nc -l 9200 &amp;gt; /tmp/foo&lt;br /&gt;
# From a lambda machine, I got this : &lt;br /&gt;
 1:  134.117.27.129    0.118ms pmtu 1500&lt;br /&gt;
 1:  134.117.27.1      5.051ms&lt;br /&gt;
 1:  134.117.27.1      5.660ms&lt;br /&gt;
 2:  10.50.254.10      5.377ms &lt;br /&gt;
 3:  10.30.33.1        5.561ms &lt;br /&gt;
 4:  10.30.53.1        5.711ms &lt;br /&gt;
 5:  134.117.254.242 114.264ms asymm  4 &lt;br /&gt;
 6:  134.117.254.243   4.572ms asymm  4 &lt;br /&gt;
 7:  10.30.56.1        4.442ms asymm  6 &lt;br /&gt;
 8:  no reply&lt;br /&gt;
 9:  154.54.40.137     7.099ms asymm  8 &lt;br /&gt;
 10:  154.54.42.85     19.135ms asymm  9 &lt;br /&gt;
 11:  154.54.44.218    19.609ms asymm 10 &lt;br /&gt;
 12:  no reply&lt;br /&gt;
 13:  204.70.198.17    20.179ms asymm 12 &lt;br /&gt;
 14:  204.70.196.241   30.987ms asymm 11 &lt;br /&gt;
 15:  no reply&lt;br /&gt;
 16:  no reply&lt;br /&gt;
 17:  64.27.160.194    35.202ms asymm 12 &lt;br /&gt;
 18:  216.34.181.45    31.924ms reached&lt;br /&gt;
5. netstat -ant&lt;br /&gt;
6. lsof +D /home&lt;br /&gt;
7. Read, Write, Execute (self-explanatory) and Private. A page can either be shared ( meaning - &amp;quot;Updates to the mapping are visible to other processes that map this file, and are carried through to the underlying file.&amp;quot; ), or private (Updates to the mapping are not visible to other processes mapping the same file, and are not carried through to the underlying file - using copy-on-write). &lt;br /&gt;
&lt;br /&gt;
===Part B ===&lt;br /&gt;
# Tracepath/Traceroute either use ICMP echo requests, or simply send a UDP packet to a big port number, and wait for ICMP destination unreachable packet. In either case, if a server doesn&#039;t receive an ICMP packet as response, tracepath says &amp;quot;no reply&amp;quot;. &lt;br /&gt;
# I see the following system calls : poll, read and write. Poll waits for the file descriptor status to change, until it is ready to perform I/O. read reads a fixed number of bytes into a buffer from a file descriptor, and write writes a given number of bytes to a file descriptor. &lt;br /&gt;
# /proc/net/tcp. Syntactically, the ip addresses and port numbers in netstat are human-readable and not in hex. The state values have been translated from numbers to words. Further, if used with flags, ( like --program ), netstat retrieves the application name related to the connection, by looking up inode info from /proc/net/tcp. &lt;br /&gt;
# When lsof recurses through a directory, and encounters a file that it cannot open, it says &amp;quot;permission denied&amp;quot;. This is because ordinary users don&#039;t have access to some protected files ( for e.g /etc/shadow )&lt;br /&gt;
#&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_7_2011&amp;diff=15210</id>
		<title>COMP 3000 Lab 7 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_7_2011&amp;diff=15210"/>
		<updated>2011-12-05T04:28:58Z</updated>

		<summary type="html">&lt;p&gt;Saran: Reverted edits by Saran (Talk) to last revision by Nhoda&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by Sunday, November 27th at 11:30 PM.  &lt;br /&gt;
* Please answer using a single text file (with a .txt extension). Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.) &#039;&#039;&#039;Anything other than a single text file will receive -1 points&#039;&#039;&#039;&lt;br /&gt;
* Show all your work.  If you find an answer by browsing code, explain your search path (e.g., I searched for X, which led me to source file Y, where I found function Z.)   Also, list any websites or individuals you consult.&lt;br /&gt;
* Do all of the following on a Linux system.  Ubuntu Linux is guaranteed to work, but other Linux systems should work.  Note that *BSD systems, including MacOS X, will produce different answers for several of the following questions.&lt;br /&gt;
* Note that the tools from Lab 2 in particular may be of use here...&lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
# [3] Run &amp;lt;tt&amp;gt;ifconfig&amp;lt;/tt&amp;gt;.  For all configured interfaces except loopback, there should be an &amp;quot;inet&amp;quot; address, a &amp;quot;HWaddr&amp;quot; (hardware) address, and perhaps even an &amp;quot;inet6&amp;quot; address.  Each of these addresses identifies the computer within a certain context.  In what context are each of these addresses used?  Specifically, for what protocol is the address used, and what other computers can access this one using this address?  (Ignore NAT issues.)&lt;br /&gt;
# [2] What does MTU refer to in the output of &amp;lt;tt&amp;gt;ifconfig&amp;lt;/tt&amp;gt;?  Specifically, what does the acronym mean, and what does this value determine?&lt;br /&gt;
# How could you use netcat (&amp;lt;tt&amp;gt;nc&amp;lt;/tt&amp;gt;) to listen on port 9200 and write received data to the file /tmp/foo?&lt;br /&gt;
# What does &amp;lt;tt&amp;gt;tracepath slashdot.org&amp;lt;/tt&amp;gt; return?&lt;br /&gt;
# How can you get &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt; to return a list of all current TCP connections and open ports, listed using numeric IP addresses (rather than DNS hostnames)?&lt;br /&gt;
# How could I use &amp;lt;tt&amp;gt;lsof&amp;lt;/tt&amp;gt; to find all of the processes accessing the directory &amp;lt;tt&amp;gt;/home&amp;lt;/tt&amp;gt;?&lt;br /&gt;
# [2] In &amp;lt;tt&amp;gt;/proc/&amp;lt;PID&amp;gt;/maps&amp;lt;/tt&amp;gt;, each entry has four permission bits: r,w,x, &amp;amp; p.  What do each of them indicate?&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# [2] Why does &amp;lt;tt&amp;gt;tracepath&amp;lt;/tt&amp;gt; return &amp;quot;no reply&amp;quot; sometimes?&lt;br /&gt;
# [3] &amp;lt;tt&amp;gt;nc&amp;lt;/tt&amp;gt; uses very few system calls (three on Ubuntu 11.10) when actually receiving data on the network.  (The other system calls are for setup and for ending the connection.)  What are those few system calls, and what do each of them do?&lt;br /&gt;
# [2] What file does &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt; access to find out about current TCP connections on Linux?  And, what is the difference between this file and the output of &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt;, in syntax and semantics?  Be specific.&lt;br /&gt;
# [2] What does &amp;lt;tt&amp;gt;lsof&amp;lt;/tt&amp;gt; sometimes get a &amp;quot;permission denied&amp;quot; error when run as a regular user?  Specifically, what files is it trying to access, and why are these protected?&lt;br /&gt;
# [2] Can you tell from &amp;lt;tt&amp;gt;/proc/&amp;lt;PID&amp;gt;/maps&amp;lt;/tt&amp;gt; which memory segments are shared for read-only access?  What about read-write access?  Explain briefly.&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_7_2011&amp;diff=15209</id>
		<title>COMP 3000 Lab 7 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_7_2011&amp;diff=15209"/>
		<updated>2011-12-05T04:27:56Z</updated>

		<summary type="html">&lt;p&gt;Saran: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;===Part A===&lt;br /&gt;
# inet address is the IPv4 address of the host. The [http://en.wikipedia.org/wiki/Reserved_IP_addresses#Reserved_IPv4_addresses scope] is usually either a subnet or global, the former meaning other computers on the same subnet can talk, and the latter meaning any computer with an access to the Internet can talk to this one. HWaddr is the MAC address of the network card. Computers on the same local network (ethernet/wifi etc) connected via a switch or a hub or some such device can talk to it without having to use the Network layer. inet6 address, or the IPv6 address is a newer replacement for the aging IPv4 address. If it&#039;s [http://en.wikipedia.org/wiki/IPv6_address#IPv6_address_scopes scope] is only link, that means it doesn&#039;t have a globally routable address. Routers drop packets that originate with a [http://en.wikipedia.org/wiki/Link-local_address link-local] address. A global scope means a globally routable ipv6 address.&lt;br /&gt;
# MTU, or the Maximum Transmission Unit, is the maximum frame size of whatever layer 2 protocol is under use, ( e.g Ethernet ). &lt;br /&gt;
# nc -l 9200 &amp;gt; /tmp/foo&lt;br /&gt;
# From a lambda machine, I got this : &lt;br /&gt;
 1:  134.117.27.129    0.118ms pmtu 1500&lt;br /&gt;
 1:  134.117.27.1      5.051ms&lt;br /&gt;
 1:  134.117.27.1      5.660ms&lt;br /&gt;
 2:  10.50.254.10      5.377ms &lt;br /&gt;
 3:  10.30.33.1        5.561ms &lt;br /&gt;
 4:  10.30.53.1        5.711ms &lt;br /&gt;
 5:  134.117.254.242 114.264ms asymm  4 &lt;br /&gt;
 6:  134.117.254.243   4.572ms asymm  4 &lt;br /&gt;
 7:  10.30.56.1        4.442ms asymm  6 &lt;br /&gt;
 8:  no reply&lt;br /&gt;
 9:  154.54.40.137     7.099ms asymm  8 &lt;br /&gt;
 10:  154.54.42.85     19.135ms asymm  9 &lt;br /&gt;
 11:  154.54.44.218    19.609ms asymm 10 &lt;br /&gt;
 12:  no reply&lt;br /&gt;
 13:  204.70.198.17    20.179ms asymm 12 &lt;br /&gt;
 14:  204.70.196.241   30.987ms asymm 11 &lt;br /&gt;
 15:  no reply&lt;br /&gt;
 16:  no reply&lt;br /&gt;
 17:  64.27.160.194    35.202ms asymm 12 &lt;br /&gt;
 18:  216.34.181.45    31.924ms reached&lt;br /&gt;
5. netstat -ant&lt;br /&gt;
6. lsof +D /home&lt;br /&gt;
7. Read, Write, Execute (self-explanatory) and Private. A page can either be shared ( meaning - &amp;quot;Updates to the mapping are visible to other processes that map this file, and are carried through to the underlying file.&amp;quot; ), or private (Updates to the mapping are not visible to other processes mapping the same file, and are not carried through to the underlying file - using copy-on-write). &lt;br /&gt;
&lt;br /&gt;
===Part B ===&lt;br /&gt;
# Tracepath/Traceroute either use ICMP echo requests, or simply send a UDP packet to a big port number, and wait for ICMP destination unreachable packet. In either case, if a server doesn&#039;t receive an ICMP packet as response, tracepath says &amp;quot;no reply&amp;quot;. &lt;br /&gt;
# I see the following system calls : poll, read and write. Poll waits for the file descriptor status to change, until it is ready to perform I/O. read reads a fixed number of bytes into a buffer from a file descriptor, and write writes a given number of bytes to a file descriptor. &lt;br /&gt;
# /proc/net/tcp. Syntactically, the ip addresses and port numbers in netstat are human-readable and not in hex. The state values have been translated from numbers to words. Further, if used with flags, ( like --program ), netstat retrieves the application name related to the connection, by looking up inode info from /proc/net/tcp. &lt;br /&gt;
# When lsof recurses through a directory, and encounters a file that it cannot open, it says &amp;quot;permission denied&amp;quot;. This is because ordinary users don&#039;t have access to some protected files ( for e.g /etc/shadow )&lt;br /&gt;
#&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_7_2011&amp;diff=15194</id>
		<title>COMP 3000 Lab 7 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_7_2011&amp;diff=15194"/>
		<updated>2011-12-05T03:03:56Z</updated>

		<summary type="html">&lt;p&gt;Saran: /* Part A */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by Sunday, November 27th at 11:30 PM.  &lt;br /&gt;
* Please answer using a single text file (with a .txt extension). Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.) &#039;&#039;&#039;Anything other than a single text file will receive -1 points&#039;&#039;&#039;&lt;br /&gt;
* Show all your work.  If you find an answer by browsing code, explain your search path (e.g., I searched for X, which led me to source file Y, where I found function Z.)   Also, list any websites or individuals you consult.&lt;br /&gt;
* Do all of the following on a Linux system.  Ubuntu Linux is guaranteed to work, but other Linux systems should work.  Note that *BSD systems, including MacOS X, will produce different answers for several of the following questions.&lt;br /&gt;
* Note that the tools from Lab 2 in particular may be of use here...&lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
# [3] Run &amp;lt;tt&amp;gt;ifconfig&amp;lt;/tt&amp;gt;.  For all configured interfaces except loopback, there should be an &amp;quot;inet&amp;quot; address, a &amp;quot;HWaddr&amp;quot; (hardware) address, and perhaps even an &amp;quot;inet6&amp;quot; address.  Each of these addresses identifies the computer within a certain context.  In what context are each of these addresses used?  Specifically, for what protocol is the address used, and what other computers can access this one using this address?  (Ignore NAT issues.)&lt;br /&gt;
# [2] What does MTU refer to in the output of &amp;lt;tt&amp;gt;ifconfig&amp;lt;/tt&amp;gt;?  Specifically, what does the acronym mean, and what does this value determine?&lt;br /&gt;
# How could you use netcat (&amp;lt;tt&amp;gt;nc&amp;lt;/tt&amp;gt;) to listen on port 9200 and write received data to the file /tmp/foo?&lt;br /&gt;
# What does &amp;lt;tt&amp;gt;tracepath slashdot.org&amp;lt;/tt&amp;gt; return?&lt;br /&gt;
# How can you get &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt; to return a list of all current TCP connections and open ports, listed using numeric IP addresses (rather than DNS hostnames)?&lt;br /&gt;
# How could I use &amp;lt;tt&amp;gt;lsof&amp;lt;/tt&amp;gt; to find all of the processes accessing the directory &amp;lt;tt&amp;gt;/home&amp;lt;/tt&amp;gt;?&lt;br /&gt;
# [2] In &amp;lt;tt&amp;gt;/proc/&amp;lt;PID&amp;gt;/maps&amp;lt;/tt&amp;gt;, each entry has four permission bits: r,w,x, &amp;amp; p.  What do each of them indicate?&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# [2] Why does &amp;lt;tt&amp;gt;tracepath&amp;lt;/tt&amp;gt; return &amp;quot;no reply&amp;quot; sometimes?&lt;br /&gt;
# [3] &amp;lt;tt&amp;gt;nc&amp;lt;/tt&amp;gt; uses very few system calls (three on Ubuntu 11.10) when actually receiving data on the network.  (The other system calls are for setup and for ending the connection.)  What are those few system calls, and what do each of them do?&lt;br /&gt;
# [2] What file does &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt; access to find out about current TCP connections on Linux?  And, what is the difference between this file and the output of &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt;, in syntax and semantics?  Be specific.&lt;br /&gt;
# [2] What does &amp;lt;tt&amp;gt;lsof&amp;lt;/tt&amp;gt; sometimes get a &amp;quot;permission denied&amp;quot; error when run as a regular user?  Specifically, what files is it trying to access, and why are these protected?&lt;br /&gt;
# [2] What kernel data structure stores the permissions listed in &amp;lt;tt&amp;gt;/proc/&amp;lt;PID&amp;gt;/maps&amp;lt;/tt&amp;gt;?&lt;br /&gt;
&lt;br /&gt;
==Answers==&lt;br /&gt;
===Part A===&lt;br /&gt;
# inet address is the IPv4 address of the host. The [http://en.wikipedia.org/wiki/Reserved_IP_addresses#Reserved_IPv4_addresses scope] is usually either a subnet or global, the former meaning other computers on the same subnet can talk, and the latter meaning any computer with an access to the Internet can talk to this one. HWaddr is the MAC address of the network card. Computers on the same local network (ethernet/wifi etc) connected via a switch or a hub or some such device can talk to it without having to use the Network layer. inet6 address, or the IPv6 address is a newer replacement for the aging IPv4 address. If it&#039;s [http://en.wikipedia.org/wiki/IPv6_address#IPv6_address_scopes scope] is only link, that means it doesn&#039;t have a globally routable address. Routers drop packets that originate with a [http://en.wikipedia.org/wiki/Link-local_address link-local] address. A global scope means a globally routable ipv6 address.&lt;br /&gt;
# MTU, or the Maximum Transmission Unit, is the maximum frame size of whatever layer 2 protocol is under use, ( e.g Ethernet ). &lt;br /&gt;
# nc -l 9200 &amp;gt; /tmp/foo&lt;br /&gt;
# From a lambda machine, I got this : &lt;br /&gt;
 1:  134.117.27.129    0.118ms pmtu 1500&lt;br /&gt;
 1:  134.117.27.1      5.051ms&lt;br /&gt;
 1:  134.117.27.1      5.660ms&lt;br /&gt;
 2:  10.50.254.10      5.377ms &lt;br /&gt;
 3:  10.30.33.1        5.561ms &lt;br /&gt;
 4:  10.30.53.1        5.711ms &lt;br /&gt;
 5:  134.117.254.242 114.264ms asymm  4 &lt;br /&gt;
 6:  134.117.254.243   4.572ms asymm  4 &lt;br /&gt;
 7:  10.30.56.1        4.442ms asymm  6 &lt;br /&gt;
 8:  no reply&lt;br /&gt;
 9:  154.54.40.137     7.099ms asymm  8 &lt;br /&gt;
 10:  154.54.42.85     19.135ms asymm  9 &lt;br /&gt;
 11:  154.54.44.218    19.609ms asymm 10 &lt;br /&gt;
 12:  no reply&lt;br /&gt;
 13:  204.70.198.17    20.179ms asymm 12 &lt;br /&gt;
 14:  204.70.196.241   30.987ms asymm 11 &lt;br /&gt;
 15:  no reply&lt;br /&gt;
 16:  no reply&lt;br /&gt;
 17:  64.27.160.194    35.202ms asymm 12 &lt;br /&gt;
 18:  216.34.181.45    31.924ms reached&lt;br /&gt;
#&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_7_2011&amp;diff=15193</id>
		<title>COMP 3000 Lab 7 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_7_2011&amp;diff=15193"/>
		<updated>2011-12-05T03:03:23Z</updated>

		<summary type="html">&lt;p&gt;Saran: /* Part A */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by Sunday, November 27th at 11:30 PM.  &lt;br /&gt;
* Please answer using a single text file (with a .txt extension). Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.) &#039;&#039;&#039;Anything other than a single text file will receive -1 points&#039;&#039;&#039;&lt;br /&gt;
* Show all your work.  If you find an answer by browsing code, explain your search path (e.g., I searched for X, which led me to source file Y, where I found function Z.)   Also, list any websites or individuals you consult.&lt;br /&gt;
* Do all of the following on a Linux system.  Ubuntu Linux is guaranteed to work, but other Linux systems should work.  Note that *BSD systems, including MacOS X, will produce different answers for several of the following questions.&lt;br /&gt;
* Note that the tools from Lab 2 in particular may be of use here...&lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
# [3] Run &amp;lt;tt&amp;gt;ifconfig&amp;lt;/tt&amp;gt;.  For all configured interfaces except loopback, there should be an &amp;quot;inet&amp;quot; address, a &amp;quot;HWaddr&amp;quot; (hardware) address, and perhaps even an &amp;quot;inet6&amp;quot; address.  Each of these addresses identifies the computer within a certain context.  In what context are each of these addresses used?  Specifically, for what protocol is the address used, and what other computers can access this one using this address?  (Ignore NAT issues.)&lt;br /&gt;
# [2] What does MTU refer to in the output of &amp;lt;tt&amp;gt;ifconfig&amp;lt;/tt&amp;gt;?  Specifically, what does the acronym mean, and what does this value determine?&lt;br /&gt;
# How could you use netcat (&amp;lt;tt&amp;gt;nc&amp;lt;/tt&amp;gt;) to listen on port 9200 and write received data to the file /tmp/foo?&lt;br /&gt;
# What does &amp;lt;tt&amp;gt;tracepath slashdot.org&amp;lt;/tt&amp;gt; return?&lt;br /&gt;
# How can you get &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt; to return a list of all current TCP connections and open ports, listed using numeric IP addresses (rather than DNS hostnames)?&lt;br /&gt;
# How could I use &amp;lt;tt&amp;gt;lsof&amp;lt;/tt&amp;gt; to find all of the processes accessing the directory &amp;lt;tt&amp;gt;/home&amp;lt;/tt&amp;gt;?&lt;br /&gt;
# [2] In &amp;lt;tt&amp;gt;/proc/&amp;lt;PID&amp;gt;/maps&amp;lt;/tt&amp;gt;, each entry has four permission bits: r,w,x, &amp;amp; p.  What do each of them indicate?&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# [2] Why does &amp;lt;tt&amp;gt;tracepath&amp;lt;/tt&amp;gt; return &amp;quot;no reply&amp;quot; sometimes?&lt;br /&gt;
# [3] &amp;lt;tt&amp;gt;nc&amp;lt;/tt&amp;gt; uses very few system calls (three on Ubuntu 11.10) when actually receiving data on the network.  (The other system calls are for setup and for ending the connection.)  What are those few system calls, and what do each of them do?&lt;br /&gt;
# [2] What file does &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt; access to find out about current TCP connections on Linux?  And, what is the difference between this file and the output of &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt;, in syntax and semantics?  Be specific.&lt;br /&gt;
# [2] What does &amp;lt;tt&amp;gt;lsof&amp;lt;/tt&amp;gt; sometimes get a &amp;quot;permission denied&amp;quot; error when run as a regular user?  Specifically, what files is it trying to access, and why are these protected?&lt;br /&gt;
# [2] What kernel data structure stores the permissions listed in &amp;lt;tt&amp;gt;/proc/&amp;lt;PID&amp;gt;/maps&amp;lt;/tt&amp;gt;?&lt;br /&gt;
&lt;br /&gt;
==Answers==&lt;br /&gt;
===Part A===&lt;br /&gt;
# inet address is the IPv4 address of the host. The [http://en.wikipedia.org/wiki/Reserved_IP_addresses#Reserved_IPv4_addresses scope] is usually either a subnet or global, the former meaning other computers on the same subnet can talk, and the latter meaning any computer with an access to the Internet can talk to this one. HWaddr is the MAC address of the network card. Computers on the same local network (ethernet/wifi etc) connected via a switch or a hub or some such device can talk to it without having to use the Network layer. inet6 address, or the IPv6 address is a newer replacement for the aging IPv4 address. If it&#039;s [http://en.wikipedia.org/wiki/IPv6_address#IPv6_address_scopes scope] is only link, that means it doesn&#039;t have a globally routable address. Routers drop packets that originate with a [http://en.wikipedia.org/wiki/Link-local_address link-local] address. A global scope means a globally routable ipv6 address.&lt;br /&gt;
# MTU, or the Maximum Transmission Unit, is the maximum frame size of whatever layer 2 protocol is under use, ( e.g Ethernet ). &lt;br /&gt;
# nc -l 9200 &amp;gt; /tmp/foo&lt;br /&gt;
# From a lambda machine, I got this : &lt;br /&gt;
1:  134.117.27.129    0.118ms pmtu 1500&lt;br /&gt;
1:  134.117.27.1      5.051ms &lt;br /&gt;
1:  134.117.27.1      5.660ms &lt;br /&gt;
2:  10.50.254.10      5.377ms &lt;br /&gt;
3:  10.30.33.1        5.561ms &lt;br /&gt;
4:  10.30.53.1        5.711ms &lt;br /&gt;
5:  134.117.254.242 114.264ms asymm  4 &lt;br /&gt;
6:  134.117.254.243   4.572ms asymm  4 &lt;br /&gt;
7:  10.30.56.1        4.442ms asymm  6 &lt;br /&gt;
8:  no reply&lt;br /&gt;
9:  154.54.40.137     7.099ms asymm  8 &lt;br /&gt;
10:  154.54.42.85     19.135ms asymm  9 &lt;br /&gt;
11:  154.54.44.218    19.609ms asymm 10 &lt;br /&gt;
12:  no reply&lt;br /&gt;
13:  204.70.198.17    20.179ms asymm 12 &lt;br /&gt;
14:  204.70.196.241   30.987ms asymm 11 &lt;br /&gt;
15:  no reply&lt;br /&gt;
16:  no reply&lt;br /&gt;
17:  64.27.160.194    35.202ms asymm 12 &lt;br /&gt;
18:  216.34.181.45    31.924ms reached&lt;br /&gt;
#&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_7_2011&amp;diff=15192</id>
		<title>COMP 3000 Lab 7 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_7_2011&amp;diff=15192"/>
		<updated>2011-12-05T03:02:54Z</updated>

		<summary type="html">&lt;p&gt;Saran: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by Sunday, November 27th at 11:30 PM.  &lt;br /&gt;
* Please answer using a single text file (with a .txt extension). Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.) &#039;&#039;&#039;Anything other than a single text file will receive -1 points&#039;&#039;&#039;&lt;br /&gt;
* Show all your work.  If you find an answer by browsing code, explain your search path (e.g., I searched for X, which led me to source file Y, where I found function Z.)   Also, list any websites or individuals you consult.&lt;br /&gt;
* Do all of the following on a Linux system.  Ubuntu Linux is guaranteed to work, but other Linux systems should work.  Note that *BSD systems, including MacOS X, will produce different answers for several of the following questions.&lt;br /&gt;
* Note that the tools from Lab 2 in particular may be of use here...&lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
# [3] Run &amp;lt;tt&amp;gt;ifconfig&amp;lt;/tt&amp;gt;.  For all configured interfaces except loopback, there should be an &amp;quot;inet&amp;quot; address, a &amp;quot;HWaddr&amp;quot; (hardware) address, and perhaps even an &amp;quot;inet6&amp;quot; address.  Each of these addresses identifies the computer within a certain context.  In what context are each of these addresses used?  Specifically, for what protocol is the address used, and what other computers can access this one using this address?  (Ignore NAT issues.)&lt;br /&gt;
# [2] What does MTU refer to in the output of &amp;lt;tt&amp;gt;ifconfig&amp;lt;/tt&amp;gt;?  Specifically, what does the acronym mean, and what does this value determine?&lt;br /&gt;
# How could you use netcat (&amp;lt;tt&amp;gt;nc&amp;lt;/tt&amp;gt;) to listen on port 9200 and write received data to the file /tmp/foo?&lt;br /&gt;
# What does &amp;lt;tt&amp;gt;tracepath slashdot.org&amp;lt;/tt&amp;gt; return?&lt;br /&gt;
# How can you get &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt; to return a list of all current TCP connections and open ports, listed using numeric IP addresses (rather than DNS hostnames)?&lt;br /&gt;
# How could I use &amp;lt;tt&amp;gt;lsof&amp;lt;/tt&amp;gt; to find all of the processes accessing the directory &amp;lt;tt&amp;gt;/home&amp;lt;/tt&amp;gt;?&lt;br /&gt;
# [2] In &amp;lt;tt&amp;gt;/proc/&amp;lt;PID&amp;gt;/maps&amp;lt;/tt&amp;gt;, each entry has four permission bits: r,w,x, &amp;amp; p.  What do each of them indicate?&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# [2] Why does &amp;lt;tt&amp;gt;tracepath&amp;lt;/tt&amp;gt; return &amp;quot;no reply&amp;quot; sometimes?&lt;br /&gt;
# [3] &amp;lt;tt&amp;gt;nc&amp;lt;/tt&amp;gt; uses very few system calls (three on Ubuntu 11.10) when actually receiving data on the network.  (The other system calls are for setup and for ending the connection.)  What are those few system calls, and what do each of them do?&lt;br /&gt;
# [2] What file does &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt; access to find out about current TCP connections on Linux?  And, what is the difference between this file and the output of &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt;, in syntax and semantics?  Be specific.&lt;br /&gt;
# [2] What does &amp;lt;tt&amp;gt;lsof&amp;lt;/tt&amp;gt; sometimes get a &amp;quot;permission denied&amp;quot; error when run as a regular user?  Specifically, what files is it trying to access, and why are these protected?&lt;br /&gt;
# [2] What kernel data structure stores the permissions listed in &amp;lt;tt&amp;gt;/proc/&amp;lt;PID&amp;gt;/maps&amp;lt;/tt&amp;gt;?&lt;br /&gt;
&lt;br /&gt;
==Answers==&lt;br /&gt;
===Part A===&lt;br /&gt;
# inet address is the IPv4 address of the host. The [http://en.wikipedia.org/wiki/Reserved_IP_addresses#Reserved_IPv4_addresses scope] is usually either a subnet or global, the former meaning other computers on the same subnet can talk, and the latter meaning any computer with an access to the Internet can talk to this one. HWaddr is the MAC address of the network card. Computers on the same local network (ethernet/wifi etc) connected via a switch or a hub or some such device can talk to it without having to use the Network layer. inet6 address, or the IPv6 address is a newer replacement for the aging IPv4 address. If it&#039;s [http://en.wikipedia.org/wiki/IPv6_address#IPv6_address_scopes scope] is only link, that means it doesn&#039;t have a globally routable address. Routers drop packets that originate with a [http://en.wikipedia.org/wiki/Link-local_address link-local] address. A global scope means a globally routable ipv6 address.&lt;br /&gt;
# MTU, or the Maximum Transmission Unit, is the maximum frame size of whatever layer 2 protocol is under use, ( e.g Ethernet ). &lt;br /&gt;
# nc -l 9200 &amp;gt; /tmp/foo&lt;br /&gt;
# From a lambda machine, I got this : &lt;br /&gt;
 1:  134.117.27.129    0.118ms pmtu 1500&lt;br /&gt;
 1:  134.117.27.1      5.051ms &lt;br /&gt;
 1:  134.117.27.1      5.660ms &lt;br /&gt;
 2:  10.50.254.10      5.377ms &lt;br /&gt;
 3:  10.30.33.1        5.561ms &lt;br /&gt;
 4:  10.30.53.1        5.711ms &lt;br /&gt;
 5:  134.117.254.242 114.264ms asymm  4 &lt;br /&gt;
 6:  134.117.254.243   4.572ms asymm  4 &lt;br /&gt;
 7:  10.30.56.1        4.442ms asymm  6 &lt;br /&gt;
 8:  no reply&lt;br /&gt;
 9:  154.54.40.137     7.099ms asymm  8 &lt;br /&gt;
10:  154.54.42.85     19.135ms asymm  9 &lt;br /&gt;
11:  154.54.44.218    19.609ms asymm 10 &lt;br /&gt;
12:  no reply&lt;br /&gt;
13:  204.70.198.17    20.179ms asymm 12 &lt;br /&gt;
14:  204.70.196.241   30.987ms asymm 11 &lt;br /&gt;
15:  no reply&lt;br /&gt;
16:  no reply&lt;br /&gt;
17:  64.27.160.194    35.202ms asymm 12 &lt;br /&gt;
18:  216.34.181.45    31.924ms reached&lt;br /&gt;
#&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_6_2011&amp;diff=15185</id>
		<title>COMP 3000 Lab 6 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_6_2011&amp;diff=15185"/>
		<updated>2011-12-05T02:22:01Z</updated>

		<summary type="html">&lt;p&gt;Saran: /* Part B */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by Sunday, November 20th at 11:30 PM.  &lt;br /&gt;
* Please answer using a single text file (with a .txt extension). Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.) &#039;&#039;&#039;Anything other than a single text file will receive -1 points&#039;&#039;&#039;&lt;br /&gt;
* Show all your work.  If you find an answer by browsing code, explain your search path (e.g., I searched for X, which led me to source file Y, where I found function Z.)   Also, list any websites or individuals you consult.&lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
For this section, you will need to refer to the source of the Linux kernel, version 3.1.1.  You may download the source code [http://www.kernel.org/pub/linux/kernel/v3.0/linux-3.1.1.tar.bz2 here]; however, you will probably prefer to navigate the code through the [http://lxr.linux.no/#linux+v3.1.1/ Linux Cross Reference Project]. *** Students have found it easier to navigate this link ***&lt;br /&gt;
&lt;br /&gt;
# Answer the following questions regarding &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/fs/exec.c#L1449 do_execve_common()]&amp;lt;/tt&amp;gt; in fs/exec.c.&lt;br /&gt;
## What information does &amp;lt;tt&amp;gt;bprm&amp;lt;/tt&amp;gt; hold?&lt;br /&gt;
## What are the many goto&#039;s for?&lt;br /&gt;
## What are the two lines immediately before this function returns with no error (i.e., the execve succeeds)?&lt;br /&gt;
# What function is the main consumer of &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/kernel/fork.c#L1042 copy_process()]&amp;lt;/tt&amp;gt; in kernel/fork.c?&lt;br /&gt;
# In mm/mmap.c, what does the line &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/mm/mmap.c#L243 SYSCALL_DEFINE1(brk, unsigned long, brk)]&amp;lt;/tt&amp;gt; do?&lt;br /&gt;
# What is the call &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/mm/mmap.c#L250 down_write(&amp;amp;mm-&amp;gt;mmap_sem);]&amp;lt;/tt&amp;gt; for?&lt;br /&gt;
# In what file is &amp;lt;tt&amp;gt;atl1e_tx_ring&amp;lt;/tt&amp;gt; declared?&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# [6 pts] Briefly explain what each statement does in &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/fs/read_write.c#L404 vfs_write()]&amp;lt;/tt&amp;gt; in fs/read_write.c.  Your explanation for each line should take up two lines at most.&lt;br /&gt;
# [3 pts] What is the purpose of each field in the struct &amp;lt;tt&amp;gt;atl1e_tx_ring&amp;lt;/tt&amp;gt;?  (Note: the last two fields are extra credit!)&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
===Part A===&lt;br /&gt;
# In fs/exec.c&lt;br /&gt;
## bprm is a pointer to a structure of type linux_binprm. This structure is used to hold arguments that are used when loading binaries, like argc, filename, vm related structures. &lt;br /&gt;
## The goto&#039;s handle error messages, and provide the same functionality as a jump statement in assembly. The retval conditional statements before the goto calls determine what return value the program is providing to the function.&lt;br /&gt;
## if (displaced) put_files_struct(displaced);&lt;br /&gt;
# do_fork&lt;br /&gt;
# SYSCALL_DEFINE1 is a macro defined in syscall.h, used to define system calls. (In this case the brk system call )&lt;br /&gt;
# The brk system call modifies the data segment size, and down_write(&amp;amp;mm-&amp;gt;mmap_sem) is used to acquire a read/write semaphore before performing the memory operation. &lt;br /&gt;
# In drivers/net/atl1e/atl1e.h&lt;br /&gt;
&lt;br /&gt;
===Part B===&lt;br /&gt;
# &lt;br /&gt;
/* If the file descriptor is not opened with WRITE mode, returns an error. */&lt;br /&gt;
      if (!(file-&amp;gt;f_mode &amp;amp; FMODE_WRITE))&lt;br /&gt;
                return -EBADF;&lt;br /&gt;
&lt;br /&gt;
/* If file_operations is NULL, or if write or asynchronous write functions in it are NULL, returns an error */&lt;br /&gt;
      if (!file-&amp;gt;f_op || (!file-&amp;gt;f_op-&amp;gt;write &amp;amp;&amp;amp; !file-&amp;gt;f_op-&amp;gt;aio_write))&lt;br /&gt;
                return -EINVAL;&lt;br /&gt;
&lt;br /&gt;
/* unlikely tells the compiler that the statement inside it is unlikely - so, the compiler can optimize in its jump statements. access_ok functions checks if memory range is withing bounds, i.e count bytes can be read from buf. Returns an error if any of these checks fail. */&lt;br /&gt;
      if (unlikely(!access_ok(VERIFY_READ, buf, count)))&lt;br /&gt;
                return -EFAULT;&lt;br /&gt;
&lt;br /&gt;
/* Checks if &amp;quot;count&amp;quot; bytes can be written to the &amp;quot;file&amp;quot; starting at &amp;quot;pos&amp;quot;. Returns how many bytes can be written. */&lt;br /&gt;
      ret = rw_verify_area(WRITE, file, pos, count);&lt;br /&gt;
&lt;br /&gt;
/* Writes using the write function provided in file_operations, which will, in general, depend on the filesystem type. */&lt;br /&gt;
      if (file-&amp;gt;f_op-&amp;gt;write)&lt;br /&gt;
         ret = file-&amp;gt;f_op-&amp;gt;write(file, buf, count, pos);&lt;br /&gt;
&lt;br /&gt;
/* Writes using do_sync_write, which calls file&#039;s aio_write function. */&lt;br /&gt;
      else&lt;br /&gt;
         ret = do_sync_write(file, buf, count, pos);&lt;br /&gt;
&lt;br /&gt;
/* If some bytes were written, it marks the file as &amp;quot;modified&amp;quot;, so anyone listening for filesystem notifications could be called. add_wchar is for accounting - it adds, &amp;quot;ret&amp;quot; bytes to task_io_accounting structure of the current process.  */&lt;br /&gt;
     if (ret &amp;gt; 0) {&lt;br /&gt;
       fsnotify_modify(file);&lt;br /&gt;
       add_wchar(current, ret);&lt;br /&gt;
     }&lt;br /&gt;
/* Again, accounting - Increments the number of &amp;quot;write&amp;quot; system calls for the current process */&lt;br /&gt;
     inc_syscw(current);&lt;br /&gt;
&lt;br /&gt;
/* Returns the number of bytes written. */&lt;br /&gt;
     return ret;&lt;br /&gt;
&lt;br /&gt;
2&lt;br /&gt;
/* A descriptor ring is a structure that a PCI device and the kernel have in common */&lt;br /&gt;
   struct atl1e_tpd_desc *desc;  /* descriptor ring virtual address  */&lt;br /&gt;
/* Physical address of the ring for the Network card driver to use. */&lt;br /&gt;
        dma_addr_t         dma;    /* descriptor ring physical address */&lt;br /&gt;
/* Number of transmit ring buffers */&lt;br /&gt;
        u16                count;  /* the count of transmit rings  */&lt;br /&gt;
/* Transmit read/write lock */&lt;br /&gt;
        rwlock_t           tx_lock;&lt;br /&gt;
/* Next ring to use */&lt;br /&gt;
        u16                next_to_use;&lt;br /&gt;
/* Next ring to clean up */&lt;br /&gt;
        atomic_t           next_to_clean;&lt;br /&gt;
/* Transmit buffer */&lt;br /&gt;
        struct atl1e_tx_buffer *tx_buffer;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/* The CMB dma address of the ring initialized : [here http://lxr.linux.no/linux+v3.1.4/drivers/net/atl1e/atl1e_main.c#L853] &lt;br /&gt;
cmb is a pointer to the virtual address. &lt;br /&gt;
These are written into the registers of the network device as shown [here http://lxr.linux.no/linux+v3.1.4/drivers/net/atl1e/atl1e_main.c#L897]&lt;br /&gt;
Seems to be a device specific internal data structure. &lt;br /&gt;
*/ &lt;br /&gt;
        dma_addr_t         cmb_dma;&lt;br /&gt;
        u32                *cmb;&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_6_2011&amp;diff=15174</id>
		<title>COMP 3000 Lab 6 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_6_2011&amp;diff=15174"/>
		<updated>2011-12-05T01:52:00Z</updated>

		<summary type="html">&lt;p&gt;Saran: /* Part B */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by Sunday, November 20th at 11:30 PM.  &lt;br /&gt;
* Please answer using a single text file (with a .txt extension). Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.) &#039;&#039;&#039;Anything other than a single text file will receive -1 points&#039;&#039;&#039;&lt;br /&gt;
* Show all your work.  If you find an answer by browsing code, explain your search path (e.g., I searched for X, which led me to source file Y, where I found function Z.)   Also, list any websites or individuals you consult.&lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
For this section, you will need to refer to the source of the Linux kernel, version 3.1.1.  You may download the source code [http://www.kernel.org/pub/linux/kernel/v3.0/linux-3.1.1.tar.bz2 here]; however, you will probably prefer to navigate the code through the [http://lxr.linux.no/#linux+v3.1.1/ Linux Cross Reference Project]. *** Students have found it easier to navigate this link ***&lt;br /&gt;
&lt;br /&gt;
# Answer the following questions regarding &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/fs/exec.c#L1449 do_execve_common()]&amp;lt;/tt&amp;gt; in fs/exec.c.&lt;br /&gt;
## What information does &amp;lt;tt&amp;gt;bprm&amp;lt;/tt&amp;gt; hold?&lt;br /&gt;
## What are the many goto&#039;s for?&lt;br /&gt;
## What are the two lines immediately before this function returns with no error (i.e., the execve succeeds)?&lt;br /&gt;
# What function is the main consumer of &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/kernel/fork.c#L1042 copy_process()]&amp;lt;/tt&amp;gt; in kernel/fork.c?&lt;br /&gt;
# In mm/mmap.c, what does the line &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/mm/mmap.c#L243 SYSCALL_DEFINE1(brk, unsigned long, brk)]&amp;lt;/tt&amp;gt; do?&lt;br /&gt;
# What is the call &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/mm/mmap.c#L250 down_write(&amp;amp;mm-&amp;gt;mmap_sem);]&amp;lt;/tt&amp;gt; for?&lt;br /&gt;
# In what file is &amp;lt;tt&amp;gt;atl1e_tx_ring&amp;lt;/tt&amp;gt; declared?&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# [6 pts] Briefly explain what each statement does in &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/fs/read_write.c#L404 vfs_write()]&amp;lt;/tt&amp;gt; in fs/read_write.c.  Your explanation for each line should take up two lines at most.&lt;br /&gt;
# [3 pts] What is the purpose of each field in the struct &amp;lt;tt&amp;gt;atl1e_tx_ring&amp;lt;/tt&amp;gt;?  (Note: the last two fields are extra credit!)&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
===Part A===&lt;br /&gt;
# In fs/exec.c&lt;br /&gt;
## bprm is a pointer to a structure of type linux_binprm. This structure is used to hold arguments that are used when loading binaries, like argc, filename, vm related structures. &lt;br /&gt;
## The goto&#039;s handle error messages, and provide the same functionality as a jump statement in assembly. The retval conditional statements before the goto calls determine what return value the program is providing to the function.&lt;br /&gt;
## if (displaced) put_files_struct(displaced);&lt;br /&gt;
# do_fork&lt;br /&gt;
# SYSCALL_DEFINE1 is a macro defined in syscall.h, used to define system calls. (In this case the brk system call )&lt;br /&gt;
# The brk system call modifies the data segment size, and down_write(&amp;amp;mm-&amp;gt;mmap_sem) is used to acquire a read/write semaphore before performing the memory operation. &lt;br /&gt;
# In drivers/net/atl1e/atl1e.h&lt;br /&gt;
&lt;br /&gt;
===Part B===&lt;br /&gt;
# &lt;br /&gt;
/* If the file descriptor is not opened with WRITE mode, returns an error. */&lt;br /&gt;
      if (!(file-&amp;gt;f_mode &amp;amp; FMODE_WRITE))&lt;br /&gt;
                return -EBADF;&lt;br /&gt;
&lt;br /&gt;
/* If file_operations is NULL, or if write or asynchronous write functions in it are NULL, returns an error */&lt;br /&gt;
      if (!file-&amp;gt;f_op || (!file-&amp;gt;f_op-&amp;gt;write &amp;amp;&amp;amp; !file-&amp;gt;f_op-&amp;gt;aio_write))&lt;br /&gt;
                return -EINVAL;&lt;br /&gt;
&lt;br /&gt;
/* unlikely tells the compiler that the statement inside it is unlikely - so, the compiler can optimize in its jump statements. access_ok functions checks if memory range is withing bounds, i.e count bytes can be read from buf. Returns an error if any of these checks fail. */&lt;br /&gt;
      if (unlikely(!access_ok(VERIFY_READ, buf, count)))&lt;br /&gt;
                return -EFAULT;&lt;br /&gt;
&lt;br /&gt;
/* Checks if &amp;quot;count&amp;quot; bytes can be written to the &amp;quot;file&amp;quot; starting at &amp;quot;pos&amp;quot;. Returns how many bytes can be written. */&lt;br /&gt;
      ret = rw_verify_area(WRITE, file, pos, count);&lt;br /&gt;
&lt;br /&gt;
/* Writes using the write function provided in file_operations, which will, in general, depend on the filesystem type. */&lt;br /&gt;
      if (file-&amp;gt;f_op-&amp;gt;write)&lt;br /&gt;
         ret = file-&amp;gt;f_op-&amp;gt;write(file, buf, count, pos);&lt;br /&gt;
&lt;br /&gt;
/* Writes using do_sync_write, which calls file&#039;s aio_write function. */&lt;br /&gt;
      else&lt;br /&gt;
         ret = do_sync_write(file, buf, count, pos);&lt;br /&gt;
&lt;br /&gt;
/* If some bytes were written, it marks the file as &amp;quot;modified&amp;quot;, so anyone listening for filesystem notifications could be called. add_wchar is for accounting - it adds, &amp;quot;ret&amp;quot; bytes to task_io_accounting structure of the current process.  */&lt;br /&gt;
     if (ret &amp;gt; 0) {&lt;br /&gt;
       fsnotify_modify(file);&lt;br /&gt;
       add_wchar(current, ret);&lt;br /&gt;
     }&lt;br /&gt;
/* Again, accounting - Increments the number of &amp;quot;write&amp;quot; system calls for the current process */&lt;br /&gt;
     inc_syscw(current);&lt;br /&gt;
&lt;br /&gt;
/* Returns the number of bytes written. */&lt;br /&gt;
     return ret;&lt;br /&gt;
&lt;br /&gt;
2&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_6_2011&amp;diff=15173</id>
		<title>COMP 3000 Lab 6 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_6_2011&amp;diff=15173"/>
		<updated>2011-12-05T01:17:26Z</updated>

		<summary type="html">&lt;p&gt;Saran: /* Part B */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by Sunday, November 20th at 11:30 PM.  &lt;br /&gt;
* Please answer using a single text file (with a .txt extension). Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.) &#039;&#039;&#039;Anything other than a single text file will receive -1 points&#039;&#039;&#039;&lt;br /&gt;
* Show all your work.  If you find an answer by browsing code, explain your search path (e.g., I searched for X, which led me to source file Y, where I found function Z.)   Also, list any websites or individuals you consult.&lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
For this section, you will need to refer to the source of the Linux kernel, version 3.1.1.  You may download the source code [http://www.kernel.org/pub/linux/kernel/v3.0/linux-3.1.1.tar.bz2 here]; however, you will probably prefer to navigate the code through the [http://lxr.linux.no/#linux+v3.1.1/ Linux Cross Reference Project]. *** Students have found it easier to navigate this link ***&lt;br /&gt;
&lt;br /&gt;
# Answer the following questions regarding &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/fs/exec.c#L1449 do_execve_common()]&amp;lt;/tt&amp;gt; in fs/exec.c.&lt;br /&gt;
## What information does &amp;lt;tt&amp;gt;bprm&amp;lt;/tt&amp;gt; hold?&lt;br /&gt;
## What are the many goto&#039;s for?&lt;br /&gt;
## What are the two lines immediately before this function returns with no error (i.e., the execve succeeds)?&lt;br /&gt;
# What function is the main consumer of &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/kernel/fork.c#L1042 copy_process()]&amp;lt;/tt&amp;gt; in kernel/fork.c?&lt;br /&gt;
# In mm/mmap.c, what does the line &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/mm/mmap.c#L243 SYSCALL_DEFINE1(brk, unsigned long, brk)]&amp;lt;/tt&amp;gt; do?&lt;br /&gt;
# What is the call &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/mm/mmap.c#L250 down_write(&amp;amp;mm-&amp;gt;mmap_sem);]&amp;lt;/tt&amp;gt; for?&lt;br /&gt;
# In what file is &amp;lt;tt&amp;gt;atl1e_tx_ring&amp;lt;/tt&amp;gt; declared?&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# [6 pts] Briefly explain what each statement does in &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/fs/read_write.c#L404 vfs_write()]&amp;lt;/tt&amp;gt; in fs/read_write.c.  Your explanation for each line should take up two lines at most.&lt;br /&gt;
# [3 pts] What is the purpose of each field in the struct &amp;lt;tt&amp;gt;atl1e_tx_ring&amp;lt;/tt&amp;gt;?  (Note: the last two fields are extra credit!)&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
===Part A===&lt;br /&gt;
# In fs/exec.c&lt;br /&gt;
## bprm is a pointer to a structure of type linux_binprm. This structure is used to hold arguments that are used when loading binaries, like argc, filename, vm related structures. &lt;br /&gt;
## The goto&#039;s handle error messages, and provide the same functionality as a jump statement in assembly. The retval conditional statements before the goto calls determine what return value the program is providing to the function.&lt;br /&gt;
## if (displaced) put_files_struct(displaced);&lt;br /&gt;
# do_fork&lt;br /&gt;
# SYSCALL_DEFINE1 is a macro defined in syscall.h, used to define system calls. (In this case the brk system call )&lt;br /&gt;
# The brk system call modifies the data segment size, and down_write(&amp;amp;mm-&amp;gt;mmap_sem) is used to acquire a read/write semaphore before performing the memory operation. &lt;br /&gt;
# In drivers/net/atl1e/atl1e.h&lt;br /&gt;
&lt;br /&gt;
===Part B===&lt;br /&gt;
# &lt;br /&gt;
/* If the file descriptor is not opened with WRITE mode, returns an error. */&lt;br /&gt;
      if (!(file-&amp;gt;f_mode &amp;amp; FMODE_WRITE))&lt;br /&gt;
                return -EBADF;&lt;br /&gt;
&lt;br /&gt;
/* If file_operations is NULL, or if write or asynchronous write functions in it are NULL, returns an error */&lt;br /&gt;
      if (!file-&amp;gt;f_op || (!file-&amp;gt;f_op-&amp;gt;write &amp;amp;&amp;amp; !file-&amp;gt;f_op-&amp;gt;aio_write))&lt;br /&gt;
                return -EINVAL;&lt;br /&gt;
&lt;br /&gt;
/* unlikely tells the compiler that the statement inside it is unlikely - so, the compiler can optimize in its jump statements. access_ok functions checks if memory range is withing bounds, i.e count bytes can be read from buf. Returns an error if any of these checks fail. */&lt;br /&gt;
      if (unlikely(!access_ok(VERIFY_READ, buf, count)))&lt;br /&gt;
                return -EFAULT;&lt;br /&gt;
&lt;br /&gt;
/* Checks if &amp;quot;count&amp;quot; bytes can be written to the &amp;quot;file&amp;quot; starting at &amp;quot;pos&amp;quot;. Returns how many bytes can be written. */&lt;br /&gt;
      ret = rw_verify_area(WRITE, file, pos, count);&lt;br /&gt;
&lt;br /&gt;
/* Writes using the write function provided in file_operations, which will, in general, depend on the filesystem type. */&lt;br /&gt;
      if (file-&amp;gt;f_op-&amp;gt;write)&lt;br /&gt;
         ret = file-&amp;gt;f_op-&amp;gt;write(file, buf, count, pos);&lt;br /&gt;
&lt;br /&gt;
/* Writes using do_sync_write, which calls file&#039;s aio_write function. */&lt;br /&gt;
      else&lt;br /&gt;
         ret = do_sync_write(file, buf, count, pos);&lt;br /&gt;
&lt;br /&gt;
/* If some bytes were written, it marks the file as &amp;quot;modified&amp;quot;, so anyone listening for filesystem notifications could be called. add_wchar is for accounting - it adds, &amp;quot;ret&amp;quot; bytes to task_io_accounting structure of the current process.  */&lt;br /&gt;
     if (ret &amp;gt; 0) {&lt;br /&gt;
       fsnotify_modify(file);&lt;br /&gt;
       add_wchar(current, ret);&lt;br /&gt;
     }&lt;br /&gt;
/* Again, accounting - Increments the number of &amp;quot;write&amp;quot; system calls for the current process */&lt;br /&gt;
     inc_syscw(current);&lt;br /&gt;
&lt;br /&gt;
/* Returns the number of bytes written. */&lt;br /&gt;
     return ret;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2&#039;&#039;&#039;&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_6_2011&amp;diff=15172</id>
		<title>COMP 3000 Lab 6 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_6_2011&amp;diff=15172"/>
		<updated>2011-12-05T01:14:27Z</updated>

		<summary type="html">&lt;p&gt;Saran: /* Part B */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by Sunday, November 20th at 11:30 PM.  &lt;br /&gt;
* Please answer using a single text file (with a .txt extension). Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.) &#039;&#039;&#039;Anything other than a single text file will receive -1 points&#039;&#039;&#039;&lt;br /&gt;
* Show all your work.  If you find an answer by browsing code, explain your search path (e.g., I searched for X, which led me to source file Y, where I found function Z.)   Also, list any websites or individuals you consult.&lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
For this section, you will need to refer to the source of the Linux kernel, version 3.1.1.  You may download the source code [http://www.kernel.org/pub/linux/kernel/v3.0/linux-3.1.1.tar.bz2 here]; however, you will probably prefer to navigate the code through the [http://lxr.linux.no/#linux+v3.1.1/ Linux Cross Reference Project]. *** Students have found it easier to navigate this link ***&lt;br /&gt;
&lt;br /&gt;
# Answer the following questions regarding &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/fs/exec.c#L1449 do_execve_common()]&amp;lt;/tt&amp;gt; in fs/exec.c.&lt;br /&gt;
## What information does &amp;lt;tt&amp;gt;bprm&amp;lt;/tt&amp;gt; hold?&lt;br /&gt;
## What are the many goto&#039;s for?&lt;br /&gt;
## What are the two lines immediately before this function returns with no error (i.e., the execve succeeds)?&lt;br /&gt;
# What function is the main consumer of &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/kernel/fork.c#L1042 copy_process()]&amp;lt;/tt&amp;gt; in kernel/fork.c?&lt;br /&gt;
# In mm/mmap.c, what does the line &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/mm/mmap.c#L243 SYSCALL_DEFINE1(brk, unsigned long, brk)]&amp;lt;/tt&amp;gt; do?&lt;br /&gt;
# What is the call &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/mm/mmap.c#L250 down_write(&amp;amp;mm-&amp;gt;mmap_sem);]&amp;lt;/tt&amp;gt; for?&lt;br /&gt;
# In what file is &amp;lt;tt&amp;gt;atl1e_tx_ring&amp;lt;/tt&amp;gt; declared?&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# [6 pts] Briefly explain what each statement does in &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/fs/read_write.c#L404 vfs_write()]&amp;lt;/tt&amp;gt; in fs/read_write.c.  Your explanation for each line should take up two lines at most.&lt;br /&gt;
# [3 pts] What is the purpose of each field in the struct &amp;lt;tt&amp;gt;atl1e_tx_ring&amp;lt;/tt&amp;gt;?  (Note: the last two fields are extra credit!)&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
===Part A===&lt;br /&gt;
# In fs/exec.c&lt;br /&gt;
## bprm is a pointer to a structure of type linux_binprm. This structure is used to hold arguments that are used when loading binaries, like argc, filename, vm related structures. &lt;br /&gt;
## The goto&#039;s handle error messages, and provide the same functionality as a jump statement in assembly. The retval conditional statements before the goto calls determine what return value the program is providing to the function.&lt;br /&gt;
## if (displaced) put_files_struct(displaced);&lt;br /&gt;
# do_fork&lt;br /&gt;
# SYSCALL_DEFINE1 is a macro defined in syscall.h, used to define system calls. (In this case the brk system call )&lt;br /&gt;
# The brk system call modifies the data segment size, and down_write(&amp;amp;mm-&amp;gt;mmap_sem) is used to acquire a read/write semaphore before performing the memory operation. &lt;br /&gt;
# In drivers/net/atl1e/atl1e.h&lt;br /&gt;
&lt;br /&gt;
===Part B===&lt;br /&gt;
# &lt;br /&gt;
/* If the file descriptor is not opened with WRITE mode, returns an error. */&lt;br /&gt;
      if (!(file-&amp;gt;f_mode &amp;amp; FMODE_WRITE))&lt;br /&gt;
                return -EBADF;&lt;br /&gt;
&lt;br /&gt;
/* If file_operations is NULL, or if write or asynchronous write functions in it are NULL, returns an error */&lt;br /&gt;
      if (!file-&amp;gt;f_op || (!file-&amp;gt;f_op-&amp;gt;write &amp;amp;&amp;amp; !file-&amp;gt;f_op-&amp;gt;aio_write))&lt;br /&gt;
                return -EINVAL;&lt;br /&gt;
&lt;br /&gt;
/* unlikely tells the compiler that the statement inside it is unlikely - so, the compiler can optimize in its jump statements. access_ok functions checks if memory range is withing bounds, i.e count bytes can be read from buf. Returns an error if any of these checks fail. */&lt;br /&gt;
      if (unlikely(!access_ok(VERIFY_READ, buf, count)))&lt;br /&gt;
                return -EFAULT;&lt;br /&gt;
&lt;br /&gt;
/* Checks if &amp;quot;count&amp;quot; bytes can be written to the &amp;quot;file&amp;quot; starting at &amp;quot;pos&amp;quot;. Returns how many bytes can be written. */&lt;br /&gt;
      ret = rw_verify_area(WRITE, file, pos, count);&lt;br /&gt;
&lt;br /&gt;
/* Writes using the write function provided in file_operations, which will, in general, depend on the filesystem type. */&lt;br /&gt;
      if (file-&amp;gt;f_op-&amp;gt;write)&lt;br /&gt;
         ret = file-&amp;gt;f_op-&amp;gt;write(file, buf, count, pos);&lt;br /&gt;
&lt;br /&gt;
/* Writes using do_sync_write, which calls file&#039;s aio_write function. */&lt;br /&gt;
      else&lt;br /&gt;
         ret = do_sync_write(file, buf, count, pos);&lt;br /&gt;
&lt;br /&gt;
/* If some bytes were written, it marks the file as &amp;quot;modified&amp;quot;, so anyone listening for filesystem notifications could be called. add_wchar is for accounting - it adds, &amp;quot;ret&amp;quot; bytes to task_io_accounting structure of the current process.  */&lt;br /&gt;
     if (ret &amp;gt; 0) {&lt;br /&gt;
       fsnotify_modify(file);&lt;br /&gt;
       add_wchar(current, ret);&lt;br /&gt;
     }&lt;br /&gt;
/* Again, accounting - Increments the number of &amp;quot;write&amp;quot; system calls for the current process */&lt;br /&gt;
     inc_syscw(current);&lt;br /&gt;
&lt;br /&gt;
/* Returns the number of bytes written. */&lt;br /&gt;
     return ret;&lt;br /&gt;
&lt;br /&gt;
#&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_6_2011&amp;diff=15171</id>
		<title>COMP 3000 Lab 6 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_6_2011&amp;diff=15171"/>
		<updated>2011-12-05T01:13:53Z</updated>

		<summary type="html">&lt;p&gt;Saran: /* Part B */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by Sunday, November 20th at 11:30 PM.  &lt;br /&gt;
* Please answer using a single text file (with a .txt extension). Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.) &#039;&#039;&#039;Anything other than a single text file will receive -1 points&#039;&#039;&#039;&lt;br /&gt;
* Show all your work.  If you find an answer by browsing code, explain your search path (e.g., I searched for X, which led me to source file Y, where I found function Z.)   Also, list any websites or individuals you consult.&lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
For this section, you will need to refer to the source of the Linux kernel, version 3.1.1.  You may download the source code [http://www.kernel.org/pub/linux/kernel/v3.0/linux-3.1.1.tar.bz2 here]; however, you will probably prefer to navigate the code through the [http://lxr.linux.no/#linux+v3.1.1/ Linux Cross Reference Project]. *** Students have found it easier to navigate this link ***&lt;br /&gt;
&lt;br /&gt;
# Answer the following questions regarding &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/fs/exec.c#L1449 do_execve_common()]&amp;lt;/tt&amp;gt; in fs/exec.c.&lt;br /&gt;
## What information does &amp;lt;tt&amp;gt;bprm&amp;lt;/tt&amp;gt; hold?&lt;br /&gt;
## What are the many goto&#039;s for?&lt;br /&gt;
## What are the two lines immediately before this function returns with no error (i.e., the execve succeeds)?&lt;br /&gt;
# What function is the main consumer of &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/kernel/fork.c#L1042 copy_process()]&amp;lt;/tt&amp;gt; in kernel/fork.c?&lt;br /&gt;
# In mm/mmap.c, what does the line &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/mm/mmap.c#L243 SYSCALL_DEFINE1(brk, unsigned long, brk)]&amp;lt;/tt&amp;gt; do?&lt;br /&gt;
# What is the call &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/mm/mmap.c#L250 down_write(&amp;amp;mm-&amp;gt;mmap_sem);]&amp;lt;/tt&amp;gt; for?&lt;br /&gt;
# In what file is &amp;lt;tt&amp;gt;atl1e_tx_ring&amp;lt;/tt&amp;gt; declared?&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# [6 pts] Briefly explain what each statement does in &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/fs/read_write.c#L404 vfs_write()]&amp;lt;/tt&amp;gt; in fs/read_write.c.  Your explanation for each line should take up two lines at most.&lt;br /&gt;
# [3 pts] What is the purpose of each field in the struct &amp;lt;tt&amp;gt;atl1e_tx_ring&amp;lt;/tt&amp;gt;?  (Note: the last two fields are extra credit!)&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
===Part A===&lt;br /&gt;
# In fs/exec.c&lt;br /&gt;
## bprm is a pointer to a structure of type linux_binprm. This structure is used to hold arguments that are used when loading binaries, like argc, filename, vm related structures. &lt;br /&gt;
## The goto&#039;s handle error messages, and provide the same functionality as a jump statement in assembly. The retval conditional statements before the goto calls determine what return value the program is providing to the function.&lt;br /&gt;
## if (displaced) put_files_struct(displaced);&lt;br /&gt;
# do_fork&lt;br /&gt;
# SYSCALL_DEFINE1 is a macro defined in syscall.h, used to define system calls. (In this case the brk system call )&lt;br /&gt;
# The brk system call modifies the data segment size, and down_write(&amp;amp;mm-&amp;gt;mmap_sem) is used to acquire a read/write semaphore before performing the memory operation. &lt;br /&gt;
# In drivers/net/atl1e/atl1e.h&lt;br /&gt;
&lt;br /&gt;
===Part B===&lt;br /&gt;
# &lt;br /&gt;
*&lt;br /&gt;
/* If the file descriptor is not opened with WRITE mode, returns an error. */&lt;br /&gt;
      if (!(file-&amp;gt;f_mode &amp;amp; FMODE_WRITE))&lt;br /&gt;
                return -EBADF;&lt;br /&gt;
&lt;br /&gt;
/* If file_operations is NULL, or if write or asynchronous write functions in it are NULL, returns an error */&lt;br /&gt;
      if (!file-&amp;gt;f_op || (!file-&amp;gt;f_op-&amp;gt;write &amp;amp;&amp;amp; !file-&amp;gt;f_op-&amp;gt;aio_write))&lt;br /&gt;
                return -EINVAL;&lt;br /&gt;
&lt;br /&gt;
/* unlikely tells the compiler that the statement inside it is unlikely - so, the compiler can optimize in its jump statements. access_ok functions checks if memory range is withing bounds, i.e count bytes can be read from buf. Returns an error if any of these checks fail. */&lt;br /&gt;
      if (unlikely(!access_ok(VERIFY_READ, buf, count)))&lt;br /&gt;
                return -EFAULT;&lt;br /&gt;
&lt;br /&gt;
/* Checks if &amp;quot;count&amp;quot; bytes can be written to the &amp;quot;file&amp;quot; starting at &amp;quot;pos&amp;quot;. Returns how many bytes can be written. */&lt;br /&gt;
      ret = rw_verify_area(WRITE, file, pos, count);&lt;br /&gt;
&lt;br /&gt;
/* Writes using the write function provided in file_operations, which will, in general, depend on the filesystem type. */&lt;br /&gt;
      if (file-&amp;gt;f_op-&amp;gt;write)&lt;br /&gt;
         ret = file-&amp;gt;f_op-&amp;gt;write(file, buf, count, pos);&lt;br /&gt;
&lt;br /&gt;
/* Writes using do_sync_write, which calls file&#039;s aio_write function. */&lt;br /&gt;
      else&lt;br /&gt;
         ret = do_sync_write(file, buf, count, pos);&lt;br /&gt;
&lt;br /&gt;
/* If some bytes were written, it marks the file as &amp;quot;modified&amp;quot;, so anyone listening for filesystem notifications could be called. add_wchar is for accounting - it adds, &amp;quot;ret&amp;quot; bytes to task_io_accounting structure of the current process.  */&lt;br /&gt;
     if (ret &amp;gt; 0) {&lt;br /&gt;
       fsnotify_modify(file);&lt;br /&gt;
       add_wchar(current, ret);&lt;br /&gt;
     }&lt;br /&gt;
/* Again, accounting - Increments the number of &amp;quot;write&amp;quot; system calls for the current process */&lt;br /&gt;
     inc_syscw(current);&lt;br /&gt;
&lt;br /&gt;
/* Returns the number of bytes written. */&lt;br /&gt;
     return ret;&lt;br /&gt;
&lt;br /&gt;
*&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_6_2011&amp;diff=15170</id>
		<title>COMP 3000 Lab 6 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_6_2011&amp;diff=15170"/>
		<updated>2011-12-05T01:13:23Z</updated>

		<summary type="html">&lt;p&gt;Saran: /* Part B */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by Sunday, November 20th at 11:30 PM.  &lt;br /&gt;
* Please answer using a single text file (with a .txt extension). Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.) &#039;&#039;&#039;Anything other than a single text file will receive -1 points&#039;&#039;&#039;&lt;br /&gt;
* Show all your work.  If you find an answer by browsing code, explain your search path (e.g., I searched for X, which led me to source file Y, where I found function Z.)   Also, list any websites or individuals you consult.&lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
For this section, you will need to refer to the source of the Linux kernel, version 3.1.1.  You may download the source code [http://www.kernel.org/pub/linux/kernel/v3.0/linux-3.1.1.tar.bz2 here]; however, you will probably prefer to navigate the code through the [http://lxr.linux.no/#linux+v3.1.1/ Linux Cross Reference Project]. *** Students have found it easier to navigate this link ***&lt;br /&gt;
&lt;br /&gt;
# Answer the following questions regarding &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/fs/exec.c#L1449 do_execve_common()]&amp;lt;/tt&amp;gt; in fs/exec.c.&lt;br /&gt;
## What information does &amp;lt;tt&amp;gt;bprm&amp;lt;/tt&amp;gt; hold?&lt;br /&gt;
## What are the many goto&#039;s for?&lt;br /&gt;
## What are the two lines immediately before this function returns with no error (i.e., the execve succeeds)?&lt;br /&gt;
# What function is the main consumer of &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/kernel/fork.c#L1042 copy_process()]&amp;lt;/tt&amp;gt; in kernel/fork.c?&lt;br /&gt;
# In mm/mmap.c, what does the line &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/mm/mmap.c#L243 SYSCALL_DEFINE1(brk, unsigned long, brk)]&amp;lt;/tt&amp;gt; do?&lt;br /&gt;
# What is the call &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/mm/mmap.c#L250 down_write(&amp;amp;mm-&amp;gt;mmap_sem);]&amp;lt;/tt&amp;gt; for?&lt;br /&gt;
# In what file is &amp;lt;tt&amp;gt;atl1e_tx_ring&amp;lt;/tt&amp;gt; declared?&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# [6 pts] Briefly explain what each statement does in &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/fs/read_write.c#L404 vfs_write()]&amp;lt;/tt&amp;gt; in fs/read_write.c.  Your explanation for each line should take up two lines at most.&lt;br /&gt;
# [3 pts] What is the purpose of each field in the struct &amp;lt;tt&amp;gt;atl1e_tx_ring&amp;lt;/tt&amp;gt;?  (Note: the last two fields are extra credit!)&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
===Part A===&lt;br /&gt;
# In fs/exec.c&lt;br /&gt;
## bprm is a pointer to a structure of type linux_binprm. This structure is used to hold arguments that are used when loading binaries, like argc, filename, vm related structures. &lt;br /&gt;
## The goto&#039;s handle error messages, and provide the same functionality as a jump statement in assembly. The retval conditional statements before the goto calls determine what return value the program is providing to the function.&lt;br /&gt;
## if (displaced) put_files_struct(displaced);&lt;br /&gt;
# do_fork&lt;br /&gt;
# SYSCALL_DEFINE1 is a macro defined in syscall.h, used to define system calls. (In this case the brk system call )&lt;br /&gt;
# The brk system call modifies the data segment size, and down_write(&amp;amp;mm-&amp;gt;mmap_sem) is used to acquire a read/write semaphore before performing the memory operation. &lt;br /&gt;
# In drivers/net/atl1e/atl1e.h&lt;br /&gt;
&lt;br /&gt;
===Part B===&lt;br /&gt;
# &lt;br /&gt;
*&lt;br /&gt;
&amp;lt;nowiki&amp;gt;&lt;br /&gt;
/* If the file descriptor is not opened with WRITE mode, returns an error. */&lt;br /&gt;
      if (!(file-&amp;gt;f_mode &amp;amp; FMODE_WRITE))&lt;br /&gt;
                return -EBADF;&lt;br /&gt;
&lt;br /&gt;
/* If file_operations is NULL, or if write or asynchronous write functions in it are NULL, returns an error */&lt;br /&gt;
      if (!file-&amp;gt;f_op || (!file-&amp;gt;f_op-&amp;gt;write &amp;amp;&amp;amp; !file-&amp;gt;f_op-&amp;gt;aio_write))&lt;br /&gt;
                return -EINVAL;&lt;br /&gt;
&lt;br /&gt;
/* unlikely tells the compiler that the statement inside it is unlikely - so, the compiler can optimize in its jump statements. access_ok functions checks if memory range is withing bounds, i.e count bytes can be read from buf. Returns an error if any of these checks fail. */&lt;br /&gt;
      if (unlikely(!access_ok(VERIFY_READ, buf, count)))&lt;br /&gt;
                return -EFAULT;&lt;br /&gt;
&lt;br /&gt;
/* Checks if &amp;quot;count&amp;quot; bytes can be written to the &amp;quot;file&amp;quot; starting at &amp;quot;pos&amp;quot;. Returns how many bytes can be written. */&lt;br /&gt;
      ret = rw_verify_area(WRITE, file, pos, count);&lt;br /&gt;
&lt;br /&gt;
/* Writes using the write function provided in file_operations, which will, in general, depend on the filesystem type. */&lt;br /&gt;
      if (file-&amp;gt;f_op-&amp;gt;write)&lt;br /&gt;
         ret = file-&amp;gt;f_op-&amp;gt;write(file, buf, count, pos);&lt;br /&gt;
&lt;br /&gt;
/* Writes using do_sync_write, which calls file&#039;s aio_write function. */&lt;br /&gt;
      else&lt;br /&gt;
         ret = do_sync_write(file, buf, count, pos);&lt;br /&gt;
&lt;br /&gt;
/* If some bytes were written, it marks the file as &amp;quot;modified&amp;quot;, so anyone listening for filesystem notifications could be called. add_wchar is for accounting - it adds, &amp;quot;ret&amp;quot; bytes to task_io_accounting structure of the current process.  */&lt;br /&gt;
     if (ret &amp;gt; 0) {&lt;br /&gt;
       fsnotify_modify(file);&lt;br /&gt;
       add_wchar(current, ret);&lt;br /&gt;
     }&lt;br /&gt;
/* Again, accounting - Increments the number of &amp;quot;write&amp;quot; system calls for the current process */&lt;br /&gt;
     inc_syscw(current);&lt;br /&gt;
&lt;br /&gt;
/* Returns the number of bytes written. */&lt;br /&gt;
     return ret;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_6_2011&amp;diff=15165</id>
		<title>COMP 3000 Lab 6 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_6_2011&amp;diff=15165"/>
		<updated>2011-12-05T00:22:26Z</updated>

		<summary type="html">&lt;p&gt;Saran: /* Solutions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by Sunday, November 20th at 11:30 PM.  &lt;br /&gt;
* Please answer using a single text file (with a .txt extension). Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.) &#039;&#039;&#039;Anything other than a single text file will receive -1 points&#039;&#039;&#039;&lt;br /&gt;
* Show all your work.  If you find an answer by browsing code, explain your search path (e.g., I searched for X, which led me to source file Y, where I found function Z.)   Also, list any websites or individuals you consult.&lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
For this section, you will need to refer to the source of the Linux kernel, version 3.1.1.  You may download the source code [http://www.kernel.org/pub/linux/kernel/v3.0/linux-3.1.1.tar.bz2 here]; however, you will probably prefer to navigate the code through the [http://lxr.linux.no/#linux+v3.1.1/ Linux Cross Reference Project]. *** Students have found it easier to navigate this link ***&lt;br /&gt;
&lt;br /&gt;
# Answer the following questions regarding &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/fs/exec.c#L1449 do_execve_common()]&amp;lt;/tt&amp;gt; in fs/exec.c.&lt;br /&gt;
## What information does &amp;lt;tt&amp;gt;bprm&amp;lt;/tt&amp;gt; hold?&lt;br /&gt;
## What are the many goto&#039;s for?&lt;br /&gt;
## What are the two lines immediately before this function returns with no error (i.e., the execve succeeds)?&lt;br /&gt;
# What function is the main consumer of &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/kernel/fork.c#L1042 copy_process()]&amp;lt;/tt&amp;gt; in kernel/fork.c?&lt;br /&gt;
# In mm/mmap.c, what does the line &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/mm/mmap.c#L243 SYSCALL_DEFINE1(brk, unsigned long, brk)]&amp;lt;/tt&amp;gt; do?&lt;br /&gt;
# What is the call &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/mm/mmap.c#L250 down_write(&amp;amp;mm-&amp;gt;mmap_sem);]&amp;lt;/tt&amp;gt; for?&lt;br /&gt;
# In what file is &amp;lt;tt&amp;gt;atl1e_tx_ring&amp;lt;/tt&amp;gt; declared?&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# [6 pts] Briefly explain what each statement does in &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/fs/read_write.c#L404 vfs_write()]&amp;lt;/tt&amp;gt; in fs/read_write.c.  Your explanation for each line should take up two lines at most.&lt;br /&gt;
# [3 pts] What is the purpose of each field in the struct &amp;lt;tt&amp;gt;atl1e_tx_ring&amp;lt;/tt&amp;gt;?  (Note: the last two fields are extra credit!)&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
===Part A===&lt;br /&gt;
# In fs/exec.c&lt;br /&gt;
## bprm is a pointer to a structure of type linux_binprm. This structure is used to hold arguments that are used when loading binaries, like argc, filename, vm related structures. &lt;br /&gt;
## The goto&#039;s handle error messages, and provide the same functionality as a jump statement in assembly. The retval conditional statements before the goto calls determine what return value the program is providing to the function.&lt;br /&gt;
## if (displaced) put_files_struct(displaced);&lt;br /&gt;
# do_fork&lt;br /&gt;
# SYSCALL_DEFINE1 is a macro defined in syscall.h, used to define system calls. (In this case the brk system call )&lt;br /&gt;
# The brk system call modifies the data segment size, and down_write(&amp;amp;mm-&amp;gt;mmap_sem) is used to acquire a read/write semaphore before performing the memory operation. &lt;br /&gt;
# In drivers/net/atl1e/atl1e.h&lt;br /&gt;
&lt;br /&gt;
===Part B===&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_6_2011&amp;diff=15164</id>
		<title>COMP 3000 Lab 6 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_6_2011&amp;diff=15164"/>
		<updated>2011-12-05T00:20:52Z</updated>

		<summary type="html">&lt;p&gt;Saran: /* Part A */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by Sunday, November 20th at 11:30 PM.  &lt;br /&gt;
* Please answer using a single text file (with a .txt extension). Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.) &#039;&#039;&#039;Anything other than a single text file will receive -1 points&#039;&#039;&#039;&lt;br /&gt;
* Show all your work.  If you find an answer by browsing code, explain your search path (e.g., I searched for X, which led me to source file Y, where I found function Z.)   Also, list any websites or individuals you consult.&lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
For this section, you will need to refer to the source of the Linux kernel, version 3.1.1.  You may download the source code [http://www.kernel.org/pub/linux/kernel/v3.0/linux-3.1.1.tar.bz2 here]; however, you will probably prefer to navigate the code through the [http://lxr.linux.no/#linux+v3.1.1/ Linux Cross Reference Project]. *** Students have found it easier to navigate this link ***&lt;br /&gt;
&lt;br /&gt;
# Answer the following questions regarding &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/fs/exec.c#L1449 do_execve_common()]&amp;lt;/tt&amp;gt; in fs/exec.c.&lt;br /&gt;
## What information does &amp;lt;tt&amp;gt;bprm&amp;lt;/tt&amp;gt; hold?&lt;br /&gt;
## What are the many goto&#039;s for?&lt;br /&gt;
## What are the two lines immediately before this function returns with no error (i.e., the execve succeeds)?&lt;br /&gt;
# What function is the main consumer of &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/kernel/fork.c#L1042 copy_process()]&amp;lt;/tt&amp;gt; in kernel/fork.c?&lt;br /&gt;
# In mm/mmap.c, what does the line &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/mm/mmap.c#L243 SYSCALL_DEFINE1(brk, unsigned long, brk)]&amp;lt;/tt&amp;gt; do?&lt;br /&gt;
# What is the call &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/mm/mmap.c#L250 down_write(&amp;amp;mm-&amp;gt;mmap_sem);]&amp;lt;/tt&amp;gt; for?&lt;br /&gt;
# In what file is &amp;lt;tt&amp;gt;atl1e_tx_ring&amp;lt;/tt&amp;gt; declared?&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# [6 pts] Briefly explain what each statement does in &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/fs/read_write.c#L404 vfs_write()]&amp;lt;/tt&amp;gt; in fs/read_write.c.  Your explanation for each line should take up two lines at most.&lt;br /&gt;
# [3 pts] What is the purpose of each field in the struct &amp;lt;tt&amp;gt;atl1e_tx_ring&amp;lt;/tt&amp;gt;?  (Note: the last two fields are extra credit!)&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
===Part A===&lt;br /&gt;
# In fs/exec.c&lt;br /&gt;
## bprm is a pointer to a structure of type linux_binprm. This structure is used to hold arguments that are used when loading binaries, like argc, filename, vm related structures. &lt;br /&gt;
## The goto&#039;s handle error messages, and provide the same functionality as a jump statement in assembly. The retval conditional statements before the goto calls determine what return value the program is providing to the function.&lt;br /&gt;
## if (displaced) put_files_struct(displaced);&lt;br /&gt;
# do_fork&lt;br /&gt;
# SYSCALL_DEFINE1 is a macro defined in syscall.h, used to define system calls. (In this case the brk system call )&lt;br /&gt;
# The brk system call modifies the data segment size, and down_write(&amp;amp;mm-&amp;gt;mmap_sem) is used to acquire a read/write semaphore before performing the memory operation. &lt;br /&gt;
# In drivers/net/atl1e/atl1e.h&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_6_2011&amp;diff=15163</id>
		<title>COMP 3000 Lab 6 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_6_2011&amp;diff=15163"/>
		<updated>2011-12-05T00:20:16Z</updated>

		<summary type="html">&lt;p&gt;Saran: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by Sunday, November 20th at 11:30 PM.  &lt;br /&gt;
* Please answer using a single text file (with a .txt extension). Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.) &#039;&#039;&#039;Anything other than a single text file will receive -1 points&#039;&#039;&#039;&lt;br /&gt;
* Show all your work.  If you find an answer by browsing code, explain your search path (e.g., I searched for X, which led me to source file Y, where I found function Z.)   Also, list any websites or individuals you consult.&lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
For this section, you will need to refer to the source of the Linux kernel, version 3.1.1.  You may download the source code [http://www.kernel.org/pub/linux/kernel/v3.0/linux-3.1.1.tar.bz2 here]; however, you will probably prefer to navigate the code through the [http://lxr.linux.no/#linux+v3.1.1/ Linux Cross Reference Project]. *** Students have found it easier to navigate this link ***&lt;br /&gt;
&lt;br /&gt;
# Answer the following questions regarding &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/fs/exec.c#L1449 do_execve_common()]&amp;lt;/tt&amp;gt; in fs/exec.c.&lt;br /&gt;
## What information does &amp;lt;tt&amp;gt;bprm&amp;lt;/tt&amp;gt; hold?&lt;br /&gt;
## What are the many goto&#039;s for?&lt;br /&gt;
## What are the two lines immediately before this function returns with no error (i.e., the execve succeeds)?&lt;br /&gt;
# What function is the main consumer of &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/kernel/fork.c#L1042 copy_process()]&amp;lt;/tt&amp;gt; in kernel/fork.c?&lt;br /&gt;
# In mm/mmap.c, what does the line &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/mm/mmap.c#L243 SYSCALL_DEFINE1(brk, unsigned long, brk)]&amp;lt;/tt&amp;gt; do?&lt;br /&gt;
# What is the call &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/mm/mmap.c#L250 down_write(&amp;amp;mm-&amp;gt;mmap_sem);]&amp;lt;/tt&amp;gt; for?&lt;br /&gt;
# In what file is &amp;lt;tt&amp;gt;atl1e_tx_ring&amp;lt;/tt&amp;gt; declared?&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# [6 pts] Briefly explain what each statement does in &amp;lt;tt&amp;gt;[http://lxr.linux.no/linux+*/fs/read_write.c#L404 vfs_write()]&amp;lt;/tt&amp;gt; in fs/read_write.c.  Your explanation for each line should take up two lines at most.&lt;br /&gt;
# [3 pts] What is the purpose of each field in the struct &amp;lt;tt&amp;gt;atl1e_tx_ring&amp;lt;/tt&amp;gt;?  (Note: the last two fields are extra credit!)&lt;br /&gt;
&lt;br /&gt;
==Solutions==&lt;br /&gt;
===Part A===&lt;br /&gt;
# In fs/exec.c&lt;br /&gt;
## bprm is a pointer to a structure of type linux_binprm. This structure is used to hold arguments that are used when loading binaries, like argc, filename, vm related structures. &lt;br /&gt;
## The goto&#039;s handle error messages, and provide the same functionality as a jump statement in assembly. The retval conditional statements before the goto calls determine what return value the program is providing to the function.&lt;br /&gt;
 if (displaced)&lt;br /&gt;
 	put_files_struct(displaced);&lt;br /&gt;
# do_fork&lt;br /&gt;
# SYSCALL_DEFINE1 is a macro defined in syscall.h, used to define system calls. (In this case the brk system call )&lt;br /&gt;
# The brk system call modifies the data segment size, and down_write(&amp;amp;mm-&amp;gt;mmap_sem) is used to acquire a read/write semaphore before performing the memory operation. &lt;br /&gt;
# In drivers/net/atl1e/atl1e.h&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_5_2011&amp;diff=14921</id>
		<title>COMP 3000 Lab 5 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_5_2011&amp;diff=14921"/>
		<updated>2011-11-21T16:59:37Z</updated>

		<summary type="html">&lt;p&gt;Saran: /* Part B */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by Sunday, November 6th at 11:30 PM.  &lt;br /&gt;
* Please answer using a single text file (with a .txt extension). Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.) &#039;&#039;&#039;Anything other than a single text file will receive -1 points&#039;&#039;&#039;&lt;br /&gt;
* Show all your work.  Short answers are not sufficient; you should list any websites or individuals you consult and should document any experiments you conducted.  For any question that you could just answer without any external reference or experiment, write &amp;quot;(no work required)&amp;quot; after your answer.&lt;br /&gt;
* All submitted code and commands should compile and run. Partial code fragments or explanations may not be given credit.  While code may use standard C and UNIX/Linux libraries, no code should rely on external binaries.&lt;br /&gt;
* &#039;&#039;&#039;The exercises in this lab require root access on a Linux system.&#039;&#039;&#039;  Note that an Ubuntu Live CD should be a sufficiently powerful environment if you do not have an already installed system.  Just boot the CD in the VM and select &amp;quot;Try Ubuntu.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
Be sure to do the following questions in order.&lt;br /&gt;
&lt;br /&gt;
# [0.5] Run the command &amp;lt;tt&amp;gt;truncate -s 2G foo&amp;lt;/tt&amp;gt;.  What is the logical size of foo, and how much space does it consume on disk?&lt;br /&gt;
# [0.5] Run &amp;lt;tt&amp;gt;mkfs.ext4 foo&amp;lt;/tt&amp;gt;.  (Say &amp;quot;yes&amp;quot; to operating on a regular file.)  What is the logical size of foo now, and how much space does it now consume on disk?&lt;br /&gt;
# [0.5] Run &amp;lt;tt&amp;gt;mount -o loop foo /mnt&amp;lt;/tt&amp;gt;.  What does this command do?&lt;br /&gt;
# [0.5] Run &amp;lt;tt&amp;gt;cp /bin/ls /mnt&amp;lt;/tt&amp;gt;.  What does this command do?&lt;br /&gt;
# [0.5] Run &amp;lt;tt&amp;gt;umount /mnt&amp;lt;/tt&amp;gt;.  Can you still access /mnt/ls?  Why or why not?&lt;br /&gt;
# [0.5] Run &amp;lt;tt&amp;gt;dd if=/dev/zero of=foo conv=notrunc count=10 bs=512&amp;lt;/tt&amp;gt;.  What does this command do?&lt;br /&gt;
# [0.5] Run &amp;lt;tt&amp;gt;mount -o loop foo /mnt&amp;lt;/tt&amp;gt; again.  Why did the command fail?&lt;br /&gt;
# [0.5] What command can you run to make foo mountable again?&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# [2] Create a simple C program to generate a file consisting of the word &amp;quot;Hello&amp;quot;, one million bytes of blank space (null bytes), and then &amp;quot;world\n&amp;quot;, but that consumes only a small number of blocks on disk while having a logical size of 1,000,011 bytes.&lt;br /&gt;
# [2] Create a C program &amp;quot;mycopy.c&amp;quot; that takes two filenames A and B as command line arguments.  This program should copy the contents of A to B, making B a byte-for-byte copy of A.  Zero bytes in A should consume little to no space in B.  (Hint: You&#039;ll have to use the sparse file creation technique of the previous question here.)&lt;br /&gt;
# [2] Describe a simple way an attacker could get root access on a system if loopback mounts were permitted for non-privileged users.&lt;br /&gt;
&lt;br /&gt;
=Answers=&lt;br /&gt;
==Part A==&lt;br /&gt;
# Logical size is 2G. Physical size is 0.&lt;br /&gt;
# Logical size is 2G. Physical size is 65M.&lt;br /&gt;
# It links foo to an unused loop device and mounts it in /mnt.&lt;br /&gt;
# It copies the file located at /bin/ls to /mnt, indirectly writing it to foo.&lt;br /&gt;
# After unmounting, files in that file system cannot be accessed, because /mnt is not linked to foo (where /bin/ls is actually stored as a file) anymore. &lt;br /&gt;
# This command copies 10 blocks of 512 bytes each from /dev/zero to foo. &lt;br /&gt;
# Because writing 5120 bytes of zeros to the filesystem at foo corrupted it, destroying the superblock and invalidating some checksums.&lt;br /&gt;
# fsck.ext4 foo&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# 1.c&lt;br /&gt;
# 2.c&lt;br /&gt;
# An attacker could open foo, modify an [http://lxr.linux.no/linux+v3.1/include/linux/fs.h#L748 inode] to add set setuid and setgid bits and modify the owner and group of the file to 0,0 ( i.e root,root). She would then mount foo and run this file - which would run as root - to gain root privileges. Note that this method does not require you to assume that loopback mounting is permitted on non-writable directories.&lt;br /&gt;
&lt;br /&gt;
 /* 1.c */&lt;br /&gt;
 #include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
        int fd = open(&amp;quot;helloemptyworld&amp;quot;,O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);&lt;br /&gt;
        if(fd == -1)&lt;br /&gt;
                exit(1);&lt;br /&gt;
        write(fd,&amp;quot;Hello&amp;quot;,5);&lt;br /&gt;
        lseek(fd,1000000,SEEK_CUR);&lt;br /&gt;
        write(fd,&amp;quot;World\n&amp;quot;,6);&lt;br /&gt;
        close(fd);&lt;br /&gt;
        return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* 2.c */&lt;br /&gt;
 #include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;errno.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;string.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
        int fdi,fdo,ret;&lt;br /&gt;
        char buf;&lt;br /&gt;
        if( (fdi = open(&amp;quot;inputfile&amp;quot;,O_RDONLY)) == -1)&lt;br /&gt;
                exit(1);&lt;br /&gt;
        if( (fdo = open(&amp;quot;outputfile&amp;quot;,O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)&lt;br /&gt;
                exit(1);&lt;br /&gt;
        if(read(fdi,&amp;amp;buf,1) == -1)&lt;br /&gt;
                printf(&amp;quot;Error = %s\n&amp;quot;,strerror(errno));&lt;br /&gt;
        while(1)&lt;br /&gt;
        {&lt;br /&gt;
                if(buf == 0)&lt;br /&gt;
                        lseek(fdo,1,SEEK_CUR);&lt;br /&gt;
                else {&lt;br /&gt;
                        if(write(fdo,&amp;amp;buf,1) == -1)&lt;br /&gt;
                                printf(&amp;quot;Error = %s\n&amp;quot;,strerror(errno));&lt;br /&gt;
                }&lt;br /&gt;
                ret = read(fdi,&amp;amp;buf,1);&lt;br /&gt;
                if(ret == -1)&lt;br /&gt;
                        printf(&amp;quot;Error = %s\n&amp;quot;,strerror(errno));&lt;br /&gt;
                else if(ret == 0) //indicates EOF&lt;br /&gt;
                        break;&lt;br /&gt;
        }&lt;br /&gt;
        close(fdi);&lt;br /&gt;
        close(fdo);&lt;br /&gt;
        return 0;&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_5_2011&amp;diff=14920</id>
		<title>COMP 3000 Lab 5 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_5_2011&amp;diff=14920"/>
		<updated>2011-11-21T16:23:57Z</updated>

		<summary type="html">&lt;p&gt;Saran: /* Part B */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by Sunday, November 6th at 11:30 PM.  &lt;br /&gt;
* Please answer using a single text file (with a .txt extension). Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.) &#039;&#039;&#039;Anything other than a single text file will receive -1 points&#039;&#039;&#039;&lt;br /&gt;
* Show all your work.  Short answers are not sufficient; you should list any websites or individuals you consult and should document any experiments you conducted.  For any question that you could just answer without any external reference or experiment, write &amp;quot;(no work required)&amp;quot; after your answer.&lt;br /&gt;
* All submitted code and commands should compile and run. Partial code fragments or explanations may not be given credit.  While code may use standard C and UNIX/Linux libraries, no code should rely on external binaries.&lt;br /&gt;
* &#039;&#039;&#039;The exercises in this lab require root access on a Linux system.&#039;&#039;&#039;  Note that an Ubuntu Live CD should be a sufficiently powerful environment if you do not have an already installed system.  Just boot the CD in the VM and select &amp;quot;Try Ubuntu.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
Be sure to do the following questions in order.&lt;br /&gt;
&lt;br /&gt;
# [0.5] Run the command &amp;lt;tt&amp;gt;truncate -s 2G foo&amp;lt;/tt&amp;gt;.  What is the logical size of foo, and how much space does it consume on disk?&lt;br /&gt;
# [0.5] Run &amp;lt;tt&amp;gt;mkfs.ext4 foo&amp;lt;/tt&amp;gt;.  (Say &amp;quot;yes&amp;quot; to operating on a regular file.)  What is the logical size of foo now, and how much space does it now consume on disk?&lt;br /&gt;
# [0.5] Run &amp;lt;tt&amp;gt;mount -o loop foo /mnt&amp;lt;/tt&amp;gt;.  What does this command do?&lt;br /&gt;
# [0.5] Run &amp;lt;tt&amp;gt;cp /bin/ls /mnt&amp;lt;/tt&amp;gt;.  What does this command do?&lt;br /&gt;
# [0.5] Run &amp;lt;tt&amp;gt;umount /mnt&amp;lt;/tt&amp;gt;.  Can you still access /mnt/ls?  Why or why not?&lt;br /&gt;
# [0.5] Run &amp;lt;tt&amp;gt;dd if=/dev/zero of=foo conv=notrunc count=10 bs=512&amp;lt;/tt&amp;gt;.  What does this command do?&lt;br /&gt;
# [0.5] Run &amp;lt;tt&amp;gt;mount -o loop foo /mnt&amp;lt;/tt&amp;gt; again.  Why did the command fail?&lt;br /&gt;
# [0.5] What command can you run to make foo mountable again?&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# [2] Create a simple C program to generate a file consisting of the word &amp;quot;Hello&amp;quot;, one million bytes of blank space (null bytes), and then &amp;quot;world\n&amp;quot;, but that consumes only a small number of blocks on disk while having a logical size of 1,000,011 bytes.&lt;br /&gt;
# [2] Create a C program &amp;quot;mycopy.c&amp;quot; that takes two filenames A and B as command line arguments.  This program should copy the contents of A to B, making B a byte-for-byte copy of A.  Zero bytes in A should consume little to no space in B.  (Hint: You&#039;ll have to use the sparse file creation technique of the previous question here.)&lt;br /&gt;
# [2] Describe a simple way an attacker could get root access on a system if loopback mounts were permitted for non-privileged users.&lt;br /&gt;
&lt;br /&gt;
=Answers=&lt;br /&gt;
==Part A==&lt;br /&gt;
# Logical size is 2G. Physical size is 0.&lt;br /&gt;
# Logical size is 2G. Physical size is 65M.&lt;br /&gt;
# It links foo to an unused loop device and mounts it in /mnt.&lt;br /&gt;
# It copies the file located at /bin/ls to /mnt, indirectly writing it to foo.&lt;br /&gt;
# After unmounting, files in that file system cannot be accessed, because /mnt is not linked to foo (where /bin/ls is actually stored as a file) anymore. &lt;br /&gt;
# This command copies 10 blocks of 512 bytes each from /dev/zero to foo. &lt;br /&gt;
# Because writing 5120 bytes of zeros to the filesystem at foo corrupted it, destroying the superblock and invalidating some checksums.&lt;br /&gt;
# fsck.ext4 foo&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# 1.c&lt;br /&gt;
# 2.c&lt;br /&gt;
# An attacker could open foo, modify an [http://lxr.linux.no/linux+v3.1/include/linux/fs.h#L748 inode] to add set setuid and setgid bits and modify the owner and group of the file to 0,0 ( i.e root,root). She would then mount foo and run this file - which would run as root - to gain root privileges.&lt;br /&gt;
&lt;br /&gt;
 /* 1.c */&lt;br /&gt;
 #include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
        int fd = open(&amp;quot;helloemptyworld&amp;quot;,O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);&lt;br /&gt;
        if(fd == -1)&lt;br /&gt;
                exit(1);&lt;br /&gt;
        write(fd,&amp;quot;Hello&amp;quot;,5);&lt;br /&gt;
        lseek(fd,1000000,SEEK_CUR);&lt;br /&gt;
        write(fd,&amp;quot;World\n&amp;quot;,6);&lt;br /&gt;
        close(fd);&lt;br /&gt;
        return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* 2.c */&lt;br /&gt;
 #include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;errno.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;string.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
        int fdi,fdo,ret;&lt;br /&gt;
        char buf;&lt;br /&gt;
        if( (fdi = open(&amp;quot;inputfile&amp;quot;,O_RDONLY)) == -1)&lt;br /&gt;
                exit(1);&lt;br /&gt;
        if( (fdo = open(&amp;quot;outputfile&amp;quot;,O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)&lt;br /&gt;
                exit(1);&lt;br /&gt;
        if(read(fdi,&amp;amp;buf,1) == -1)&lt;br /&gt;
                printf(&amp;quot;Error = %s\n&amp;quot;,strerror(errno));&lt;br /&gt;
        while(1)&lt;br /&gt;
        {&lt;br /&gt;
                if(buf == 0)&lt;br /&gt;
                        lseek(fdo,1,SEEK_CUR);&lt;br /&gt;
                else {&lt;br /&gt;
                        if(write(fdo,&amp;amp;buf,1) == -1)&lt;br /&gt;
                                printf(&amp;quot;Error = %s\n&amp;quot;,strerror(errno));&lt;br /&gt;
                }&lt;br /&gt;
                ret = read(fdi,&amp;amp;buf,1);&lt;br /&gt;
                if(ret == -1)&lt;br /&gt;
                        printf(&amp;quot;Error = %s\n&amp;quot;,strerror(errno));&lt;br /&gt;
                else if(ret == 0) //indicates EOF&lt;br /&gt;
                        break;&lt;br /&gt;
        }&lt;br /&gt;
        close(fdi);&lt;br /&gt;
        close(fdo);&lt;br /&gt;
        return 0;&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_5_2011&amp;diff=14919</id>
		<title>COMP 3000 Lab 5 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_5_2011&amp;diff=14919"/>
		<updated>2011-11-21T16:23:42Z</updated>

		<summary type="html">&lt;p&gt;Saran: /* Part B */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by Sunday, November 6th at 11:30 PM.  &lt;br /&gt;
* Please answer using a single text file (with a .txt extension). Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.) &#039;&#039;&#039;Anything other than a single text file will receive -1 points&#039;&#039;&#039;&lt;br /&gt;
* Show all your work.  Short answers are not sufficient; you should list any websites or individuals you consult and should document any experiments you conducted.  For any question that you could just answer without any external reference or experiment, write &amp;quot;(no work required)&amp;quot; after your answer.&lt;br /&gt;
* All submitted code and commands should compile and run. Partial code fragments or explanations may not be given credit.  While code may use standard C and UNIX/Linux libraries, no code should rely on external binaries.&lt;br /&gt;
* &#039;&#039;&#039;The exercises in this lab require root access on a Linux system.&#039;&#039;&#039;  Note that an Ubuntu Live CD should be a sufficiently powerful environment if you do not have an already installed system.  Just boot the CD in the VM and select &amp;quot;Try Ubuntu.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
Be sure to do the following questions in order.&lt;br /&gt;
&lt;br /&gt;
# [0.5] Run the command &amp;lt;tt&amp;gt;truncate -s 2G foo&amp;lt;/tt&amp;gt;.  What is the logical size of foo, and how much space does it consume on disk?&lt;br /&gt;
# [0.5] Run &amp;lt;tt&amp;gt;mkfs.ext4 foo&amp;lt;/tt&amp;gt;.  (Say &amp;quot;yes&amp;quot; to operating on a regular file.)  What is the logical size of foo now, and how much space does it now consume on disk?&lt;br /&gt;
# [0.5] Run &amp;lt;tt&amp;gt;mount -o loop foo /mnt&amp;lt;/tt&amp;gt;.  What does this command do?&lt;br /&gt;
# [0.5] Run &amp;lt;tt&amp;gt;cp /bin/ls /mnt&amp;lt;/tt&amp;gt;.  What does this command do?&lt;br /&gt;
# [0.5] Run &amp;lt;tt&amp;gt;umount /mnt&amp;lt;/tt&amp;gt;.  Can you still access /mnt/ls?  Why or why not?&lt;br /&gt;
# [0.5] Run &amp;lt;tt&amp;gt;dd if=/dev/zero of=foo conv=notrunc count=10 bs=512&amp;lt;/tt&amp;gt;.  What does this command do?&lt;br /&gt;
# [0.5] Run &amp;lt;tt&amp;gt;mount -o loop foo /mnt&amp;lt;/tt&amp;gt; again.  Why did the command fail?&lt;br /&gt;
# [0.5] What command can you run to make foo mountable again?&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# [2] Create a simple C program to generate a file consisting of the word &amp;quot;Hello&amp;quot;, one million bytes of blank space (null bytes), and then &amp;quot;world\n&amp;quot;, but that consumes only a small number of blocks on disk while having a logical size of 1,000,011 bytes.&lt;br /&gt;
&lt;br /&gt;
# [2] Create a C program &amp;quot;mycopy.c&amp;quot; that takes two filenames A and B as command line arguments.  This program should copy the contents of A to B, making B a byte-for-byte copy of A.  Zero bytes in A should consume little to no space in B.  (Hint: You&#039;ll have to use the sparse file creation technique of the previous question here.)&lt;br /&gt;
&lt;br /&gt;
# [2] Describe a simple way an attacker could get root access on a system if loopback mounts were permitted for non-privileged users.&lt;br /&gt;
&lt;br /&gt;
=Answers=&lt;br /&gt;
==Part A==&lt;br /&gt;
# Logical size is 2G. Physical size is 0.&lt;br /&gt;
# Logical size is 2G. Physical size is 65M.&lt;br /&gt;
# It links foo to an unused loop device and mounts it in /mnt.&lt;br /&gt;
# It copies the file located at /bin/ls to /mnt, indirectly writing it to foo.&lt;br /&gt;
# After unmounting, files in that file system cannot be accessed, because /mnt is not linked to foo (where /bin/ls is actually stored as a file) anymore. &lt;br /&gt;
# This command copies 10 blocks of 512 bytes each from /dev/zero to foo. &lt;br /&gt;
# Because writing 5120 bytes of zeros to the filesystem at foo corrupted it, destroying the superblock and invalidating some checksums.&lt;br /&gt;
# fsck.ext4 foo&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# 1.c&lt;br /&gt;
# 2.c&lt;br /&gt;
# An attacker could open foo, modify an [http://lxr.linux.no/linux+v3.1/include/linux/fs.h#L748 inode] to add set setuid and setgid bits and modify the owner and group of the file to 0,0 ( i.e root,root). She would then mount foo and run this file - which would run as root - to gain root privileges.&lt;br /&gt;
&lt;br /&gt;
 /* 1.c */&lt;br /&gt;
 #include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
        int fd = open(&amp;quot;helloemptyworld&amp;quot;,O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);&lt;br /&gt;
        if(fd == -1)&lt;br /&gt;
                exit(1);&lt;br /&gt;
        write(fd,&amp;quot;Hello&amp;quot;,5);&lt;br /&gt;
        lseek(fd,1000000,SEEK_CUR);&lt;br /&gt;
        write(fd,&amp;quot;World\n&amp;quot;,6);&lt;br /&gt;
        close(fd);&lt;br /&gt;
        return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* 2.c */&lt;br /&gt;
 #include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;errno.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;string.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
        int fdi,fdo,ret;&lt;br /&gt;
        char buf;&lt;br /&gt;
        if( (fdi = open(&amp;quot;inputfile&amp;quot;,O_RDONLY)) == -1)&lt;br /&gt;
                exit(1);&lt;br /&gt;
        if( (fdo = open(&amp;quot;outputfile&amp;quot;,O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)&lt;br /&gt;
                exit(1);&lt;br /&gt;
        if(read(fdi,&amp;amp;buf,1) == -1)&lt;br /&gt;
                printf(&amp;quot;Error = %s\n&amp;quot;,strerror(errno));&lt;br /&gt;
        while(1)&lt;br /&gt;
        {&lt;br /&gt;
                if(buf == 0)&lt;br /&gt;
                        lseek(fdo,1,SEEK_CUR);&lt;br /&gt;
                else {&lt;br /&gt;
                        if(write(fdo,&amp;amp;buf,1) == -1)&lt;br /&gt;
                                printf(&amp;quot;Error = %s\n&amp;quot;,strerror(errno));&lt;br /&gt;
                }&lt;br /&gt;
                ret = read(fdi,&amp;amp;buf,1);&lt;br /&gt;
                if(ret == -1)&lt;br /&gt;
                        printf(&amp;quot;Error = %s\n&amp;quot;,strerror(errno));&lt;br /&gt;
                else if(ret == 0) //indicates EOF&lt;br /&gt;
                        break;&lt;br /&gt;
        }&lt;br /&gt;
        close(fdi);&lt;br /&gt;
        close(fdo);&lt;br /&gt;
        return 0;&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_5_2011&amp;diff=14918</id>
		<title>COMP 3000 Lab 5 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_5_2011&amp;diff=14918"/>
		<updated>2011-11-21T16:23:21Z</updated>

		<summary type="html">&lt;p&gt;Saran: /* Part B */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by Sunday, November 6th at 11:30 PM.  &lt;br /&gt;
* Please answer using a single text file (with a .txt extension). Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.) &#039;&#039;&#039;Anything other than a single text file will receive -1 points&#039;&#039;&#039;&lt;br /&gt;
* Show all your work.  Short answers are not sufficient; you should list any websites or individuals you consult and should document any experiments you conducted.  For any question that you could just answer without any external reference or experiment, write &amp;quot;(no work required)&amp;quot; after your answer.&lt;br /&gt;
* All submitted code and commands should compile and run. Partial code fragments or explanations may not be given credit.  While code may use standard C and UNIX/Linux libraries, no code should rely on external binaries.&lt;br /&gt;
* &#039;&#039;&#039;The exercises in this lab require root access on a Linux system.&#039;&#039;&#039;  Note that an Ubuntu Live CD should be a sufficiently powerful environment if you do not have an already installed system.  Just boot the CD in the VM and select &amp;quot;Try Ubuntu.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
Be sure to do the following questions in order.&lt;br /&gt;
&lt;br /&gt;
# [0.5] Run the command &amp;lt;tt&amp;gt;truncate -s 2G foo&amp;lt;/tt&amp;gt;.  What is the logical size of foo, and how much space does it consume on disk?&lt;br /&gt;
# [0.5] Run &amp;lt;tt&amp;gt;mkfs.ext4 foo&amp;lt;/tt&amp;gt;.  (Say &amp;quot;yes&amp;quot; to operating on a regular file.)  What is the logical size of foo now, and how much space does it now consume on disk?&lt;br /&gt;
# [0.5] Run &amp;lt;tt&amp;gt;mount -o loop foo /mnt&amp;lt;/tt&amp;gt;.  What does this command do?&lt;br /&gt;
# [0.5] Run &amp;lt;tt&amp;gt;cp /bin/ls /mnt&amp;lt;/tt&amp;gt;.  What does this command do?&lt;br /&gt;
# [0.5] Run &amp;lt;tt&amp;gt;umount /mnt&amp;lt;/tt&amp;gt;.  Can you still access /mnt/ls?  Why or why not?&lt;br /&gt;
# [0.5] Run &amp;lt;tt&amp;gt;dd if=/dev/zero of=foo conv=notrunc count=10 bs=512&amp;lt;/tt&amp;gt;.  What does this command do?&lt;br /&gt;
# [0.5] Run &amp;lt;tt&amp;gt;mount -o loop foo /mnt&amp;lt;/tt&amp;gt; again.  Why did the command fail?&lt;br /&gt;
# [0.5] What command can you run to make foo mountable again?&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
[2] # Create a simple C program to generate a file consisting of the word &amp;quot;Hello&amp;quot;, one million bytes of blank space (null bytes), and then &amp;quot;world\n&amp;quot;, but that consumes only a small number of blocks on disk while having a logical size of 1,000,011 bytes.&lt;br /&gt;
&lt;br /&gt;
[2] # Create a C program &amp;quot;mycopy.c&amp;quot; that takes two filenames A and B as command line arguments.  This program should copy the contents of A to B, making B a byte-for-byte copy of A.  Zero bytes in A should consume little to no space in B.  (Hint: You&#039;ll have to use the sparse file creation technique of the previous question here.)&lt;br /&gt;
&lt;br /&gt;
[2] # Describe a simple way an attacker could get root access on a system if loopback mounts were permitted for non-privileged users.&lt;br /&gt;
&lt;br /&gt;
=Answers=&lt;br /&gt;
==Part A==&lt;br /&gt;
# Logical size is 2G. Physical size is 0.&lt;br /&gt;
# Logical size is 2G. Physical size is 65M.&lt;br /&gt;
# It links foo to an unused loop device and mounts it in /mnt.&lt;br /&gt;
# It copies the file located at /bin/ls to /mnt, indirectly writing it to foo.&lt;br /&gt;
# After unmounting, files in that file system cannot be accessed, because /mnt is not linked to foo (where /bin/ls is actually stored as a file) anymore. &lt;br /&gt;
# This command copies 10 blocks of 512 bytes each from /dev/zero to foo. &lt;br /&gt;
# Because writing 5120 bytes of zeros to the filesystem at foo corrupted it, destroying the superblock and invalidating some checksums.&lt;br /&gt;
# fsck.ext4 foo&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# 1.c&lt;br /&gt;
# 2.c&lt;br /&gt;
# An attacker could open foo, modify an [http://lxr.linux.no/linux+v3.1/include/linux/fs.h#L748 inode] to add set setuid and setgid bits and modify the owner and group of the file to 0,0 ( i.e root,root). She would then mount foo and run this file - which would run as root - to gain root privileges.&lt;br /&gt;
&lt;br /&gt;
 /* 1.c */&lt;br /&gt;
 #include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
        int fd = open(&amp;quot;helloemptyworld&amp;quot;,O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);&lt;br /&gt;
        if(fd == -1)&lt;br /&gt;
                exit(1);&lt;br /&gt;
        write(fd,&amp;quot;Hello&amp;quot;,5);&lt;br /&gt;
        lseek(fd,1000000,SEEK_CUR);&lt;br /&gt;
        write(fd,&amp;quot;World\n&amp;quot;,6);&lt;br /&gt;
        close(fd);&lt;br /&gt;
        return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* 2.c */&lt;br /&gt;
 #include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;errno.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;string.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
        int fdi,fdo,ret;&lt;br /&gt;
        char buf;&lt;br /&gt;
        if( (fdi = open(&amp;quot;inputfile&amp;quot;,O_RDONLY)) == -1)&lt;br /&gt;
                exit(1);&lt;br /&gt;
        if( (fdo = open(&amp;quot;outputfile&amp;quot;,O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)&lt;br /&gt;
                exit(1);&lt;br /&gt;
        if(read(fdi,&amp;amp;buf,1) == -1)&lt;br /&gt;
                printf(&amp;quot;Error = %s\n&amp;quot;,strerror(errno));&lt;br /&gt;
        while(1)&lt;br /&gt;
        {&lt;br /&gt;
                if(buf == 0)&lt;br /&gt;
                        lseek(fdo,1,SEEK_CUR);&lt;br /&gt;
                else {&lt;br /&gt;
                        if(write(fdo,&amp;amp;buf,1) == -1)&lt;br /&gt;
                                printf(&amp;quot;Error = %s\n&amp;quot;,strerror(errno));&lt;br /&gt;
                }&lt;br /&gt;
                ret = read(fdi,&amp;amp;buf,1);&lt;br /&gt;
                if(ret == -1)&lt;br /&gt;
                        printf(&amp;quot;Error = %s\n&amp;quot;,strerror(errno));&lt;br /&gt;
                else if(ret == 0) //indicates EOF&lt;br /&gt;
                        break;&lt;br /&gt;
        }&lt;br /&gt;
        close(fdi);&lt;br /&gt;
        close(fdo);&lt;br /&gt;
        return 0;&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_5_2011&amp;diff=14917</id>
		<title>COMP 3000 Lab 5 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_5_2011&amp;diff=14917"/>
		<updated>2011-11-21T16:23:06Z</updated>

		<summary type="html">&lt;p&gt;Saran: /* Part B */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by Sunday, November 6th at 11:30 PM.  &lt;br /&gt;
* Please answer using a single text file (with a .txt extension). Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.) &#039;&#039;&#039;Anything other than a single text file will receive -1 points&#039;&#039;&#039;&lt;br /&gt;
* Show all your work.  Short answers are not sufficient; you should list any websites or individuals you consult and should document any experiments you conducted.  For any question that you could just answer without any external reference or experiment, write &amp;quot;(no work required)&amp;quot; after your answer.&lt;br /&gt;
* All submitted code and commands should compile and run. Partial code fragments or explanations may not be given credit.  While code may use standard C and UNIX/Linux libraries, no code should rely on external binaries.&lt;br /&gt;
* &#039;&#039;&#039;The exercises in this lab require root access on a Linux system.&#039;&#039;&#039;  Note that an Ubuntu Live CD should be a sufficiently powerful environment if you do not have an already installed system.  Just boot the CD in the VM and select &amp;quot;Try Ubuntu.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
Be sure to do the following questions in order.&lt;br /&gt;
&lt;br /&gt;
# [0.5] Run the command &amp;lt;tt&amp;gt;truncate -s 2G foo&amp;lt;/tt&amp;gt;.  What is the logical size of foo, and how much space does it consume on disk?&lt;br /&gt;
# [0.5] Run &amp;lt;tt&amp;gt;mkfs.ext4 foo&amp;lt;/tt&amp;gt;.  (Say &amp;quot;yes&amp;quot; to operating on a regular file.)  What is the logical size of foo now, and how much space does it now consume on disk?&lt;br /&gt;
# [0.5] Run &amp;lt;tt&amp;gt;mount -o loop foo /mnt&amp;lt;/tt&amp;gt;.  What does this command do?&lt;br /&gt;
# [0.5] Run &amp;lt;tt&amp;gt;cp /bin/ls /mnt&amp;lt;/tt&amp;gt;.  What does this command do?&lt;br /&gt;
# [0.5] Run &amp;lt;tt&amp;gt;umount /mnt&amp;lt;/tt&amp;gt;.  Can you still access /mnt/ls?  Why or why not?&lt;br /&gt;
# [0.5] Run &amp;lt;tt&amp;gt;dd if=/dev/zero of=foo conv=notrunc count=10 bs=512&amp;lt;/tt&amp;gt;.  What does this command do?&lt;br /&gt;
# [0.5] Run &amp;lt;tt&amp;gt;mount -o loop foo /mnt&amp;lt;/tt&amp;gt; again.  Why did the command fail?&lt;br /&gt;
# [0.5] What command can you run to make foo mountable again?&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
[2] # Create a simple C program to generate a file consisting of the word &amp;quot;Hello&amp;quot;, one million bytes of blank space (null bytes), and then &amp;quot;world\n&amp;quot;, but that consumes only a small number of blocks on disk while having a logical size of 1,000,011 bytes.&lt;br /&gt;
[2] # Create a C program &amp;quot;mycopy.c&amp;quot; that takes two filenames A and B as command line arguments.  This program should copy the contents of A to B, making B a byte-for-byte copy of A.  Zero bytes in A should consume little to no space in B.  (Hint: You&#039;ll have to use the sparse file creation technique of the previous question here.)&lt;br /&gt;
[2] # Describe a simple way an attacker could get root access on a system if loopback mounts were permitted for non-privileged users.&lt;br /&gt;
&lt;br /&gt;
=Answers=&lt;br /&gt;
==Part A==&lt;br /&gt;
# Logical size is 2G. Physical size is 0.&lt;br /&gt;
# Logical size is 2G. Physical size is 65M.&lt;br /&gt;
# It links foo to an unused loop device and mounts it in /mnt.&lt;br /&gt;
# It copies the file located at /bin/ls to /mnt, indirectly writing it to foo.&lt;br /&gt;
# After unmounting, files in that file system cannot be accessed, because /mnt is not linked to foo (where /bin/ls is actually stored as a file) anymore. &lt;br /&gt;
# This command copies 10 blocks of 512 bytes each from /dev/zero to foo. &lt;br /&gt;
# Because writing 5120 bytes of zeros to the filesystem at foo corrupted it, destroying the superblock and invalidating some checksums.&lt;br /&gt;
# fsck.ext4 foo&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# 1.c&lt;br /&gt;
# 2.c&lt;br /&gt;
# An attacker could open foo, modify an [http://lxr.linux.no/linux+v3.1/include/linux/fs.h#L748 inode] to add set setuid and setgid bits and modify the owner and group of the file to 0,0 ( i.e root,root). She would then mount foo and run this file - which would run as root - to gain root privileges.&lt;br /&gt;
&lt;br /&gt;
 /* 1.c */&lt;br /&gt;
 #include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
        int fd = open(&amp;quot;helloemptyworld&amp;quot;,O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);&lt;br /&gt;
        if(fd == -1)&lt;br /&gt;
                exit(1);&lt;br /&gt;
        write(fd,&amp;quot;Hello&amp;quot;,5);&lt;br /&gt;
        lseek(fd,1000000,SEEK_CUR);&lt;br /&gt;
        write(fd,&amp;quot;World\n&amp;quot;,6);&lt;br /&gt;
        close(fd);&lt;br /&gt;
        return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* 2.c */&lt;br /&gt;
 #include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;errno.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;string.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
        int fdi,fdo,ret;&lt;br /&gt;
        char buf;&lt;br /&gt;
        if( (fdi = open(&amp;quot;inputfile&amp;quot;,O_RDONLY)) == -1)&lt;br /&gt;
                exit(1);&lt;br /&gt;
        if( (fdo = open(&amp;quot;outputfile&amp;quot;,O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)&lt;br /&gt;
                exit(1);&lt;br /&gt;
        if(read(fdi,&amp;amp;buf,1) == -1)&lt;br /&gt;
                printf(&amp;quot;Error = %s\n&amp;quot;,strerror(errno));&lt;br /&gt;
        while(1)&lt;br /&gt;
        {&lt;br /&gt;
                if(buf == 0)&lt;br /&gt;
                        lseek(fdo,1,SEEK_CUR);&lt;br /&gt;
                else {&lt;br /&gt;
                        if(write(fdo,&amp;amp;buf,1) == -1)&lt;br /&gt;
                                printf(&amp;quot;Error = %s\n&amp;quot;,strerror(errno));&lt;br /&gt;
                }&lt;br /&gt;
                ret = read(fdi,&amp;amp;buf,1);&lt;br /&gt;
                if(ret == -1)&lt;br /&gt;
                        printf(&amp;quot;Error = %s\n&amp;quot;,strerror(errno));&lt;br /&gt;
                else if(ret == 0) //indicates EOF&lt;br /&gt;
                        break;&lt;br /&gt;
        }&lt;br /&gt;
        close(fdi);&lt;br /&gt;
        close(fdo);&lt;br /&gt;
        return 0;&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_5_2011&amp;diff=14916</id>
		<title>COMP 3000 Lab 5 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_5_2011&amp;diff=14916"/>
		<updated>2011-11-21T16:20:59Z</updated>

		<summary type="html">&lt;p&gt;Saran: /* Part A */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by Sunday, November 6th at 11:30 PM.  &lt;br /&gt;
* Please answer using a single text file (with a .txt extension). Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.) &#039;&#039;&#039;Anything other than a single text file will receive -1 points&#039;&#039;&#039;&lt;br /&gt;
* Show all your work.  Short answers are not sufficient; you should list any websites or individuals you consult and should document any experiments you conducted.  For any question that you could just answer without any external reference or experiment, write &amp;quot;(no work required)&amp;quot; after your answer.&lt;br /&gt;
* All submitted code and commands should compile and run. Partial code fragments or explanations may not be given credit.  While code may use standard C and UNIX/Linux libraries, no code should rely on external binaries.&lt;br /&gt;
* &#039;&#039;&#039;The exercises in this lab require root access on a Linux system.&#039;&#039;&#039;  Note that an Ubuntu Live CD should be a sufficiently powerful environment if you do not have an already installed system.  Just boot the CD in the VM and select &amp;quot;Try Ubuntu.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
Be sure to do the following questions in order.&lt;br /&gt;
&lt;br /&gt;
# [0.5] Run the command &amp;lt;tt&amp;gt;truncate -s 2G foo&amp;lt;/tt&amp;gt;.  What is the logical size of foo, and how much space does it consume on disk?&lt;br /&gt;
# [0.5] Run &amp;lt;tt&amp;gt;mkfs.ext4 foo&amp;lt;/tt&amp;gt;.  (Say &amp;quot;yes&amp;quot; to operating on a regular file.)  What is the logical size of foo now, and how much space does it now consume on disk?&lt;br /&gt;
# [0.5] Run &amp;lt;tt&amp;gt;mount -o loop foo /mnt&amp;lt;/tt&amp;gt;.  What does this command do?&lt;br /&gt;
# [0.5] Run &amp;lt;tt&amp;gt;cp /bin/ls /mnt&amp;lt;/tt&amp;gt;.  What does this command do?&lt;br /&gt;
# [0.5] Run &amp;lt;tt&amp;gt;umount /mnt&amp;lt;/tt&amp;gt;.  Can you still access /mnt/ls?  Why or why not?&lt;br /&gt;
# [0.5] Run &amp;lt;tt&amp;gt;dd if=/dev/zero of=foo conv=notrunc count=10 bs=512&amp;lt;/tt&amp;gt;.  What does this command do?&lt;br /&gt;
# [0.5] Run &amp;lt;tt&amp;gt;mount -o loop foo /mnt&amp;lt;/tt&amp;gt; again.  Why did the command fail?&lt;br /&gt;
# [0.5] What command can you run to make foo mountable again?&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# Create a simple C program to generate a file consisting of the word &amp;quot;Hello&amp;quot;, one million bytes of blank space (null bytes), and then &amp;quot;world\n&amp;quot;, but that consumes only a small number of blocks on disk while having a logical size of 1,000,011 bytes.&lt;br /&gt;
# Create a C program &amp;quot;mycopy.c&amp;quot; that takes two filenames A and B as command line arguments.  This program should copy the contents of A to B, making B a byte-for-byte copy of A.  Zero bytes in A should consume little to no space in B.  (Hint: You&#039;ll have to use the sparse file creation technique of the previous question here.)&lt;br /&gt;
# Describe a simple way an attacker could get root access on a system if loopback mounts were permitted for non-privileged users.&lt;br /&gt;
&lt;br /&gt;
=Answers=&lt;br /&gt;
==Part A==&lt;br /&gt;
# Logical size is 2G. Physical size is 0.&lt;br /&gt;
# Logical size is 2G. Physical size is 65M.&lt;br /&gt;
# It links foo to an unused loop device and mounts it in /mnt.&lt;br /&gt;
# It copies the file located at /bin/ls to /mnt, indirectly writing it to foo.&lt;br /&gt;
# After unmounting, files in that file system cannot be accessed, because /mnt is not linked to foo (where /bin/ls is actually stored as a file) anymore. &lt;br /&gt;
# This command copies 10 blocks of 512 bytes each from /dev/zero to foo. &lt;br /&gt;
# Because writing 5120 bytes of zeros to the filesystem at foo corrupted it, destroying the superblock and invalidating some checksums.&lt;br /&gt;
# fsck.ext4 foo&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# 1.c&lt;br /&gt;
# 2.c&lt;br /&gt;
# An attacker could open foo, modify an [http://lxr.linux.no/linux+v3.1/include/linux/fs.h#L748 inode] to add set setuid and setgid bits and modify the owner and group of the file to 0,0 ( i.e root,root). She would then mount foo and run this file - which would run as root - to gain root privileges.&lt;br /&gt;
&lt;br /&gt;
 /* 1.c */&lt;br /&gt;
 #include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
        int fd = open(&amp;quot;helloemptyworld&amp;quot;,O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);&lt;br /&gt;
        if(fd == -1)&lt;br /&gt;
                exit(1);&lt;br /&gt;
        write(fd,&amp;quot;Hello&amp;quot;,5);&lt;br /&gt;
        lseek(fd,1000000,SEEK_CUR);&lt;br /&gt;
        write(fd,&amp;quot;World\n&amp;quot;,6);&lt;br /&gt;
        close(fd);&lt;br /&gt;
        return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* 2.c */&lt;br /&gt;
 #include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;errno.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;string.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
        int fdi,fdo,ret;&lt;br /&gt;
        char buf;&lt;br /&gt;
        if( (fdi = open(&amp;quot;inputfile&amp;quot;,O_RDONLY)) == -1)&lt;br /&gt;
                exit(1);&lt;br /&gt;
        if( (fdo = open(&amp;quot;outputfile&amp;quot;,O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)&lt;br /&gt;
                exit(1);&lt;br /&gt;
        if(read(fdi,&amp;amp;buf,1) == -1)&lt;br /&gt;
                printf(&amp;quot;Error = %s\n&amp;quot;,strerror(errno));&lt;br /&gt;
        while(1)&lt;br /&gt;
        {&lt;br /&gt;
                if(buf == 0)&lt;br /&gt;
                        lseek(fdo,1,SEEK_CUR);&lt;br /&gt;
                else {&lt;br /&gt;
                        if(write(fdo,&amp;amp;buf,1) == -1)&lt;br /&gt;
                                printf(&amp;quot;Error = %s\n&amp;quot;,strerror(errno));&lt;br /&gt;
                }&lt;br /&gt;
                ret = read(fdi,&amp;amp;buf,1);&lt;br /&gt;
                if(ret == -1)&lt;br /&gt;
                        printf(&amp;quot;Error = %s\n&amp;quot;,strerror(errno));&lt;br /&gt;
                else if(ret == 0) //indicates EOF&lt;br /&gt;
                        break;&lt;br /&gt;
        }&lt;br /&gt;
        close(fdi);&lt;br /&gt;
        close(fdo);&lt;br /&gt;
        return 0;&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_7_2011&amp;diff=14915</id>
		<title>COMP 3000 Lab 7 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_7_2011&amp;diff=14915"/>
		<updated>2011-11-21T16:13:00Z</updated>

		<summary type="html">&lt;p&gt;Saran: /* Part B */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by Sunday, November 27th at 11:30 PM.  &lt;br /&gt;
* Please answer using a single text file (with a .txt extension). Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.) &#039;&#039;&#039;Anything other than a single text file will receive -1 points&#039;&#039;&#039;&lt;br /&gt;
* Show all your work.  If you find an answer by browsing code, explain your search path (e.g., I searched for X, which led me to source file Y, where I found function Z.)   Also, list any websites or individuals you consult.&lt;br /&gt;
* Do all of the following on a Linux system.  Ubuntu Linux is guaranteed to work, but other Linux systems should work.  Note that *BSD systems, including MacOS X, will produce different answers for several of the following questions.&lt;br /&gt;
* Note that the tools from Lab 2 in particular may be of use here...&lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
# [3] Run &amp;lt;tt&amp;gt;ifconfig&amp;lt;/tt&amp;gt;.  For all configured interfaces except loopback, there should be an &amp;quot;inet&amp;quot; address, a &amp;quot;HWaddr&amp;quot; (hardware) address, and perhaps even an &amp;quot;inet6&amp;quot; address.  Each of these addresses identifies the computer within a certain context.  In what context are each of these addresses used?  Specifically, for what protocol is the address used, and what other computers can access this one using this address?  (Ignore NAT issues.)&lt;br /&gt;
# [2] What does MTU refer to in the output of &amp;lt;tt&amp;gt;ifconfig&amp;lt;/tt&amp;gt;?  Specifically, what does the acronym mean, and what does this value determine?&lt;br /&gt;
# How could you use netcat (&amp;lt;tt&amp;gt;nc&amp;lt;/tt&amp;gt;) to listen on port 9200 and write received data to the file /tmp/foo?&lt;br /&gt;
# What does &amp;lt;tt&amp;gt;tracepath slashdot.org&amp;lt;/tt&amp;gt; return?&lt;br /&gt;
# How can you get &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt; to return a list of all current TCP connections and open ports, listed using numeric IP addresses (rather than DNS hostnames)?&lt;br /&gt;
# How could I use &amp;lt;tt&amp;gt;lsof&amp;lt;/tt&amp;gt; to find all of the processes accessing the directory &amp;lt;tt&amp;gt;/home&amp;lt;/tt&amp;gt;?&lt;br /&gt;
# [2] In &amp;lt;tt&amp;gt;/proc/&amp;lt;PID&amp;gt;/maps&amp;lt;/tt&amp;gt;, each entry has four permission bits: r,w,x, &amp;amp; p.  What do each of them indicate?&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# [2] Why does &amp;lt;tt&amp;gt;tracepath&amp;lt;/tt&amp;gt; return &amp;quot;no reply&amp;quot; sometimes?&lt;br /&gt;
# [3] &amp;lt;tt&amp;gt;nc&amp;lt;/tt&amp;gt; uses very few system calls (three on Ubuntu 11.10) when actually receiving data on the network.  (The other system calls are for setup and for ending the connection.)  What are those few system calls, and what do each of them do?&lt;br /&gt;
# [2] What file does &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt; access to find out about current TCP connections on Linux?  And, what is the difference between this file and the output of &amp;lt;tt&amp;gt;netstat&amp;lt;/tt&amp;gt;, in syntax and semantics?  Be specific.&lt;br /&gt;
# [2] What does &amp;lt;tt&amp;gt;lsof&amp;lt;/tt&amp;gt; sometimes get a &amp;quot;permission denied&amp;quot; error when run as a regular user?  Specifically, what files is it trying to access, and why are these protected?&lt;br /&gt;
# [2] What kernel data structure stores the permissions listed in &amp;lt;tt&amp;gt;/proc/&amp;lt;PID&amp;gt;/maps&amp;lt;/tt&amp;gt;?&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_5_2011&amp;diff=13057</id>
		<title>COMP 3000 Lab 5 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_5_2011&amp;diff=13057"/>
		<updated>2011-11-07T21:31:20Z</updated>

		<summary type="html">&lt;p&gt;Saran: /* Part A */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by Sunday, November 6th at 11:30 PM.  &lt;br /&gt;
* Please answer using a single text file (with a .txt extension). Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.) &#039;&#039;&#039;Anything other than a single text file will receive -1 points&#039;&#039;&#039;&lt;br /&gt;
* Show all your work.  Short answers are not sufficient; you should list any websites or individuals you consult and should document any experiments you conducted.  For any question that you could just answer without any external reference or experiment, write &amp;quot;(no work required)&amp;quot; after your answer.&lt;br /&gt;
* All submitted code and commands should compile and run. Partial code fragments or explanations may not be given credit.  While code may use standard C and UNIX/Linux libraries, no code should rely on external binaries.&lt;br /&gt;
* &#039;&#039;&#039;The exercises in this lab require root access on a Linux system.&#039;&#039;&#039;  Note that an Ubuntu Live CD should be a sufficiently powerful environment if you do not have an already installed system.  Just boot the CD in the VM and select &amp;quot;Try Ubuntu.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
Be sure to do the following questions in order.&lt;br /&gt;
&lt;br /&gt;
# Run the command &amp;lt;tt&amp;gt;truncate -s 2G foo&amp;lt;/tt&amp;gt;.  What is the logical size of foo, and how much space does it consume on disk?&lt;br /&gt;
# Run &amp;lt;tt&amp;gt;mkfs.ext4 foo&amp;lt;/tt&amp;gt;.  (Say &amp;quot;yes&amp;quot; to operating on a regular file.)  What is the logical size of foo now, and how much space does it now consume on disk?&lt;br /&gt;
# Run &amp;lt;tt&amp;gt;mount -o loop foo /mnt&amp;lt;/tt&amp;gt;.  What does this command do?&lt;br /&gt;
# Run &amp;lt;tt&amp;gt;cp /bin/ls /mnt&amp;lt;/tt&amp;gt;.  What does this command do?&lt;br /&gt;
# Run &amp;lt;tt&amp;gt;umount /mnt&amp;lt;/tt&amp;gt;.  Can you still access /mnt/ls?  Why or why not?&lt;br /&gt;
# Run &amp;lt;tt&amp;gt;dd if=/dev/zero of=foo conv=notrunc count=10 bs=512&amp;lt;/tt&amp;gt;.  What does this command do?&lt;br /&gt;
# Run &amp;lt;tt&amp;gt;mount -o loop foo /mnt&amp;lt;/tt&amp;gt; again.  Why did the command fail?&lt;br /&gt;
# What command can you run to make foo mountable again?&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# Create a simple C program to generate a file consisting of the word &amp;quot;Hello&amp;quot;, one million bytes of blank space (null bytes), and then &amp;quot;world\n&amp;quot;, but that consumes only a small number of blocks on disk while having a logical size of 1,000,011 bytes.&lt;br /&gt;
# Create a C program &amp;quot;mycopy.c&amp;quot; that takes two filenames A and B as command line arguments.  This program should copy the contents of A to B, making B a byte-for-byte copy of A.  Zero bytes in A should consume little to no space in B.  (Hint: You&#039;ll have to use the sparse file creation technique of the previous question here.)&lt;br /&gt;
# Describe a simple way an attacker could get root access on a system if loopback mounts were permitted for non-privileged users.&lt;br /&gt;
&lt;br /&gt;
=Answers=&lt;br /&gt;
==Part A==&lt;br /&gt;
# Logical size is 2G. Physical size is 0.&lt;br /&gt;
# Logical size is 2G. Physical size is 65M.&lt;br /&gt;
# It links foo to an unused loop device and mounts it in /mnt.&lt;br /&gt;
# It copies the file located at /bin/ls to /mnt, indirectly writing it to foo.&lt;br /&gt;
# After unmounting, files in that file system cannot be accessed, because /mnt is not linked to foo (where /bin/ls is actually stored as a file) anymore. &lt;br /&gt;
# This command copies 10 blocks of 512 bytes each from /dev/zero to foo. &lt;br /&gt;
# Because writing 5120 bytes of zeros to the filesystem at foo corrupted it, destroying the superblock and invalidating some checksums.&lt;br /&gt;
# fsck.ext4 foo&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# 1.c&lt;br /&gt;
# 2.c&lt;br /&gt;
# An attacker could open foo, modify an [http://lxr.linux.no/linux+v3.1/include/linux/fs.h#L748 inode] to add set setuid and setgid bits and modify the owner and group of the file to 0,0 ( i.e root,root). She would then mount foo and run this file - which would run as root - to gain root privileges.&lt;br /&gt;
&lt;br /&gt;
 /* 1.c */&lt;br /&gt;
 #include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
        int fd = open(&amp;quot;helloemptyworld&amp;quot;,O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);&lt;br /&gt;
        if(fd == -1)&lt;br /&gt;
                exit(1);&lt;br /&gt;
        write(fd,&amp;quot;Hello&amp;quot;,5);&lt;br /&gt;
        lseek(fd,1000000,SEEK_CUR);&lt;br /&gt;
        write(fd,&amp;quot;World\n&amp;quot;,6);&lt;br /&gt;
        close(fd);&lt;br /&gt;
        return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* 2.c */&lt;br /&gt;
 #include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;errno.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;string.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
        int fdi,fdo,ret;&lt;br /&gt;
        char buf;&lt;br /&gt;
        if( (fdi = open(&amp;quot;inputfile&amp;quot;,O_RDONLY)) == -1)&lt;br /&gt;
                exit(1);&lt;br /&gt;
        if( (fdo = open(&amp;quot;outputfile&amp;quot;,O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)&lt;br /&gt;
                exit(1);&lt;br /&gt;
        if(read(fdi,&amp;amp;buf,1) == -1)&lt;br /&gt;
                printf(&amp;quot;Error = %s\n&amp;quot;,strerror(errno));&lt;br /&gt;
        while(1)&lt;br /&gt;
        {&lt;br /&gt;
                if(buf == 0)&lt;br /&gt;
                        lseek(fdo,1,SEEK_CUR);&lt;br /&gt;
                else {&lt;br /&gt;
                        if(write(fdo,&amp;amp;buf,1) == -1)&lt;br /&gt;
                                printf(&amp;quot;Error = %s\n&amp;quot;,strerror(errno));&lt;br /&gt;
                }&lt;br /&gt;
                ret = read(fdi,&amp;amp;buf,1);&lt;br /&gt;
                if(ret == -1)&lt;br /&gt;
                        printf(&amp;quot;Error = %s\n&amp;quot;,strerror(errno));&lt;br /&gt;
                else if(ret == 0) //indicates EOF&lt;br /&gt;
                        break;&lt;br /&gt;
        }&lt;br /&gt;
        close(fdi);&lt;br /&gt;
        close(fdo);&lt;br /&gt;
        return 0;&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_5_2011&amp;diff=13056</id>
		<title>COMP 3000 Lab 5 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_5_2011&amp;diff=13056"/>
		<updated>2011-11-07T21:29:26Z</updated>

		<summary type="html">&lt;p&gt;Saran: /* Part B */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by Sunday, November 6th at 11:30 PM.  &lt;br /&gt;
* Please answer using a single text file (with a .txt extension). Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.) &#039;&#039;&#039;Anything other than a single text file will receive -1 points&#039;&#039;&#039;&lt;br /&gt;
* Show all your work.  Short answers are not sufficient; you should list any websites or individuals you consult and should document any experiments you conducted.  For any question that you could just answer without any external reference or experiment, write &amp;quot;(no work required)&amp;quot; after your answer.&lt;br /&gt;
* All submitted code and commands should compile and run. Partial code fragments or explanations may not be given credit.  While code may use standard C and UNIX/Linux libraries, no code should rely on external binaries.&lt;br /&gt;
* &#039;&#039;&#039;The exercises in this lab require root access on a Linux system.&#039;&#039;&#039;  Note that an Ubuntu Live CD should be a sufficiently powerful environment if you do not have an already installed system.  Just boot the CD in the VM and select &amp;quot;Try Ubuntu.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
Be sure to do the following questions in order.&lt;br /&gt;
&lt;br /&gt;
# Run the command &amp;lt;tt&amp;gt;truncate -s 2G foo&amp;lt;/tt&amp;gt;.  What is the logical size of foo, and how much space does it consume on disk?&lt;br /&gt;
# Run &amp;lt;tt&amp;gt;mkfs.ext4 foo&amp;lt;/tt&amp;gt;.  (Say &amp;quot;yes&amp;quot; to operating on a regular file.)  What is the logical size of foo now, and how much space does it now consume on disk?&lt;br /&gt;
# Run &amp;lt;tt&amp;gt;mount -o loop foo /mnt&amp;lt;/tt&amp;gt;.  What does this command do?&lt;br /&gt;
# Run &amp;lt;tt&amp;gt;cp /bin/ls /mnt&amp;lt;/tt&amp;gt;.  What does this command do?&lt;br /&gt;
# Run &amp;lt;tt&amp;gt;umount /mnt&amp;lt;/tt&amp;gt;.  Can you still access /mnt/ls?  Why or why not?&lt;br /&gt;
# Run &amp;lt;tt&amp;gt;dd if=/dev/zero of=foo conv=notrunc count=10 bs=512&amp;lt;/tt&amp;gt;.  What does this command do?&lt;br /&gt;
# Run &amp;lt;tt&amp;gt;mount -o loop foo /mnt&amp;lt;/tt&amp;gt; again.  Why did the command fail?&lt;br /&gt;
# What command can you run to make foo mountable again?&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# Create a simple C program to generate a file consisting of the word &amp;quot;Hello&amp;quot;, one million bytes of blank space (null bytes), and then &amp;quot;world\n&amp;quot;, but that consumes only a small number of blocks on disk while having a logical size of 1,000,011 bytes.&lt;br /&gt;
# Create a C program &amp;quot;mycopy.c&amp;quot; that takes two filenames A and B as command line arguments.  This program should copy the contents of A to B, making B a byte-for-byte copy of A.  Zero bytes in A should consume little to no space in B.  (Hint: You&#039;ll have to use the sparse file creation technique of the previous question here.)&lt;br /&gt;
# Describe a simple way an attacker could get root access on a system if loopback mounts were permitted for non-privileged users.&lt;br /&gt;
&lt;br /&gt;
=Answers=&lt;br /&gt;
==Part A==&lt;br /&gt;
# Logical size is 2G. Physical size is 0.&lt;br /&gt;
# Logical size is 2G. Physical size is 65M.&lt;br /&gt;
# It links foo to an unused loop device and mounts it in /mnt.&lt;br /&gt;
# It copies the file located at /bin/ls to /mnt, indirectly writing it to foo.&lt;br /&gt;
# This command copies 10 blocks of 512 bytes each from /dev/zero to foo. &lt;br /&gt;
# Because writing 5120 bytes of zeros to the filesystem at foo corrupted it, destroying the superblock and invalidating some checksums.&lt;br /&gt;
# fsck.ext4 foo&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# 1.c&lt;br /&gt;
# 2.c&lt;br /&gt;
# An attacker could open foo, modify an [http://lxr.linux.no/linux+v3.1/include/linux/fs.h#L748 inode] to add set setuid and setgid bits and modify the owner and group of the file to 0,0 ( i.e root,root). She would then mount foo and run this file - which would run as root - to gain root privileges.&lt;br /&gt;
&lt;br /&gt;
 /* 1.c */&lt;br /&gt;
 #include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
        int fd = open(&amp;quot;helloemptyworld&amp;quot;,O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);&lt;br /&gt;
        if(fd == -1)&lt;br /&gt;
                exit(1);&lt;br /&gt;
        write(fd,&amp;quot;Hello&amp;quot;,5);&lt;br /&gt;
        lseek(fd,1000000,SEEK_CUR);&lt;br /&gt;
        write(fd,&amp;quot;World\n&amp;quot;,6);&lt;br /&gt;
        close(fd);&lt;br /&gt;
        return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* 2.c */&lt;br /&gt;
 #include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;errno.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;string.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
        int fdi,fdo,ret;&lt;br /&gt;
        char buf;&lt;br /&gt;
        if( (fdi = open(&amp;quot;inputfile&amp;quot;,O_RDONLY)) == -1)&lt;br /&gt;
                exit(1);&lt;br /&gt;
        if( (fdo = open(&amp;quot;outputfile&amp;quot;,O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)&lt;br /&gt;
                exit(1);&lt;br /&gt;
        if(read(fdi,&amp;amp;buf,1) == -1)&lt;br /&gt;
                printf(&amp;quot;Error = %s\n&amp;quot;,strerror(errno));&lt;br /&gt;
        while(1)&lt;br /&gt;
        {&lt;br /&gt;
                if(buf == 0)&lt;br /&gt;
                        lseek(fdo,1,SEEK_CUR);&lt;br /&gt;
                else {&lt;br /&gt;
                        if(write(fdo,&amp;amp;buf,1) == -1)&lt;br /&gt;
                                printf(&amp;quot;Error = %s\n&amp;quot;,strerror(errno));&lt;br /&gt;
                }&lt;br /&gt;
                ret = read(fdi,&amp;amp;buf,1);&lt;br /&gt;
                if(ret == -1)&lt;br /&gt;
                        printf(&amp;quot;Error = %s\n&amp;quot;,strerror(errno));&lt;br /&gt;
                else if(ret == 0) //indicates EOF&lt;br /&gt;
                        break;&lt;br /&gt;
        }&lt;br /&gt;
        close(fdi);&lt;br /&gt;
        close(fdo);&lt;br /&gt;
        return 0;&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_5_2011&amp;diff=13055</id>
		<title>COMP 3000 Lab 5 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_5_2011&amp;diff=13055"/>
		<updated>2011-11-07T21:28:55Z</updated>

		<summary type="html">&lt;p&gt;Saran: /* Part B */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by Sunday, November 6th at 11:30 PM.  &lt;br /&gt;
* Please answer using a single text file (with a .txt extension). Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.) &#039;&#039;&#039;Anything other than a single text file will receive -1 points&#039;&#039;&#039;&lt;br /&gt;
* Show all your work.  Short answers are not sufficient; you should list any websites or individuals you consult and should document any experiments you conducted.  For any question that you could just answer without any external reference or experiment, write &amp;quot;(no work required)&amp;quot; after your answer.&lt;br /&gt;
* All submitted code and commands should compile and run. Partial code fragments or explanations may not be given credit.  While code may use standard C and UNIX/Linux libraries, no code should rely on external binaries.&lt;br /&gt;
* &#039;&#039;&#039;The exercises in this lab require root access on a Linux system.&#039;&#039;&#039;  Note that an Ubuntu Live CD should be a sufficiently powerful environment if you do not have an already installed system.  Just boot the CD in the VM and select &amp;quot;Try Ubuntu.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
Be sure to do the following questions in order.&lt;br /&gt;
&lt;br /&gt;
# Run the command &amp;lt;tt&amp;gt;truncate -s 2G foo&amp;lt;/tt&amp;gt;.  What is the logical size of foo, and how much space does it consume on disk?&lt;br /&gt;
# Run &amp;lt;tt&amp;gt;mkfs.ext4 foo&amp;lt;/tt&amp;gt;.  (Say &amp;quot;yes&amp;quot; to operating on a regular file.)  What is the logical size of foo now, and how much space does it now consume on disk?&lt;br /&gt;
# Run &amp;lt;tt&amp;gt;mount -o loop foo /mnt&amp;lt;/tt&amp;gt;.  What does this command do?&lt;br /&gt;
# Run &amp;lt;tt&amp;gt;cp /bin/ls /mnt&amp;lt;/tt&amp;gt;.  What does this command do?&lt;br /&gt;
# Run &amp;lt;tt&amp;gt;umount /mnt&amp;lt;/tt&amp;gt;.  Can you still access /mnt/ls?  Why or why not?&lt;br /&gt;
# Run &amp;lt;tt&amp;gt;dd if=/dev/zero of=foo conv=notrunc count=10 bs=512&amp;lt;/tt&amp;gt;.  What does this command do?&lt;br /&gt;
# Run &amp;lt;tt&amp;gt;mount -o loop foo /mnt&amp;lt;/tt&amp;gt; again.  Why did the command fail?&lt;br /&gt;
# What command can you run to make foo mountable again?&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# Create a simple C program to generate a file consisting of the word &amp;quot;Hello&amp;quot;, one million bytes of blank space (null bytes), and then &amp;quot;world\n&amp;quot;, but that consumes only a small number of blocks on disk while having a logical size of 1,000,011 bytes.&lt;br /&gt;
# Create a C program &amp;quot;mycopy.c&amp;quot; that takes two filenames A and B as command line arguments.  This program should copy the contents of A to B, making B a byte-for-byte copy of A.  Zero bytes in A should consume little to no space in B.  (Hint: You&#039;ll have to use the sparse file creation technique of the previous question here.)&lt;br /&gt;
# Describe a simple way an attacker could get root access on a system if loopback mounts were permitted for non-privileged users.&lt;br /&gt;
&lt;br /&gt;
=Answers=&lt;br /&gt;
==Part A==&lt;br /&gt;
# Logical size is 2G. Physical size is 0.&lt;br /&gt;
# Logical size is 2G. Physical size is 65M.&lt;br /&gt;
# It links foo to an unused loop device and mounts it in /mnt.&lt;br /&gt;
# It copies the file located at /bin/ls to /mnt, indirectly writing it to foo.&lt;br /&gt;
# This command copies 10 blocks of 512 bytes each from /dev/zero to foo. &lt;br /&gt;
# Because writing 5120 bytes of zeros to the filesystem at foo corrupted it, destroying the superblock and invalidating some checksums.&lt;br /&gt;
# fsck.ext4 foo&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# 1.c&lt;br /&gt;
# 2.c&lt;br /&gt;
# An attacker could open foo, modify an [http://lxr.linux.no/linux+v3.1/include/linux/fs.h#L748 inode] to add set setuid and setgid bits and modify the owner and group of the file to 0,0 ( i.e root,root). She would then mount foo and run this file - which would run as root - to gain root privileges.&lt;br /&gt;
&lt;br /&gt;
 /* 1.c */&lt;br /&gt;
 #include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
        int fd = open(&amp;quot;helloemptyworld&amp;quot;,O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);&lt;br /&gt;
        if(fd == -1)&lt;br /&gt;
                exit(1);&lt;br /&gt;
        write(fd,&amp;quot;Hello&amp;quot;,5);&lt;br /&gt;
        lseek(fd,1000000,SEEK_CUR);&lt;br /&gt;
        write(fd,&amp;quot;World\n&amp;quot;,6);&lt;br /&gt;
        close(fd);&lt;br /&gt;
        return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* 2.c */&lt;br /&gt;
 #include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;errno.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;string.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
        int fdi,fdo,ret;&lt;br /&gt;
        char buf;&lt;br /&gt;
   if( (fdi = open(&amp;quot;inputfile&amp;quot;,O_RDONLY)) == -1)&lt;br /&gt;
                exit(1);&lt;br /&gt;
        if( (fdo = open(&amp;quot;outputfile&amp;quot;,O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)&lt;br /&gt;
                exit(1);&lt;br /&gt;
        if(read(fdi,&amp;amp;buf,1) == -1)&lt;br /&gt;
                printf(&amp;quot;Error = %s\n&amp;quot;,strerror(errno));&lt;br /&gt;
        while(1)&lt;br /&gt;
        {&lt;br /&gt;
                if(buf == 0)&lt;br /&gt;
                        lseek(fdo,1,SEEK_CUR);&lt;br /&gt;
                else {&lt;br /&gt;
                        if(write(fdo,&amp;amp;buf,1) == -1)&lt;br /&gt;
                                printf(&amp;quot;Error = %s\n&amp;quot;,strerror(errno));&lt;br /&gt;
                }&lt;br /&gt;
                ret = read(fdi,&amp;amp;buf,1);&lt;br /&gt;
                if(ret == -1)&lt;br /&gt;
                        printf(&amp;quot;Error = %s\n&amp;quot;,strerror(errno));&lt;br /&gt;
                else if(ret == 0) //indicated EOF&lt;br /&gt;
                        break;&lt;br /&gt;
        }&lt;br /&gt;
        close(fdi);&lt;br /&gt;
        close(fdo);&lt;br /&gt;
        return 0;&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_5_2011&amp;diff=13054</id>
		<title>COMP 3000 Lab 5 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_5_2011&amp;diff=13054"/>
		<updated>2011-11-07T21:27:48Z</updated>

		<summary type="html">&lt;p&gt;Saran: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by Sunday, November 6th at 11:30 PM.  &lt;br /&gt;
* Please answer using a single text file (with a .txt extension). Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.) &#039;&#039;&#039;Anything other than a single text file will receive -1 points&#039;&#039;&#039;&lt;br /&gt;
* Show all your work.  Short answers are not sufficient; you should list any websites or individuals you consult and should document any experiments you conducted.  For any question that you could just answer without any external reference or experiment, write &amp;quot;(no work required)&amp;quot; after your answer.&lt;br /&gt;
* All submitted code and commands should compile and run. Partial code fragments or explanations may not be given credit.  While code may use standard C and UNIX/Linux libraries, no code should rely on external binaries.&lt;br /&gt;
* &#039;&#039;&#039;The exercises in this lab require root access on a Linux system.&#039;&#039;&#039;  Note that an Ubuntu Live CD should be a sufficiently powerful environment if you do not have an already installed system.  Just boot the CD in the VM and select &amp;quot;Try Ubuntu.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
Be sure to do the following questions in order.&lt;br /&gt;
&lt;br /&gt;
# Run the command &amp;lt;tt&amp;gt;truncate -s 2G foo&amp;lt;/tt&amp;gt;.  What is the logical size of foo, and how much space does it consume on disk?&lt;br /&gt;
# Run &amp;lt;tt&amp;gt;mkfs.ext4 foo&amp;lt;/tt&amp;gt;.  (Say &amp;quot;yes&amp;quot; to operating on a regular file.)  What is the logical size of foo now, and how much space does it now consume on disk?&lt;br /&gt;
# Run &amp;lt;tt&amp;gt;mount -o loop foo /mnt&amp;lt;/tt&amp;gt;.  What does this command do?&lt;br /&gt;
# Run &amp;lt;tt&amp;gt;cp /bin/ls /mnt&amp;lt;/tt&amp;gt;.  What does this command do?&lt;br /&gt;
# Run &amp;lt;tt&amp;gt;umount /mnt&amp;lt;/tt&amp;gt;.  Can you still access /mnt/ls?  Why or why not?&lt;br /&gt;
# Run &amp;lt;tt&amp;gt;dd if=/dev/zero of=foo conv=notrunc count=10 bs=512&amp;lt;/tt&amp;gt;.  What does this command do?&lt;br /&gt;
# Run &amp;lt;tt&amp;gt;mount -o loop foo /mnt&amp;lt;/tt&amp;gt; again.  Why did the command fail?&lt;br /&gt;
# What command can you run to make foo mountable again?&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# Create a simple C program to generate a file consisting of the word &amp;quot;Hello&amp;quot;, one million bytes of blank space (null bytes), and then &amp;quot;world\n&amp;quot;, but that consumes only a small number of blocks on disk while having a logical size of 1,000,011 bytes.&lt;br /&gt;
# Create a C program &amp;quot;mycopy.c&amp;quot; that takes two filenames A and B as command line arguments.  This program should copy the contents of A to B, making B a byte-for-byte copy of A.  Zero bytes in A should consume little to no space in B.  (Hint: You&#039;ll have to use the sparse file creation technique of the previous question here.)&lt;br /&gt;
# Describe a simple way an attacker could get root access on a system if loopback mounts were permitted for non-privileged users.&lt;br /&gt;
&lt;br /&gt;
=Answers=&lt;br /&gt;
==Part A==&lt;br /&gt;
# Logical size is 2G. Physical size is 0.&lt;br /&gt;
# Logical size is 2G. Physical size is 65M.&lt;br /&gt;
# It links foo to an unused loop device and mounts it in /mnt.&lt;br /&gt;
# It copies the file located at /bin/ls to /mnt, indirectly writing it to foo.&lt;br /&gt;
# This command copies 10 blocks of 512 bytes each from /dev/zero to foo. &lt;br /&gt;
# Because writing 5120 bytes of zeros to the filesystem at foo corrupted it, destroying the superblock and invalidating some checksums.&lt;br /&gt;
# fsck.ext4 foo&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# &lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
        int fd = open(&amp;quot;helloemptyworld&amp;quot;,O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);&lt;br /&gt;
        if(fd == -1)&lt;br /&gt;
                exit(1);&lt;br /&gt;
        write(fd,&amp;quot;Hello&amp;quot;,5);&lt;br /&gt;
        lseek(fd,1000000,SEEK_CUR);&lt;br /&gt;
        write(fd,&amp;quot;World\n&amp;quot;,6);&lt;br /&gt;
        close(fd);&lt;br /&gt;
        return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
# &lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;errno.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;string.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
        int fdi,fdo,ret;&lt;br /&gt;
        char buf;&lt;br /&gt;
   if( (fdi = open(&amp;quot;inputfile&amp;quot;,O_RDONLY)) == -1)&lt;br /&gt;
                exit(1);&lt;br /&gt;
        if( (fdo = open(&amp;quot;outputfile&amp;quot;,O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)&lt;br /&gt;
                exit(1);&lt;br /&gt;
        if(read(fdi,&amp;amp;buf,1) == -1)&lt;br /&gt;
                printf(&amp;quot;Error = %s\n&amp;quot;,strerror(errno));&lt;br /&gt;
        while(1)&lt;br /&gt;
        {&lt;br /&gt;
                if(buf == 0)&lt;br /&gt;
                        lseek(fdo,1,SEEK_CUR);&lt;br /&gt;
                else {&lt;br /&gt;
                        if(write(fdo,&amp;amp;buf,1) == -1)&lt;br /&gt;
                                printf(&amp;quot;Error = %s\n&amp;quot;,strerror(errno));&lt;br /&gt;
                }&lt;br /&gt;
                ret = read(fdi,&amp;amp;buf,1);&lt;br /&gt;
                if(ret == -1)&lt;br /&gt;
                        printf(&amp;quot;Error = %s\n&amp;quot;,strerror(errno));&lt;br /&gt;
                else if(ret == 0) //indicated EOF&lt;br /&gt;
                        break;&lt;br /&gt;
        }&lt;br /&gt;
        close(fdi);&lt;br /&gt;
        close(fdo);&lt;br /&gt;
        return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
# An attacker could open foo, modify an [http://lxr.linux.no/linux+v3.1/include/linux/fs.h#L748 inode] to add set setuid and setgid bits and modify the owner and group of the file to 0,0 ( i.e root,root). She would then mount foo and run this file - which would run as root - to gain root privileges.&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_5_2011&amp;diff=13053</id>
		<title>COMP 3000 Lab 5 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_5_2011&amp;diff=13053"/>
		<updated>2011-11-07T21:23:41Z</updated>

		<summary type="html">&lt;p&gt;Saran: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by Sunday, November 6th at 11:30 PM.  &lt;br /&gt;
* Please answer using a single text file (with a .txt extension). Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.) &#039;&#039;&#039;Anything other than a single text file will receive -1 points&#039;&#039;&#039;&lt;br /&gt;
* Show all your work.  Short answers are not sufficient; you should list any websites or individuals you consult and should document any experiments you conducted.  For any question that you could just answer without any external reference or experiment, write &amp;quot;(no work required)&amp;quot; after your answer.&lt;br /&gt;
* All submitted code and commands should compile and run. Partial code fragments or explanations may not be given credit.  While code may use standard C and UNIX/Linux libraries, no code should rely on external binaries.&lt;br /&gt;
* &#039;&#039;&#039;The exercises in this lab require root access on a Linux system.&#039;&#039;&#039;  Note that an Ubuntu Live CD should be a sufficiently powerful environment if you do not have an already installed system.  Just boot the CD in the VM and select &amp;quot;Try Ubuntu.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
Be sure to do the following questions in order.&lt;br /&gt;
&lt;br /&gt;
# Run the command &amp;lt;tt&amp;gt;truncate -s 2G foo&amp;lt;/tt&amp;gt;.  What is the logical size of foo, and how much space does it consume on disk?&lt;br /&gt;
# Run &amp;lt;tt&amp;gt;mkfs.ext4 foo&amp;lt;/tt&amp;gt;.  (Say &amp;quot;yes&amp;quot; to operating on a regular file.)  What is the logical size of foo now, and how much space does it now consume on disk?&lt;br /&gt;
# Run &amp;lt;tt&amp;gt;mount -o loop foo /mnt&amp;lt;/tt&amp;gt;.  What does this command do?&lt;br /&gt;
# Run &amp;lt;tt&amp;gt;cp /bin/ls /mnt&amp;lt;/tt&amp;gt;.  What does this command do?&lt;br /&gt;
# Run &amp;lt;tt&amp;gt;umount /mnt&amp;lt;/tt&amp;gt;.  Can you still access /mnt/ls?  Why or why not?&lt;br /&gt;
# Run &amp;lt;tt&amp;gt;dd if=/dev/zero of=foo conv=notrunc count=10 bs=512&amp;lt;/tt&amp;gt;.  What does this command do?&lt;br /&gt;
# Run &amp;lt;tt&amp;gt;mount -o loop foo /mnt&amp;lt;/tt&amp;gt; again.  Why did the command fail?&lt;br /&gt;
# What command can you run to make foo mountable again?&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# Create a simple C program to generate a file consisting of the word &amp;quot;Hello&amp;quot;, one million bytes of blank space (null bytes), and then &amp;quot;world\n&amp;quot;, but that consumes only a small number of blocks on disk while having a logical size of 1,000,011 bytes.&lt;br /&gt;
# Create a C program &amp;quot;mycopy.c&amp;quot; that takes two filenames A and B as command line arguments.  This program should copy the contents of A to B, making B a byte-for-byte copy of A.  Zero bytes in A should consume little to no space in B.  (Hint: You&#039;ll have to use the sparse file creation technique of the previous question here.)&lt;br /&gt;
# Describe a simple way an attacker could get root access on a system if loopback mounts were permitted for non-privileged users.&lt;br /&gt;
&lt;br /&gt;
=Answers=&lt;br /&gt;
==Part A==&lt;br /&gt;
# Logical size is 2G. Physical size is 0.&lt;br /&gt;
# Logical size is 2G. Physical size is 65M.&lt;br /&gt;
# It links foo to an unused loop device and mounts it in /mnt.&lt;br /&gt;
# It copies the file located at /bin/ls to /mnt, indirectly writing it to foo.&lt;br /&gt;
# This command copies 10 blocks of 512 bytes each from /dev/zero to foo. &lt;br /&gt;
# Because writing 5120 bytes of zeros to the filesystem at foo corrupted it, destroying the superblock and invalidating some checksums.&lt;br /&gt;
# fsck.ext4 foo&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_5_2011&amp;diff=12888</id>
		<title>COMP 3000 Lab 5 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_5_2011&amp;diff=12888"/>
		<updated>2011-10-31T14:56:22Z</updated>

		<summary type="html">&lt;p&gt;Saran: /* Part B */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by Sunday, November 6th at 11:30 PM.  &lt;br /&gt;
* Please answer using a single text file (with a .txt extension). Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.) &#039;&#039;&#039;Anything other than a single text file will receive -1 points&#039;&#039;&#039;&lt;br /&gt;
* Show all your work.  Short answers are not sufficient; you should list any websites or individuals you consult and should document any experiments you conducted.  For any question that you could just answer without any external reference or experiment, write &amp;quot;(no work required)&amp;quot; after your answer.&lt;br /&gt;
* All submitted code and commands should compile and run. Partial code fragments or explanations may not be given credit.  While code may use standard C and UNIX/Linux libraries, no code should rely on external binaries.&lt;br /&gt;
* &#039;&#039;&#039;The exercises in this lab require root access on a Linux system.&#039;&#039;&#039;  Note that an Ubuntu Live CD should be a sufficiently powerful environment if you do not have an already installed system.  Just boot the CD in the VM and select &amp;quot;Try Ubuntu.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
Be sure to do the following questions in order.&lt;br /&gt;
&lt;br /&gt;
# Run the command &amp;lt;tt&amp;gt;truncate -s 2G foo&amp;lt;/tt&amp;gt;.  What is the logical size of foo, and how much space does it consume on disk?&lt;br /&gt;
# Run &amp;lt;tt&amp;gt;mkfs.ext4 foo&amp;lt;/tt&amp;gt;.  (Say &amp;quot;yes&amp;quot; to operating on a regular file.)  What is the logical size of foo now, and how much space does it now consume on disk?&lt;br /&gt;
# Run &amp;lt;tt&amp;gt;mount -o loop foo /mnt&amp;lt;/tt&amp;gt;.  What does this command do?&lt;br /&gt;
# Run &amp;lt;tt&amp;gt;cp /bin/ls /mnt&amp;lt;/tt&amp;gt;.  What does this command do?&lt;br /&gt;
# Run &amp;lt;tt&amp;gt;umount /mnt&amp;lt;/tt&amp;gt;.  Can you still access /mnt/ls?  Why or why not?&lt;br /&gt;
# Run &amp;lt;tt&amp;gt;dd if=/dev/zero of=foo conv=notrunc count=10 bs=512&amp;lt;/tt&amp;gt;.  What does this command do?&lt;br /&gt;
# Run &amp;lt;tt&amp;gt;mount -o loop foo /mnt&amp;lt;/tt&amp;gt; again.  Why did the command fail?&lt;br /&gt;
# What command can you run to make foo mountable again?&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# Create a simple C program to generate a file consisting of the word &amp;quot;Hello&amp;quot;, one million bytes of blank space, and then &amp;quot;world\n&amp;quot;, but that consumes only a bit more than 11 bytes in space on disk while having a logical size of 1,000,011 bytes.&lt;br /&gt;
# Create a C program &amp;quot;mycopy.c&amp;quot; that takes two filenames A and B as command line arguments.  This program should copy the contents of A to B, making B a byte-for-byte copy of A.  Zero bytes in A should consume little to no space in B.  (Hint: You&#039;ll have to use the sparse file creation technique of the previous question here.)&lt;br /&gt;
# Describe a simple way an attacker could get root access on a system if loopback mounts were permitted for non-privileged users.&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_5_2011&amp;diff=12887</id>
		<title>COMP 3000 Lab 5 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_5_2011&amp;diff=12887"/>
		<updated>2011-10-31T14:40:10Z</updated>

		<summary type="html">&lt;p&gt;Saran: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by Sunday, November 6th at 11:30 PM.  &lt;br /&gt;
* Please answer using a single text file (with a .txt extension). Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.) &#039;&#039;&#039;Anything other than a single text file will receive -1 points&#039;&#039;&#039;&lt;br /&gt;
* Show all your work.  Short answers are not sufficient; you should list any websites or individuals you consult and should document any experiments you conducted.  For any question that you could just answer without any external reference or experiment, write &amp;quot;(no work required)&amp;quot; after your answer.&lt;br /&gt;
* All submitted code and commands should compile and run. Partial code fragments or explanations may not be given credit.  While code may use standard C and UNIX/Linux libraries, no code should rely on external binaries.&lt;br /&gt;
* &#039;&#039;&#039;The exercises in this lab require root access on a Linux system.&#039;&#039;&#039;  Note that an Ubuntu Live CD should be a sufficiently powerful environment if you do not have an already installed system.  Just boot the CD in the VM and select &amp;quot;Try Ubuntu.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
Be sure to do the following questions in order.&lt;br /&gt;
&lt;br /&gt;
# Run the command &amp;lt;tt&amp;gt;truncate -s 2G foo&amp;lt;/tt&amp;gt;.  What is the logical size of foo, and how much space does it consume on disk?&lt;br /&gt;
# Run &amp;lt;tt&amp;gt;mkfs.ext4 foo&amp;lt;/tt&amp;gt;.  (Say &amp;quot;yes&amp;quot; to operating on a regular file.)  What is the logical size of foo now, and how much space does it now consume on disk?&lt;br /&gt;
# Run &amp;lt;tt&amp;gt;mount -o loop foo /mnt&amp;lt;/tt&amp;gt;.  What does this command do?&lt;br /&gt;
# Run &amp;lt;tt&amp;gt;cp /bin/ls /mnt&amp;lt;/tt&amp;gt;.  What does this command do?&lt;br /&gt;
# Run &amp;lt;tt&amp;gt;umount /mnt&amp;lt;/tt&amp;gt;.  Can you still access /mnt/ls?  Why or why not?&lt;br /&gt;
# Run &amp;lt;tt&amp;gt;dd if=/dev/zero of=foo conv=notrunc count=10 bs=512&amp;lt;/tt&amp;gt;.  What does this command do?&lt;br /&gt;
# Run &amp;lt;tt&amp;gt;mount -o loop foo /mnt&amp;lt;/tt&amp;gt; again.  Why did the command fail?&lt;br /&gt;
# What command can you run to make foo mountable again?&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
# Create a simple C program to generate a file consisting of the word &amp;quot;Hello&amp;quot;, one million bytes of blank space, and then &amp;quot;world\n&amp;quot;, but that consumes only a bit more than 11 bytes in space on disk while having a logical size of 1,000,011 bytes.&lt;br /&gt;
# Create a C program &amp;quot;mycopy.c&amp;quot; that takes two filenames A and B as command line arguments.  This program should copy the contents of A to B, making B a byte-for-byte copy of A.  Zero bytes in A should consume little to no space in B.  (Hint: You&#039;ll have to use the sparse file creation technique of the previous question here.)&lt;br /&gt;
# Why do loopback mounts require root access, when the filesystem image file can be created and manually manipulated without any privileges?&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_3_2011&amp;diff=12879</id>
		<title>COMP 3000 Lab 3 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_3_2011&amp;diff=12879"/>
		<updated>2011-10-31T01:05:16Z</updated>

		<summary type="html">&lt;p&gt;Saran: /* Part A */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by &#039;&#039;&#039;Monday, October 24th at 11:30 PM&#039;&#039;&#039; (note the extension).  &lt;br /&gt;
* Please answer using a single text file. Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.)&lt;br /&gt;
* Show all your work.  Short answers are not sufficient; you should list any websites or individuals you consult and should document any experiments you conducted.  For any question that you could just answer without any external reference or experiment, write &amp;quot;(no work required)&amp;quot; after your answer.&lt;br /&gt;
* All your codes should compile and run. Partial code fragments or explanations will not be sufficient. &lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Compile run-program.c with the command &#039;&#039;gcc -g run-program.c -o run-program&#039;&#039; and use it to answer the following questions.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: lower-alpha;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Is the line &amp;quot;Is this line printed?&amp;quot; printed when you execute &#039;&#039;&#039;./run-program /bin/ls -a /tmp&#039;&#039;&#039;?  Why? (1 mark) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Change the program to use execve instead of execvp. What is the difference between the two system calls? (1 mark) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Linux signals is a simple form of IPC that is used for a variety of purposes.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: lower-alpha;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; What signal is generated when a program attempts to dereference an invalid pointer?  Give a simple program that generates such a signal.(1 mark) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; How would you send a signal to a process to have it pause execution?  Resume? (1 mark) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;What command can lower the priority of an already running process? (1 mark) &lt;br /&gt;
&amp;lt;li&amp;gt;Create &#039;&#039;run-program-reverse.c&#039;&#039; that is the same as run-program.c except that it interprets its arguments in reverse order.  Hence, &#039;&#039;./run-program-reverse /tmp -a /bin/ls&#039;&#039; will do the same as &#039;&#039;./run-program /bin/ls -a /tmp&#039;&#039;. (2 marks) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Create &#039;&#039;sanitize-env.c&#039;&#039; that does the same as run-program.c except it exec&#039;s the program with all environment variables stripped except the PATH and TERM variables (assuming they are defined).  Be sure to document how you tested your program. (2 marks)&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li value=6&amp;gt; Create a simple program &#039;&#039;receive-usr1.c&#039;&#039; that uses sigaction in order to print &amp;quot;Ouch!&amp;quot; when you send the program the USR1 signal.  Note that your program should not consume much CPU time while waiting (i.e., do not poll for the signal).(2 marks)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Create a simple program &#039;&#039;nocrash.c&#039;&#039; that accesses an invalid pointer but then prints &amp;quot;Oops, my mistake&amp;quot; rather than crash.(2 marks) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Create a program &#039;&#039;run-program-dots.c&#039;&#039; that works the same as run-program.c, except that it prints one dot every second while a given program runs.  Note that it should stop printing dots once the exec&#039;d program terminates.  Your solution should use the fork(), sleep(), execve(), and sigaction() calls.(2 marks)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; What is the difference between the PR and NI columns in the top command?  Explain in the context of running a CPU-intensive program.  (Hint: you should try running a CPU intensive program or two and observe these columns...) (1 mark)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Program Listings==&lt;br /&gt;
&lt;br /&gt;
 /* run-program.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int main( int argc, char *argv[], char *envp[] ) {&lt;br /&gt;
 &lt;br /&gt;
    if( argc &amp;lt; 2 ) {&lt;br /&gt;
        printf( &amp;quot;Insufficient arguments.\n&amp;quot; );&lt;br /&gt;
        return -1;&lt;br /&gt;
    }&lt;br /&gt;
    execvp( argv[1], argv + 1 );&lt;br /&gt;
    printf( &amp;quot;Is this line printed?\n&amp;quot; );&lt;br /&gt;
    return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Answers=&lt;br /&gt;
All questions graded by Saran. Email him for any queries. &lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt; &amp;lt;li&amp;gt;&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: lower-alpha;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;quot;is this line printed?&amp;quot; is not printed because execvp/execve replaces the program binary with a different one.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; execve( argv[1], argv + 1 , envp); &amp;lt;br&amp;gt;&lt;br /&gt;
execve requires an environment argument which may be different from the existing one, where as execvp runs the program using the current environment. Another difference is that execvp searches the PATH environment variable for folders containing binary files, if it can&#039;t find the file in the current working directory. &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt; &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;ol style=&amp;quot;list-style-type: lower-alpha;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;SIGSEGV or SIGBUS is raised for invalid memory references. SIGBUS is low level device specific signal. The following program generates a SIGSEGV in my computer : int *a; *a = 10; &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; SIGSTOP (which cannot be caught) or SIGTSTP (can be caught) for pausing, and SIGCONT for continuing.&lt;br /&gt;
You can use &amp;quot;kill -SIGWHATEVER pid&amp;quot; from the terminal, or from code. For ex :&lt;br /&gt;
 pause-and-cont.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; renice &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; run-program-reverse.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; sanitize-env.c. Run it with /usr/bin/env &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
(Part B)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li value=6&amp;gt; Compile and run receive-usr1.c and issue &amp;quot;kill -SIGUSR1 pid&amp;quot; &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Use gdb to figure out what signal is thrown for invalid pointer accesses, then catch that. nocrash.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; run-program-dots.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;NI is the nice value of a process, which can be changed by the user. It ranges from -20 to 19 and is used as an input to the scheduler. Depending on all other processes and available resources, the scheduler decides the priority of a process and displays its calculated value in the PR column. &lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Answers : Program Listings== &lt;br /&gt;
&lt;br /&gt;
 /* pause-and-cont.c */&lt;br /&gt;
 &lt;br /&gt;
 #include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include&amp;lt;signal.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int main(int argc, char * argv[], char *envp[])&lt;br /&gt;
 {&lt;br /&gt;
   int child = fork();&lt;br /&gt;
   if(child == 0)&lt;br /&gt;
   {   &lt;br /&gt;
     execvp(&amp;quot;/usr/bin/yes&amp;quot;,argv);&lt;br /&gt;
   }   &lt;br /&gt;
   else&lt;br /&gt;
   {   &lt;br /&gt;
     kill(child,SIGSTOP);&lt;br /&gt;
     sleep(4);&lt;br /&gt;
     kill(child,SIGCONT);&lt;br /&gt;
     sleep(4);&lt;br /&gt;
     kill(child,SIGKILL);&lt;br /&gt;
   }   &lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* run-program-reverse.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;errno.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;string.h&amp;gt;  &lt;br /&gt;
 &lt;br /&gt;
 int main( int argc, char **argv, char *envp[] ) {&lt;br /&gt;
         char **newenv;&lt;br /&gt;
         int i = 0;&lt;br /&gt;
   if( argc &amp;lt; 2 ) {&lt;br /&gt;
       printf( &amp;quot;Insufficient arguments.\n&amp;quot; );&lt;br /&gt;
       return -1;&lt;br /&gt;
   }&lt;br /&gt;
         newenv = (char**)malloc(argc*sizeof(char**));&lt;br /&gt;
         for(i = 0; i &amp;lt; argc; i++)&lt;br /&gt;
         {&lt;br /&gt;
                 newenv[i] = (char*)malloc(sizeof(argv[argc-i-1]));&lt;br /&gt;
                 strcpy(newenv[i],argv[argc-i-1]);&lt;br /&gt;
         }&lt;br /&gt;
         newenv[argc-1] = NULL;&lt;br /&gt;
         if(execvp(newenv[0],newenv + 1) == -1)&lt;br /&gt;
                 printf(&amp;quot;error! = %s&amp;quot;,strerror(errno));&lt;br /&gt;
   &lt;br /&gt;
         printf( &amp;quot;Is this line printed?\n&amp;quot; );&lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* sanitize-env.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;  &lt;br /&gt;
 &lt;br /&gt;
 int main( int argc, char *argv[], char *envp[] ) {&lt;br /&gt;
         extern char **environ;&lt;br /&gt;
         int i = 0;   &lt;br /&gt;
 &lt;br /&gt;
         char *path = getenv(&amp;quot;PATH&amp;quot;);&lt;br /&gt;
         char *term = getenv(&amp;quot;TERM&amp;quot;);&lt;br /&gt;
         if(clearenv())&lt;br /&gt;
                 exit(-1);&lt;br /&gt;
         setenv(&amp;quot;PATH&amp;quot;,path,1);&lt;br /&gt;
         setenv(&amp;quot;TERM&amp;quot;,term,1);  &lt;br /&gt;
 &lt;br /&gt;
         //Check now.&lt;br /&gt;
         while(environ[i] != NULL) {&lt;br /&gt;
                 printf(&amp;quot;environ[%d] = %s\n&amp;quot;,i,environ[i++]); &lt;br /&gt;
         } &lt;br /&gt;
 &lt;br /&gt;
   if( argc &amp;lt; 2 ) {&lt;br /&gt;
       printf( &amp;quot;Insufficient arguments.\n&amp;quot; );&lt;br /&gt;
       return -1;&lt;br /&gt;
   }&lt;br /&gt;
   execvp(argv[1], argv + 1);&lt;br /&gt;
   printf( &amp;quot;Is this line printed?\n&amp;quot; );&lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* receive-usr1.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;signal.h&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
 int child;&lt;br /&gt;
 void sig_handler(int signum)&lt;br /&gt;
 {&lt;br /&gt;
          if(signum == SIGUSR1)&lt;br /&gt;
                 printf(&amp;quot;Ouch!.\n&amp;quot;);&lt;br /&gt;
    fflush(stdout);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   struct sigaction act;&lt;br /&gt;
         sigset_t set;&lt;br /&gt;
   &lt;br /&gt;
   act.sa_handler = sig_handler;&lt;br /&gt;
   act.sa_flags = 0;&lt;br /&gt;
   sigemptyset(&amp;amp;act.sa_mask); &lt;br /&gt;
 &lt;br /&gt;
   sigaction(SIGUSR1, &amp;amp;act, NULL);&lt;br /&gt;
   sigfillset(&amp;amp;set);//Block on all signals&lt;br /&gt;
   sigdelset(&amp;amp;set,SIGUSR1);//except this one&lt;br /&gt;
   sigsuspend(&amp;amp;set);//now wait.&lt;br /&gt;
   return 0;  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* nocrash.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;signal.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int child;&lt;br /&gt;
 void sig_handler(int signum)&lt;br /&gt;
 {&lt;br /&gt;
         if(signum == SIGSEGV)&lt;br /&gt;
                printf(&amp;quot;Oops, my mistake.\n&amp;quot;);&lt;br /&gt;
         fflush(stdout);&lt;br /&gt;
         exit(1);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
        int *a;&lt;br /&gt;
   struct sigaction act;&lt;br /&gt;
  &lt;br /&gt;
  act.sa_handler = sig_handler;&lt;br /&gt;
  act.sa_flags = 0;&lt;br /&gt;
  sigemptyset(&amp;amp;act.sa_mask);&lt;br /&gt;
 &lt;br /&gt;
  sigaction(SIGSEGV, &amp;amp;act, NULL);&lt;br /&gt;
  sleep(1);&lt;br /&gt;
  *a = 10; // Make a mistake. &lt;br /&gt;
  return 0;  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* run-program-dots.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;signal.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int child;&lt;br /&gt;
 void sigchild_handler(int signum)&lt;br /&gt;
 {&lt;br /&gt;
         exit(0); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   struct sigaction act;&lt;br /&gt;
  &lt;br /&gt;
  act.sa_handler = sigchild_handler;&lt;br /&gt;
  act.sa_flags = 0;&lt;br /&gt;
  sigemptyset(&amp;amp;act.sa_mask);&lt;br /&gt;
 &lt;br /&gt;
  child = fork();&lt;br /&gt;
  &lt;br /&gt;
  if(child == 0) {&lt;br /&gt;
         execve(&amp;quot;program&amp;quot;,NULL,NULL);&lt;br /&gt;
    return 0;&lt;br /&gt;
  }&lt;br /&gt;
  else {&lt;br /&gt;
    sigaction(SIGCHLD, &amp;amp;act, NULL); &lt;br /&gt;
         while(1)&lt;br /&gt;
         {&lt;br /&gt;
                printf(&amp;quot;.&amp;quot;);&lt;br /&gt;
                fflush(stdout);&lt;br /&gt;
                sleep(1);&lt;br /&gt;
         } &lt;br /&gt;
    return 0;  &lt;br /&gt;
  }   &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 where my program.c is &lt;br /&gt;
 #include&amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
         sleep(8);&lt;br /&gt;
         return 0;&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_3_2011&amp;diff=12877</id>
		<title>COMP 3000 Lab 3 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_3_2011&amp;diff=12877"/>
		<updated>2011-10-27T20:27:58Z</updated>

		<summary type="html">&lt;p&gt;Saran: /* Part A */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by &#039;&#039;&#039;Monday, October 24th at 11:30 PM&#039;&#039;&#039; (note the extension).  &lt;br /&gt;
* Please answer using a single text file. Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.)&lt;br /&gt;
* Show all your work.  Short answers are not sufficient; you should list any websites or individuals you consult and should document any experiments you conducted.  For any question that you could just answer without any external reference or experiment, write &amp;quot;(no work required)&amp;quot; after your answer.&lt;br /&gt;
* All your codes should compile and run. Partial code fragments or explanations will not be sufficient. &lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Compile run-program.c with the command &#039;&#039;gcc -g run-program.c -o run-program&#039;&#039; and use it to answer the following questions.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: lower-alpha;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Is the line &amp;quot;Is this line printed?&amp;quot; printed when you execute &#039;&#039;&#039;./run-program /bin/ls -a /tmp&#039;&#039;&#039;?  Why? (1 mark) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Change the program to use execve instead of execvp. What is the difference between the two system calls? (1 mark) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Linux signals is a simple form of IPC that is used for a variety of purposes.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: lower-alpha;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; What signal is generated when a program attempts to dereference an invalid pointer?  Give a simple program that generates such a signal.(1 mark) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; How would you send a signal to a process to have it pause execution?  Resume? (1 mark) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;What command can lower the priority of an already running process? (1 mark) &lt;br /&gt;
&amp;lt;li&amp;gt;Create &#039;&#039;run-program-reverse.c&#039;&#039; that is the same as run-program.c except that it interprets its arguments in reverse order.  Hence, &#039;&#039;./run-program-reverse /tmp -a /bin/ls&#039;&#039; will do the same as &#039;&#039;./run-program /bin/ls -a /tmp&#039;&#039;. (2 marks) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Create &#039;&#039;sanitize-env.c&#039;&#039; that does the same as run-program.c except it exec&#039;s the program with all environment variables stripped except the PATH and TERM variables (assuming they are defined).  Be sure to document how you tested your program. (2 marks)&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li value=6&amp;gt; Create a simple program &#039;&#039;receive-usr1.c&#039;&#039; that uses sigaction in order to print &amp;quot;Ouch!&amp;quot; when you send the program the USR1 signal.  Note that your program should not consume much CPU time while waiting (i.e., do not poll for the signal).(2 marks)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Create a simple program &#039;&#039;nocrash.c&#039;&#039; that accesses an invalid pointer but then prints &amp;quot;Oops, my mistake&amp;quot; rather than crash.(2 marks) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Create a program &#039;&#039;run-program-dots.c&#039;&#039; that works the same as run-program.c, except that it prints one dot every second while a given program runs.  Note that it should stop printing dots once the exec&#039;d program terminates.  Your solution should use the fork(), sleep(), execve(), and sigaction() calls.(2 marks)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; What is the difference between the PR and NI columns in the top command?  Explain in the context of running a CPU-intensive program.  (Hint: you should try running a CPU intensive program or two and observe these columns...) (1 mark)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Program Listings==&lt;br /&gt;
&lt;br /&gt;
 /* run-program.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int main( int argc, char *argv[], char *envp[] ) {&lt;br /&gt;
 &lt;br /&gt;
    if( argc &amp;lt; 2 ) {&lt;br /&gt;
        printf( &amp;quot;Insufficient arguments.\n&amp;quot; );&lt;br /&gt;
        return -1;&lt;br /&gt;
    }&lt;br /&gt;
    execvp( argv[1], argv + 1 );&lt;br /&gt;
    printf( &amp;quot;Is this line printed?\n&amp;quot; );&lt;br /&gt;
    return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Answers=&lt;br /&gt;
All questions graded by Saran. Email him for any queries. &lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt; &amp;lt;li&amp;gt;&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: lower-alpha;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;quot;is this line printed?&amp;quot; is not printed because execvp/execve replaces the program binary with a different one.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; execve( argv[1], argv + 1 , envp); &amp;lt;br&amp;gt;&lt;br /&gt;
execve requires an environment argument which may be different from the existing one, where as execvp runs the program using the current environment. Another difference is that execvp searches the PATH environment variable for folders containing binary files, if it can&#039;t find the file in the current working directory. &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt; &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;ol style=&amp;quot;list-style-type: lower-alpha;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;SIGSEGV or SIGBUS is raised for invalid memory references. SIGBUS is low level device specific signal. The following program generates a SIGSEGV in my computer : int *a; *a = 10; &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; SIGSTOP (which cannot be caught) or SIGTSTP (can be caught) for pausing, and SIGCONT for continuing.&lt;br /&gt;
You can use &amp;quot;kill -SIGWHATEVER pid&amp;quot; from the terminal, or from code. For ex :&lt;br /&gt;
 pause-and-cont.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; renice &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; run-program-reverse.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; sanitize-env.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
(Part B)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li value=6&amp;gt; Compile and run receive-usr1.c and issue &amp;quot;kill -SIGUSR1 pid&amp;quot; &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Use gdb to figure out what signal is thrown for invalid pointer accesses, then catch that. nocrash.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; run-program-dots.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;NI is the nice value of a process, which can be changed by the user. It ranges from -20 to 19 and is used as an input to the scheduler. Depending on all other processes and available resources, the scheduler decides the priority of a process and displays its calculated value in the PR column. &lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Answers : Program Listings== &lt;br /&gt;
&lt;br /&gt;
 /* pause-and-cont.c */&lt;br /&gt;
 &lt;br /&gt;
 #include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include&amp;lt;signal.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int main(int argc, char * argv[], char *envp[])&lt;br /&gt;
 {&lt;br /&gt;
   int child = fork();&lt;br /&gt;
   if(child == 0)&lt;br /&gt;
   {   &lt;br /&gt;
     execvp(&amp;quot;/usr/bin/yes&amp;quot;,argv);&lt;br /&gt;
   }   &lt;br /&gt;
   else&lt;br /&gt;
   {   &lt;br /&gt;
     kill(child,SIGSTOP);&lt;br /&gt;
     sleep(4);&lt;br /&gt;
     kill(child,SIGCONT);&lt;br /&gt;
     sleep(4);&lt;br /&gt;
     kill(child,SIGKILL);&lt;br /&gt;
   }   &lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* run-program-reverse.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;errno.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;string.h&amp;gt;  &lt;br /&gt;
 &lt;br /&gt;
 int main( int argc, char **argv, char *envp[] ) {&lt;br /&gt;
         char **newenv;&lt;br /&gt;
         int i = 0;&lt;br /&gt;
   if( argc &amp;lt; 2 ) {&lt;br /&gt;
       printf( &amp;quot;Insufficient arguments.\n&amp;quot; );&lt;br /&gt;
       return -1;&lt;br /&gt;
   }&lt;br /&gt;
         newenv = (char**)malloc(argc*sizeof(char**));&lt;br /&gt;
         for(i = 0; i &amp;lt; argc; i++)&lt;br /&gt;
         {&lt;br /&gt;
                 newenv[i] = (char*)malloc(sizeof(argv[argc-i-1]));&lt;br /&gt;
                 strcpy(newenv[i],argv[argc-i-1]);&lt;br /&gt;
         }&lt;br /&gt;
         newenv[argc-1] = NULL;&lt;br /&gt;
         if(execvp(newenv[0],newenv + 1) == -1)&lt;br /&gt;
                 printf(&amp;quot;error! = %s&amp;quot;,strerror(errno));&lt;br /&gt;
   &lt;br /&gt;
         printf( &amp;quot;Is this line printed?\n&amp;quot; );&lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* sanitize-env.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;  &lt;br /&gt;
 &lt;br /&gt;
 int main( int argc, char *argv[], char *envp[] ) {&lt;br /&gt;
         extern char **environ;&lt;br /&gt;
         int i = 0;   &lt;br /&gt;
 &lt;br /&gt;
         char *path = getenv(&amp;quot;PATH&amp;quot;);&lt;br /&gt;
         char *term = getenv(&amp;quot;TERM&amp;quot;);&lt;br /&gt;
         if(clearenv())&lt;br /&gt;
                 exit(-1);&lt;br /&gt;
         setenv(&amp;quot;PATH&amp;quot;,path,1);&lt;br /&gt;
         setenv(&amp;quot;TERM&amp;quot;,term,1);  &lt;br /&gt;
 &lt;br /&gt;
         //Check now.&lt;br /&gt;
         while(environ[i] != NULL) {&lt;br /&gt;
                 printf(&amp;quot;environ[%d] = %s\n&amp;quot;,i,environ[i++]); &lt;br /&gt;
         } &lt;br /&gt;
 &lt;br /&gt;
   if( argc &amp;lt; 2 ) {&lt;br /&gt;
       printf( &amp;quot;Insufficient arguments.\n&amp;quot; );&lt;br /&gt;
       return -1;&lt;br /&gt;
   }&lt;br /&gt;
   execvp(argv[1], argv + 1);&lt;br /&gt;
   printf( &amp;quot;Is this line printed?\n&amp;quot; );&lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* receive-usr1.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;signal.h&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
 int child;&lt;br /&gt;
 void sig_handler(int signum)&lt;br /&gt;
 {&lt;br /&gt;
          if(signum == SIGUSR1)&lt;br /&gt;
                 printf(&amp;quot;Ouch!.\n&amp;quot;);&lt;br /&gt;
    fflush(stdout);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   struct sigaction act;&lt;br /&gt;
         sigset_t set;&lt;br /&gt;
   &lt;br /&gt;
   act.sa_handler = sig_handler;&lt;br /&gt;
   act.sa_flags = 0;&lt;br /&gt;
   sigemptyset(&amp;amp;act.sa_mask); &lt;br /&gt;
 &lt;br /&gt;
   sigaction(SIGUSR1, &amp;amp;act, NULL);&lt;br /&gt;
   sigfillset(&amp;amp;set);//Block on all signals&lt;br /&gt;
   sigdelset(&amp;amp;set,SIGUSR1);//except this one&lt;br /&gt;
   sigsuspend(&amp;amp;set);//now wait.&lt;br /&gt;
   return 0;  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* nocrash.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;signal.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int child;&lt;br /&gt;
 void sig_handler(int signum)&lt;br /&gt;
 {&lt;br /&gt;
         if(signum == SIGSEGV)&lt;br /&gt;
                printf(&amp;quot;Oops, my mistake.\n&amp;quot;);&lt;br /&gt;
         fflush(stdout);&lt;br /&gt;
         exit(1);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
        int *a;&lt;br /&gt;
   struct sigaction act;&lt;br /&gt;
  &lt;br /&gt;
  act.sa_handler = sig_handler;&lt;br /&gt;
  act.sa_flags = 0;&lt;br /&gt;
  sigemptyset(&amp;amp;act.sa_mask);&lt;br /&gt;
 &lt;br /&gt;
  sigaction(SIGSEGV, &amp;amp;act, NULL);&lt;br /&gt;
  sleep(1);&lt;br /&gt;
  *a = 10; // Make a mistake. &lt;br /&gt;
  return 0;  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* run-program-dots.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;signal.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int child;&lt;br /&gt;
 void sigchild_handler(int signum)&lt;br /&gt;
 {&lt;br /&gt;
         exit(0); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   struct sigaction act;&lt;br /&gt;
  &lt;br /&gt;
  act.sa_handler = sigchild_handler;&lt;br /&gt;
  act.sa_flags = 0;&lt;br /&gt;
  sigemptyset(&amp;amp;act.sa_mask);&lt;br /&gt;
 &lt;br /&gt;
  child = fork();&lt;br /&gt;
  &lt;br /&gt;
  if(child == 0) {&lt;br /&gt;
         execve(&amp;quot;program&amp;quot;,NULL,NULL);&lt;br /&gt;
    return 0;&lt;br /&gt;
  }&lt;br /&gt;
  else {&lt;br /&gt;
    sigaction(SIGCHLD, &amp;amp;act, NULL); &lt;br /&gt;
         while(1)&lt;br /&gt;
         {&lt;br /&gt;
                printf(&amp;quot;.&amp;quot;);&lt;br /&gt;
                fflush(stdout);&lt;br /&gt;
                sleep(1);&lt;br /&gt;
         } &lt;br /&gt;
    return 0;  &lt;br /&gt;
  }   &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 where my program.c is &lt;br /&gt;
 #include&amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
         sleep(8);&lt;br /&gt;
         return 0;&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_3_2011&amp;diff=12876</id>
		<title>COMP 3000 Lab 3 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_3_2011&amp;diff=12876"/>
		<updated>2011-10-27T20:18:04Z</updated>

		<summary type="html">&lt;p&gt;Saran: /* Answers */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by &#039;&#039;&#039;Monday, October 24th at 11:30 PM&#039;&#039;&#039; (note the extension).  &lt;br /&gt;
* Please answer using a single text file. Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.)&lt;br /&gt;
* Show all your work.  Short answers are not sufficient; you should list any websites or individuals you consult and should document any experiments you conducted.  For any question that you could just answer without any external reference or experiment, write &amp;quot;(no work required)&amp;quot; after your answer.&lt;br /&gt;
* All your codes should compile and run. Partial code fragments or explanations will not be sufficient. &lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Compile run-program.c with the command &#039;&#039;gcc -g run-program.c -o run-program&#039;&#039; and use it to answer the following questions.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: lower-alpha;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Is the line &amp;quot;Is this line printed?&amp;quot; printed when you execute &#039;&#039;&#039;./run-program /bin/ls -a /tmp&#039;&#039;&#039;?  Why? (1 mark) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Change the program to use execve instead of execvp. What is the difference between the two system calls? (1 mark) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Linux signals is a simple form of IPC that is used for a variety of purposes.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: lower-alpha;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; What signal is generated when a program attempts to dereference an invalid pointer?  Give a simple program that generates such a signal.(1 mark) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; How would you send a signal to a process to have it pause execution?  Resume? (1 mark) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;What command can lower the priority of an already running process? (1 mark) &lt;br /&gt;
&amp;lt;li&amp;gt;Create &#039;&#039;run-program-reverse.c&#039;&#039; that is the same as run-program.c except that it interprets its arguments in reverse order.  Hence, &#039;&#039;./run-program-reverse /tmp -a /bin/ls&#039;&#039; will do the same as &#039;&#039;./run-program /bin/ls -a /tmp&#039;&#039;. (2 marks) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Create &#039;&#039;sanitize-env.c&#039;&#039; that does the same as run-program.c except it exec&#039;s the program with all environment variables stripped except the PATH and TERM variables (assuming they are defined).  Be sure to document how you tested your program. (2 marks)&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li value=6&amp;gt; Create a simple program &#039;&#039;receive-usr1.c&#039;&#039; that uses sigaction in order to print &amp;quot;Ouch!&amp;quot; when you send the program the USR1 signal.  Note that your program should not consume much CPU time while waiting (i.e., do not poll for the signal).(2 marks)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Create a simple program &#039;&#039;nocrash.c&#039;&#039; that accesses an invalid pointer but then prints &amp;quot;Oops, my mistake&amp;quot; rather than crash.(2 marks) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Create a program &#039;&#039;run-program-dots.c&#039;&#039; that works the same as run-program.c, except that it prints one dot every second while a given program runs.  Note that it should stop printing dots once the exec&#039;d program terminates.  Your solution should use the fork(), sleep(), execve(), and sigaction() calls.(2 marks)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; What is the difference between the PR and NI columns in the top command?  Explain in the context of running a CPU-intensive program.  (Hint: you should try running a CPU intensive program or two and observe these columns...) (1 mark)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Program Listings==&lt;br /&gt;
&lt;br /&gt;
 /* run-program.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int main( int argc, char *argv[], char *envp[] ) {&lt;br /&gt;
 &lt;br /&gt;
    if( argc &amp;lt; 2 ) {&lt;br /&gt;
        printf( &amp;quot;Insufficient arguments.\n&amp;quot; );&lt;br /&gt;
        return -1;&lt;br /&gt;
    }&lt;br /&gt;
    execvp( argv[1], argv + 1 );&lt;br /&gt;
    printf( &amp;quot;Is this line printed?\n&amp;quot; );&lt;br /&gt;
    return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Answers=&lt;br /&gt;
All questions graded by Saran. Email him for any queries. &lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt; &amp;lt;li&amp;gt;&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: lower-alpha;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;quot;is this line printed?&amp;quot; is not printed because execvp/execve replaces the program binary with a different one.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; execve( argv[1], argv + 1 , envp); &amp;lt;br&amp;gt;&lt;br /&gt;
execve requires an environment argument which may be different from the existing one, where as execvp runs the program using the current environment. Another difference is that execvp searches the PATH environment variable for folders containing binary files, if it can&#039;t find the file in the current working directory. &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt; &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;ol style=&amp;quot;list-style-type: lower-alpha;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;SIGSEGV or SIGBUS is raised for invalid memory references. SIGBUS is low level device specific signal. The following program generates a SIGSEGV in my computer : int *a; *a = 10; &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; SIGSTOP for pausing, and SIGCONT for continuing. &lt;br /&gt;
You can use &amp;quot;kill -SIGWHATEVER pid&amp;quot; from the terminal, or from code. For ex :&lt;br /&gt;
 pause-and-cont.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; renice &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; run-program-reverse.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; sanitize-env.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
(Part B)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li value=6&amp;gt; Compile and run receive-usr1.c and issue &amp;quot;kill -SIGUSR1 pid&amp;quot; &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Use gdb to figure out what signal is thrown for invalid pointer accesses, then catch that. nocrash.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; run-program-dots.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;NI is the nice value of a process, which can be changed by the user. It ranges from -20 to 19 and is used as an input to the scheduler. Depending on all other processes and available resources, the scheduler decides the priority of a process and displays its calculated value in the PR column. &lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Answers : Program Listings== &lt;br /&gt;
&lt;br /&gt;
 /* pause-and-cont.c */&lt;br /&gt;
 &lt;br /&gt;
 #include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include&amp;lt;signal.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int main(int argc, char * argv[], char *envp[])&lt;br /&gt;
 {&lt;br /&gt;
   int child = fork();&lt;br /&gt;
   if(child == 0)&lt;br /&gt;
   {   &lt;br /&gt;
     execvp(&amp;quot;/usr/bin/yes&amp;quot;,argv);&lt;br /&gt;
   }   &lt;br /&gt;
   else&lt;br /&gt;
   {   &lt;br /&gt;
     kill(child,SIGSTOP);&lt;br /&gt;
     sleep(4);&lt;br /&gt;
     kill(child,SIGCONT);&lt;br /&gt;
     sleep(4);&lt;br /&gt;
     kill(child,SIGKILL);&lt;br /&gt;
   }   &lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* run-program-reverse.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;errno.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;string.h&amp;gt;  &lt;br /&gt;
 &lt;br /&gt;
 int main( int argc, char **argv, char *envp[] ) {&lt;br /&gt;
         char **newenv;&lt;br /&gt;
         int i = 0;&lt;br /&gt;
   if( argc &amp;lt; 2 ) {&lt;br /&gt;
       printf( &amp;quot;Insufficient arguments.\n&amp;quot; );&lt;br /&gt;
       return -1;&lt;br /&gt;
   }&lt;br /&gt;
         newenv = (char**)malloc(argc*sizeof(char**));&lt;br /&gt;
         for(i = 0; i &amp;lt; argc; i++)&lt;br /&gt;
         {&lt;br /&gt;
                 newenv[i] = (char*)malloc(sizeof(argv[argc-i-1]));&lt;br /&gt;
                 strcpy(newenv[i],argv[argc-i-1]);&lt;br /&gt;
         }&lt;br /&gt;
         newenv[argc-1] = NULL;&lt;br /&gt;
         if(execvp(newenv[0],newenv + 1) == -1)&lt;br /&gt;
                 printf(&amp;quot;error! = %s&amp;quot;,strerror(errno));&lt;br /&gt;
   &lt;br /&gt;
         printf( &amp;quot;Is this line printed?\n&amp;quot; );&lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* sanitize-env.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;  &lt;br /&gt;
 &lt;br /&gt;
 int main( int argc, char *argv[], char *envp[] ) {&lt;br /&gt;
         extern char **environ;&lt;br /&gt;
         int i = 0;   &lt;br /&gt;
 &lt;br /&gt;
         char *path = getenv(&amp;quot;PATH&amp;quot;);&lt;br /&gt;
         char *term = getenv(&amp;quot;TERM&amp;quot;);&lt;br /&gt;
         if(clearenv())&lt;br /&gt;
                 exit(-1);&lt;br /&gt;
         setenv(&amp;quot;PATH&amp;quot;,path,1);&lt;br /&gt;
         setenv(&amp;quot;TERM&amp;quot;,term,1);  &lt;br /&gt;
 &lt;br /&gt;
         //Check now.&lt;br /&gt;
         while(environ[i] != NULL) {&lt;br /&gt;
                 printf(&amp;quot;environ[%d] = %s\n&amp;quot;,i,environ[i++]); &lt;br /&gt;
         } &lt;br /&gt;
 &lt;br /&gt;
   if( argc &amp;lt; 2 ) {&lt;br /&gt;
       printf( &amp;quot;Insufficient arguments.\n&amp;quot; );&lt;br /&gt;
       return -1;&lt;br /&gt;
   }&lt;br /&gt;
   execvp(argv[1], argv + 1);&lt;br /&gt;
   printf( &amp;quot;Is this line printed?\n&amp;quot; );&lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* receive-usr1.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;signal.h&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
 int child;&lt;br /&gt;
 void sig_handler(int signum)&lt;br /&gt;
 {&lt;br /&gt;
          if(signum == SIGUSR1)&lt;br /&gt;
                 printf(&amp;quot;Ouch!.\n&amp;quot;);&lt;br /&gt;
    fflush(stdout);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   struct sigaction act;&lt;br /&gt;
         sigset_t set;&lt;br /&gt;
   &lt;br /&gt;
   act.sa_handler = sig_handler;&lt;br /&gt;
   act.sa_flags = 0;&lt;br /&gt;
   sigemptyset(&amp;amp;act.sa_mask); &lt;br /&gt;
 &lt;br /&gt;
   sigaction(SIGUSR1, &amp;amp;act, NULL);&lt;br /&gt;
   sigfillset(&amp;amp;set);//Block on all signals&lt;br /&gt;
   sigdelset(&amp;amp;set,SIGUSR1);//except this one&lt;br /&gt;
   sigsuspend(&amp;amp;set);//now wait.&lt;br /&gt;
   return 0;  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* nocrash.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;signal.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int child;&lt;br /&gt;
 void sig_handler(int signum)&lt;br /&gt;
 {&lt;br /&gt;
         if(signum == SIGSEGV)&lt;br /&gt;
                printf(&amp;quot;Oops, my mistake.\n&amp;quot;);&lt;br /&gt;
         fflush(stdout);&lt;br /&gt;
         exit(1);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
        int *a;&lt;br /&gt;
   struct sigaction act;&lt;br /&gt;
  &lt;br /&gt;
  act.sa_handler = sig_handler;&lt;br /&gt;
  act.sa_flags = 0;&lt;br /&gt;
  sigemptyset(&amp;amp;act.sa_mask);&lt;br /&gt;
 &lt;br /&gt;
  sigaction(SIGSEGV, &amp;amp;act, NULL);&lt;br /&gt;
  sleep(1);&lt;br /&gt;
  *a = 10; // Make a mistake. &lt;br /&gt;
  return 0;  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* run-program-dots.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;signal.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int child;&lt;br /&gt;
 void sigchild_handler(int signum)&lt;br /&gt;
 {&lt;br /&gt;
         exit(0); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   struct sigaction act;&lt;br /&gt;
  &lt;br /&gt;
  act.sa_handler = sigchild_handler;&lt;br /&gt;
  act.sa_flags = 0;&lt;br /&gt;
  sigemptyset(&amp;amp;act.sa_mask);&lt;br /&gt;
 &lt;br /&gt;
  child = fork();&lt;br /&gt;
  &lt;br /&gt;
  if(child == 0) {&lt;br /&gt;
         execve(&amp;quot;program&amp;quot;,NULL,NULL);&lt;br /&gt;
    return 0;&lt;br /&gt;
  }&lt;br /&gt;
  else {&lt;br /&gt;
    sigaction(SIGCHLD, &amp;amp;act, NULL); &lt;br /&gt;
         while(1)&lt;br /&gt;
         {&lt;br /&gt;
                printf(&amp;quot;.&amp;quot;);&lt;br /&gt;
                fflush(stdout);&lt;br /&gt;
                sleep(1);&lt;br /&gt;
         } &lt;br /&gt;
    return 0;  &lt;br /&gt;
  }   &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 where my program.c is &lt;br /&gt;
 #include&amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
         sleep(8);&lt;br /&gt;
         return 0;&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_3_2011&amp;diff=12875</id>
		<title>COMP 3000 Lab 3 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_3_2011&amp;diff=12875"/>
		<updated>2011-10-27T20:17:49Z</updated>

		<summary type="html">&lt;p&gt;Saran: /* Answers */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by &#039;&#039;&#039;Monday, October 24th at 11:30 PM&#039;&#039;&#039; (note the extension).  &lt;br /&gt;
* Please answer using a single text file. Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.)&lt;br /&gt;
* Show all your work.  Short answers are not sufficient; you should list any websites or individuals you consult and should document any experiments you conducted.  For any question that you could just answer without any external reference or experiment, write &amp;quot;(no work required)&amp;quot; after your answer.&lt;br /&gt;
* All your codes should compile and run. Partial code fragments or explanations will not be sufficient. &lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Compile run-program.c with the command &#039;&#039;gcc -g run-program.c -o run-program&#039;&#039; and use it to answer the following questions.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: lower-alpha;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Is the line &amp;quot;Is this line printed?&amp;quot; printed when you execute &#039;&#039;&#039;./run-program /bin/ls -a /tmp&#039;&#039;&#039;?  Why? (1 mark) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Change the program to use execve instead of execvp. What is the difference between the two system calls? (1 mark) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Linux signals is a simple form of IPC that is used for a variety of purposes.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: lower-alpha;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; What signal is generated when a program attempts to dereference an invalid pointer?  Give a simple program that generates such a signal.(1 mark) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; How would you send a signal to a process to have it pause execution?  Resume? (1 mark) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;What command can lower the priority of an already running process? (1 mark) &lt;br /&gt;
&amp;lt;li&amp;gt;Create &#039;&#039;run-program-reverse.c&#039;&#039; that is the same as run-program.c except that it interprets its arguments in reverse order.  Hence, &#039;&#039;./run-program-reverse /tmp -a /bin/ls&#039;&#039; will do the same as &#039;&#039;./run-program /bin/ls -a /tmp&#039;&#039;. (2 marks) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Create &#039;&#039;sanitize-env.c&#039;&#039; that does the same as run-program.c except it exec&#039;s the program with all environment variables stripped except the PATH and TERM variables (assuming they are defined).  Be sure to document how you tested your program. (2 marks)&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li value=6&amp;gt; Create a simple program &#039;&#039;receive-usr1.c&#039;&#039; that uses sigaction in order to print &amp;quot;Ouch!&amp;quot; when you send the program the USR1 signal.  Note that your program should not consume much CPU time while waiting (i.e., do not poll for the signal).(2 marks)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Create a simple program &#039;&#039;nocrash.c&#039;&#039; that accesses an invalid pointer but then prints &amp;quot;Oops, my mistake&amp;quot; rather than crash.(2 marks) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Create a program &#039;&#039;run-program-dots.c&#039;&#039; that works the same as run-program.c, except that it prints one dot every second while a given program runs.  Note that it should stop printing dots once the exec&#039;d program terminates.  Your solution should use the fork(), sleep(), execve(), and sigaction() calls.(2 marks)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; What is the difference between the PR and NI columns in the top command?  Explain in the context of running a CPU-intensive program.  (Hint: you should try running a CPU intensive program or two and observe these columns...) (1 mark)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Program Listings==&lt;br /&gt;
&lt;br /&gt;
 /* run-program.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int main( int argc, char *argv[], char *envp[] ) {&lt;br /&gt;
 &lt;br /&gt;
    if( argc &amp;lt; 2 ) {&lt;br /&gt;
        printf( &amp;quot;Insufficient arguments.\n&amp;quot; );&lt;br /&gt;
        return -1;&lt;br /&gt;
    }&lt;br /&gt;
    execvp( argv[1], argv + 1 );&lt;br /&gt;
    printf( &amp;quot;Is this line printed?\n&amp;quot; );&lt;br /&gt;
    return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Answers=&lt;br /&gt;
Both Parts Graded by Saran. Email him for any queries. &lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt; &amp;lt;li&amp;gt;&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: lower-alpha;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;quot;is this line printed?&amp;quot; is not printed because execvp/execve replaces the program binary with a different one.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; execve( argv[1], argv + 1 , envp); &amp;lt;br&amp;gt;&lt;br /&gt;
execve requires an environment argument which may be different from the existing one, where as execvp runs the program using the current environment. Another difference is that execvp searches the PATH environment variable for folders containing binary files, if it can&#039;t find the file in the current working directory. &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt; &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;ol style=&amp;quot;list-style-type: lower-alpha;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;SIGSEGV or SIGBUS is raised for invalid memory references. SIGBUS is low level device specific signal. The following program generates a SIGSEGV in my computer : int *a; *a = 10; &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; SIGSTOP for pausing, and SIGCONT for continuing. &lt;br /&gt;
You can use &amp;quot;kill -SIGWHATEVER pid&amp;quot; from the terminal, or from code. For ex :&lt;br /&gt;
 pause-and-cont.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; renice &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; run-program-reverse.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; sanitize-env.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
(Part B)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li value=6&amp;gt; Compile and run receive-usr1.c and issue &amp;quot;kill -SIGUSR1 pid&amp;quot; &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Use gdb to figure out what signal is thrown for invalid pointer accesses, then catch that. nocrash.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; run-program-dots.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;NI is the nice value of a process, which can be changed by the user. It ranges from -20 to 19 and is used as an input to the scheduler. Depending on all other processes and available resources, the scheduler decides the priority of a process and displays its calculated value in the PR column. &lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Answers : Program Listings== &lt;br /&gt;
&lt;br /&gt;
 /* pause-and-cont.c */&lt;br /&gt;
 &lt;br /&gt;
 #include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include&amp;lt;signal.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int main(int argc, char * argv[], char *envp[])&lt;br /&gt;
 {&lt;br /&gt;
   int child = fork();&lt;br /&gt;
   if(child == 0)&lt;br /&gt;
   {   &lt;br /&gt;
     execvp(&amp;quot;/usr/bin/yes&amp;quot;,argv);&lt;br /&gt;
   }   &lt;br /&gt;
   else&lt;br /&gt;
   {   &lt;br /&gt;
     kill(child,SIGSTOP);&lt;br /&gt;
     sleep(4);&lt;br /&gt;
     kill(child,SIGCONT);&lt;br /&gt;
     sleep(4);&lt;br /&gt;
     kill(child,SIGKILL);&lt;br /&gt;
   }   &lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* run-program-reverse.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;errno.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;string.h&amp;gt;  &lt;br /&gt;
 &lt;br /&gt;
 int main( int argc, char **argv, char *envp[] ) {&lt;br /&gt;
         char **newenv;&lt;br /&gt;
         int i = 0;&lt;br /&gt;
   if( argc &amp;lt; 2 ) {&lt;br /&gt;
       printf( &amp;quot;Insufficient arguments.\n&amp;quot; );&lt;br /&gt;
       return -1;&lt;br /&gt;
   }&lt;br /&gt;
         newenv = (char**)malloc(argc*sizeof(char**));&lt;br /&gt;
         for(i = 0; i &amp;lt; argc; i++)&lt;br /&gt;
         {&lt;br /&gt;
                 newenv[i] = (char*)malloc(sizeof(argv[argc-i-1]));&lt;br /&gt;
                 strcpy(newenv[i],argv[argc-i-1]);&lt;br /&gt;
         }&lt;br /&gt;
         newenv[argc-1] = NULL;&lt;br /&gt;
         if(execvp(newenv[0],newenv + 1) == -1)&lt;br /&gt;
                 printf(&amp;quot;error! = %s&amp;quot;,strerror(errno));&lt;br /&gt;
   &lt;br /&gt;
         printf( &amp;quot;Is this line printed?\n&amp;quot; );&lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* sanitize-env.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;  &lt;br /&gt;
 &lt;br /&gt;
 int main( int argc, char *argv[], char *envp[] ) {&lt;br /&gt;
         extern char **environ;&lt;br /&gt;
         int i = 0;   &lt;br /&gt;
 &lt;br /&gt;
         char *path = getenv(&amp;quot;PATH&amp;quot;);&lt;br /&gt;
         char *term = getenv(&amp;quot;TERM&amp;quot;);&lt;br /&gt;
         if(clearenv())&lt;br /&gt;
                 exit(-1);&lt;br /&gt;
         setenv(&amp;quot;PATH&amp;quot;,path,1);&lt;br /&gt;
         setenv(&amp;quot;TERM&amp;quot;,term,1);  &lt;br /&gt;
 &lt;br /&gt;
         //Check now.&lt;br /&gt;
         while(environ[i] != NULL) {&lt;br /&gt;
                 printf(&amp;quot;environ[%d] = %s\n&amp;quot;,i,environ[i++]); &lt;br /&gt;
         } &lt;br /&gt;
 &lt;br /&gt;
   if( argc &amp;lt; 2 ) {&lt;br /&gt;
       printf( &amp;quot;Insufficient arguments.\n&amp;quot; );&lt;br /&gt;
       return -1;&lt;br /&gt;
   }&lt;br /&gt;
   execvp(argv[1], argv + 1);&lt;br /&gt;
   printf( &amp;quot;Is this line printed?\n&amp;quot; );&lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* receive-usr1.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;signal.h&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
 int child;&lt;br /&gt;
 void sig_handler(int signum)&lt;br /&gt;
 {&lt;br /&gt;
          if(signum == SIGUSR1)&lt;br /&gt;
                 printf(&amp;quot;Ouch!.\n&amp;quot;);&lt;br /&gt;
    fflush(stdout);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   struct sigaction act;&lt;br /&gt;
         sigset_t set;&lt;br /&gt;
   &lt;br /&gt;
   act.sa_handler = sig_handler;&lt;br /&gt;
   act.sa_flags = 0;&lt;br /&gt;
   sigemptyset(&amp;amp;act.sa_mask); &lt;br /&gt;
 &lt;br /&gt;
   sigaction(SIGUSR1, &amp;amp;act, NULL);&lt;br /&gt;
   sigfillset(&amp;amp;set);//Block on all signals&lt;br /&gt;
   sigdelset(&amp;amp;set,SIGUSR1);//except this one&lt;br /&gt;
   sigsuspend(&amp;amp;set);//now wait.&lt;br /&gt;
   return 0;  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* nocrash.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;signal.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int child;&lt;br /&gt;
 void sig_handler(int signum)&lt;br /&gt;
 {&lt;br /&gt;
         if(signum == SIGSEGV)&lt;br /&gt;
                printf(&amp;quot;Oops, my mistake.\n&amp;quot;);&lt;br /&gt;
         fflush(stdout);&lt;br /&gt;
         exit(1);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
        int *a;&lt;br /&gt;
   struct sigaction act;&lt;br /&gt;
  &lt;br /&gt;
  act.sa_handler = sig_handler;&lt;br /&gt;
  act.sa_flags = 0;&lt;br /&gt;
  sigemptyset(&amp;amp;act.sa_mask);&lt;br /&gt;
 &lt;br /&gt;
  sigaction(SIGSEGV, &amp;amp;act, NULL);&lt;br /&gt;
  sleep(1);&lt;br /&gt;
  *a = 10; // Make a mistake. &lt;br /&gt;
  return 0;  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* run-program-dots.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;signal.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int child;&lt;br /&gt;
 void sigchild_handler(int signum)&lt;br /&gt;
 {&lt;br /&gt;
         exit(0); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   struct sigaction act;&lt;br /&gt;
  &lt;br /&gt;
  act.sa_handler = sigchild_handler;&lt;br /&gt;
  act.sa_flags = 0;&lt;br /&gt;
  sigemptyset(&amp;amp;act.sa_mask);&lt;br /&gt;
 &lt;br /&gt;
  child = fork();&lt;br /&gt;
  &lt;br /&gt;
  if(child == 0) {&lt;br /&gt;
         execve(&amp;quot;program&amp;quot;,NULL,NULL);&lt;br /&gt;
    return 0;&lt;br /&gt;
  }&lt;br /&gt;
  else {&lt;br /&gt;
    sigaction(SIGCHLD, &amp;amp;act, NULL); &lt;br /&gt;
         while(1)&lt;br /&gt;
         {&lt;br /&gt;
                printf(&amp;quot;.&amp;quot;);&lt;br /&gt;
                fflush(stdout);&lt;br /&gt;
                sleep(1);&lt;br /&gt;
         } &lt;br /&gt;
    return 0;  &lt;br /&gt;
  }   &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 where my program.c is &lt;br /&gt;
 #include&amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
         sleep(8);&lt;br /&gt;
         return 0;&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_3_2011&amp;diff=12874</id>
		<title>COMP 3000 Lab 3 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_3_2011&amp;diff=12874"/>
		<updated>2011-10-27T20:08:45Z</updated>

		<summary type="html">&lt;p&gt;Saran: /* Part A */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by &#039;&#039;&#039;Monday, October 24th at 11:30 PM&#039;&#039;&#039; (note the extension).  &lt;br /&gt;
* Please answer using a single text file. Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.)&lt;br /&gt;
* Show all your work.  Short answers are not sufficient; you should list any websites or individuals you consult and should document any experiments you conducted.  For any question that you could just answer without any external reference or experiment, write &amp;quot;(no work required)&amp;quot; after your answer.&lt;br /&gt;
* All your codes should compile and run. Partial code fragments or explanations will not be sufficient. &lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Compile run-program.c with the command &#039;&#039;gcc -g run-program.c -o run-program&#039;&#039; and use it to answer the following questions.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: lower-alpha;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Is the line &amp;quot;Is this line printed?&amp;quot; printed when you execute &#039;&#039;&#039;./run-program /bin/ls -a /tmp&#039;&#039;&#039;?  Why? (1 mark) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Change the program to use execve instead of execvp. What is the difference between the two system calls? (1 mark) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Linux signals is a simple form of IPC that is used for a variety of purposes.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: lower-alpha;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; What signal is generated when a program attempts to dereference an invalid pointer?  Give a simple program that generates such a signal.(1 mark) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; How would you send a signal to a process to have it pause execution?  Resume? (1 mark) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;What command can lower the priority of an already running process? (1 mark) &lt;br /&gt;
&amp;lt;li&amp;gt;Create &#039;&#039;run-program-reverse.c&#039;&#039; that is the same as run-program.c except that it interprets its arguments in reverse order.  Hence, &#039;&#039;./run-program-reverse /tmp -a /bin/ls&#039;&#039; will do the same as &#039;&#039;./run-program /bin/ls -a /tmp&#039;&#039;. (2 marks) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Create &#039;&#039;sanitize-env.c&#039;&#039; that does the same as run-program.c except it exec&#039;s the program with all environment variables stripped except the PATH and TERM variables (assuming they are defined).  Be sure to document how you tested your program. (2 marks)&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li value=6&amp;gt; Create a simple program &#039;&#039;receive-usr1.c&#039;&#039; that uses sigaction in order to print &amp;quot;Ouch!&amp;quot; when you send the program the USR1 signal.  Note that your program should not consume much CPU time while waiting (i.e., do not poll for the signal).(2 marks)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Create a simple program &#039;&#039;nocrash.c&#039;&#039; that accesses an invalid pointer but then prints &amp;quot;Oops, my mistake&amp;quot; rather than crash.(2 marks) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Create a program &#039;&#039;run-program-dots.c&#039;&#039; that works the same as run-program.c, except that it prints one dot every second while a given program runs.  Note that it should stop printing dots once the exec&#039;d program terminates.  Your solution should use the fork(), sleep(), execve(), and sigaction() calls.(2 marks)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; What is the difference between the PR and NI columns in the top command?  Explain in the context of running a CPU-intensive program.  (Hint: you should try running a CPU intensive program or two and observe these columns...) (1 mark)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Program Listings==&lt;br /&gt;
&lt;br /&gt;
 /* run-program.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int main( int argc, char *argv[], char *envp[] ) {&lt;br /&gt;
 &lt;br /&gt;
    if( argc &amp;lt; 2 ) {&lt;br /&gt;
        printf( &amp;quot;Insufficient arguments.\n&amp;quot; );&lt;br /&gt;
        return -1;&lt;br /&gt;
    }&lt;br /&gt;
    execvp( argv[1], argv + 1 );&lt;br /&gt;
    printf( &amp;quot;Is this line printed?\n&amp;quot; );&lt;br /&gt;
    return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Answers=&lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt; &amp;lt;li&amp;gt;&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: lower-alpha;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;quot;is this line printed?&amp;quot; is not printed because execvp/execve replaces the program binary with a different one.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; execve( argv[1], argv + 1 , envp); &amp;lt;br&amp;gt;&lt;br /&gt;
execve requires an environment argument which may be different from the existing one, where as execvp runs the program using the current environment. Another difference is that execvp searches the PATH environment variable for folders containing binary files, if it can&#039;t find the file in the current working directory. &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt; &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;ol style=&amp;quot;list-style-type: lower-alpha;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;SIGSEGV or SIGBUS is raised for invalid memory references. SIGBUS is low level device specific signal. The following program generates a SIGSEGV in my computer : int *a; *a = 10; &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; SIGSTOP for pausing, and SIGCONT for continuing. &lt;br /&gt;
You can use &amp;quot;kill -SIGWHATEVER pid&amp;quot; from the terminal, or from code. For ex :&lt;br /&gt;
 pause-and-cont.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; renice &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; run-program-reverse.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; sanitize-env.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
(Part B)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li value=6&amp;gt; Compile and run receive-usr1.c and issue &amp;quot;kill -SIGUSR1 pid&amp;quot; &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Use gdb to figure out what signal is thrown for invalid pointer accesses, then catch that. nocrash.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; run-program-dots.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;NI is the nice value of a process, which can be changed by the user. It ranges from -20 to 19 and is used as an input to the scheduler. Depending on all other processes and available resources, the scheduler decides the priority of a process and displays its calculated value in the PR column. &lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Answers : Program Listings== &lt;br /&gt;
&lt;br /&gt;
 /* pause-and-cont.c */&lt;br /&gt;
 &lt;br /&gt;
 #include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include&amp;lt;signal.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int main(int argc, char * argv[], char *envp[])&lt;br /&gt;
 {&lt;br /&gt;
   int child = fork();&lt;br /&gt;
   if(child == 0)&lt;br /&gt;
   {   &lt;br /&gt;
     execvp(&amp;quot;/usr/bin/yes&amp;quot;,argv);&lt;br /&gt;
   }   &lt;br /&gt;
   else&lt;br /&gt;
   {   &lt;br /&gt;
     kill(child,SIGSTOP);&lt;br /&gt;
     sleep(4);&lt;br /&gt;
     kill(child,SIGCONT);&lt;br /&gt;
     sleep(4);&lt;br /&gt;
     kill(child,SIGKILL);&lt;br /&gt;
   }   &lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* run-program-reverse.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;errno.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;string.h&amp;gt;  &lt;br /&gt;
 &lt;br /&gt;
 int main( int argc, char **argv, char *envp[] ) {&lt;br /&gt;
         char **newenv;&lt;br /&gt;
         int i = 0;&lt;br /&gt;
   if( argc &amp;lt; 2 ) {&lt;br /&gt;
       printf( &amp;quot;Insufficient arguments.\n&amp;quot; );&lt;br /&gt;
       return -1;&lt;br /&gt;
   }&lt;br /&gt;
         newenv = (char**)malloc(argc*sizeof(char**));&lt;br /&gt;
         for(i = 0; i &amp;lt; argc; i++)&lt;br /&gt;
         {&lt;br /&gt;
                 newenv[i] = (char*)malloc(sizeof(argv[argc-i-1]));&lt;br /&gt;
                 strcpy(newenv[i],argv[argc-i-1]);&lt;br /&gt;
         }&lt;br /&gt;
         newenv[argc-1] = NULL;&lt;br /&gt;
         if(execvp(newenv[0],newenv + 1) == -1)&lt;br /&gt;
                 printf(&amp;quot;error! = %s&amp;quot;,strerror(errno));&lt;br /&gt;
   &lt;br /&gt;
         printf( &amp;quot;Is this line printed?\n&amp;quot; );&lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* sanitize-env.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;  &lt;br /&gt;
 &lt;br /&gt;
 int main( int argc, char *argv[], char *envp[] ) {&lt;br /&gt;
         extern char **environ;&lt;br /&gt;
         int i = 0;   &lt;br /&gt;
 &lt;br /&gt;
         char *path = getenv(&amp;quot;PATH&amp;quot;);&lt;br /&gt;
         char *term = getenv(&amp;quot;TERM&amp;quot;);&lt;br /&gt;
         if(clearenv())&lt;br /&gt;
                 exit(-1);&lt;br /&gt;
         setenv(&amp;quot;PATH&amp;quot;,path,1);&lt;br /&gt;
         setenv(&amp;quot;TERM&amp;quot;,term,1);  &lt;br /&gt;
 &lt;br /&gt;
         //Check now.&lt;br /&gt;
         while(environ[i] != NULL) {&lt;br /&gt;
                 printf(&amp;quot;environ[%d] = %s\n&amp;quot;,i,environ[i++]); &lt;br /&gt;
         } &lt;br /&gt;
 &lt;br /&gt;
   if( argc &amp;lt; 2 ) {&lt;br /&gt;
       printf( &amp;quot;Insufficient arguments.\n&amp;quot; );&lt;br /&gt;
       return -1;&lt;br /&gt;
   }&lt;br /&gt;
   execvp(argv[1], argv + 1);&lt;br /&gt;
   printf( &amp;quot;Is this line printed?\n&amp;quot; );&lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* receive-usr1.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;signal.h&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
 int child;&lt;br /&gt;
 void sig_handler(int signum)&lt;br /&gt;
 {&lt;br /&gt;
          if(signum == SIGUSR1)&lt;br /&gt;
                 printf(&amp;quot;Ouch!.\n&amp;quot;);&lt;br /&gt;
    fflush(stdout);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   struct sigaction act;&lt;br /&gt;
         sigset_t set;&lt;br /&gt;
   &lt;br /&gt;
   act.sa_handler = sig_handler;&lt;br /&gt;
   act.sa_flags = 0;&lt;br /&gt;
   sigemptyset(&amp;amp;act.sa_mask); &lt;br /&gt;
 &lt;br /&gt;
   sigaction(SIGUSR1, &amp;amp;act, NULL);&lt;br /&gt;
   sigfillset(&amp;amp;set);//Block on all signals&lt;br /&gt;
   sigdelset(&amp;amp;set,SIGUSR1);//except this one&lt;br /&gt;
   sigsuspend(&amp;amp;set);//now wait.&lt;br /&gt;
   return 0;  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* nocrash.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;signal.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int child;&lt;br /&gt;
 void sig_handler(int signum)&lt;br /&gt;
 {&lt;br /&gt;
         if(signum == SIGSEGV)&lt;br /&gt;
                printf(&amp;quot;Oops, my mistake.\n&amp;quot;);&lt;br /&gt;
         fflush(stdout);&lt;br /&gt;
         exit(1);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
        int *a;&lt;br /&gt;
   struct sigaction act;&lt;br /&gt;
  &lt;br /&gt;
  act.sa_handler = sig_handler;&lt;br /&gt;
  act.sa_flags = 0;&lt;br /&gt;
  sigemptyset(&amp;amp;act.sa_mask);&lt;br /&gt;
 &lt;br /&gt;
  sigaction(SIGSEGV, &amp;amp;act, NULL);&lt;br /&gt;
  sleep(1);&lt;br /&gt;
  *a = 10; // Make a mistake. &lt;br /&gt;
  return 0;  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* run-program-dots.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;signal.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int child;&lt;br /&gt;
 void sigchild_handler(int signum)&lt;br /&gt;
 {&lt;br /&gt;
         exit(0); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   struct sigaction act;&lt;br /&gt;
  &lt;br /&gt;
  act.sa_handler = sigchild_handler;&lt;br /&gt;
  act.sa_flags = 0;&lt;br /&gt;
  sigemptyset(&amp;amp;act.sa_mask);&lt;br /&gt;
 &lt;br /&gt;
  child = fork();&lt;br /&gt;
  &lt;br /&gt;
  if(child == 0) {&lt;br /&gt;
         execve(&amp;quot;program&amp;quot;,NULL,NULL);&lt;br /&gt;
    return 0;&lt;br /&gt;
  }&lt;br /&gt;
  else {&lt;br /&gt;
    sigaction(SIGCHLD, &amp;amp;act, NULL); &lt;br /&gt;
         while(1)&lt;br /&gt;
         {&lt;br /&gt;
                printf(&amp;quot;.&amp;quot;);&lt;br /&gt;
                fflush(stdout);&lt;br /&gt;
                sleep(1);&lt;br /&gt;
         } &lt;br /&gt;
    return 0;  &lt;br /&gt;
  }   &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 where my program.c is &lt;br /&gt;
 #include&amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
         sleep(8);&lt;br /&gt;
         return 0;&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_3_2011&amp;diff=12873</id>
		<title>COMP 3000 Lab 3 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_3_2011&amp;diff=12873"/>
		<updated>2011-10-27T20:08:37Z</updated>

		<summary type="html">&lt;p&gt;Saran: /* Part A */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by &#039;&#039;&#039;Monday, October 24th at 11:30 PM&#039;&#039;&#039; (note the extension).  &lt;br /&gt;
* Please answer using a single text file. Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.)&lt;br /&gt;
* Show all your work.  Short answers are not sufficient; you should list any websites or individuals you consult and should document any experiments you conducted.  For any question that you could just answer without any external reference or experiment, write &amp;quot;(no work required)&amp;quot; after your answer.&lt;br /&gt;
* All your codes should compile and run. Partial code fragments or explanations will not be sufficient. &lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Compile run-program.c with the command &#039;&#039;gcc -g run-program.c -o run-program&#039;&#039; and use it to answer the following questions.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: lower-alpha;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Is the line &amp;quot;Is this line printed?&amp;quot; printed when you execute &#039;&#039;&#039;./run-program /bin/ls -a /tmp&#039;&#039;&#039;?  Why? (1 mark) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Change the program to use execve instead of execvp. What is the difference between the two system calls? (1 mark) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Linux signals is a simple form of IPC that is used for a variety of purposes.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: lower-alpha;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; What signal is generated when a program attempts to dereference an invalid pointer?  Give a simple program that generates such a signal.(1 mark) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; How would you send a signal to a process to have it pause execution?  Resume? (1 mark) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;What command can lower the priority of an already running process? (1 mark) &lt;br /&gt;
&amp;lt;li&amp;gt;Create &#039;&#039;run-program-reverse.c&#039;&#039; that is the same as run-program.c except that it interprets its arguments in reverse order.  Hence, &#039;&#039;./run-program-reverse /tmp -a /bin/ls&#039;&#039; will do the same as &#039;&#039;./run-program /bin/ls -a /tmp&#039;&#039;.(2 marks) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Create &#039;&#039;sanitize-env.c&#039;&#039; that does the same as run-program.c except it exec&#039;s the program with all environment variables stripped except the PATH and TERM variables (assuming they are defined).  Be sure to document how you tested your program. (2 marks)&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li value=6&amp;gt; Create a simple program &#039;&#039;receive-usr1.c&#039;&#039; that uses sigaction in order to print &amp;quot;Ouch!&amp;quot; when you send the program the USR1 signal.  Note that your program should not consume much CPU time while waiting (i.e., do not poll for the signal).(2 marks)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Create a simple program &#039;&#039;nocrash.c&#039;&#039; that accesses an invalid pointer but then prints &amp;quot;Oops, my mistake&amp;quot; rather than crash.(2 marks) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Create a program &#039;&#039;run-program-dots.c&#039;&#039; that works the same as run-program.c, except that it prints one dot every second while a given program runs.  Note that it should stop printing dots once the exec&#039;d program terminates.  Your solution should use the fork(), sleep(), execve(), and sigaction() calls.(2 marks)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; What is the difference between the PR and NI columns in the top command?  Explain in the context of running a CPU-intensive program.  (Hint: you should try running a CPU intensive program or two and observe these columns...) (1 mark)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Program Listings==&lt;br /&gt;
&lt;br /&gt;
 /* run-program.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int main( int argc, char *argv[], char *envp[] ) {&lt;br /&gt;
 &lt;br /&gt;
    if( argc &amp;lt; 2 ) {&lt;br /&gt;
        printf( &amp;quot;Insufficient arguments.\n&amp;quot; );&lt;br /&gt;
        return -1;&lt;br /&gt;
    }&lt;br /&gt;
    execvp( argv[1], argv + 1 );&lt;br /&gt;
    printf( &amp;quot;Is this line printed?\n&amp;quot; );&lt;br /&gt;
    return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Answers=&lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt; &amp;lt;li&amp;gt;&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: lower-alpha;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;quot;is this line printed?&amp;quot; is not printed because execvp/execve replaces the program binary with a different one.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; execve( argv[1], argv + 1 , envp); &amp;lt;br&amp;gt;&lt;br /&gt;
execve requires an environment argument which may be different from the existing one, where as execvp runs the program using the current environment. Another difference is that execvp searches the PATH environment variable for folders containing binary files, if it can&#039;t find the file in the current working directory. &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt; &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;ol style=&amp;quot;list-style-type: lower-alpha;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;SIGSEGV or SIGBUS is raised for invalid memory references. SIGBUS is low level device specific signal. The following program generates a SIGSEGV in my computer : int *a; *a = 10; &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; SIGSTOP for pausing, and SIGCONT for continuing. &lt;br /&gt;
You can use &amp;quot;kill -SIGWHATEVER pid&amp;quot; from the terminal, or from code. For ex :&lt;br /&gt;
 pause-and-cont.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; renice &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; run-program-reverse.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; sanitize-env.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
(Part B)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li value=6&amp;gt; Compile and run receive-usr1.c and issue &amp;quot;kill -SIGUSR1 pid&amp;quot; &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Use gdb to figure out what signal is thrown for invalid pointer accesses, then catch that. nocrash.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; run-program-dots.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;NI is the nice value of a process, which can be changed by the user. It ranges from -20 to 19 and is used as an input to the scheduler. Depending on all other processes and available resources, the scheduler decides the priority of a process and displays its calculated value in the PR column. &lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Answers : Program Listings== &lt;br /&gt;
&lt;br /&gt;
 /* pause-and-cont.c */&lt;br /&gt;
 &lt;br /&gt;
 #include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include&amp;lt;signal.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int main(int argc, char * argv[], char *envp[])&lt;br /&gt;
 {&lt;br /&gt;
   int child = fork();&lt;br /&gt;
   if(child == 0)&lt;br /&gt;
   {   &lt;br /&gt;
     execvp(&amp;quot;/usr/bin/yes&amp;quot;,argv);&lt;br /&gt;
   }   &lt;br /&gt;
   else&lt;br /&gt;
   {   &lt;br /&gt;
     kill(child,SIGSTOP);&lt;br /&gt;
     sleep(4);&lt;br /&gt;
     kill(child,SIGCONT);&lt;br /&gt;
     sleep(4);&lt;br /&gt;
     kill(child,SIGKILL);&lt;br /&gt;
   }   &lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* run-program-reverse.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;errno.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;string.h&amp;gt;  &lt;br /&gt;
 &lt;br /&gt;
 int main( int argc, char **argv, char *envp[] ) {&lt;br /&gt;
         char **newenv;&lt;br /&gt;
         int i = 0;&lt;br /&gt;
   if( argc &amp;lt; 2 ) {&lt;br /&gt;
       printf( &amp;quot;Insufficient arguments.\n&amp;quot; );&lt;br /&gt;
       return -1;&lt;br /&gt;
   }&lt;br /&gt;
         newenv = (char**)malloc(argc*sizeof(char**));&lt;br /&gt;
         for(i = 0; i &amp;lt; argc; i++)&lt;br /&gt;
         {&lt;br /&gt;
                 newenv[i] = (char*)malloc(sizeof(argv[argc-i-1]));&lt;br /&gt;
                 strcpy(newenv[i],argv[argc-i-1]);&lt;br /&gt;
         }&lt;br /&gt;
         newenv[argc-1] = NULL;&lt;br /&gt;
         if(execvp(newenv[0],newenv + 1) == -1)&lt;br /&gt;
                 printf(&amp;quot;error! = %s&amp;quot;,strerror(errno));&lt;br /&gt;
   &lt;br /&gt;
         printf( &amp;quot;Is this line printed?\n&amp;quot; );&lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* sanitize-env.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;  &lt;br /&gt;
 &lt;br /&gt;
 int main( int argc, char *argv[], char *envp[] ) {&lt;br /&gt;
         extern char **environ;&lt;br /&gt;
         int i = 0;   &lt;br /&gt;
 &lt;br /&gt;
         char *path = getenv(&amp;quot;PATH&amp;quot;);&lt;br /&gt;
         char *term = getenv(&amp;quot;TERM&amp;quot;);&lt;br /&gt;
         if(clearenv())&lt;br /&gt;
                 exit(-1);&lt;br /&gt;
         setenv(&amp;quot;PATH&amp;quot;,path,1);&lt;br /&gt;
         setenv(&amp;quot;TERM&amp;quot;,term,1);  &lt;br /&gt;
 &lt;br /&gt;
         //Check now.&lt;br /&gt;
         while(environ[i] != NULL) {&lt;br /&gt;
                 printf(&amp;quot;environ[%d] = %s\n&amp;quot;,i,environ[i++]); &lt;br /&gt;
         } &lt;br /&gt;
 &lt;br /&gt;
   if( argc &amp;lt; 2 ) {&lt;br /&gt;
       printf( &amp;quot;Insufficient arguments.\n&amp;quot; );&lt;br /&gt;
       return -1;&lt;br /&gt;
   }&lt;br /&gt;
   execvp(argv[1], argv + 1);&lt;br /&gt;
   printf( &amp;quot;Is this line printed?\n&amp;quot; );&lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* receive-usr1.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;signal.h&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
 int child;&lt;br /&gt;
 void sig_handler(int signum)&lt;br /&gt;
 {&lt;br /&gt;
          if(signum == SIGUSR1)&lt;br /&gt;
                 printf(&amp;quot;Ouch!.\n&amp;quot;);&lt;br /&gt;
    fflush(stdout);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   struct sigaction act;&lt;br /&gt;
         sigset_t set;&lt;br /&gt;
   &lt;br /&gt;
   act.sa_handler = sig_handler;&lt;br /&gt;
   act.sa_flags = 0;&lt;br /&gt;
   sigemptyset(&amp;amp;act.sa_mask); &lt;br /&gt;
 &lt;br /&gt;
   sigaction(SIGUSR1, &amp;amp;act, NULL);&lt;br /&gt;
   sigfillset(&amp;amp;set);//Block on all signals&lt;br /&gt;
   sigdelset(&amp;amp;set,SIGUSR1);//except this one&lt;br /&gt;
   sigsuspend(&amp;amp;set);//now wait.&lt;br /&gt;
   return 0;  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* nocrash.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;signal.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int child;&lt;br /&gt;
 void sig_handler(int signum)&lt;br /&gt;
 {&lt;br /&gt;
         if(signum == SIGSEGV)&lt;br /&gt;
                printf(&amp;quot;Oops, my mistake.\n&amp;quot;);&lt;br /&gt;
         fflush(stdout);&lt;br /&gt;
         exit(1);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
        int *a;&lt;br /&gt;
   struct sigaction act;&lt;br /&gt;
  &lt;br /&gt;
  act.sa_handler = sig_handler;&lt;br /&gt;
  act.sa_flags = 0;&lt;br /&gt;
  sigemptyset(&amp;amp;act.sa_mask);&lt;br /&gt;
 &lt;br /&gt;
  sigaction(SIGSEGV, &amp;amp;act, NULL);&lt;br /&gt;
  sleep(1);&lt;br /&gt;
  *a = 10; // Make a mistake. &lt;br /&gt;
  return 0;  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* run-program-dots.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;signal.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int child;&lt;br /&gt;
 void sigchild_handler(int signum)&lt;br /&gt;
 {&lt;br /&gt;
         exit(0); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   struct sigaction act;&lt;br /&gt;
  &lt;br /&gt;
  act.sa_handler = sigchild_handler;&lt;br /&gt;
  act.sa_flags = 0;&lt;br /&gt;
  sigemptyset(&amp;amp;act.sa_mask);&lt;br /&gt;
 &lt;br /&gt;
  child = fork();&lt;br /&gt;
  &lt;br /&gt;
  if(child == 0) {&lt;br /&gt;
         execve(&amp;quot;program&amp;quot;,NULL,NULL);&lt;br /&gt;
    return 0;&lt;br /&gt;
  }&lt;br /&gt;
  else {&lt;br /&gt;
    sigaction(SIGCHLD, &amp;amp;act, NULL); &lt;br /&gt;
         while(1)&lt;br /&gt;
         {&lt;br /&gt;
                printf(&amp;quot;.&amp;quot;);&lt;br /&gt;
                fflush(stdout);&lt;br /&gt;
                sleep(1);&lt;br /&gt;
         } &lt;br /&gt;
    return 0;  &lt;br /&gt;
  }   &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 where my program.c is &lt;br /&gt;
 #include&amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
         sleep(8);&lt;br /&gt;
         return 0;&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_3_2011&amp;diff=12872</id>
		<title>COMP 3000 Lab 3 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_3_2011&amp;diff=12872"/>
		<updated>2011-10-27T20:08:03Z</updated>

		<summary type="html">&lt;p&gt;Saran: /* Part B */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by &#039;&#039;&#039;Monday, October 24th at 11:30 PM&#039;&#039;&#039; (note the extension).  &lt;br /&gt;
* Please answer using a single text file. Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.)&lt;br /&gt;
* Show all your work.  Short answers are not sufficient; you should list any websites or individuals you consult and should document any experiments you conducted.  For any question that you could just answer without any external reference or experiment, write &amp;quot;(no work required)&amp;quot; after your answer.&lt;br /&gt;
* All your codes should compile and run. Partial code fragments or explanations will not be sufficient. &lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Compile run-program.c with the command &#039;&#039;gcc -g run-program.c -o run-program&#039;&#039; and use it to answer the following questions.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: lower-alpha;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Is the line &amp;quot;Is this line printed?&amp;quot; printed when you execute &#039;&#039;&#039;./run-program /bin/ls -a /tmp&#039;&#039;&#039;?  Why? (1 mark) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Change the program to use execve instead of execvp. What is the difference between the two system calls? (1 mark) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Linux signals is a simple form of IPC that is used for a variety of purposes.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: lower-alpha;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; What signal is generated when a program attempts to dereference an invalid pointer?  Give a simple program that generates such a signal.(1 mark) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; How would you send a signal to a process to have it pause execution?  Resume? (1 mark) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;What command can lower the priority of an already running process? (1 mark) &lt;br /&gt;
&amp;lt;li&amp;gt;Create &#039;&#039;run-program-reverse.c&#039;&#039; that is the same as run-program.c except that it interprets its arguments in reverse order.  Hence, &#039;&#039;./run-program-reverse /tmp -a /bin/ls&#039;&#039; will do the same as &#039;&#039;./run-program /bin/ls -a /tmp&#039;&#039;.(2 mark) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Create &#039;&#039;sanitize-env.c&#039;&#039; that does the same as run-program.c except it exec&#039;s the program with all environment variables stripped except the PATH and TERM variables (assuming they are defined).  Be sure to document how you tested your program. (2 mark)&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li value=6&amp;gt; Create a simple program &#039;&#039;receive-usr1.c&#039;&#039; that uses sigaction in order to print &amp;quot;Ouch!&amp;quot; when you send the program the USR1 signal.  Note that your program should not consume much CPU time while waiting (i.e., do not poll for the signal).(2 marks)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Create a simple program &#039;&#039;nocrash.c&#039;&#039; that accesses an invalid pointer but then prints &amp;quot;Oops, my mistake&amp;quot; rather than crash.(2 marks) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Create a program &#039;&#039;run-program-dots.c&#039;&#039; that works the same as run-program.c, except that it prints one dot every second while a given program runs.  Note that it should stop printing dots once the exec&#039;d program terminates.  Your solution should use the fork(), sleep(), execve(), and sigaction() calls.(2 marks)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; What is the difference between the PR and NI columns in the top command?  Explain in the context of running a CPU-intensive program.  (Hint: you should try running a CPU intensive program or two and observe these columns...) (1 mark)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Program Listings==&lt;br /&gt;
&lt;br /&gt;
 /* run-program.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int main( int argc, char *argv[], char *envp[] ) {&lt;br /&gt;
 &lt;br /&gt;
    if( argc &amp;lt; 2 ) {&lt;br /&gt;
        printf( &amp;quot;Insufficient arguments.\n&amp;quot; );&lt;br /&gt;
        return -1;&lt;br /&gt;
    }&lt;br /&gt;
    execvp( argv[1], argv + 1 );&lt;br /&gt;
    printf( &amp;quot;Is this line printed?\n&amp;quot; );&lt;br /&gt;
    return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Answers=&lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt; &amp;lt;li&amp;gt;&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: lower-alpha;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;quot;is this line printed?&amp;quot; is not printed because execvp/execve replaces the program binary with a different one.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; execve( argv[1], argv + 1 , envp); &amp;lt;br&amp;gt;&lt;br /&gt;
execve requires an environment argument which may be different from the existing one, where as execvp runs the program using the current environment. Another difference is that execvp searches the PATH environment variable for folders containing binary files, if it can&#039;t find the file in the current working directory. &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt; &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;ol style=&amp;quot;list-style-type: lower-alpha;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;SIGSEGV or SIGBUS is raised for invalid memory references. SIGBUS is low level device specific signal. The following program generates a SIGSEGV in my computer : int *a; *a = 10; &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; SIGSTOP for pausing, and SIGCONT for continuing. &lt;br /&gt;
You can use &amp;quot;kill -SIGWHATEVER pid&amp;quot; from the terminal, or from code. For ex :&lt;br /&gt;
 pause-and-cont.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; renice &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; run-program-reverse.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; sanitize-env.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
(Part B)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li value=6&amp;gt; Compile and run receive-usr1.c and issue &amp;quot;kill -SIGUSR1 pid&amp;quot; &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Use gdb to figure out what signal is thrown for invalid pointer accesses, then catch that. nocrash.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; run-program-dots.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;NI is the nice value of a process, which can be changed by the user. It ranges from -20 to 19 and is used as an input to the scheduler. Depending on all other processes and available resources, the scheduler decides the priority of a process and displays its calculated value in the PR column. &lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Answers : Program Listings== &lt;br /&gt;
&lt;br /&gt;
 /* pause-and-cont.c */&lt;br /&gt;
 &lt;br /&gt;
 #include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include&amp;lt;signal.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int main(int argc, char * argv[], char *envp[])&lt;br /&gt;
 {&lt;br /&gt;
   int child = fork();&lt;br /&gt;
   if(child == 0)&lt;br /&gt;
   {   &lt;br /&gt;
     execvp(&amp;quot;/usr/bin/yes&amp;quot;,argv);&lt;br /&gt;
   }   &lt;br /&gt;
   else&lt;br /&gt;
   {   &lt;br /&gt;
     kill(child,SIGSTOP);&lt;br /&gt;
     sleep(4);&lt;br /&gt;
     kill(child,SIGCONT);&lt;br /&gt;
     sleep(4);&lt;br /&gt;
     kill(child,SIGKILL);&lt;br /&gt;
   }   &lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* run-program-reverse.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;errno.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;string.h&amp;gt;  &lt;br /&gt;
 &lt;br /&gt;
 int main( int argc, char **argv, char *envp[] ) {&lt;br /&gt;
         char **newenv;&lt;br /&gt;
         int i = 0;&lt;br /&gt;
   if( argc &amp;lt; 2 ) {&lt;br /&gt;
       printf( &amp;quot;Insufficient arguments.\n&amp;quot; );&lt;br /&gt;
       return -1;&lt;br /&gt;
   }&lt;br /&gt;
         newenv = (char**)malloc(argc*sizeof(char**));&lt;br /&gt;
         for(i = 0; i &amp;lt; argc; i++)&lt;br /&gt;
         {&lt;br /&gt;
                 newenv[i] = (char*)malloc(sizeof(argv[argc-i-1]));&lt;br /&gt;
                 strcpy(newenv[i],argv[argc-i-1]);&lt;br /&gt;
         }&lt;br /&gt;
         newenv[argc-1] = NULL;&lt;br /&gt;
         if(execvp(newenv[0],newenv + 1) == -1)&lt;br /&gt;
                 printf(&amp;quot;error! = %s&amp;quot;,strerror(errno));&lt;br /&gt;
   &lt;br /&gt;
         printf( &amp;quot;Is this line printed?\n&amp;quot; );&lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* sanitize-env.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;  &lt;br /&gt;
 &lt;br /&gt;
 int main( int argc, char *argv[], char *envp[] ) {&lt;br /&gt;
         extern char **environ;&lt;br /&gt;
         int i = 0;   &lt;br /&gt;
 &lt;br /&gt;
         char *path = getenv(&amp;quot;PATH&amp;quot;);&lt;br /&gt;
         char *term = getenv(&amp;quot;TERM&amp;quot;);&lt;br /&gt;
         if(clearenv())&lt;br /&gt;
                 exit(-1);&lt;br /&gt;
         setenv(&amp;quot;PATH&amp;quot;,path,1);&lt;br /&gt;
         setenv(&amp;quot;TERM&amp;quot;,term,1);  &lt;br /&gt;
 &lt;br /&gt;
         //Check now.&lt;br /&gt;
         while(environ[i] != NULL) {&lt;br /&gt;
                 printf(&amp;quot;environ[%d] = %s\n&amp;quot;,i,environ[i++]); &lt;br /&gt;
         } &lt;br /&gt;
 &lt;br /&gt;
   if( argc &amp;lt; 2 ) {&lt;br /&gt;
       printf( &amp;quot;Insufficient arguments.\n&amp;quot; );&lt;br /&gt;
       return -1;&lt;br /&gt;
   }&lt;br /&gt;
   execvp(argv[1], argv + 1);&lt;br /&gt;
   printf( &amp;quot;Is this line printed?\n&amp;quot; );&lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* receive-usr1.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;signal.h&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
 int child;&lt;br /&gt;
 void sig_handler(int signum)&lt;br /&gt;
 {&lt;br /&gt;
          if(signum == SIGUSR1)&lt;br /&gt;
                 printf(&amp;quot;Ouch!.\n&amp;quot;);&lt;br /&gt;
    fflush(stdout);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   struct sigaction act;&lt;br /&gt;
         sigset_t set;&lt;br /&gt;
   &lt;br /&gt;
   act.sa_handler = sig_handler;&lt;br /&gt;
   act.sa_flags = 0;&lt;br /&gt;
   sigemptyset(&amp;amp;act.sa_mask); &lt;br /&gt;
 &lt;br /&gt;
   sigaction(SIGUSR1, &amp;amp;act, NULL);&lt;br /&gt;
   sigfillset(&amp;amp;set);//Block on all signals&lt;br /&gt;
   sigdelset(&amp;amp;set,SIGUSR1);//except this one&lt;br /&gt;
   sigsuspend(&amp;amp;set);//now wait.&lt;br /&gt;
   return 0;  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* nocrash.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;signal.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int child;&lt;br /&gt;
 void sig_handler(int signum)&lt;br /&gt;
 {&lt;br /&gt;
         if(signum == SIGSEGV)&lt;br /&gt;
                printf(&amp;quot;Oops, my mistake.\n&amp;quot;);&lt;br /&gt;
         fflush(stdout);&lt;br /&gt;
         exit(1);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
        int *a;&lt;br /&gt;
   struct sigaction act;&lt;br /&gt;
  &lt;br /&gt;
  act.sa_handler = sig_handler;&lt;br /&gt;
  act.sa_flags = 0;&lt;br /&gt;
  sigemptyset(&amp;amp;act.sa_mask);&lt;br /&gt;
 &lt;br /&gt;
  sigaction(SIGSEGV, &amp;amp;act, NULL);&lt;br /&gt;
  sleep(1);&lt;br /&gt;
  *a = 10; // Make a mistake. &lt;br /&gt;
  return 0;  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* run-program-dots.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;signal.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int child;&lt;br /&gt;
 void sigchild_handler(int signum)&lt;br /&gt;
 {&lt;br /&gt;
         exit(0); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   struct sigaction act;&lt;br /&gt;
  &lt;br /&gt;
  act.sa_handler = sigchild_handler;&lt;br /&gt;
  act.sa_flags = 0;&lt;br /&gt;
  sigemptyset(&amp;amp;act.sa_mask);&lt;br /&gt;
 &lt;br /&gt;
  child = fork();&lt;br /&gt;
  &lt;br /&gt;
  if(child == 0) {&lt;br /&gt;
         execve(&amp;quot;program&amp;quot;,NULL,NULL);&lt;br /&gt;
    return 0;&lt;br /&gt;
  }&lt;br /&gt;
  else {&lt;br /&gt;
    sigaction(SIGCHLD, &amp;amp;act, NULL); &lt;br /&gt;
         while(1)&lt;br /&gt;
         {&lt;br /&gt;
                printf(&amp;quot;.&amp;quot;);&lt;br /&gt;
                fflush(stdout);&lt;br /&gt;
                sleep(1);&lt;br /&gt;
         } &lt;br /&gt;
    return 0;  &lt;br /&gt;
  }   &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 where my program.c is &lt;br /&gt;
 #include&amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
         sleep(8);&lt;br /&gt;
         return 0;&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_3_2011&amp;diff=12871</id>
		<title>COMP 3000 Lab 3 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_3_2011&amp;diff=12871"/>
		<updated>2011-10-27T20:07:40Z</updated>

		<summary type="html">&lt;p&gt;Saran: /* Part A */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by &#039;&#039;&#039;Monday, October 24th at 11:30 PM&#039;&#039;&#039; (note the extension).  &lt;br /&gt;
* Please answer using a single text file. Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.)&lt;br /&gt;
* Show all your work.  Short answers are not sufficient; you should list any websites or individuals you consult and should document any experiments you conducted.  For any question that you could just answer without any external reference or experiment, write &amp;quot;(no work required)&amp;quot; after your answer.&lt;br /&gt;
* All your codes should compile and run. Partial code fragments or explanations will not be sufficient. &lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Compile run-program.c with the command &#039;&#039;gcc -g run-program.c -o run-program&#039;&#039; and use it to answer the following questions.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: lower-alpha;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Is the line &amp;quot;Is this line printed?&amp;quot; printed when you execute &#039;&#039;&#039;./run-program /bin/ls -a /tmp&#039;&#039;&#039;?  Why? (1 mark) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Change the program to use execve instead of execvp. What is the difference between the two system calls? (1 mark) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Linux signals is a simple form of IPC that is used for a variety of purposes.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: lower-alpha;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; What signal is generated when a program attempts to dereference an invalid pointer?  Give a simple program that generates such a signal.(1 mark) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; How would you send a signal to a process to have it pause execution?  Resume? (1 mark) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;What command can lower the priority of an already running process? (1 mark) &lt;br /&gt;
&amp;lt;li&amp;gt;Create &#039;&#039;run-program-reverse.c&#039;&#039; that is the same as run-program.c except that it interprets its arguments in reverse order.  Hence, &#039;&#039;./run-program-reverse /tmp -a /bin/ls&#039;&#039; will do the same as &#039;&#039;./run-program /bin/ls -a /tmp&#039;&#039;.(2 mark) &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Create &#039;&#039;sanitize-env.c&#039;&#039; that does the same as run-program.c except it exec&#039;s the program with all environment variables stripped except the PATH and TERM variables (assuming they are defined).  Be sure to document how you tested your program. (2 mark)&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li value=6&amp;gt; Create a simple program &#039;&#039;receive-usr1.c&#039;&#039; that uses sigaction in order to print &amp;quot;Ouch!&amp;quot; when you send the program the USR1 signal.  Note that your program should not consume much CPU time while waiting (i.e., do not poll for the signal).&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Create a simple program &#039;&#039;nocrash.c&#039;&#039; that accesses an invalid pointer but then prints &amp;quot;Oops, my mistake&amp;quot; rather than crash.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Create a program &#039;&#039;run-program-dots.c&#039;&#039; that works the same as run-program.c, except that it prints one dot every second while a given program runs.  Note that it should stop printing dots once the exec&#039;d program terminates.  Your solution should use the fork(), sleep(), execve(), and sigaction() calls.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; What is the difference between the PR and NI columns in the top command?  Explain in the context of running a CPU-intensive program.  (Hint: you should try running a CPU intensive program or two and observe these columns...)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Program Listings==&lt;br /&gt;
&lt;br /&gt;
 /* run-program.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int main( int argc, char *argv[], char *envp[] ) {&lt;br /&gt;
 &lt;br /&gt;
    if( argc &amp;lt; 2 ) {&lt;br /&gt;
        printf( &amp;quot;Insufficient arguments.\n&amp;quot; );&lt;br /&gt;
        return -1;&lt;br /&gt;
    }&lt;br /&gt;
    execvp( argv[1], argv + 1 );&lt;br /&gt;
    printf( &amp;quot;Is this line printed?\n&amp;quot; );&lt;br /&gt;
    return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Answers=&lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt; &amp;lt;li&amp;gt;&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: lower-alpha;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;quot;is this line printed?&amp;quot; is not printed because execvp/execve replaces the program binary with a different one.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; execve( argv[1], argv + 1 , envp); &amp;lt;br&amp;gt;&lt;br /&gt;
execve requires an environment argument which may be different from the existing one, where as execvp runs the program using the current environment. Another difference is that execvp searches the PATH environment variable for folders containing binary files, if it can&#039;t find the file in the current working directory. &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt; &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;ol style=&amp;quot;list-style-type: lower-alpha;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;SIGSEGV or SIGBUS is raised for invalid memory references. SIGBUS is low level device specific signal. The following program generates a SIGSEGV in my computer : int *a; *a = 10; &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; SIGSTOP for pausing, and SIGCONT for continuing. &lt;br /&gt;
You can use &amp;quot;kill -SIGWHATEVER pid&amp;quot; from the terminal, or from code. For ex :&lt;br /&gt;
 pause-and-cont.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; renice &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; run-program-reverse.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; sanitize-env.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
(Part B)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li value=6&amp;gt; Compile and run receive-usr1.c and issue &amp;quot;kill -SIGUSR1 pid&amp;quot; &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Use gdb to figure out what signal is thrown for invalid pointer accesses, then catch that. nocrash.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; run-program-dots.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;NI is the nice value of a process, which can be changed by the user. It ranges from -20 to 19 and is used as an input to the scheduler. Depending on all other processes and available resources, the scheduler decides the priority of a process and displays its calculated value in the PR column. &lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Answers : Program Listings== &lt;br /&gt;
&lt;br /&gt;
 /* pause-and-cont.c */&lt;br /&gt;
 &lt;br /&gt;
 #include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include&amp;lt;signal.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int main(int argc, char * argv[], char *envp[])&lt;br /&gt;
 {&lt;br /&gt;
   int child = fork();&lt;br /&gt;
   if(child == 0)&lt;br /&gt;
   {   &lt;br /&gt;
     execvp(&amp;quot;/usr/bin/yes&amp;quot;,argv);&lt;br /&gt;
   }   &lt;br /&gt;
   else&lt;br /&gt;
   {   &lt;br /&gt;
     kill(child,SIGSTOP);&lt;br /&gt;
     sleep(4);&lt;br /&gt;
     kill(child,SIGCONT);&lt;br /&gt;
     sleep(4);&lt;br /&gt;
     kill(child,SIGKILL);&lt;br /&gt;
   }   &lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* run-program-reverse.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;errno.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;string.h&amp;gt;  &lt;br /&gt;
 &lt;br /&gt;
 int main( int argc, char **argv, char *envp[] ) {&lt;br /&gt;
         char **newenv;&lt;br /&gt;
         int i = 0;&lt;br /&gt;
   if( argc &amp;lt; 2 ) {&lt;br /&gt;
       printf( &amp;quot;Insufficient arguments.\n&amp;quot; );&lt;br /&gt;
       return -1;&lt;br /&gt;
   }&lt;br /&gt;
         newenv = (char**)malloc(argc*sizeof(char**));&lt;br /&gt;
         for(i = 0; i &amp;lt; argc; i++)&lt;br /&gt;
         {&lt;br /&gt;
                 newenv[i] = (char*)malloc(sizeof(argv[argc-i-1]));&lt;br /&gt;
                 strcpy(newenv[i],argv[argc-i-1]);&lt;br /&gt;
         }&lt;br /&gt;
         newenv[argc-1] = NULL;&lt;br /&gt;
         if(execvp(newenv[0],newenv + 1) == -1)&lt;br /&gt;
                 printf(&amp;quot;error! = %s&amp;quot;,strerror(errno));&lt;br /&gt;
   &lt;br /&gt;
         printf( &amp;quot;Is this line printed?\n&amp;quot; );&lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* sanitize-env.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;  &lt;br /&gt;
 &lt;br /&gt;
 int main( int argc, char *argv[], char *envp[] ) {&lt;br /&gt;
         extern char **environ;&lt;br /&gt;
         int i = 0;   &lt;br /&gt;
 &lt;br /&gt;
         char *path = getenv(&amp;quot;PATH&amp;quot;);&lt;br /&gt;
         char *term = getenv(&amp;quot;TERM&amp;quot;);&lt;br /&gt;
         if(clearenv())&lt;br /&gt;
                 exit(-1);&lt;br /&gt;
         setenv(&amp;quot;PATH&amp;quot;,path,1);&lt;br /&gt;
         setenv(&amp;quot;TERM&amp;quot;,term,1);  &lt;br /&gt;
 &lt;br /&gt;
         //Check now.&lt;br /&gt;
         while(environ[i] != NULL) {&lt;br /&gt;
                 printf(&amp;quot;environ[%d] = %s\n&amp;quot;,i,environ[i++]); &lt;br /&gt;
         } &lt;br /&gt;
 &lt;br /&gt;
   if( argc &amp;lt; 2 ) {&lt;br /&gt;
       printf( &amp;quot;Insufficient arguments.\n&amp;quot; );&lt;br /&gt;
       return -1;&lt;br /&gt;
   }&lt;br /&gt;
   execvp(argv[1], argv + 1);&lt;br /&gt;
   printf( &amp;quot;Is this line printed?\n&amp;quot; );&lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* receive-usr1.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;signal.h&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
 int child;&lt;br /&gt;
 void sig_handler(int signum)&lt;br /&gt;
 {&lt;br /&gt;
          if(signum == SIGUSR1)&lt;br /&gt;
                 printf(&amp;quot;Ouch!.\n&amp;quot;);&lt;br /&gt;
    fflush(stdout);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   struct sigaction act;&lt;br /&gt;
         sigset_t set;&lt;br /&gt;
   &lt;br /&gt;
   act.sa_handler = sig_handler;&lt;br /&gt;
   act.sa_flags = 0;&lt;br /&gt;
   sigemptyset(&amp;amp;act.sa_mask); &lt;br /&gt;
 &lt;br /&gt;
   sigaction(SIGUSR1, &amp;amp;act, NULL);&lt;br /&gt;
   sigfillset(&amp;amp;set);//Block on all signals&lt;br /&gt;
   sigdelset(&amp;amp;set,SIGUSR1);//except this one&lt;br /&gt;
   sigsuspend(&amp;amp;set);//now wait.&lt;br /&gt;
   return 0;  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* nocrash.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;signal.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int child;&lt;br /&gt;
 void sig_handler(int signum)&lt;br /&gt;
 {&lt;br /&gt;
         if(signum == SIGSEGV)&lt;br /&gt;
                printf(&amp;quot;Oops, my mistake.\n&amp;quot;);&lt;br /&gt;
         fflush(stdout);&lt;br /&gt;
         exit(1);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
        int *a;&lt;br /&gt;
   struct sigaction act;&lt;br /&gt;
  &lt;br /&gt;
  act.sa_handler = sig_handler;&lt;br /&gt;
  act.sa_flags = 0;&lt;br /&gt;
  sigemptyset(&amp;amp;act.sa_mask);&lt;br /&gt;
 &lt;br /&gt;
  sigaction(SIGSEGV, &amp;amp;act, NULL);&lt;br /&gt;
  sleep(1);&lt;br /&gt;
  *a = 10; // Make a mistake. &lt;br /&gt;
  return 0;  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* run-program-dots.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;signal.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int child;&lt;br /&gt;
 void sigchild_handler(int signum)&lt;br /&gt;
 {&lt;br /&gt;
         exit(0); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   struct sigaction act;&lt;br /&gt;
  &lt;br /&gt;
  act.sa_handler = sigchild_handler;&lt;br /&gt;
  act.sa_flags = 0;&lt;br /&gt;
  sigemptyset(&amp;amp;act.sa_mask);&lt;br /&gt;
 &lt;br /&gt;
  child = fork();&lt;br /&gt;
  &lt;br /&gt;
  if(child == 0) {&lt;br /&gt;
         execve(&amp;quot;program&amp;quot;,NULL,NULL);&lt;br /&gt;
    return 0;&lt;br /&gt;
  }&lt;br /&gt;
  else {&lt;br /&gt;
    sigaction(SIGCHLD, &amp;amp;act, NULL); &lt;br /&gt;
         while(1)&lt;br /&gt;
         {&lt;br /&gt;
                printf(&amp;quot;.&amp;quot;);&lt;br /&gt;
                fflush(stdout);&lt;br /&gt;
                sleep(1);&lt;br /&gt;
         } &lt;br /&gt;
    return 0;  &lt;br /&gt;
  }   &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 where my program.c is &lt;br /&gt;
 #include&amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
         sleep(8);&lt;br /&gt;
         return 0;&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_3_2011&amp;diff=12870</id>
		<title>COMP 3000 Lab 3 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_3_2011&amp;diff=12870"/>
		<updated>2011-10-27T18:52:28Z</updated>

		<summary type="html">&lt;p&gt;Saran: /* Part A */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by &#039;&#039;&#039;Monday, October 24th at 11:30 PM&#039;&#039;&#039; (note the extension).  &lt;br /&gt;
* Please answer using a single text file. Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.)&lt;br /&gt;
* Show all your work.  Short answers are not sufficient; you should list any websites or individuals you consult and should document any experiments you conducted.  For any question that you could just answer without any external reference or experiment, write &amp;quot;(no work required)&amp;quot; after your answer.&lt;br /&gt;
* All your codes should compile and run. Partial code fragments or explanations will not be sufficient. &lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Compile run-program.c with the command &#039;&#039;gcc -g run-program.c -o run-program&#039;&#039; and use it to answer the following questions.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: lower-alpha;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Is the line &amp;quot;Is this line printed?&amp;quot; printed when you execute &#039;&#039;&#039;./run-program /bin/ls -a /tmp&#039;&#039;&#039;?  Why?&lt;br /&gt;
&amp;lt;li&amp;gt; Change the program to use execve instead of execvp. What is the difference between the two system calls? &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Linux signals is a simple form of IPC that is used for a variety of purposes.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: lower-alpha;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; What signal is generated when a program attempts to dereference an invalid pointer?  Give a simple program that generates such a signal.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; How would you send a signal to a process to have it pause execution?  Resume?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;What command can lower the priority of an already running process? &lt;br /&gt;
&amp;lt;li&amp;gt;Create &#039;&#039;run-program-reverse.c&#039;&#039; that is the same as run-program.c except that it interprets its arguments in reverse order.  Hence, &#039;&#039;./run-program-reverse /tmp -a /bin/ls&#039;&#039; will do the same as &#039;&#039;./run-program /bin/ls -a /tmp&#039;&#039;.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Create &#039;&#039;sanitize-env.c&#039;&#039; that does the same as run-program.c except it exec&#039;s the program with all environment variables stripped except the PATH and TERM variables (assuming they are defined).  Be sure to document how you tested your program.&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li value=6&amp;gt; Create a simple program &#039;&#039;receive-usr1.c&#039;&#039; that uses sigaction in order to print &amp;quot;Ouch!&amp;quot; when you send the program the USR1 signal.  Note that your program should not consume much CPU time while waiting (i.e., do not poll for the signal).&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Create a simple program &#039;&#039;nocrash.c&#039;&#039; that accesses an invalid pointer but then prints &amp;quot;Oops, my mistake&amp;quot; rather than crash.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Create a program &#039;&#039;run-program-dots.c&#039;&#039; that works the same as run-program.c, except that it prints one dot every second while a given program runs.  Note that it should stop printing dots once the exec&#039;d program terminates.  Your solution should use the fork(), sleep(), execve(), and sigaction() calls.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; What is the difference between the PR and NI columns in the top command?  Explain in the context of running a CPU-intensive program.  (Hint: you should try running a CPU intensive program or two and observe these columns...)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Program Listings==&lt;br /&gt;
&lt;br /&gt;
 /* run-program.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int main( int argc, char *argv[], char *envp[] ) {&lt;br /&gt;
 &lt;br /&gt;
    if( argc &amp;lt; 2 ) {&lt;br /&gt;
        printf( &amp;quot;Insufficient arguments.\n&amp;quot; );&lt;br /&gt;
        return -1;&lt;br /&gt;
    }&lt;br /&gt;
    execvp( argv[1], argv + 1 );&lt;br /&gt;
    printf( &amp;quot;Is this line printed?\n&amp;quot; );&lt;br /&gt;
    return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Answers=&lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt; &amp;lt;li&amp;gt;&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: lower-alpha;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;quot;is this line printed?&amp;quot; is not printed because execvp/execve replaces the program binary with a different one.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; execve( argv[1], argv + 1 , envp); &amp;lt;br&amp;gt;&lt;br /&gt;
execve requires an environment argument which may be different from the existing one, where as execvp runs the program using the current environment. Another difference is that execvp searches the PATH environment variable for folders containing binary files, if it can&#039;t find the file in the current working directory. &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt; &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;ol style=&amp;quot;list-style-type: lower-alpha;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;SIGSEGV or SIGBUS is raised for invalid memory references. SIGBUS is low level device specific signal. The following program generates a SIGSEGV in my computer : int *a; *a = 10; &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; SIGSTOP for pausing, and SIGCONT for continuing. &lt;br /&gt;
You can use &amp;quot;kill -SIGWHATEVER pid&amp;quot; from the terminal, or from code. For ex :&lt;br /&gt;
 pause-and-cont.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; renice &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; run-program-reverse.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; sanitize-env.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
(Part B)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li value=6&amp;gt; Compile and run receive-usr1.c and issue &amp;quot;kill -SIGUSR1 pid&amp;quot; &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Use gdb to figure out what signal is thrown for invalid pointer accesses, then catch that. nocrash.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; run-program-dots.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;NI is the nice value of a process, which can be changed by the user. It ranges from -20 to 19 and is used as an input to the scheduler. Depending on all other processes and available resources, the scheduler decides the priority of a process and displays its calculated value in the PR column. &lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Answers : Program Listings== &lt;br /&gt;
&lt;br /&gt;
 /* pause-and-cont.c */&lt;br /&gt;
 &lt;br /&gt;
 #include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include&amp;lt;signal.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int main(int argc, char * argv[], char *envp[])&lt;br /&gt;
 {&lt;br /&gt;
   int child = fork();&lt;br /&gt;
   if(child == 0)&lt;br /&gt;
   {   &lt;br /&gt;
     execvp(&amp;quot;/usr/bin/yes&amp;quot;,argv);&lt;br /&gt;
   }   &lt;br /&gt;
   else&lt;br /&gt;
   {   &lt;br /&gt;
     kill(child,SIGSTOP);&lt;br /&gt;
     sleep(4);&lt;br /&gt;
     kill(child,SIGCONT);&lt;br /&gt;
     sleep(4);&lt;br /&gt;
     kill(child,SIGKILL);&lt;br /&gt;
   }   &lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* run-program-reverse.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;errno.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;string.h&amp;gt;  &lt;br /&gt;
 &lt;br /&gt;
 int main( int argc, char **argv, char *envp[] ) {&lt;br /&gt;
         char **newenv;&lt;br /&gt;
         int i = 0;&lt;br /&gt;
   if( argc &amp;lt; 2 ) {&lt;br /&gt;
       printf( &amp;quot;Insufficient arguments.\n&amp;quot; );&lt;br /&gt;
       return -1;&lt;br /&gt;
   }&lt;br /&gt;
         newenv = (char**)malloc(argc*sizeof(char**));&lt;br /&gt;
         for(i = 0; i &amp;lt; argc; i++)&lt;br /&gt;
         {&lt;br /&gt;
                 newenv[i] = (char*)malloc(sizeof(argv[argc-i-1]));&lt;br /&gt;
                 strcpy(newenv[i],argv[argc-i-1]);&lt;br /&gt;
         }&lt;br /&gt;
         newenv[argc-1] = NULL;&lt;br /&gt;
         if(execvp(newenv[0],newenv + 1) == -1)&lt;br /&gt;
                 printf(&amp;quot;error! = %s&amp;quot;,strerror(errno));&lt;br /&gt;
   &lt;br /&gt;
         printf( &amp;quot;Is this line printed?\n&amp;quot; );&lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* sanitize-env.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;  &lt;br /&gt;
 &lt;br /&gt;
 int main( int argc, char *argv[], char *envp[] ) {&lt;br /&gt;
         extern char **environ;&lt;br /&gt;
         int i = 0;   &lt;br /&gt;
 &lt;br /&gt;
         char *path = getenv(&amp;quot;PATH&amp;quot;);&lt;br /&gt;
         char *term = getenv(&amp;quot;TERM&amp;quot;);&lt;br /&gt;
         if(clearenv())&lt;br /&gt;
                 exit(-1);&lt;br /&gt;
         setenv(&amp;quot;PATH&amp;quot;,path,1);&lt;br /&gt;
         setenv(&amp;quot;TERM&amp;quot;,term,1);  &lt;br /&gt;
 &lt;br /&gt;
         //Check now.&lt;br /&gt;
         while(environ[i] != NULL) {&lt;br /&gt;
                 printf(&amp;quot;environ[%d] = %s\n&amp;quot;,i,environ[i++]); &lt;br /&gt;
         } &lt;br /&gt;
 &lt;br /&gt;
   if( argc &amp;lt; 2 ) {&lt;br /&gt;
       printf( &amp;quot;Insufficient arguments.\n&amp;quot; );&lt;br /&gt;
       return -1;&lt;br /&gt;
   }&lt;br /&gt;
   execvp(argv[1], argv + 1);&lt;br /&gt;
   printf( &amp;quot;Is this line printed?\n&amp;quot; );&lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* receive-usr1.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;signal.h&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
 int child;&lt;br /&gt;
 void sig_handler(int signum)&lt;br /&gt;
 {&lt;br /&gt;
          if(signum == SIGUSR1)&lt;br /&gt;
                 printf(&amp;quot;Ouch!.\n&amp;quot;);&lt;br /&gt;
    fflush(stdout);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   struct sigaction act;&lt;br /&gt;
         sigset_t set;&lt;br /&gt;
   &lt;br /&gt;
   act.sa_handler = sig_handler;&lt;br /&gt;
   act.sa_flags = 0;&lt;br /&gt;
   sigemptyset(&amp;amp;act.sa_mask); &lt;br /&gt;
 &lt;br /&gt;
   sigaction(SIGUSR1, &amp;amp;act, NULL);&lt;br /&gt;
   sigfillset(&amp;amp;set);//Block on all signals&lt;br /&gt;
   sigdelset(&amp;amp;set,SIGUSR1);//except this one&lt;br /&gt;
   sigsuspend(&amp;amp;set);//now wait.&lt;br /&gt;
   return 0;  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* nocrash.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;signal.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int child;&lt;br /&gt;
 void sig_handler(int signum)&lt;br /&gt;
 {&lt;br /&gt;
         if(signum == SIGSEGV)&lt;br /&gt;
                printf(&amp;quot;Oops, my mistake.\n&amp;quot;);&lt;br /&gt;
         fflush(stdout);&lt;br /&gt;
         exit(1);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
        int *a;&lt;br /&gt;
   struct sigaction act;&lt;br /&gt;
  &lt;br /&gt;
  act.sa_handler = sig_handler;&lt;br /&gt;
  act.sa_flags = 0;&lt;br /&gt;
  sigemptyset(&amp;amp;act.sa_mask);&lt;br /&gt;
 &lt;br /&gt;
  sigaction(SIGSEGV, &amp;amp;act, NULL);&lt;br /&gt;
  sleep(1);&lt;br /&gt;
  *a = 10; // Make a mistake. &lt;br /&gt;
  return 0;  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* run-program-dots.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;signal.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int child;&lt;br /&gt;
 void sigchild_handler(int signum)&lt;br /&gt;
 {&lt;br /&gt;
         exit(0); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   struct sigaction act;&lt;br /&gt;
  &lt;br /&gt;
  act.sa_handler = sigchild_handler;&lt;br /&gt;
  act.sa_flags = 0;&lt;br /&gt;
  sigemptyset(&amp;amp;act.sa_mask);&lt;br /&gt;
 &lt;br /&gt;
  child = fork();&lt;br /&gt;
  &lt;br /&gt;
  if(child == 0) {&lt;br /&gt;
         execve(&amp;quot;program&amp;quot;,NULL,NULL);&lt;br /&gt;
    return 0;&lt;br /&gt;
  }&lt;br /&gt;
  else {&lt;br /&gt;
    sigaction(SIGCHLD, &amp;amp;act, NULL); &lt;br /&gt;
         while(1)&lt;br /&gt;
         {&lt;br /&gt;
                printf(&amp;quot;.&amp;quot;);&lt;br /&gt;
                fflush(stdout);&lt;br /&gt;
                sleep(1);&lt;br /&gt;
         } &lt;br /&gt;
    return 0;  &lt;br /&gt;
  }   &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 where my program.c is &lt;br /&gt;
 #include&amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
         sleep(8);&lt;br /&gt;
         return 0;&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_3_2011&amp;diff=12869</id>
		<title>COMP 3000 Lab 3 2011</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP_3000_Lab_3_2011&amp;diff=12869"/>
		<updated>2011-10-27T18:51:41Z</updated>

		<summary type="html">&lt;p&gt;Saran: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A few guidelines:&lt;br /&gt;
* Submit your solutions for both Part A and Part B via WebCT by &#039;&#039;&#039;Monday, October 24th at 11:30 PM&#039;&#039;&#039; (note the extension).  &lt;br /&gt;
* Please answer using a single text file. Do not submit doc, docx, pdf, or other formats.  Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don&#039;t just cut and paste your work into a text box on webct.)&lt;br /&gt;
* Show all your work.  Short answers are not sufficient; you should list any websites or individuals you consult and should document any experiments you conducted.  For any question that you could just answer without any external reference or experiment, write &amp;quot;(no work required)&amp;quot; after your answer.&lt;br /&gt;
* All your codes should compile and run. Partial code fragments or explanations will not be sufficient. &lt;br /&gt;
&lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Compile run-program.c with the command &#039;&#039;gcc -g run-program.c -o run-program&#039;&#039; and use it to answer the following questions.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: lower-alpha;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Is the line &amp;quot;Is this line printed?&amp;quot; printed when you execute &#039;&#039;&#039;./run-program /bin/ls -a /tmp&#039;&#039;&#039;?  Why?&lt;br /&gt;
&amp;lt;li&amp;gt; Change the program to use execve instead of execvp. What is the difference between the two system calls? &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Linux signals is a simple form of IPC that is used for a variety of purposes.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: lower-alpha;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; What signal is generated when a program attempts to dereference an invalid pointer?  Give a simple program that generates such a signal.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; How would you send a signal to a process to have it pause execution?  Resume?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;What command can lower the priority of an already running process? &lt;br /&gt;
&amp;lt;li&amp;gt;Create &#039;&#039;run-program-reverse.c&#039;&#039; that is the same as run-program.c except that it interprets its arguments in reverse order.  Hence, &#039;&#039;./run-program-reverse /tmp -a /bin/ls&#039;&#039; will do the same as &#039;&#039;./run-program /bin/ls -a /tmp&#039;&#039;.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Create &#039;&#039;sanitize-env.c&#039;&#039; that does the same as run-program.c except it exec&#039;s the program with all environment variables stripped except the PATH and TERM variables (assuming they are defined).  Be sure to document how you tested your program.&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Part B==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li value=6&amp;gt; Create a simple program &#039;&#039;receive-usr1.c&#039;&#039; that uses sigaction in order to print &amp;quot;Ouch!&amp;quot; when you send the program the USR1 signal.  Note that your program should not consume much CPU time while waiting (i.e., do not poll for the signal).&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Create a simple program &#039;&#039;nocrash.c&#039;&#039; that accesses an invalid pointer but then prints &amp;quot;Oops, my mistake&amp;quot; rather than crash.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Create a program &#039;&#039;run-program-dots.c&#039;&#039; that works the same as run-program.c, except that it prints one dot every second while a given program runs.  Note that it should stop printing dots once the exec&#039;d program terminates.  Your solution should use the fork(), sleep(), execve(), and sigaction() calls.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; What is the difference between the PR and NI columns in the top command?  Explain in the context of running a CPU-intensive program.  (Hint: you should try running a CPU intensive program or two and observe these columns...)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Program Listings==&lt;br /&gt;
&lt;br /&gt;
 /* run-program.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int main( int argc, char *argv[], char *envp[] ) {&lt;br /&gt;
 &lt;br /&gt;
    if( argc &amp;lt; 2 ) {&lt;br /&gt;
        printf( &amp;quot;Insufficient arguments.\n&amp;quot; );&lt;br /&gt;
        return -1;&lt;br /&gt;
    }&lt;br /&gt;
    execvp( argv[1], argv + 1 );&lt;br /&gt;
    printf( &amp;quot;Is this line printed?\n&amp;quot; );&lt;br /&gt;
    return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=Answers=&lt;br /&gt;
==Part A==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt; &amp;lt;li&amp;gt;&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: lower-alpha;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;quot;is this line printed?&amp;quot; is not printed because execvp/execve replaces the program binary with a different one.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; execve( argv[1], argv + 1 , envp); &amp;lt;br&amp;gt;&lt;br /&gt;
execve requires an environment argument which may be different from the existing one, where as execvp runs the program using the current environment. Another difference is that execvp searches the PATH environment variable for folders containing binary files, if it can&#039;t find the file in the current working directory. &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt; &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; &amp;lt;ol style=&amp;quot;list-style-type: lower-alpha;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;SIGSEGV or SIGBUS is raised for invalid memory references. SIGBUS is low level device specific signal. The following program generates a SIGSEGV in my computer : int *a; *a = 10; &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; SIGSTOP for pausing, and SIGCONT for continuing. &lt;br /&gt;
You can use &amp;quot;kill -SIGWHATEVER pid&amp;quot; from the terminal, or from code. For ex :&lt;br /&gt;
 pause-and-cont.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; renice &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; run-program-reverse.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; sanitize-env.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
(Part B)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li value=6&amp;gt; Compile and run receive-usr1.c and issue &amp;quot;kill -SIGUSR1 pid&amp;quot; &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Use gdb to figure out what signal is thrown for invalid pointer accesses, then catch that. &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; run-program-dots.c &amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;NI is the nice value of a process, which can be changed by the user. It ranges from -20 to 19 and is used as an input to the scheduler. Depending on all other processes and available resources, the scheduler decides the priority of a process and displays its calculated value in the PR column. &lt;br /&gt;
&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Answers : Program Listings== &lt;br /&gt;
&lt;br /&gt;
 /* pause-and-cont.c */&lt;br /&gt;
 &lt;br /&gt;
 #include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include&amp;lt;signal.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int main(int argc, char * argv[], char *envp[])&lt;br /&gt;
 {&lt;br /&gt;
   int child = fork();&lt;br /&gt;
   if(child == 0)&lt;br /&gt;
   {   &lt;br /&gt;
     execvp(&amp;quot;/usr/bin/yes&amp;quot;,argv);&lt;br /&gt;
   }   &lt;br /&gt;
   else&lt;br /&gt;
   {   &lt;br /&gt;
     kill(child,SIGSTOP);&lt;br /&gt;
     sleep(4);&lt;br /&gt;
     kill(child,SIGCONT);&lt;br /&gt;
     sleep(4);&lt;br /&gt;
     kill(child,SIGKILL);&lt;br /&gt;
   }   &lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* run-program-reverse.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;errno.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;string.h&amp;gt;  &lt;br /&gt;
 &lt;br /&gt;
 int main( int argc, char **argv, char *envp[] ) {&lt;br /&gt;
         char **newenv;&lt;br /&gt;
         int i = 0;&lt;br /&gt;
   if( argc &amp;lt; 2 ) {&lt;br /&gt;
       printf( &amp;quot;Insufficient arguments.\n&amp;quot; );&lt;br /&gt;
       return -1;&lt;br /&gt;
   }&lt;br /&gt;
         newenv = (char**)malloc(argc*sizeof(char**));&lt;br /&gt;
         for(i = 0; i &amp;lt; argc; i++)&lt;br /&gt;
         {&lt;br /&gt;
                 newenv[i] = (char*)malloc(sizeof(argv[argc-i-1]));&lt;br /&gt;
                 strcpy(newenv[i],argv[argc-i-1]);&lt;br /&gt;
         }&lt;br /&gt;
         newenv[argc-1] = NULL;&lt;br /&gt;
         if(execvp(newenv[0],newenv + 1) == -1)&lt;br /&gt;
                 printf(&amp;quot;error! = %s&amp;quot;,strerror(errno));&lt;br /&gt;
   &lt;br /&gt;
         printf( &amp;quot;Is this line printed?\n&amp;quot; );&lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* sanitize-env.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;  &lt;br /&gt;
 &lt;br /&gt;
 int main( int argc, char *argv[], char *envp[] ) {&lt;br /&gt;
         extern char **environ;&lt;br /&gt;
         int i = 0;   &lt;br /&gt;
 &lt;br /&gt;
         char *path = getenv(&amp;quot;PATH&amp;quot;);&lt;br /&gt;
         char *term = getenv(&amp;quot;TERM&amp;quot;);&lt;br /&gt;
         if(clearenv())&lt;br /&gt;
                 exit(-1);&lt;br /&gt;
         setenv(&amp;quot;PATH&amp;quot;,path,1);&lt;br /&gt;
         setenv(&amp;quot;TERM&amp;quot;,term,1);  &lt;br /&gt;
 &lt;br /&gt;
         //Check now.&lt;br /&gt;
         while(environ[i] != NULL) {&lt;br /&gt;
                 printf(&amp;quot;environ[%d] = %s\n&amp;quot;,i,environ[i++]); &lt;br /&gt;
         } &lt;br /&gt;
 &lt;br /&gt;
   if( argc &amp;lt; 2 ) {&lt;br /&gt;
       printf( &amp;quot;Insufficient arguments.\n&amp;quot; );&lt;br /&gt;
       return -1;&lt;br /&gt;
   }&lt;br /&gt;
   execvp(argv[1], argv + 1);&lt;br /&gt;
   printf( &amp;quot;Is this line printed?\n&amp;quot; );&lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* receive-usr1.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;signal.h&amp;gt; &lt;br /&gt;
 &lt;br /&gt;
 int child;&lt;br /&gt;
 void sig_handler(int signum)&lt;br /&gt;
 {&lt;br /&gt;
          if(signum == SIGUSR1)&lt;br /&gt;
                 printf(&amp;quot;Ouch!.\n&amp;quot;);&lt;br /&gt;
    fflush(stdout);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   struct sigaction act;&lt;br /&gt;
         sigset_t set;&lt;br /&gt;
   &lt;br /&gt;
   act.sa_handler = sig_handler;&lt;br /&gt;
   act.sa_flags = 0;&lt;br /&gt;
   sigemptyset(&amp;amp;act.sa_mask); &lt;br /&gt;
 &lt;br /&gt;
   sigaction(SIGUSR1, &amp;amp;act, NULL);&lt;br /&gt;
   sigfillset(&amp;amp;set);//Block on all signals&lt;br /&gt;
   sigdelset(&amp;amp;set,SIGUSR1);//except this one&lt;br /&gt;
   sigsuspend(&amp;amp;set);//now wait.&lt;br /&gt;
   return 0;  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* nocrash.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;signal.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int child;&lt;br /&gt;
 void sig_handler(int signum)&lt;br /&gt;
 {&lt;br /&gt;
         if(signum == SIGSEGV)&lt;br /&gt;
                printf(&amp;quot;Oops, my mistake.\n&amp;quot;);&lt;br /&gt;
         fflush(stdout);&lt;br /&gt;
         exit(1);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
        int *a;&lt;br /&gt;
   struct sigaction act;&lt;br /&gt;
  &lt;br /&gt;
  act.sa_handler = sig_handler;&lt;br /&gt;
  act.sa_flags = 0;&lt;br /&gt;
  sigemptyset(&amp;amp;act.sa_mask);&lt;br /&gt;
 &lt;br /&gt;
  sigaction(SIGSEGV, &amp;amp;act, NULL);&lt;br /&gt;
  sleep(1);&lt;br /&gt;
  *a = 10; // Make a mistake. &lt;br /&gt;
  return 0;  &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 /* run-program-dots.c */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;signal.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int child;&lt;br /&gt;
 void sigchild_handler(int signum)&lt;br /&gt;
 {&lt;br /&gt;
         exit(0); &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   struct sigaction act;&lt;br /&gt;
  &lt;br /&gt;
  act.sa_handler = sigchild_handler;&lt;br /&gt;
  act.sa_flags = 0;&lt;br /&gt;
  sigemptyset(&amp;amp;act.sa_mask);&lt;br /&gt;
 &lt;br /&gt;
  child = fork();&lt;br /&gt;
  &lt;br /&gt;
  if(child == 0) {&lt;br /&gt;
         execve(&amp;quot;program&amp;quot;,NULL,NULL);&lt;br /&gt;
    return 0;&lt;br /&gt;
  }&lt;br /&gt;
  else {&lt;br /&gt;
    sigaction(SIGCHLD, &amp;amp;act, NULL); &lt;br /&gt;
         while(1)&lt;br /&gt;
         {&lt;br /&gt;
                printf(&amp;quot;.&amp;quot;);&lt;br /&gt;
                fflush(stdout);&lt;br /&gt;
                sleep(1);&lt;br /&gt;
         } &lt;br /&gt;
    return 0;  &lt;br /&gt;
  }   &lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 where my program.c is &lt;br /&gt;
 #include&amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
         sleep(8);&lt;br /&gt;
         return 0;&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Saran</name></author>
	</entry>
</feed>