Cruel Summer in 2024

To record what I did in the summer of 2024

9.6-now

  • Read RUDRA: Finding Memory Safety Bugs in Rust at the Ecosystem Scale
  • Read OCOLOS: Online COde Layout OptimizationS

9.1-9.5

  • Solve LeetCode problem

8.30

Error handling in C++

#include <iostream>
#include <utility>
int divide(int a, int b) {
  if (b == 0)
    throw "Divide by zero";
  return a / b;
}

int main() {
  try {
    divide(1, 0);

  } catch (const char *msg) {
    std::cout << msg << std::endl;
  }
  try {
    divide(10, 0);
  } catch (...) {
    using namespace std;
    cout << "handling error" << endl;
  }
  return 0;
}

Error handling in Go

package main

import "fmt"

func main() {

        test()

}

func test() {
        defer func() {
                if err := recover(); err != nil {

                        fmt.Println(err)
                }
        }()

        panic("Some err")

}

decltype and auto in C++

#include <iostream>
#include <tuple>
#include <utility>
#include <vector>
auto add(int x, int y) -> decltype(x + y) { return x + y; }
using namespace std;

auto id(int &&x) -> int {
  cout << "rv ref" << endl;
  return x;
}

auto id(int &x) -> int {
  cout << "lv ref" << endl;
  return x;
}

auto sub(int x, int y) { return x - y; }
int main() {
  cout << add(1, 1) << endl;
  int i{0};
  id(i++);
  id(++i);
  const int x = 1;
  auto y = x;
  y++; // no problem
  auto init = {1, 2, 3, 4, 5};

  vector<int> v = init;
  sub(1, 1);

  pair<int, int> p = {1, 1};
  cout << p.first << " " << p.second << endl;
  auto [a, b] = p;
  int t1, t2;
  tuple tp = {1, 2};
  cout << a << " " << b << endl;
  tie(t1, t2) = tp;
  cout << t1 << " " << t2 << endl;
  tie(t1, t2) = p;
  cout << t1 << " " << t2 << endl;

  return 0;
}

Perfect forwarding in C++

#include <iostream>
#include <utility>
using namespace std;

template <typename T> void test(T &&t) { cout << "rvalue ref" << endl; }

template <typename T> void test(T &t) { cout << "lvalue ref" << endl; }

template <typename T> void forwardTest(T &&t) {
  test(t);
  test(std::move(t));
  test(std::forward<T>(t));
  cout << "---end---" << endl;
}

int main() {
  auto &&v1 = int(0);
  // universal reference
  int x = 1;
  forwardTest(x);
  forwardTest(int(42));
  return 0;
}

Lambda and constexpr in C++

#include <iostream>
using namespace std;
#include <algorithm>
#include <utility>
constexpr auto first = 42;
struct Data {
public:
  int x;
  int y;
  Data(int _x, int _y) : x(_x), y(_y) {}
};
// Example for constexpr and lambda (value, reference and std::move)
// Something about the Person.
constexpr int add(int x, int y) { return x + y; }

struct Person {
  int height;
  Person(int h) : height(h) {}
};
int main(int argc, char *argv[]) {

  int i = 0;
  auto f1 = [&i](int x) -> int {
    i += x;
    return 42;
  };

  f1(1);
  f1(1);
  // non-static variable can be captrued
  vector<Data> v1;

  v1.emplace_back(1, 2);
  v1.emplace_back(3, 2);
  v1.emplace_back(1, 3);
  sort(v1.begin(), v1.end(), [](Data d1, Data d2) {
    if (d1.x > d2.x) {
      return false;
    }
    if (d1.x < d2.x) {
      return true;
    }
    return d1.x < d2.y;
  });
  for (auto d : v1) {
    cout << d.x << " " << d.y << endl;
  }
  vector<Person> v2;

  v2.emplace_back(178);
  v2.emplace_back(160);
  v2.emplace_back(159);

  sort(v2.begin(), v2.end(),
       [](Person a, Person b) { return a.height < b.height; });
  for (auto &p : v2) {
    cout << p.height << endl;
  }

  vector<Person> v3 = move(v2);
  cout << v2.size() << endl;
  // moved into lambda expression here?
  auto f2 = [v4 = std::move(v3)]() {
    cout << v4.size() << endl;
    return;
  };
  f2();
  cout << first << endl;
  cout << add(1, 1) << " " << add(v3.size(), argc) << endl;
  array<int, add(1, 1)> arr;
  // constexpr in constructor function??
  // not allow!  first=1000;
  return 0;
}
echo ${!map1[@]}
for key in ${!map1[@]};do
echo $key
done
package main

import (
        "fmt"
        "math/rand"
        "sync"
        "time"
)

func main() {

        var wg sync.WaitGroup
        ch := make(chan Data, 5)
        wg.Add(2)
        go produce(&wg, ch)
        go consume(&wg, ch)
        wg.Wait()

        ch1 := make(chan Data)

        select {
        case d := <-ch1:
                fmt.Println(d)

        case <-time.After(1):
                fmt.Println("timeout")

        }

        close(ch1)
        // ok的作用???
        _, ok := <-ch1
        fmt.Println(ok)
        multiChannel()
        ch3 := make(chan int)
        close(ch3)
        <-ch3
        ch4 := make(chan int)

        select {
        case ch4 <- 1:
                fmt.Println("ch4")
        default:
                fmt.Println("default")
        }

}

func send(ch chan<- int, num int) {

        for i := 0; i < 10; i++ {
                ch <- num + i
                t := time.Duration(rand.Int() % 800)
                time.Sleep(t * time.Millisecond)
        }
        close(ch)

}

func multiChannel() {

        ch1 := make(chan int)
        ch2 := make(chan int)
        go send(ch1, 1)
        go send(ch2, 2)
        // cnt:=0

        for i := 0; i < 20; i++ {

                select {
                case x, ok := <-ch1:
                        fmt.Println(x, 1, ok)

                case x, ok := <-ch2:
                        fmt.Println(x, 2, ok)

                }

        }

}

type Data struct {
}

func produce(wg *sync.WaitGroup, ch chan<- Data) {
        for i := 0; i < 10; i++ {
                ch <- Data{}
        }
        close(ch)
        wg.Done()
}

func consume(wg *sync.WaitGroup, ch <-chan Data) {
        for _ = range ch {
                fmt.Println("consuming")
        }
        wg.Done()
}

8.28-8.29

  • Interview…

8.9-8.24

  • Solve Leetcode problems…(Hot 100 and Hot 150)

8.8

  • Solve Leetcode problems…

8.7

8.6

package main

import "fmt"

type Animal interface {
	run()
	skip()
}

type Cat struct {
	x int
}

func (c Cat) run() {
	fmt.Println("this is cat")
	c.x = 0
}

// not a good practice
func (c *Cat) skip() {
	fmt.Println("cat skip1!")
	for i, v := range "hello world" {
		fmt.Println(i, v)
	}
}

func main() {
	c := Cat{x: 42}
	c.run()
	var a Animal
	fmt.Println(a == nil)

	a = &c
	// a = c is not ok
	a.run()
	fmt.Println(c.x)
}

8.5

  • Learn how to use snort
sudo ./snort -vd -i <interface_name> "port 9999"
curl http://localhost --connect-timeout 2000
mkdir dir
if [[ "foo" == "f*" ]] ; then echo hello; else  echo test; fi
# blank is important
if [[ "foo" == f* ]] ; then echo hello ; else echo test; fi
for (( i=0;i<5;i++)) ; do echo hello ; done
# Here document
 cat <<EOF                               
`ls`
EOF
# (())
if [ $((1+1)) -eq 2 ] ; then echo yes ; fi

# Why export?
x=1
export y=2
# case statement
case $p in
world)
echo hello 
;;
esac

declare -a arr=(a b c)
declare -a map=([hello]=world)
echo "${arr[@]}"
echo "${arr}"

echo $$
echo $@
echo $?

. example.sh
  • Learn about the implication of batch size

8.4

  • Read something about diffusion model

A note about promiscuous vs. non-promiscuous sniffing: The two techniques are very different in style. In standard, non-promiscuous sniffing, a host is sniffing only traffic that is directly related to it. Only traffic to, from, or routed through the host will be picked up by the sniffer. Promiscuous mode, on the other hand, sniffs all traffic on the wire. In a non-switched environment, this could be all network traffic. The obvious advantage to this is that it provides more packets for sniffing, which may or may not be helpful depending on the reason you are sniffing the network. However, there are regressions. Promiscuous mode sniffing is detectable; a host can test with strong reliability to determine if another host is doing promiscuous sniffing. Second, it only works in a non-switched environment (such as a hub, or a switch that is being ARP flooded). Third, on high traffic networks, the host can become quite taxed for system resources.

  • perf usage notes
taskset --cpu-list 2 ls # 2 is not a mask
taskset -p pid

8.3

8.1-8.2

  • Back to Xi’an
  • Read Matrix Calculus

7.31

7.30

7.29

7.26-7.27

7.25

7.24

7.23

7.22

7.21

一类是学习率不受梯度影响的随机梯度下降(Stochastic Gradient Descent)及SGD的一些改进方法,如带有Momentum的SGD;另一类是自适应学习率如AdaGrad、RMSProp、Adam等。

7.20

7.19

7.18

7.17

7.16

7.11-7.15

  • Interview…
  • Review Linear Algebra & Probability & Advanced Mathematic

7.8

7.6-7.7

7.5

7.4

  • Interview…

7.2

7.1

6.30

6.29

Ruitian Zhong
Ruitian Zhong
Undergraduate

My interests include operating system, virtualization, distributed system, etc.

Next
Previous