<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://homeostasis.scs.carleton.ca/wiki/index.php?action=history&amp;feed=atom&amp;title=Operating_Systems_2019W%3A_Tutorial_3</id>
	<title>Operating Systems 2019W: Tutorial 3 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://homeostasis.scs.carleton.ca/wiki/index.php?action=history&amp;feed=atom&amp;title=Operating_Systems_2019W%3A_Tutorial_3"/>
	<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=Operating_Systems_2019W:_Tutorial_3&amp;action=history"/>
	<updated>2026-04-05T22:56:50Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.42.1</generator>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=Operating_Systems_2019W:_Tutorial_3&amp;diff=22133&amp;oldid=prev</id>
		<title>Soma: Created page with &quot;In this tutorial you will be experimenting with and extending [http://homeostasis.scs.carleton.ca/~soma/os-2017f/code/tut3/3000test.c 3000test.c] (listed below).  ==Tasks/Ques...&quot;</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=Operating_Systems_2019W:_Tutorial_3&amp;diff=22133&amp;oldid=prev"/>
		<updated>2019-01-21T20:02:30Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;In this tutorial you will be experimenting with and extending [http://homeostasis.scs.carleton.ca/~soma/os-2017f/code/tut3/3000test.c 3000test.c] (listed below).  ==Tasks/Ques...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;In this tutorial you will be experimenting with and extending [http://homeostasis.scs.carleton.ca/~soma/os-2017f/code/tut3/3000test.c 3000test.c] (listed below).&lt;br /&gt;
&lt;br /&gt;
==Tasks/Questions==&lt;br /&gt;
&lt;br /&gt;
# Compile and run [http://homeostasis.scs.carleton.ca/~soma/os-2017f/code/tut3/3000test.c 3000test.c].  It takes a filename as an argument and reports information on the file.  Try giving it the following and see what it reports:&lt;br /&gt;
#* a regular text file that exists&lt;br /&gt;
#* a directory&lt;br /&gt;
#* a symbolic link&lt;br /&gt;
#* a device file&lt;br /&gt;
# Change 3000test to use lstat rather than stat.  How does its behavior change?&lt;br /&gt;
# Modify 3000test so when it is given a symbolic link it reports the name of the target.  Use readlink(2).&lt;br /&gt;
# Are there files or directories that you cannot run 3000test on?  Can you configure file/directory permissions so as to make something inaccessible to 3000test?&lt;br /&gt;
# How does the memory use of 3000test change as it runs?  You may want to add calls to sleep(3) so you can observe its memory usage.  You can create a 1 GB file of random data with the command &amp;lt;tt&amp;gt;dd if=/dev/urandom of=test bs=1024 count=1000000&amp;lt;/tt&amp;gt;.&lt;br /&gt;
# Create a program 3000compare.c based on 3000test that compares the contents of two files and says whether or not they differ.  &lt;br /&gt;
#* If given symbolic links it should report on where they point and only say they are equal if they refer to the same file.&lt;br /&gt;
#* If given two device files, it should say that they are equal if they are both the same kind of device file and have the same major and minor numbers.&lt;br /&gt;
#* If given two hard links to the same file, it should say that the files are identical because they refer to the same inode.&lt;br /&gt;
#* Other kinds of files (directories, pipes) should not be compared.  Instead, it should report on the type of each file.&lt;br /&gt;
&lt;br /&gt;
==Code==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot; line&amp;gt;&lt;br /&gt;
/* 3000test.c */&lt;br /&gt;
/* v1 Oct. 1, 2017 */&lt;br /&gt;
/* Licenced under the GPLv3, copyright Anil Somayaji */&lt;br /&gt;
/* You really shouldn&amp;#039;t be incorporating parts of this in any other code,&lt;br /&gt;
   it is meant for teaching, not production */&lt;br /&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;unistd.h&amp;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;sys/mman.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.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;
void report_error(char *error)&lt;br /&gt;
{&lt;br /&gt;
        fprintf(stderr, &amp;quot;Error: %s\n&amp;quot;, error);&lt;br /&gt;
&lt;br /&gt;
        exit(-1);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[])&lt;br /&gt;
{&lt;br /&gt;
        struct stat statbuf;&lt;br /&gt;
        char *fn;&lt;br /&gt;
        int fd;&lt;br /&gt;
        size_t len, i, count;&lt;br /&gt;
        &lt;br /&gt;
        char *data;&lt;br /&gt;
&lt;br /&gt;
        if (argc &amp;lt; 2) {&lt;br /&gt;
                if (argc &amp;lt; 1) {&lt;br /&gt;
                        report_error(&amp;quot;no command line&amp;quot;);&lt;br /&gt;
                        fprintf(stderr, &amp;quot;Usage: %s &amp;lt;file&amp;gt;\n&amp;quot;, argv[0]); &lt;br /&gt;
                } else {&lt;br /&gt;
                        report_error(&amp;quot;Not enough arguments&amp;quot;);&lt;br /&gt;
                        fprintf(stderr, &amp;quot;Usage: %s &amp;lt;file&amp;gt;\n&amp;quot;, argv[0]); &lt;br /&gt;
                }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        fn = argv[1];&lt;br /&gt;
&lt;br /&gt;
        if (stat(fn, &amp;amp;statbuf)) {&lt;br /&gt;
                report_error(strerror(errno));&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        len = statbuf.st_size;&lt;br /&gt;
        printf(&amp;quot;File %s: \n&amp;quot;, fn);&lt;br /&gt;
        printf(&amp;quot;   inode %ld\n&amp;quot;, statbuf.st_ino);&lt;br /&gt;
        printf(&amp;quot;  length %ld\n&amp;quot;, len);        &lt;br /&gt;
&lt;br /&gt;
        if (S_ISREG(statbuf.st_mode)) {&lt;br /&gt;
                fd = open(fn, O_RDONLY);&lt;br /&gt;
                if (fd == -1) {&lt;br /&gt;
                        report_error(strerror(errno));&lt;br /&gt;
                }&lt;br /&gt;
                data = (char *) mmap(NULL, len,&lt;br /&gt;
                                     PROT_READ, MAP_SHARED, fd, 0);&lt;br /&gt;
                if (data == MAP_FAILED) {&lt;br /&gt;
                        report_error(strerror(errno));&lt;br /&gt;
                }&lt;br /&gt;
&lt;br /&gt;
                count = 0;&lt;br /&gt;
                for (i=0; i&amp;lt;len; i++) {&lt;br /&gt;
                        if (data[i] == &amp;#039;a&amp;#039;) {&lt;br /&gt;
                                count++;&lt;br /&gt;
                        }&lt;br /&gt;
                }&lt;br /&gt;
&lt;br /&gt;
                printf(&amp;quot; a count %ld\n&amp;quot;, count);&lt;br /&gt;
&lt;br /&gt;
                if (munmap(data, len) == -1) {&lt;br /&gt;
                        report_error(strerror(errno));                        &lt;br /&gt;
                }&lt;br /&gt;
                close(fd);&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Soma</name></author>
	</entry>
</feed>