Monday, July 16, 2012

Maximum number of processes/threads in Linux

In Linux there are three variables that define how many threads one process can create:
  1. /proc/sys/kernel/threads-max
  2. /proc/sys/kernel/pid_max
  3. /proc/sys/vm/max_map_count

threads-max

Threads max defines how many threads / process can be created (if the other two parameters do not limit it to a lower number). The default value is calculated at boot time as

max_threads = totalram_pages / (8 * THREAD_SIZE / PAGE_SIZE) 

pid_max 

This  setting simply sets the maximum number that can be used a process identifier (pid). Since threads are also processes (Light Weight Processes to be correct) they also use up pids. This means that if the other two parameters are not limiting, this parameter will define the maximum number of threads one can create in the system. Of course, this is a system wide limitation.

Reaching this limit results that the system is not able to clone new processes (whether new process or thread). This leads to unstable system behavior.

 

max_map_count

This parameter sets the maximum number of Virtual Memory Areas (VMAs) that one process can own. VMA is a contiguous area of virtual address space. One can check these areas by cat /proc/PID/maps. Each new thread need new stack, created by calling malloc typically implicitly. These mallocs need these VMAs to be allocated to the process. If the other two parameters are not limiting, this parameter will define the maximum number of threads one process can start. 

More information:

http://www.novell.com/support/kb/doc.php?id=7000830
http://www.redhat.com/magazine/001nov04/features/vm/

Sunday, June 3, 2012

Huawei E220 and USSD

Huawei E220 modem when connected creates two devices:

- /dev/ttyUSB0
- /dev/ttyUSB1

In order to get an USSD message e.g balance check one shall execute:

# echo "AT+CUSD=1,*102#^M" > /dev/ttyUSB0; cat /dev/ttyUSB1

IMPORTANT: ^M (Carriage Return character) is typed as CTRL+v and CTRL+m

Let's create a one liner that gives back the balance check as a single line:

# echo "AT+CUSD=1,*102#,15^M" > /dev/ttyUSB0; for i in `seq 15`; \
 do read line; if echo $line | grep '+CUSD'; then break; fi ; done < /dev/ttyUSB1

I use this as an input content for my monthly SMS from my server to my mobile phone.