Chapter II:
Two-point question:
20135327 Guo Hao: 3
20135329 Li Haiyin: 1
2.67 give you a task, write a procedure int_size_is_32 (), when running on a machine with an int that is 32 bits, the program generates 1, while the other case produces 0. The sizeof operator is not allowed. Here's what to try at the beginning:
/*the following code does not run properly on some machies*/
int bad_int_size_is_32 () {
int SET_MSB = 1<<31;
int BEYOND_MSB = 1<<32;
return SET_MSB &&! BEYOND_MSB;
}
When compiling and running on a 32-bit machine such as Sun SPARC, this process returns 0. The compiler information for the following main gives us an indication of the problem.
Warning:left shift count >= width of type
A: The left shift number is greater than or equal to the int length, the 32-bit machine moves up to the left 31 bits, and the 16-bit machine moves up to 15 bits left.
B: Use two left shift to achieve <<32:
int set_msb=1<<31;
int beyond_msb=set_msb<<1;
C: Use three left shift to achieve <<31 and <<32:
int temp=1<<15;
temp<<=15;
int set_msb=temp<<1;
int beyond_msb=temp<<2;
Three-part question:
20135327 Guo Hao: 4
20135329 Li Haiyin: 2
2.69 write the code for a function with the following prototype
/*
*do rotating right SHITF. Assume 0 <= N < w
*examples when x = 0x12345678 and w = 32:
* n = 4-0x81234567, n = 0x45678123
*/
unsigned rotate_right (unsigned x, int n);
A: The main question is to write a loop right-shift function, the specific code is as follows:
#include <stdio.h>
unsigned rotate_right (unsigned x, int n)
{
unsigned A;
A = x<< (32-n);
printf ("%x\n", x>>n|a);
}
int main ()
{
unsigned x;
int n;
printf ("Please enter 32 hexadecimal digits");
scanf ("%x", &x);
printf ("Please enter the right shift number (less than 32 is greater than or equal to 0)");
scanf ("%d", &n);
if (n>32| | n<=0)
{
printf ("Right shift number less than 32 is greater than or equal to 0!!! ");
return 0;
}
Rotate_right (X,n);
return 0;
}
Here I default w=32 because if the hexadecimal number within 32 bits must change the value of W or the system defaults to 32-bit hexadecimal, if you need to modify just change 32 to W.
As the test result:
Try other numbers:
The sixth chapter: 6.30
Two-point question:
20135327 Guo Hao: 2.5
20135329 Li Haiyin: 1.5
A:
By test instructions b=2 s=2 so two low is the block offset, then the two-bit group index, the remaining bits are marked:
B:
1. For address 0x409, block offset (CO): 0x1 Group (CI): 0x2 cache Marker (CT): 0x40 visible The valid bit of this tag is 0, so it is not hit.
2. For address 0x40a, block offset (CO): 0x2 Group (CI): 0x2 cache Marker (CT): 0x40 The valid bit of the marker is 0, so it is not hit.
3. For address 0x833, block offset (CO): 0x3 Group (CI): 0x0 cache Marker (CT): 0x83 visible The valid bit of this token is 1, the cached marker bit matches the mark bit in the address, so the hit read value is 0xd0.
6.37
Two-point question:
20135327 Guo Hao: 2.5
20135329 Li Haiyin: 1.5
A
The non-hit rate is 100%, the direct mapping is only one cache row per group, the block size is 32 bytes, which means that 8 int values can be stored. Arrays are stored in row-first order, and the size of the array row is 256*4=1024, so the cache is sufficient to store only one row of the array. So thecache for each element of x[0] and x[1] is the same block. Therefore, each request is loaded, evicted, and replaced. The non-hit rate is 100%.
B
No hit is 1/8, only when new rows are loaded, the cache is large enough to store the entire array so only cold misses, and the block size is 32 bytes, which means that 8 int values can be stored so that each time it loads x[0][i]~x[0][i+7] A total of 8 values into the cache group, where only x[0][i] is not hit, so no hit rate is 1/8.
C
Do not hit 1/8, the cache is only 1024 bytes, not enough to store the array, the first row of the array and the second row are loaded into the cache the same group of different rows, will not conflict, so the last is equivalent to only a cold miss, the equivalent of only one per 8 data is not hit, so do not hit 1/8.
D
No, the block size is constant, because the probability of cold misses cannot be reduced.
E
Can, because the size of the block increases, the frequency of cold misses is reduced.
6.38 2 min
function N=64 n=60
SumA 0.25 0.25
Sumb 1 0.25
SUMC 0.25 0.25
N = 64
Suma only cold not hit, no hit rate for 0.25;sumb every time will conflict, do not hit the 1;SUMC have cold life, no hit rate of 0.25.
N = 60
Suma only cold not hit, do not hit the 0.25;sumb to eliminate the conflict is not hit, only cold not hit, no hit rate for 0.25;SUMC only cold not hit, no hit rate of 0.25.
11.8 2 min
Add code before the main function:
int chdended;
#include <signal.h>
void child_signal (int sig) {
pid_t pid;
while (PID = Waitpid ( -1, NULL, Wnohang)) > 0)
chdended = 1;
}
Add the statement signal in the main function (Sigchild, child_handle); Before each accept, let chdended = 0; And after serve_dynamic in doit (), add: while (!chdended) pause (),//or do sth delete Wait (NULL) in serve_dynamic;
20135329 li Hai Empty homework