sudo -H python -m ensurepip
sudo -H python3 -m ensurepip
Korn's laboratory
My lab reports
Sunday, August 1, 2021
Friday, August 11, 2017
PostgreSQL on Ubuntu
Installing PostgreSQL on Ubuntu
To install both client and server with the newest version of Postgresql (the newest one on Ubuntu's repository), we can do it by apt-get
Setting up PostgreSQL
Installing via apt-get, everything (all configurations) has been set up, and Ubuntu's user 'postgres' will be created. We can access the database as this postgres user. Some tutorial talks about initdb command to create database cluster (database location on file system) but I can't find this command. However, database cluster has been created already after the installation.
There is a database has been created after install, the name is postgres. We can access this database to give the password of our postgres user.
Start using database
We can create database with createdb command.
To access it, use psql command.
References
https://help.ubuntu.com/community/PostgreSQL
To install both client and server with the newest version of Postgresql (the newest one on Ubuntu's repository), we can do it by apt-get
apt-get install postgresql
Setting up PostgreSQL
Installing via apt-get, everything (all configurations) has been set up, and Ubuntu's user 'postgres' will be created. We can access the database as this postgres user. Some tutorial talks about initdb command to create database cluster (database location on file system) but I can't find this command. However, database cluster has been created already after the installation.
There is a database has been created after install, the name is postgres. We can access this database to give the password of our postgres user.
sudo -u postgres psql postgres
\password
Start using database
We can create database with createdb command.
sudo -u postgres createdb mydb
To access it, use psql command.
psql -h localhost mydb postgres
References
https://help.ubuntu.com/community/PostgreSQL
Thursday, July 4, 2013
My vimrc and bash_profile files
Every time I cleanly install my mac, I always to forget backup vimrc and bash_profile (or bashrc for Linux) files, and need to think how they look like. To solve this problem, I'll note here.
vimrc
bash_profile
Although, I can remember every set command for vim, I can't remember how to make vim colorful (i.e. syntax on). Same as bash.
vimrc
set nu
set ai
set ic
set ts=2
set et
syntax on
bash_profile
alias ll="ls -la"
alias la="ls -a"
export CLICOLOR=1
Although, I can remember every set command for vim, I can't remember how to make vim colorful (i.e. syntax on). Same as bash.
Tuesday, June 14, 2011
Fork bomb in 13 characters
วันก่อนเล่นเน็ตไปมาแล้วก็ไปเจอบทความในวิกิเกี่ยวกับเรื่อง fork bomb เข้า ผมก็เลยนั่งอ่านๆดูครับ ผมเชื่อว่าพวก geek หลายคนถึงจะไม่รู้จัก fork bomb มาก่อนแต่ถ้าได้เห็นชื่อก็น่าจะพอเดาได้ว่า fork bomb คืออะไร
fork bomb ก็เป็นการทำ denial-of-service แบบนึง ที่จะพยายาม fork process บนเครื่องออกมาให้มากๆ ในเวลาสั้นๆ ถึงแม้ว่า process เหล่านั้นจะไม่ได้ทำอะไรก็ตาม แต่สุดท้ายแล้วพอมี process โดน fork ออกมาเยอะๆ process table ของ OS ก็จะเต็ม ทำให้ไม่สามารถรันโปรแกรมใดๆได้อีก แนวคิดของ fork bomb ก็ประมาณเท่านี้ครับ ไม่มีอะไรมาก โจมตีด้วยจำนวนเข้าว่าเหมือน DOS ปกติ แต่สิ่งที่ทำให้ผมติดใจจนต้องมาเขียนเป็นบล็อกก็คือ โค้ดโปรแกรมที่ใช้เขียน fork bomb ครับ
ตัวอย่างโค้ดแรกที่ในวิกิเขียนไว้มองผ่านๆผมนึกว่าเป็นภาษาระดับต่ำซักภาษานึงซะอีก แต่ว่าคำอธิบายที่อยู่เหนือตัวอย่างนั้นมันดันเขียนว่า "shell fork bomb" ตอนแรกก็คิดว่า shell ของมันหมายถึง shell อย่างอื่นรึปล่าว ไม่น่าจะหมายถึง shell script ของ UNIX เพราะถ้ามันเป็น shell script ของ UNIX ผมน่าจะอ่านออก แต่นี่มองแล้วไม่รู้สึกว่ามันเป็น shell script แม้แต่นิดเดียว
:(){ :|:& };:
หลังจากการก็อปปี้โค้ดนี้ไปลองรันดูก็พบว่ามันเป็นโค้ดทำ fork bomb ด้วย shell script จริงๆครับ (แต่เครื่องผมลองไม่ตายนะ คาดว่าคงมีระบบป้องกันการทำ fork bomb อยู่) โชคดีครับที่ในวิกิมีเขียนอธิบายการทำงานของโค้ดนี้ไว้ด้วย ไม่งั้นให้ตายผมก็คงงงอยู่ว่ามันทำได้ไง
โค้ดนี้เขียนโดยรวบเอาโค้ดทั้งหมดที่ปกติเราจะเขียนด้วยหลายๆบรรทัดมาอยู่บรรทัดเดียวกันครับ ถ้าแยกบรรทัดกันก็จะเห็นเป็นแบบนี้
:()
{
: | : &
}
;
:
โดยที่ : ตัวแรกในโค้ดถูกใช้เป็นชื่อฟังก์ชันครับ (ความรู้ใหม่ : สามารถใช้เป็นชื่อฟังก์ชันได้ด้วยแฮะ) ถ้าจะให้อ่านง่ายขึ้นก็ลองเปลี่ยน : เป็นชื่อ fork_bomb ดูครับ
fork_bomb(){ fork_bomb | fork_bomb &};fork_bomb
จะเห็นว่าโค้ดนี้เป็นการเขียนฟังก์ชัน recursive แล้วมีการเรียกใช้ฟังก์ชันที่บรรทัดสุดท้ายของโค้ด จะเห็นว่าส่วนหลักๆของโค้ดนี้ก็คือบรรทัด
fork_bomb | fork_bomb &
จะเห็นว่าเราเรียกฟังก์ชัน fork_bomb (ตัวที่อยู่ด้านหน้า) ให้ทำงานแล้ว pipe ผลลัพธ์ไปให้ fork_bomb ตัวที่อยู่ด้านหลังทำงานต่อ การเรียกฟังก์ชันหนึ่งครั้ง มันจะโหลดตัวเองขึ้นมาอีกสองครั้ง ทวีคูณอย่างนี้ไปเรื่อยๆ ส่วน & ที่อยู่ข้างหลังเพื่อทำให้ประโยคนี้ทำใน background ครับ และถ้า fork_bomb ตัวหน้าตาย fork_bomb ที่โดนเรียกตัวต่อๆมาก็จะไม่โดน auto-kill
หลังจากที่พยายามเสิร์ชภูมิหลังของโค้ดนี้ก็พบว่าโค้ดนี้ได้รับการยกย่องว่าเป็น "The most elegant example of fork bomb code" ถูกเขียนโดย Jaromil
หลังจากที่อ่านจบแล้วก็ได้แต่ทอดถอนใจ เฮ่อ อยากเขียนโค้ดเท่ๆแบบนี้ได้บ้างจัง
Friday, February 18, 2011
Problem in swap code
Our only one blog fan, m3rlinez, told me that the one line swap code that I wrote in the previos entry cannot use when two variables are the same allocation such as
a[i] ^= a[j] ^= a[i] ^= a[j]; // when i = j
Thanks him to pointed this issue.
Wednesday, February 16, 2011
double fork magic
เริ่มที่ zombie process
zombie process คืออะไร
zombie process คือ process ที่ทำงานจบแล้วแต่ตัวมันยังอยู่ใน process table ของ OS เพื่อรอให้ parent process มาอ่านค่า exit status ของมัน แต่ก็ไม่มาอ่านซักที
แล้วเมื่อไหร่ parent process จะมาอ่าน exit status
เป็นไปได้สองทาง
- ในโค้ดเรียก function wait ( pid_t wait(int *stat_loc) ) แล้วโปรแกรมจะหยุดรอจนกว่า process ลูกจะทำงานเสร็จ
- เขียน signal handling ให้จัดการ SIGCHLD (SIGCHLD เป็น signal ที่ส่งมาบอกว่า process ลูกทำงานเสร็จแล้ว ให้ parent process ไปจัดการต่อด้วย)
double fork คืออะไร
double fork เป็นรูปแบบการเขียนโค้ดแบบหนึ่งที่ไว้ป้องกันการเกิด zombie process การเขียนโค้ดแบบ double fork นี้ใช้ประโยชน์จากการที่เมื่อ parent process จบการทำงานในขณะที่ child process ยังทำงานไม่เสร็จ โดย child process นั้นจะกลายเป็น process ลูกของ init process แทน ซึ่งจะคอยกำจัด child process พวกนี้ภายหลัง (init process คืออะไรอะเหรอ โอ๊ย เรื่องมันยาว)
แล้วตกลงมันทำงานยังไงหละว้อย
ยกตัวอย่างเปรียบเทียบให้เห็นภาพเลยละกัน
- ตัวอย่างแรกของการเกิด zombie สมมติว่ามี process A fork ลูกออกมาเป็น process B และเมื่อ B ทำงานเสร็จ A ไม่มาจัดการ SIGCHLD ที่ส่งมาทำให้ B กลายเป็น zombie
- ตัวอย่างการทำ double fork แทนที่ process A จะ fork B ออกมาเลย เราเขียนโค้ดให้ A fork process X ออกมาก่อน โดยหน้าที่อย่างเดียวของ X คือ fork B เมื่อ fork B เสร็จก็ให้ X จบการทำงาน ซึ่งเมื่อมาถึงขั้นนี้ B จะกลายเป็น orphan process และจะกลายเป็น process ลูกของ init เมื่อ B ทำงานเสร็จ init ก็จะมาจัดการเคลียร์ B ออกจาก process table ให้
ทีนี้ก็จะไม่มี zombie process อีกต่อไป
Friday, February 11, 2011
Swap two variable values code in one line.
First code pattern that comes to my mind when I want to swap two variable value is
int c;c = a;a = b;b = c;
I found this code in every novice programming book, also in my programming class.
But I've heard one challenging problem (for me as a novice) that try to swap without temporary variable (that is c, in my code.) The code uses knowledge technique from logic class.
a = a ^ b;b = a ^ b;a = a ^ b;
The caret symbol (^) represents bitwise xor.
Recently, I found the code that written it in one line for making it more cool and less understanding that is
a ^= b ^= a ^= b;
Impressed!
Subscribe to:
Posts (Atom)