Posts

Showing posts with the label C

Embedded Languages Footprint

One of my personal project has a requirement to have scripting support, and while the research of the options is still in progress, I’d like to share my preliminary results. The initial requirements for the scripting language were having the smallest footprint, but good functionality. That has given a start to the Embedded Languages Footprint project. The requirements are quite specific though (please see below), hence the most popular languages like Lua and Python are not considered suitable, but were included for the reference. For now, the most promising languages are Chibi-Scheme and Wasm3, but it’s still on-going research and final choice will be made later. The major requirements: C/C++ only (to ensure the best portability); No extra compilation dependencies (to build with the bare devkits); Actively maintained (to compile without its source code changes); Strong typing (to reduce the number of possible errors); Permissive license for both...

Emulating line-buffering mode (Gradle example)

C printf() function doesn't flush buffers by default, and a lot of code using it may miss this, therefore creating interactivity problems to the end user. It's not specific to C though - it's about the standard I/O streams, and the problem may appear anywhere. The post is based on StackExchange discussion, so you can dig deeper if you want:  Turn off buffering in pipe .

Memory reclaiming in Python

Running GC-based languages on embedded systems always give a challenge to limit the physical memory amount taken by the processes. Python scripting is obviously a good example what would happen if you use long-running processes and which problems you could face. Let me show my research and the way I've used to fix the memory consumption. First, a trivial example which is used in the Internet, and which is actually wrong and doesn't show the problem: import gc import os iterations = 1000000 pid = os.getpid() def rss():     with open('/proc/%d/status' % pid, 'r') as f:         for line in f:             if 'VmRSS' in line:                 return line def main():     print 'Before allocating ', rss(),     l = []     for i in xrange(iterations):         l.append({})     print 'After allocating  ', rss()...

Minimalistic Linux threading

Embedded system development always poses certain challenges and one of them is the threading support. Including POSIX threads (pthread) is not an option sometimes, but fortunately Linux kernel allows to make LWP (light-weight processes) which are similar to threads. The key function here is the  clone  function. fork internally uses clone too, but clone allows to create child processes with the different settings including  CLONE_VM (share the memory between parent and children processes) which is essential for threading. Without further ado let me give an example, and I'll describe key pieces below: //Linux light-weight processes usage example #define _GNU_SOURCE #include <fcntl.h> #include <sched.h> #include <stdarg.h> #include <stdio.h> #include <stdlib.h> #include <sys/syscall.h> #include <sys/types.h> #include <sys/wait.h> // Default thread number #define THREAD_NUM 5 int message(const char *f...

Embedding Python

Just want to share some useful links about embedding Python to your C-based application: the main article:  Embedding Python in Another Application additional article that shows peculiarities of multithreading, sockets and shared memory: Embedding Python in C/C++ ( Part1 , Part2 ) Cython (or Pyrex ) can be used to reduce handwritten code for Python interoperability: A quick Cython introduction If you have doubts about Python size, there are some minimal implementations: see Embedded Python article. Let me quote tinypy : tinypy is a minimalist implementation of python in 64k of code ... What more could you possibly want?? a pony? However, I highly recommend to use classic CPython implementation (basically because it has a huge number of contributors and supporters, and has an excellent documentation). It can be stripped up to 1-2 megabytes depending of your requirements.

Facebook and PHP

There is a common mistake about "If everybody use it, so I also have to use it - millions of people can't be wrong". Apparently, they can, and huge codebase, support and knowledge mean nothing, otherwise we would still use Fortran, Cobol, Basic and other almost died monsters. Also there is an another common mistake about "If big corporation use it, I also have to use it". It's very doubtful, almost always decision are made in hurry and/or by wrong people and/or without serious consideration. And after some period of time, it's difficult to nullify previous decision because it would require huge efforts. Good example - Facebook. Let me quote the presentation  HipHop for PHP Tech Tasting : PHP is problematic for Facebook: High CPU usage High memory usage Reuse of PHP logic in other systems Extensions are hard to write for most PHP developers But huge codebase, strange affection towards PHP (in what universe "loose typing and universal ar...

C modules unit-testing in Linux

Image
In spite of its age, C programming language is still very popular, especially for developing system or low-level software like drivers, compilers, virtual machines etc. And as any software, it have to be tested. Let me show brief introduction in unit-testing for C modules. There are many unit-testing frameworks for C, and one of the most well-known is cmockery . But I'll show the usage of much more simpler "framework" - FCTX . The main advantage of it is that it consists of just one header file, so it can be easily used for test tasks, small projects and examples. For calculating code coverage I use gcov / lcov tools. Gcov is included in GCC, so you don't have to install it. Lcov is a graphical front-end for Gcov and should be installed from the repository: $ sudo apt-get install lcov As a sample code for testing I'll use a simple hash function from Robert Sedgwicks Algorithms in C book: #include "hash.h" unsigned int RSHash(char* str, uns...

Brief introduction to Metasploit

Image
As a part of increasing IT-infrastructure security, penetration testing is one of the most valuable tools. Of course, system updates, using firewalls, IDS/IPS, right ACL and other methods are very efficient, but you can't be 100% assured that everything is fine. Security is a battle between defenders and attackers, and usually attackers are one step ahead in this battle. To be a good security professional, you have to know how attackers work, which tools and methods they are using, you have to be an attacker (of course, white-hat) - embrace Dark Side, but not be dominated by it and stay with Light Side. So, the Metasploit is one of must-be-known tool for every security professional: Metasploit provides useful information and tools for penetration testers, security researchers, and IDS signature developers. This project was created to provide information on exploit techniques and to create a functional knowledgebase for exploit developers and security professionals. The t...

Cross-compilation in Linux

Sometimes it is necessary to create Windows application from Linux. I will briefly introduce the method for it in the article. The basic principle is simple and common for all cross-compilations (e.g., creating Symbian applications in Linux/Windows or other desktop OS): Get and install a toolchain for target platform (compiler, linker and other tools); Compile all required frameworks using this toolchain (for example, GStreamer, Qt, wxWidgets etc) Compile your own project with this toolchain and precompiled frameworks. For creating Windows applications from Linux you can use MingWG: Minimalist GNU for Windows . The installation is pretty easy: $ sudo apt-get install mingw32 As an example, let's compile a simple Windows application with a message box (msgbox.c file): #include <windows.h>   INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,                    ...

Static code analysis tools (with multicasting chat sample)

In the modern world with the fast cycles of software development it is critical to develop applications with high quality but without long periods of testing and bug fixing. One of the key points for gaining such performance is using static code analysis tools. Let me provide some samples of such tools which can be useful for software development: Clang Static Analyzer - open source source code analysis tool that finds bugs in C, C++, and Objective-C programs; PyLint - a static code analyser for Python; Perl-Critic - a static code analysis tool for Perl; FxCop - static analysis for Microsoft .NET programs. There are many other tools, a list of which you can find in wikipedia . Let's do static code analysis of a simple multicasting chat application: Run PyLint for this script. See results below: <skipped> C: 1, 0: Missing module docstring (missing-docstring) C: 9, 0: Invalid constant name "is_listening" (invalid-name) W: 12,20: Redefining name '...