Rabbit House

時間の中 泳ぎ続ける

@leo9年前

2017-01-12
19:37
Tech

Linux OS Final

1. Linux Basic Commands (5 marks)

Make rational utilization of instruction, completing the following tasks:

(1) Print current time information of system

lsz@Test:~$ date
Thu Jan 12 01:35:05 EST 2017

(2) Display processor architecture and the kernel version

lsz@Test:~$ uname -a
Linux Test 2.6.32-042stab112.15 #1 SMP Tue Oct 20 17:22:56 MSK 2015 x86_64 x86_64 x86_64 GNU/Linux

(3) Reboot the system after twenty minutes and inform all users logged on so that they can save their work

root@Test:~# shutdown -r +20 "The system will reboot after 20 minutes!"

2. Crontab (10 marks)

A system administrator may have to do some repetitive work every day, while crontab is a very useful tool, you should answer following questions.

Complete the following tasks with the tool of crontab:

(1) Remove all the subdirectories and files in “/cdf” directory at 2:10 a.m every day

(2) If Sunday, copy all the directories and files in /user/backup to /tmp at 0:0 a.m

root@Test:~# vim task.sh
10 2 * * * rm -rf /cdf/*
0 0 * * 6 copy -r /user/backup /tmp
root@Test:~# crontab task.sh

3. Shell Programming (10 marks)

a1=0
a2=0
a3=0
sum=0
while read def num rel
do 
a3=$(($a3+1))
if [ "$rel" == "1" ];
then a2=$(($bb+1))
     a1=$a2
     tt=$(echo "scale=5;$a1/$k"|bc -l)         
     sum=$(echo "scale=5;$sum+$tt"|bc -l)
fi
done < result.txt                                             
AP=$(echo "scale=5;$sum/$a2"|bc -l)   
echo "a2=$a2 sum=$sum AP=$AP"

4. SSH Login (15 marks)

Write one method of non-interactive ssh connection between two linux servers.

Server A IP : 10.193.251.191

Server B IP : 10.193.251.192

The two linux servers both have root users and you can take root users as example.

Generate a pair of keys in Server A (in root)

root@Test:~# ssh-keygen -t rsa

Login to Server B, make directory .ssh

lsz@Test:~# mkdir .ssh
lsz@Test:~# chmod 700 .ssh

Copy public key to B from A

root@Test:~# scp .ssh/id_rsa.pub root@10.193.251.192:~/.ssh/authorized_keys

5. Shell Programming to Compute (15 marks)

0 0 1
0 100 1
0 101 0
0 102 0
0 103 1
0 104 0
0 105 1
0 106 1
0 107 1
0 108 0
0 109 0
0 10 0
0 110 1
…

Above is the top 1000 result from an image retrieval system. The first column is default value, and the second and third column indicate the rank and the relevance of the image. For example, (0 100 1) means that the 100th image is relevant. Please write shell program to compute the AP(average precision).

Rq is the number of relevant images regarding the instance topic q. rel (k) is an indicator function of relevance and irrelevance, and if the k th ranked video is relevant to q, rel (k) is 1. On the other hand, if the k th ranked video is irrelevant to q, rel (k) is 0. c(k) is the accumulated number of correct videos retrieved upper k th rank.

a1=0
a2=0
a3=0
sum=0
while read def num rel
do 
a3=$(($a3+1))
if [ "$rel" == "1" ];
then a2=$(($bb+1))
     a1=$a2
     tt=$(echo "scale=5;$a1/$k"|bc -l)         
     sum=$(echo "scale=5;$sum+$tt"|bc -l)
fi
done < result.txt                                             
AP=$(echo "scale=5;$sum/$a2"|bc -l)   
echo "a2=$a2 sum=$sum AP=$AP"

7. gcc

Write an ANSI C /C++ code. Use a Makefile to build it (including compile and link) into binary file. The function of this binary file should be: Given two binary strings, return their sum (also a binary string). or example, a = “11” b = “1” Return c = “100”.

You should give:

1. Makefile (hint: four files, main.cpp,function.cpp,function.h, makefile).

2. All C/C++ source code. main.cpp only passes parameter of input output and use the command line to pass the parameters to the main() .

3. The whole procedure of how you compile and link C code.

8

// main.cpp
#include <iostream>
#include <string>
#include <math.h>
using namespace std;

int b2d(string b)
{
    int d=0;
    int len=b.size();
    for(int i=len-1;i>=0;i--)
    {
        int f=0;
        if(b[i]=='1') f=1;
        d=d+f*(pow(2,len-i-1));
    }
    return d;
}

int d2b(int d)
{
    int s[30];
    int b=0;
    for (int i=0;i<30;i++) s[i]=pow(2,i-1);
    int digit=30;
    while (d>0)
    {
        for (int i=digit-1;i>=0;i--) 
        {
            if(d>=s[i]) 
            {
                digit=i+1;
                b=b+pow(10,i-1);
                d=d-s[i];
            }
        }
    }
    return b;
}

int main()
{
    string m,n;
    cout<<"Please input m:";cin>>m;
    cout<<"Please input n:";cin>>n;
    cout<<d2b(b2d(m)+b2d(n));
    return 0;
}
CC=gcc
OBJS=main.o reverse.o
SRCS=main.cpp reverse.c

main:main.o reverse.o
    $(CC) -o main $(SRCS)
clean:
    rm -rf main $(OBJS)

8. NFS Mount

一、服务器端
编辑/etc/exports
/home/data1 10.193.251.192.(ro,all_squash)
/home/data2 10.193.251.192.
(rw,sync,root_squash)
/home/data3 10.193.251.192.*(rw,async,no_root_squash)
使用以下命令启动NFS协议:
/etc/rc.d/init.d/portmap start
/etc/rc.d/init.d/nfs start
二、客户端的操作
本地挂载点为/mnt/nfs
mount -t nfs 10.193.251.191:/home/data1 /mnt/nfs
mount -t nfs 10.193.251.191:/home/data2 /mnt/nfs
mount -t nfs 10.193.251.191:/home/data3 /mnt/nfs

Linux OS Final