≡ Menu

C Programming

Poll: Your Favorite Scripting Language?

Like most sys admin, I'm lazy. I try to automate almost all things in order to save time. Inexperienced sys admin and help desk staff working under me finds all these tools useful. It saves their time and avoids security issues. Automation allows help desk staff to do things that they don't have enough direct system knowledge to do themselves. However, selecting correct tool and applying correct methodology is very important.
[click to continue…]

GNU/GCC Compiler Techniques Used in Linux Kernel

Excellent article - you can find information about GCC extensions for the C language. The Linux kernel uses several special capabilities of the GNU Compiler Collection (GCC) suite. These capabilities range from giving you shortcuts and simplifications to providing the compiler with hints for optimization.

This article provides a glimpse of the techniques made available by GCC in the Linux kernel. You can read more about all the available extensions for both C and C++ in the GNU GCC manual.

Why Does The Segmentation Fault Occur on Linux / UNIX Systems?

According to wikipedia:

A segmentation fault occurs when a program attempts to access a memory location that it is not allowed to access, or attempts to access a memory location in a way that is not allowed (for example, attempting to write to a read-only location, or to overwrite part of the operating system).

Usually signal #11 (SIGSEGV) set, which is defined in the header file signal.h file. The default action for a program upon receiving SIGSEGV is abnormal termination. This action will end the process, but may generate a core file (also known as core dump) to aid debugging, or perform some other platform-dependent action. A core dump is the recorded state of the working memory of a computer program at a specific time, generally when the program has terminated abnormally.

Segmentation fault can also occur under following circumstances:

a) A buggy program / command, which can be only fixed by applying patch.

b) It can also appear when you try to access an array beyond the end of an array under C programming.

c) Inside a chrooted jail this can occur when critical shared libs, config file or /dev/ entry missing.

d) Sometime hardware or faulty memory or driver can also create problem.

e) Maintain suggested environment for all computer equipment (overheating can also generate this problem).

Suggestions to debug Segmentation Fault errors

To debug this kind of error try one or all of the following techniques :

  • Use gdb to track exact source of problem.
  • Make sure correct hardware installed and configured.
  • Always apply all patches and use updated system.
  • Make sure all dependencies installed inside jail.
  • Turn on core dumping for supported services such as Apache.
  • Use strace which is a useful diagnostic, instructional, and debugging tool.
  • Google and find out if there is a solution to problem.
  • Fix your C program for logical errors such as pointer, null pointer, arrays and so on.
  • Analyze core dump file generated by your system using gdb

Further readings:

Please add your suggestions and debugging techniques in the comment below.

Debugging Book: The Art of Debugging with GDB, DDD, and Eclipse

A debugger is a computer program that is used to test and debug other programs. Both sys admin and developers may use debugger to examine code or halt instructions when specific conditions are encountered using an instruction set simulator (ISS).

The GNU Debugger (GDB) is used as debugger for the GNU software. It is a portable debugger that runs on many Unix-like systems and works for many programming languages, including Ada, C, and C++.

Debugging, the demanding process of finding and fixing programming errors, is fundamental to successful software development. But even many experienced programmers find debugging a challenge.

The Art of Debugging with GDB, DDD, and Eclipse (No Starch Press, September 2008, 280 pp, ISBN 9781593271749) teaches readers how to effectively use the three most popular open source debugging tools:

  • GDB
  • DDD
  • Eclipse

The text-command based GDB is included with most Linux distributions. DDD is a popular GUI front end for GDB, while Eclipse provides a complete integrated development environment.

In the book, readers learn how to:

  • Inspect variables and data structures
  • Understand segmentation faults and core dumps
  • Use features like catchpoints, convenience variables, and artificial arrays
  • Avoid common debugging pitfalls
  • Prevent errors in the first place by making the best usage of editors, compilers, and static code checkers

The book also includes coverage of advanced topics like thread, client-server, GUI, and parallel programming. Whether readers dread the thought of debugging programs or simply want to improve their current debugging efforts, they'll find a valuable ally in The Art of Debugging with GDB, DDD, and Eclipse.

The book is written by Norman Matloff - a computer science professor at UC Davis and Peter Jay Salzman who is currently a programmer with a Wall Street financial firm. The authors are well-known for their popular online debugging tutorials, and a printed book is long overdue.

The Art of Debugging offers readers specific advice for debugging with each tool and explains general debugging strategies. Real world examples of coding errors help to clarify the authors' guiding principles, and readers will quickly understand why their programs crash or throw exceptions.

Book Information

  • Publisher: No Starch Press
  • Pub date: September 2008, 280 pp
  • ISBN: 9781593271749
  • Price: $39.95 USD
  • Order Info: order@oreilly.com / 1-800-998-9938 / 1-707-827-7000 / Oreilly web store
  • Support nixcraft: Order book from Amazon

Linux / UNIX: Geany Lightweight Integrated Development Environment

If you wanted to quickly edit PHP, Java, C etc, Geany IDE is perfect. It is a small and lightweight integrated development environment. It was developed to provide a small and fast IDE, which has only a few dependencies from other packages. Geany is known to run under Linux, FreeBSD, NetBSD, MacOS X, AIX v5.3, Solaris Express and Windows.

Basic features of Geany
=> syntax highlighting
=> code folding
=> code completion
=> auto completion of often used constructs like if, for and while
=> auto completion of XML and HTML tags
=> call tips
=> many supported filetypes like C, Java, PHP, HTML, Python, Perl, Pascal and many other
=> symbol lists
=> build support (to compile, link, run programs)
=> plugin interface (C, Lua)

Tutorial: Linux MPI Parallel Clusters Programming

The MPI (Message Passing Interface) is a language-independent communications protocol used to program parallel computers. It allows many computers to communicate with one another. It is used in computer clusters.

The OpenMP (Open Multi-Processing) is an application programming interface (API) that supports multi-platform shared memory multiprocessing programming in C/C++ and Fortran on many architectures, including Unix / Linux and Microsoft Windows platforms. It consists of a set of compiler directives, library routines, and environment variables that influence run-time behavior.

This tutorial explains how to obtain, build, and use an MPI stack for Linux machines. This tutorial will take you from "hello world" to parallel matrix multiplication in a matter of minutes. The exercise takes slightly more than 30 minutes and allows one to develop and run MPI codes on a multi-core server or on a HPC cluster.

OpenMP has several strong points: it is a very simple system to use and it is widely available in compilers for most major platforms. There are however, other methods to express parallelism in your code. On distributed parallel systems, like Linux clusters, the Message Passing Interface (MPI) is widely used. MPI is not a programming language, but rather a standard library that is used to send messages between multiple processes. These processes can be located on the same system (a single multi-core SMP system) or on a collection of distributed servers. Unlike OpenMP, the distributed nature of MPI allows it to work on almost any parallel environment.

History and Culture of Unix Programming – The Art of Unix Programming

The Art of Unix Programming by Eric Raymond is a book about the history and culture of Unix programming from its earliest days in 1969 to now, covering both genetic derivations such as BSD and conceptual ones such as Linux.

You should read this book if you are an experienced Unix programmer who is often in the position of either educating novice programmers or debating partisans of other operating systems, and you find it hard to articulate the benefits of the Unix approach.

You should read this book if you are a C, C++, or Java programmer with experience on other operating systems and you are about to start a Unix-based project.

You should read this book if you are a Unix user with novice-level up to middle-level skills in the operating system, but little development experience, and want to learn how to design software effectively under Unix.

You can read HTML version of "The Art of Unix Programming" online at Eric's website.