Oppgave 1
In this assignment it is presumed that we are running bash on a linux machine.
a) Type a command that would move the file to the directory you are currently in
b) Type a command that shows number of lines in the file
c) Given the following
cube$ cat IQ.txt 134 toreo 78 haugerud 120 sigmunds 112 mark cube$ sort IQ.txt | head -n 2What is the output from the
-command?
d) Explain briefly the error message from the command :
$ whoami hh $ ls -l d--x------ 2 hh hh 4096 Nov 28 12:16 dir -rw-r--r-- 1 hh hh 0 Nov 28 12:16 fil $ cp fil dir cp: cannot create regular file `dir/fil': Permission denied
e) The command sequence is continued by
$ chmod 200 dir $ ls -l d-w------- 2 hh hh 4096 Nov 28 12:16 dir -rw-r--r-- 1 hh hh 0 Nov 28 12:16 fil $ cp fil dir cp: cannot stat `dir/fil': Permission deniedExplain briefly the error message from the command
.
f) In continuation from the example in the last assignment, give a command that will set the minimum of rights the directory must have in order for
to be executed without errors.
g) Examine the following example:
$ ls -l drwx------ 2 hh hh 4096 Nov 28 12:24 dir -rw-r--r-- 1 hh hh 0 Nov 28 12:16 fil -rw-r--r-- 1 hh hh 0 Nov 28 13:01 fil.txt $ cp fil dir $ cp -p fil.txt dir $ ls -l dir -rw-r--r-- 1 hh hh 0 Nov 28 13:03 fil -rw-r--r-- 1 hh hh 0 Nov 28 13:01 fil.txtExplain briefly from this what is the difference between the commands
and
.
Oppgave 2
It is advicable to do the last 4 part-assignments from assignment 1 before you do this assignemnt.
It is often useful to keep older versions of files, for instance logfiles or when developing a program. The problem however is that the fisk might be filled up unless the oldest versions are removed. In this assignment you are going to create a bash-script called
which will copy a file (given as argument 1) to a directory (given as argument 2). If there is already a file with this same name in the directory (an older version) the program should make a copy of this file with versionnumer 1 and if there are more versions they should all be rotated so that every file gets a higher number. Up to 10 old versions of a file should be stored. Some examples will better show how the script should work if you do
$ rotate fil.txt dir
| The following exists in dir | After rotate it should be |
| No file with name fil.txt | fil.txt |
| fil.txt | fil.txt (new) and fil.txt.1 (was fil.txt) |
| fil.txt, fil.txt.1 | fil.txt (new) and fil.txt.1 (was fil.txt), fil.txt.2 (was fil.txt.1) |
| fil.txt, fil.txt.1, fil.txt.2 | fil.txt, fil.txt.1, fil.txt.2, fil.txt.3 |
| fil.txt, fil.txt.1, ... ,fil.txt.9 | fil.txt, fil.txt.1, ...,fil.txt.10 |
| fil.txt, fil.txt.1, ... ,fil.txt.10 | fil.txt, fil.txt.1, ...,fil.txt.10 |
should not be kept, so that the maximum of stored files is 10. The file argument to this script should be given with the path in front as in
$ rotate /tmp/fil.txt dirThe script should exit with an apropriate error message if
Oppgave 3
a) One of the advantages with the new 64-bit cpus and Itanium is that they are able to adress more RAM. How many bytes of internal memory is the maximum possible to adress with a 32-bit registry as most of todays 32-bit cpus have?
b) You are running the program JBuilder and a few other programs which are using alot of internal memory. When these programs are running you experience after a while that they are running more and more slowly and you can hear that the harddrive is getting used alot even though the programs you are using is not writing or reading from/to the harddrive. What is the explanation for this? Explain briefly.
c) Assume that you are going to make a CPU-intensive program which will perform big mathematical calculations. You are thinking about using the languages bash, C++, Java and Perl. Order these languages with 1 as the fastest through 4 which is the slowest.
d) Assume that you have a Linuc and a Windows machine which has the same type of x86 hardware and that you have a Sun Solaris machine which runs on sparc. All the machines has Java installed. There is a mark in the first column of the following table because a Java program compiled on a Windows machine can run on a Windows machine. Fill out the rest of the table as you think it should be.
| Java program compiled on Windows | Linux | Solaris | |
| Runs on Windows | X | ||
| Runs on Linux | |||
| Runs on Solaris |
e) Assume that you have the same machines as in the last part-assignemtn, except now all of them has a C-compiler.
Since a C-program compiled on a Windows machine will run on a Windows machine there is also here a mark in the first column. Fill out the rest.
| C-program compiled on | Windows | Linux | Solaris |
| Runs on Windows | X | ||
| Runs on Linux | |||
| Runs on Solaris |
f) A program is runnning on a multitasking OS on a machine with only one CPU. In the code of the program there is a loop that never ends, how does the machine let other process use the CPU? Explain briefly.
g) When a program is going to read from a file, the modebit has to be changed to superusermode so that kernel code can be executed. Assume that the program is doing this with the following code:
switchToSuperuserModus JMP readfilewhere the 2nd step is what jumps to the kernel code. What is the problem with this solution and how is the problem solved? Explain briefly.
Oppgave 4
a) Make a Perl subroutine that takes two arguments. The first beeing a filename and the second a text string. The subroutine should open the file, add the text at the end of the file and then close the file again. If it is not possible to open the file for writing an error message should be written to SDERR. You have to use .
b) In this part you are going to write the client part of a Perl chat program which is going to be used between two Unix hosts on the Internet. The code for the server part is named and looks like this:
#! /bin/perl
use strict;
use IO::Socket::INET;
my $serverPort = 9002;
my $socket = IO::Socket::INET->new(LocalPort => $serverPort,
Reuse => 1,
Listen => 5)
or die "Cannot start the chat-server on port $serverPort!\n";
while (my $socketConnection = $socket->accept())
{
while(my $line = <$socketConnection>)
{
addLine("/tmp/chat",$line);
}
$socketConnection->close();
}
The code for the subroutine is the code from part a) and is meant to be included at the end in the server. Both the hosts used for the chat is going to run one of these servers. A typical chat between the user on host and the user on host could look like this for the user on the host :
rex$ ./server& rex$ ./client cube haugerud@rex> Hey! haugerud@rex> Are you there? haugerud@rex> OK. i have to go. haugerud@rex> With quit haugerud@rex> quit rex$and for
on host :
cube$ ./server& cube$ ./client rex os@cube> Yes... os@cube> OK, how do you end this thing? os@cube> OK os@cube> quit cube$Both users will get a xterm window that displays messages as they are sent when they start the
:
haugerud@rex> Hey! haugerud@rex> Are you there? os@cube> Yes... haugerud@rex> OK. I have to go. os@cube> OK, how do you end this thing? haugerud@rex> With quit os@cube> OKThis is done with the program
(which you are going to write) by opening a socket to port 9002 on the server. The name of the server that is going to connect to is supposed to be given as the first argument. After that the
is going to send each line that the user is typing through the socket to the server. In addition the client is writing every typed line to the file with the subrouting from part a). Do not write again. This will make the file on both hosts contain all typed lines by both clients. The two chat'ers sees everything that is written by running the command
tail -f /tmp/chatin a terminal window. Then each line is shown as they are added to the file
.
the script should do the following::
to show everything that is written. But the program must first execute and then execute the command in child, otherwise the client will hang on this command
with
and no other words except blank spaces, TAB or equivalent either before or after
-THE END-
Oppgave 1
a)
b) or which will give the number only
c)
112 mark 120 sigmundsbecause sort defaults to sorting alphabetically and then 11 is before 78. On the other hand,
would give
78 haugerud 112 mark
d)
To be able to write to a directory (or a file), the bit needs to be set
e) To be able to copy anything to a directory it has to be accible and then the x bit needs to be set
f) would set both x and w bits, both has to be set. r is only necessary to be able to read from files in the directory
g)
A new timestamp will be set on the file by using ,while keeps the original timestamp (and other atributes). stands for preserve.
Oppgave 2
#! /bin/bash
if [ $# -ne 2 ]; then
echo "Syntax: $0 file directory"
exit
fi
times=10
fil=$1
dir=$2
if [ ! -f $fil ]; then
echo "$fil is not a file"
exit
fi
if [ ! -r $fil ]; then
echo "Can not read file $fil"
exit
fi
if [ ! -d $dir ]; then
echo "$dir is not a directory"
exit
fi
if [ ! -w $dir -o ! -x $dir ]; then
echo "Can not write to directory $dir"
exit
fi
navn=`basename $fil`
if [ -f $dir/$navn ]; then
(( i = $times - 1 ))
while [ $i -gt 0 ]
do
if [ -f $dir/$navn.$i ]; then
(( ipp = $i + 1 ))
cp -p $dir/$navn.$i $dir/$navn.$ipp
fi
(( i = $i - 1 ))
done
cp -p $dir/$navn $dir/$navn.1
elif [ -e $dir/$navn ]; then
echo "$dir/$navn excists, but is not a file"
exit
fi
cp -p $fil $dir
Oppgave 3
a)
byte (= 4294967296 = 4 x 1073741824 = 4 x 1G) = 4 Giga byte
b) When the physical memory (RAM) is depleted, the processes starts to use virtual memory from the harddrive (swapping). Reading to and from disk is much slower than reading to/from memory
c)
d) Java is platform independant and Java byte-code can run on all platforms no matter what platform it is compiled on.
| Java-program compiled on | Windows | Linux | Solaris |
| runs on Windows | X | X | X |
| runs on Linux | X | X | X |
| runs on Solaris | X | X | X |
e) A compiled C-program is machine code which is only executable on the architecture which it is compiled for. THe machine code is communicating with the OS and so it is also OS-dependant.
| C-program compiled on | Windows | Linux | Solaris |
| runs on Windows | X | ||
| Runs on Linux | X | ||
| Runs on Solaris | X |
f) Without the help from the hardware a process like this can not be forced to end. A hardware timer sends out interrupts with fixed intervals which makes the next instruction perform a jump to kernel code which gives the OS the control so that it can evaluate which process should be allowed access to the CPU next.
g) If any user program could switch to superusermode it would be able to do whatever instruction, manipulate the memory and take control of the CPU. Modern OSs uses hardware instructions (trap on x86) which switches to superusermode and jumps to kernel code in the same operation.
Oppgave 4
a)
sub addLine()
{
my ($fil,$line) = @_;
if(open(FIL,">>$fil"))
{
print FIL $line;
close(FIL);
}
else
{
print STDERR "Cannot write to $fil\n";
}
}
b)
#! /bin/perl
use strict;
use IO::Socket::INET;
my $file = "/tmp/chat";
my $host = $ARGV[0];
$host or die "Give host name!";
my $port = 9002; # Post on the server which the client is connecting to
my $socket = IO::Socket::INET->new("$host:$port")
or die "Cant connect to host $host on port $port\n";
my $pid = fork();
if(! $pid)
{ # The next to lines are not mandatory but usefull
$socket->close(); # THe socket is not beeing used in this process
`touch $file`; # If it doesnt exist, otherwise we get an error
`xterm -e tail -f $file`;
exit;
}
my $name = `whoami`;
my $host = `hostname`;
chomp($name);
chomp($host);
my $prompt = "$name\@$host> ";
print "$prompt";
while (my $line = <STDIN>)
{
last if($line =~ /^\s*quit\s*$/);
$socket->send("$prompt$line");
addLine($file,"$prompt$line");
print "$prompt";
}
$socket->close(); # Close connection