Sample Midterm Answer CC-BY-NC

Maintainer: admin

1Question 1

  • A unix session is the period after user authentication and before the user logs out in which the user is allowed to create processes and kill them inside the same session. It is essentially a group of process.
  • A shell is an environment which allows the user to interact with the system. It is used to allow the user to run processes and use system resources. It is invoked upon the creation of a session.
  • The internet is an array of layers which work together to help a message (packet/datagram) reach its desired destination as best as possible. For data to transmit over the internet, it would have to use a specific application which sends packets to a server. The packet would have to follow a specific protocol like TCP or UDP. The packets would then be sent to a specific address like an IPv4 address. At the server, it would need to listen for that packet at a specific port and read it depending on the type it is. The hardware involved includes a router which receives the data sent from your computer's network card, the router then passes the data to the modem, which sends the data to your ISP, which ultimately sends out the data to its next destination. TL;DR: it is a series of tubes.

2Question 2

2.1Part 1

  • Create x and instantiate it with the value of 10, create y and instantiate it with the value of 2, create z.
  • Increment the value of x, then return the incremented value of x, then subtract the value of y and multiply everything by 2, then assign the value to z. Then decrement the value of y.

2.2Part 2

  • Create x and instantiate it with the value of 5, create y and instantiate it with the value of 2.
  • Evaluate x * 2, and shift it 2 bits to the right(divide by 4 then flooring the result). If the value is non-zero, print "Yes\n".

2.3Part 3

  • Create p and assign it the memory address of a double on the stack. Create x which is a double.
  • Assign x the double value located at memory address of a place which is 3 bytes ahead of p.

2.44

  • Create s and assign it the memory address of the character 'm' and the subsequent characters are next it in memory on the stack.
  • Create the variables a,b,c,d,e and assign each the memory address of 10 bytes on the stack.
  • Call sscanf on the arguments, which will give a the value of "my", b the value of "name", c the value of "is" and d the value of "Bob", then sscanf will return the value 4. Calling printf with the arguments "%s\n" and 4 will tell it to print the string starting at 0x00000004 (which will probably segfault because the process is not allowed to access data located at that memory address).

3Question 3

3.1Part 1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/bin/bash

function factorial  {
    N=$1
    A=$1
    while [ $N -gt 0 ]
    do
        A=`expr $A '*' $N`
        N=`expr $N - 1`
    done
    echo $A
}

function usage ()
{
        echo "Usage:"
        echo "        myprog.sh [--help|--version] n"
        echo "Where n is the number for factorial"
}

case $1 in
       --help|-h)
               usage
       ;;
       --version|-v)
               echo "myprog.sh version 0.0.2"
              exit 0
       ;;
       -)
               echo "Error: no such option $1"
               usage
               exit 1
       ;;
esac

factorial $1

3.2Part 2

Given a list of files, it will copy the first file with the name of FIRSTFILENAME.BAK-(ith File's name), if the file exists already it will not
copy it.

4Question 4

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <stdio.h>
#include <stdlib.h>

int a[10][10];
int b[10][10];

void FillMatrix(int x, int y, int m[10][10]);
void MultMatrix(int a[10][10], int b[10][10], int c[10][10]);
int main(){
    printf("Please enter the dimensions of the first array in the format of 'NxM':");
    int ax,ay,bx,by;
    scanf("%dx%d",&ax,&ay);
    printf("Please enter the dimensions of the second array in the format of 'NxM':");
    scanf("%dx%d",&bx,&by);
    if(ax!=by || ax > 10 || ay > 10 || bx > 10 || by >10){
        printf("Invalid size");
        return 1;
    }
    printf("Please fill in the first matrix\n");
    FillMatrix(ax,ay,a);
    printf("Please fill in the second matrix\n");
    FillMatrix(bx,by,b);
    int c[10][10];
    MultMatrix(a,b,c);
    int i,j;
    for(i=0;i<ay;i++){
        for(j=0;j<bx;j++){
            printf("%d\t",c[j][i]);
        }
        printf("\n");
    }
}
void MultMatrix(int a[10][10], int b[10][10],int c[10][10]){
    int i,j;
    for(i =0;i<10;i++){
        for(j=0;j<10;j++){
            int entrySum = 0;
            int k;
            for(k=0;k<10;k++){
                entrySum += a[k][i] * b[j][k];
            }
            c[j][i] = entrySum;
        }
    }
}
void FillMatrix(int x, int y, int m[10][10]){
    int i,j;
    for(i =0;i<y;i++){
        for(j=0;j<x;j++){
            printf("Enter the entry at (%d,%d):",i,j);
            scanf("%d",&m[j][i]);
        }
    }
}