Category: 技术向

Status
Blaok
#!/bin/bash
max_jobs=${JOBS:=$(nproc)}
pipe_file=$(mktemp --dry-run)
mkfifo ${pipe_file}
exec {pipe_fd}<>${pipe_file}
rm ${pipe_file}
printf "%-${max_jobs}s" >&${pipe_fd}
 
for job in ...
do
  read -n 1 -u ${pipe_fd}
  (
    echo "start ${job}"
    # do stuff for ${job} ...
    echo "finish ${file}"
 
    printf '%-1s' >&${pipe_fd}
  )&
done
wait

This piece of script uses a FIFO to manage the parallel jobs. Initially, the FIFO is filled with n characters, each representing an available slot for a job. Before a job starts, it reads a character from the FIFO. Once a job finishes, a character is written back to the FIFO. Thus the FIFO is empty if and only if there are n jobs running. New jobs will not be spawned because the FIFO read will block until at least 1 running job finishes.

{varname} style automatic file descriptor allocation requires bash 4.1+.

Reference:
https://superuser.com/questions/184307/bash-create-anonymous-fifo

Status
Blaok

There are problems in the rt2800usb driver on Linux and according to this post I have to set nohwcrypt=1 for the rt2800usb kernel module.

sudo ifconfig wlan0 down
sudo rmmod -f rt2800usb
sudo modprobe rt2800usb nohwcrypt=1
sudo ifconfig wlan0 up

To make it permenant I have to put the following in /etc/modprobe.d/rt2800usb.conf

options rt2800usb nohwcrypt=1

Damn it… I really should have found this earlier…

Pass a Device to a Linux Container

Recently I was trying to make FPGA tools running inside a LinuX Container (LXC). Getting the software tools running is basically about coping with Xilinx’s horrible bash scripts. Making the container see the FPGA hardware seems more challenging and there are very few references on the Internet. The major trick is to pass the correct devices to the LXC. FPGAs (at least those…

Status
Blaok

If you are working with some software which requires you to source some code and messes up with your PATH and other environment variables like Vivado and Merlin compiler, here is a simple and straightforward method to get rid of the mess (to some extent).

#!/bin/bash
VERSION=2016.1
. /space/Xilinx/SDAccel/${VERSION}/settings64.sh
# do something if necessary
if ! test "$0" -ef "$(which $(basename $0))"
then
    exec $(basename $0) "$@"
else
    if test -t 2
    then
        echo "Recursive call detected!" 1&>2
    fi
fi

Some notes:

  • Start with #!/bin/bash if you or your script use bash-specific syntax!
  • Give this file execution permission with chmod +x!
  • Make sure this file itself has the same name as the target program, or is soft-linked to it!
  • Use . instead of source, which improves compatibility!
Status
Blaok

On Linux, if you see

mtr: unable to get raw sockets.

The following might help:

sudo chmod 4755 $(which mtr)

Reference


The reason this happens is that only privileged processes (or processes with proper capabilities(7) on Linux) can create raw sockets, which is needed to send/receive ICMP packets. By chmod(2), the executable will get the S_ISUID mode, which means it can set process effective user ID when execve(2).


A safer solution is

sudo setcap cap_net_raw+ep $(which mtr)

which only gives mtr the cap_net_raw capability.

Reinstalling the mtr package via the package manager should also solve the problem since it will do whatever appropriate to make it work.

 

Status
Blaok

According to cplusplus.com, std::vector<bool> in C++ is specialized and consumes only 1/8 space as you may have expected. However, as 8-bit bytes are usually the shortest available type in most implementations, thread-safety is not guaranteed. This could have caused significant trouble in my current project. How lucky I am to notice this interesting feature before I have actually encountered trouble.

Besides, std::bitset provides a fixed size bit array whose length is fixed at compile-time.

μtorrent Optimization Guide

Let’s assume you have sufficiently high bandwidth. For example, you are in college and your university does not care about torrenting. One frustrating thing when you are torrenting is that your hard disk does not provide zero latency or infinite bandwidth. μtorrent might be complaining “100% Disk IO” all the time and refuses to download or upload for quite a long time. After a…

Enable HTTP/2 in Nginx

For Nginx 1.9.5 and later, change listen 443 ssl; to listen 443 ssl http2; and you are all set. Note that all implementations of HTTP/2 require HTTPS. Ubuntu 16.04 LTS already comes with Nginx 1.10. To automatically renew certificates from letsencrypt, just run sudo /path/to/letsencrypt-auto renew 1>/dev/null 2>&1 && sudo nginx -s reload periodically if you have initiated the configurations correctly. 🙂…

OpenWRT’s way

I’ve been using OpenWRT since 2012. For now I’ve been able to make it work under multiple environments, with or without native IPv6 support, bridged or NATed. By “work”, I mean both IPv4 and IPv6, with a reliable connection to the rest of the world. 这是计划的一部分。——罗辑 Note that you may need to have some prerequisite knowledge to read the this article, including…

How do I surf the Internet

You guys know what I mean. Quite a few friends of mine asked me about this sort of thing. After a few days of study… I realized that it is not a trivial question. So I share some techniques I use. But first of all, I’d like to make it clear that the Intranet works the way it works for…