Librenix
Headlines | Linux | Apps | Coding | BSD | Admin | News
Information for Linux System Administration 

Linux Find Command Examples

Up
vote
Down

Fifteen examples of the find command...
Apart from the basic operation of looking for files under a directory structure, you can also perform several practical operations using find command that will make your command line journey easy.

In this article, let us review 15 practical examples of Linux find command that will be very useful to both newbies and experts.
read more...
permapage | score:9390 | -Ray, May 12, 2009

Tutorial: Linux Dialog Boxes

Up
vote
Down

Dialog lets you create dialog boxes from Unix/Linux shell scripts...
'dialog' is a utility for building console-based 'front ends' in UNIX like operating systems.

In this brief tutorial I am mentioning the usage of few important basic controls available with this 'dialog' utility and later I have created a very simple front end application in UNIX bash scripting using dialog.
read more...
permapage | score:9369 | -Ray, January 1, 2010

Currency Traders Telnet Game

Up
vote
Down

A new, large Currency Traders game is up. To connect enter the following command from an xterm, konsole, or other terminal/command line window:

[link removed]

It runs on port 23, just like traditional telnet. It's free to play and no software is required to play. All you need is an internet connection.

This is an old-school, no-graphics strategy game. All you need is a telnet client to play -- and every modern operating system comes with one. It's free and you can play anonymously. Just choose a name and a password and log in. If you don't know what to do, just press your H key for a Hint and a context-sensitive Tip.

This game is played in a persistent world where whatever you build, buy, or otherwise 'acquire' in the game will still be there tomorrow. Unless you make an enemy of another player, that is. PVP (player-versus-player) is always enabled here so other players can attack your deployed fortifications -- or even, heaven forbid, you.

This is a turn-based game that can be played any time of the day, night, or week. Each player is issued a certain amount of energy (turns) per day that is used to travel, trade, or play at the arcade in the several Malls. If you don't use your turns, they accumulate for as much as several months -- so there's no disadvantage to skipping a day or even a few weeks.


(Try the [read more] link if you want to see something similar, a text-based mmorpg)
read more...
mail this link | permapage | score:9356 | -Ray, January 23, 2013 (Updated: May 13, 2014)

Tutorial: Installing Desktop FreeBSD

Up
vote
Down

If you would like to use your personal, dial-up system as a FreeBSD desktop computer, read on...
What follows is a tutorial aimed specifically at the ordinary desktop user interested in getting started with FreeBSD. Ed provides an easy to understand guide through FreeBSD's Sysinstall installer in part one of this series.
read more...
permapage | score:9338 | -Ray, December 4, 2003

Benchmarks: FreeBSD 8.0 vs. Solaris vs. Linux

Up
vote
Down

FreeBSD 8.0 takes on Fedora 12 and Ubuntu 9.10 as well as OpenSolaris 2010.02 b127 in a performance free-for-all..
The hardware we are using for benchmarking this time was a Lenovo ThinkPad T61 notebook with an Intel Core 2 Duo T9300 processor, 2GB of system memory, a 100GB Hitachi HTS72201 7200RPM SATA HDD, and a NVIDIA Quadro NVS 140M graphics processor powering a 1680 x 1050 LVDS panel.
read more...
permapage | score:9336 | -Ray, December 1, 2009

Scripting: Bash Array Tutorial

Up
vote
Down

An excellent introduction to bash arrays including 15 examples...
$ cat arraymanip.sh
#! /bin/bash
Unix[0]='Debian'
Unix[1]='Red hat'
Unix[2]='Ubuntu'
Unix[3]='Suse'

echo ${Unix[1]}

$./arraymanip.sh
Red hat
read more...
permapage | score:9328 | -Ray, June 7, 2010

Gnome Encfs Manager

Up
vote
Down

The Gnome Encfs Manager (or short GEncfsM) is an easy to use manager and mounter for encfs stashes featuring per-stash configuration, Gnome Keyring support, a tray menu inspired by Cryptkeeper but using the AppIndicator API and lots of unique features. Whether you want to let it do things as simple as mounting a stash at startup, which is often used in conjunction with cloud-synced folders on services like Dropbox and Ubuntu one, or whether you want to let it automatically mount and unmount your stashes on removeable drives like USB-sticks, SD cards or even network-resources, GEncfsM is designed to do all the work for you. read more...
mail this link | permapage | score:9323 | -gg234, June 19, 2013

perl dispatch table examples

Up
vote
Down

Dispatch Table: The fancy name for a hash of code-refs...
In perl, code is a first class data type... you can talk about it in the same way you would talk about arrays or hashes, you can take references to them (giving you a code-ref ) and you can de-reference them (which runs the code);

Code-references are just scalars that refers to something, and you get them by using the reference-to operator:
read more...
permapage | score:9322 | -f00li5h, December 17, 2010

Online Ruby Interpreter

Up
vote
Down

Try Ruby online with this online tutorial / interpreter. It runs in your browser.
Ruby is a programming language from Japan (available at ruby-lang.org) which is revolutionizing the web. The beauty of Ruby is found in its balance between simplicity and power.

Try out Ruby code at the prompt above. In addition to Ruby's builtin methods, the following commands are available:
read more...
permapage | score:9275 | -Ray, November 30, 2005

Linux Top command examples

Up
vote
Down

Fifteen examples of Linux usage of the top command with short explanations...
In this article, let us review 15 examples for Linux top command that will be helpful for both newbies and experts.

1. Show Processes Sorted by any Top Output Column – Press O

By default top command displays the processes in the order of CPU usage. When the top command is running, press M (upper-case) to display processes sorted by memory usage as shown below.
read more...
permapage | score:9266 | -Ray, January 12, 2010

Missing the point of the Mac Mini

Up
vote
Down

I've read several articles and numerous comments over the past week detailing just how overpriced Apple's new Mac Mini is. Reviewers seem to conclude that because they can assemble a PC of similar performance to the Mini for less money, that the new Mac simply costs too much.

What they have not done, however, is duplicate the Mac Mini in any important way. The closest comparison I've seen pitted the Mini against a machine 2.5 times its size. At least that reviewer understood that size matters. I'm a fan of small systems. I own 2 Biostar iDEQ cubes, one Shuttle, and three Book PC's. The Book PC's are the oldest and most obsolete, of course, with the fastest one containing a Pentium III 667. I've gotten rid of several systems over the past two years that were faster than the Book PC's. Why keep the slower computers while getting rid of systems up to twice as fast, you might ask.

I've kept the Book PC's because they are so small that keeping them around isn't a burden. They take about as much space on a bookshelf as an unabridged dictionary. I have one, currently disconnected, functioning only as a monitor stand for the system I'm using right now. A book PC will fit in my briefcase; I've hauled them around with me as if they were laptops. With a dozen computers around the house, space is precious and small is beautiful.

The Book PC's are 3.2" x 10.5" x 11.9". That's 4.8 times the volume of a Mac Mini. The Mac is truly tiny. I've worked to build fast, small, quiet Linux systems for years now. The iDEQ 200V is the cheapest system I've made that is fast, quiet, and runs Linux without complaint. Without software and with only the on-board graphics chip, it cost about the same amount as the Mac Mini. At 12.5" x 7" x 8", however, it is much larger than the Mini and weighs several times as much.

I challenge the anti-Mini crowd to build a PC of any shape that displaces approximately the same volume as the Mini plus power supply. Then, compare prices again. The SFF computer fans are clearly going to notice this machine and are going to buy a few truckloads of them. In the small form factor (SFF) computer market, even ignoring the software, this machine is clearly a bargain.

SFF computer fans who are committed to Windows will still covet this system; a few of them might even make the switch to OS X just to get one. I even expect some SFF Linux geeks to buy them because they're tiny, cheap, and can run Linux. Conclusion: the anti-Mini reviewers and posters are not SFF people.

Next, the Mini is an affordable and typically stylish Mac. A smallish PC does not run OS X. The Mini comes with OS X and will make a great second (or third) computer for many Mac users. I use Linux as my primary desktop OS (SuSE 9.2 Professional for the last three weeks, Fedora Core 2 the previous year) and FreeBSD and Linux (Fedora, Slackware) for my servers. I'm hardly a Mac guy but, as a Unix geek, I'm perfectly fine with OS X. I used a Mac as my primary desktop for a couple of weeks after a recent move.

Many Mac users -- at least those who need a second system -- will find the price -- and the size -- of this system quite appealing. Clearly, the negative reviewers and posters are not OS X users.

Therefore, I've come to the conclusion that these anti-Mac Mini arguments are coming from people who appreciate neither of the core characteristics of the machine. They don't understand the appeal of the SFF systems market, nor are they OS X / Mac users.

Apple, on the other hand, appreciates both and they have produced an impressively priced small form factor OS X system.

I wish for Apple responsive suppliers with scalable production facilities. They will surely need them in order to satisfy the demand for the Mac Mini.
mail this link | permapage | score:9231 | -Ray, January 21, 2005

Tutorial: Introduction to Linux files

Up
vote
Down

This newbie-level Linux tutorial is an introduction to handling files from the Linux command line. It will cover finding files, determining their type, renaming, copying, examining their attributes, reading their contents, and, in the case of binary files, how to get clues to learn something more about them. Further reading will be suggested for editing files since that topic is beyond the scope of this article.

Getting Started
The reader of this tutorial is expected to have access to a Linux system and to perform the example commands as we progress through the tutorial. Once logged in to your Linux system, open a terminal session. Under Red Hat Linux, terminal is found in the 'system tools' section of the menu. (Your system may, alternatively, use a terminal program called 'konsole', 'xterm', or 'shell'. Look around your system for a menu with 'tools' or 'utilities' in the name if necessary.)

ls: Listing files
Let's start with the ls command. ls is an abbreviation for list files. Type ls now, then press the 'enter' key to see the names of the files in your current directory. The results from my 'tmp' directory are listed in bold below:

 $ ls /tmp
tardir.0.log
$
Note that I said 'your current directory'. To get the a listing of files in another directory, enter ls [dir] where [dir] is the name of the directory you wish to look at. For example, to see the file names in your top level directory, '/', type the following:
 $ ls /
bin dev home mnt
proc sbin tmp var boot etc
initrd lib opt root sys usr

$
For more information on the files, use one or more of the ls command line switches. Here I use ls with the -l switch for a 'long' listing:
 $ ls -l
total 14
-rw-r--r-- 1 root root 9649
Mar 28 02:47 tardir.0.log

$
Note that with the -l switch we get the file permissions, the inode links, the owner and group names, the file size in bytes, and the timestamp of the file as well in addition to the name. The ls command has many more options. Type man ls for a full list of options.

file: What is this file?
Linux also provides a handy command to help determine what type of files you are dealing with:

 $ file tardir.0.log
tardir.0.log: ASCII text
$
The Linux (and Unix) file command knows about, and can detect, many different file types. In our example, file tells us that tardir.0.log is a simple ASCII text file.

less: Paging through a file
Now, to actually look at the contents of a text file, we have many options. The most common is the more command and a more elaborate, newer command is less. I like less because it lets you use the arrow keys for scrolling and the pgup/pgdn keys for paging through the file. The following is a condensed page from the command less tardir.0.log:

 home/tfr/
home/tfr/doc/
home/tfr/doc/1-WAY
home/tfr/doc/0
home/tfr/doc/ADMIN
[ . . . ]
home/tfr/doc/BAT
home/tfr/doc/BATT
:
From the ':' prompt we can page or scroll forward or backward. We can also type /star to search for the next occurrance of the string 'star'. Enter man more or man less for more information on the more or less commands, respectively.

mv: Renaming a file
Now, suppose we want to rename a file. Under Linux (and Unix) we 'move' it with the mv command as follows:

 $ ls
tardir.0.log
$ mv tardir.0.log tar.log
$ ls
tar.log
$
Note that the mv command only produces output when there is an error. In this case, we encountered no error so mv quietly performed its work.

cp: Copying files
To make an actual copy of a file, we use the cp command. For example, to make a backup copy of tar.log named tar.log.2, we enter the following:

 $ cp tar.log tar.log.2
$ ls
tar.log tar.log.2
$
Again, we get no output to the screen when the cp command is used without error. We had to use the ls command to see the result of the command. Enter man cp for more details of the cp command.

strings: Looking for text in a binary file
Now, to actually look inside an unknown binary file for text strings there is a command called, appropriately enough, strings. For example, if we run the strings command on the 'echo' program, we get, in part, the following:

 $ strings /bin/echo
Copyright (C) 2004 Free
Software Foundation, Inc.
Written by %s, %s, %s,
%s, %s, %s, %s,
%s, %s, and others.

$
Type man strings for more information.

grep: Finding particular strings in a file
To look for a particular text string in a file, we use the grep command:

 $ grep html tar.log
home/tfr/timeout.html
home/tfr/hello.html

$
And, of course, man grep will retrieve additional instructions for the grep command.

find: Finding files by name
To find all files with a particular name on your system, use the find command. For example, to find files named 'echo', enter the following:

 $ find / -name 'echo'
/bin/echo
/etc/xinetd.d/echo

$
Further, to find all files in the /var filesystem with the string 'echo' in their names, enter this:
 $ find /var -name '*echo*' 
/var/mod/mod_echo.html
/var/mod/mod_echo.html.en

$

More information...
To get started editing text files try this tiny vi tutorial. After going through the quick tutorial, you can click the contents button and reach an advanced vi tutorial as well as other vi information.

For information on moving around in a Linux filesystem try this Introduction to Linux in ten commands. That article also provides additional examples on some of the commands covered here.

mail this link | permapage | score:9229 | -Ray, April 2, 2005

Smbind: Web management for DNS

Up
vote
Down

Manage your DNS server and tables from a web browser with Simple Managment for BIND (Smbind)...
Smbind is a PHP-based software tool for managing DNS zones for BIND via the web interface. This supports the per-user administration of zones, error checking, and a PEAR DB database backend.
read more...
permapage | score:9223 | -Ray, April 26, 2007

Space Tyrant: A multiplayer network game for Linux

Up
vote
Down

Since the last release of Space Tyrant, it has gained some actual game-like functionality. The new code can be downloaded from st2.c. Download it as well as the shell script you’ll need to compile it: makeit2.sh.

It’s now possible to connect to the game via telnet and to create an account, log in, and be issued a ship. Once you’re logged in, there is a universe to explore filled with ports for buying and selling goods and planets for scooping free goods. From those trading activities you can earn money, called microbots. Other than trading to earn more money, you only use your microbots to buy fighters -- which you can use to attack other players or the neutral fighters that guard some sectors.

The neutral fighters are only good for parking under while you’re not playing. They afford a little bit of free protection for your ship since no one can attack you until they first destroy the neutral fighters. Note that you can attack other players whether they are logged in or not and any fighters with their ship will automatically try to defend them.

Each player starts out with a specific supply of fuel, called antimatter. Each minute a small amount of additional antimatter is issued by a function called updatefuel(). If you’re not logged in, fuel just accumulates in your ship. This continuous allocation of fuel makes Space Tyrant a type of turn-based game. Unlike traditional turn-based games, however, you can play your fuel all at once or a little at a time and completely independently of how and when other players play.

You can also talk to other players on the radio. There is only one channel so everyone who is logged in hears everything that is said on the radio.

The game isn’t yet playable in any reliable sense, however, because it doesn’t yet back up and reload the data from disk files. The software creates an instance of the game when you run it and it remains running until you stop it or the system goes down. There is also no way to establish a time limit on a game.

That’s pretty much the extent of the functionality changes. Now, on to the code.

First, there is a login function. That function looks in the player database struct and, if the player name doesn’t already exist, it creates an account there. If the name does exist, it prompts for a password, matches it against the stored password, and logs you in if the two match. There is not yet a way to change your password.

Once a player is logged in, he is faced with a sector description and a ‘choice:’ prompt. Any character that the player types at this prompt is immediately acted on as if it were a command. There is a string called ‘commlist’, short for command list, containing the letters and characters that are used as commands. A function pointer array, ‘fp[]()’, is used to store the locations of the command functions. Another function called ‘commscan()’ looks up the command letter typed and returns an index into the fp[]() function pointer array. This combination of the commlist string, the commscan function, and the fp function pointer array constitute the command processing loop of the game, as shown below:

userndx=commscan(
tolower(
threc[th].inbuf
[threc[th].inptr][0]
),commlist
);
result=fp[userndx](th);

These lines are embedded in a loop where each user’s input thread, represented by the variable ‘th’, is examined for new input. The new input arrives in string called inbuf. Since each thread has several buffers, an index called inptr is used to keep track of which one is currently being processed.

And, as described above, commscan is used to extract the appropriate function index and place it in a variable called ‘userndx’. Then, userndx is used to index into the fp function pointer array and the thread index (th) is passed to the appropriate command-processing function.

There’s a small amount of misdirection in that first line but, once understood, it becomes trivial to add additional commands. Basically, you just need to replace the placeholder function, ‘nullrtn’, with your new function name adjacent to the command letter you select in the fp[]() definition list.

The new functions are discussed below.

makemap()
In keeping with the evolving game nature of this project, several actual game functions have been added. The first new function, makemap(), builds a 20,000-sector single-galaxy universe and populates it with objects. By changing the GAMESIZE constant, you can build a universe of arbitrary size, but make sure you don’t try to build a universe so big as to consume too much of your system’s memory. I’ve tested universes of up to 1,000,000 sectors, which seem to work just fine. The makemap() function randomly puts ports, planets, fleets of neutral fighters, and nebulas in various sectors throughout the universe, and interconnects the sectors with randomly-generated one-way ‘warps’. Note that planets and ports are randomly given varying productivity's and random initial inventories of our three commodities: Iron, Alcohol, and Hardware.

Note that makemap() builds each sectors array of six warps, sorts them into ascending order, and then looks for duplicate warps. If any duplicates are found, it decrements the loop-controlling variable and simply rebuilds that entire sector from scratch. The sorted warps are a convention that we will maintain throughout the project. Each new galaxy type that we add in the future will adhere to that convention and other functions (and users) will be allowed to assume that warps are in ascending order.

command()
We have added a function to simply list the implemented commands and a short line of description. For now, the function is attached to the command letters ‘?’ and ‘H’ via the function pointer array and the commlist array. This function is mostly useful to illustrate a design limitation. The output of any single sprintf buffer-building function cannot exceed the MAXLINE buffer size constant. This function produces a single buffer out output very near the current 511-byte limit and will soon have to be split to produce two buffers of output. Assuming, of course, that we don’t increase the MAXLINE buffer size.

jettison()
The ‘J’ command activates the jettison() function. It’s only purpose is to dump any cargo out of your cargo holds. It illustrates the method we use to let a function that requires multiple characters of input -- or simple confirmation -- to temporarily turn off command processing and send the next character of input back to it. Jettison requires confirmation so that you don’t accidentally dump your cargo just by hitting the J key. (Since all commands operate as ‘hot’ keys you do not have to hit the [Enter] key to activate a command -- each command immediately executes as soon as you press a key.)

Each command function has the ability to set the thread’s control variable to it’s own command letter. That way, the command processing loop can simply check ‘control’ pass the next buffer of input directly to whichever function is indicated.

warprtn()
jumprtn()
The warprtn and jumprtn functions process requests to move to another sector. warprtn() processes the commands 1, 2, 3, 4, 5, and 6, which represent a user’s request to move to the first through the sixth sector number in the warp list, respectively. jumprtn() processes requests to move to a randomly selected sector in the warp list via the ‘-’ (or ‘=’) key. jumprtn() also implements commands to move to the next larger sector, the next smaller sector, as well as the largest and smallest sector number via the ‘.’, ‘,’, ‘>’, and ‘game design and the programming model. The second article discusses the IO handling code and more of the details of the programming model.]

[Update, June 25, 2005: A Space Tyrant home page has been created as a central index to the various ST articles, links, and files.]

[Update, March 21, 2007: Space Tyrant now has a website of its own!. This site is new but growing and will be the quickest way to find new information and code on the Space Tyrant project.]
mail this link | permapage | score:9222 | -Ray, May 30, 2005 (Updated: March 21, 2007)

Free versions of Arial, Courier New, and Times New Roman fonts

Up
vote
Down

Red Hat releases free replacements for Windows core fonts...
Available for immediate download, the Liberation fonts are intended to let users share documents between free operating systems and Windows without involuntarily reformatting the documents because the fonts don't match. The Liberation fonts are designed to be metrically equivalent to the Windows core fonts, with each letter occupying the same horizontal space as its equivalent in a proprietary font.

Red Hat has a long history of interest in high-quality fonts that allow interoperability between operating systems.
read more...
mail this link | permapage | score:9213 | -Ray, May 19, 2007 (Updated: May 20, 2007)

Create a bootable Ubuntu 10.04 USB drive from Windows

Up
vote
Down

Put Ubuntu 10.04 in your pocket on a USB thumb drive from your Windows box...
The first method will create a bootable USB stick with a live version and the second process will create a live version with persistence. Both methods are an excellent way to always have your favorite Ubuntu system and software with you at all times and it makes for one of the simplest ways to conduct an install to a hard drive.

First you need a linux image to put on your USB drive. You can obtain the latest Ubuntu file from this location. http://releases.ubuntu.com/lucid/ Unless you have some specific reason to choose differently, choose the Ubuntu Desktop 10.04 LTS (32 bit) file.
read more...
mail this link | permapage | score:9212 | -Ray, May 2, 2010

Linux vs Mac OS X

Up
vote
Down

Five ways Linux beats Mac OS X...
First is the question of permissions: Linux users are not automatically given administrator privileges on their computers, meaning that viruses and malware don't automatically have access to everything in the proverbial "castle." So, when a computer is compromised, the most the malware can typically do is trash the user's local files and programs.

With Apple, on the other hand--as with Windows--social engineering is painfully easy. Just convince the user to click on something, and away you go, with the castle keys in hand.
read more...
mail this link | permapage | score:9177 | -Ray, October 4, 2010

Space Tyrant: A threaded game server project in C

Up
vote
Down

[Update, June 25, 2005: A Space Tyrant home page has been created as a central index to the various ST articles, links, and files.]

[Update, March 21, 2007: A Space Tyrant has its own website! It's small but growing and will provide quick access to the latest code and developments in the ST universe.]

Space Tyrant: Today we kick off a new multithreaded, network socket programming project which we will call Space Tyrant. Our mission is to write an open source, multiplayer, networked, strategy game in the C programming language. The goal of this project is to make a solid code base which implements a simple space trading game upon which other games can then be built. The game will be a subset of The Last Resort (TLR) that currently runs at [offline]. This project will be a learning exercise for me as well as for any interested readers. The current state of the source code will be released with each article update.

The game design: While my TLR game consists of over 25,000 lines of C source code and supports a web interface as well as telnet and a graphical client, this code will be far smaller and simpler. It will initially only support telnet and will implement a far simpler game design.

Players will be able to telnet into the game, create an account, and play in a universe that contains ports, planets, as well as other players. Each player will be issued a starship, some cargo holds, and an amount of starship fuel. Additional fuel will be issued hourly and will accumulate in the starship. Fuel will be used to move the ship between sectors -- locations within the game universe -- and to dock with ports. Once a ship runs out of fuel it can't move at all until new fuel is issued.

Players will be able to buy and sell commodities (Iron, alcohol, and hardware) between the three different kinds of ports. Each port type will sell one of the three commodities and buy the other two. Prices will be based on supply and demand with rarely-used ports offering the better prices.

With the money players earn trading they will be able to buy more cargo holds to make their ships more efficient for trading. They will also be able to buy fighters -- small military drones -- that can be used to attack other ships or deployed to guard a sector and its contents. The fighters carried with a ship will guard it against attacks from other players.

Games will run for a predetermined length of time, then reset and start anew.

The programming model: Now, on to the software design. I've compared and considered various models for the server design. TLR is based on the forking model using inetd or xinetd to handle the listening and forking. While the forking model is inherently distributable to multiple processors, it introduces inefficiencies (forking multiple processes) and makes interprocess communications more difficult and slower.

Next, I considered a non-blocking, single process model. In this approach, one process handles everything in a single thread. It would use non-blocking IO (read and write functions that never wait for completion but, rather, return immediately if they aren't ready to read or write actual data). The thttpd web server is an example of a non-blocking, single process server. It's extremely fast and efficient. However, this model is quite complicated to code, and, I believe would make it more likely to introduce subtle timing bugs.

Next, I considered a pure multithreaded, single process model with a thread for each player. While appealing in many ways, this model would require the same kind of coordination between threads that the forking model requires between processes. Such interprocess communication would be simplified in that the various threads share memory, but the coordination issues otherwise remain the same.

Last, I considered another multithreaded model, this time with only IO threads for each user and a single thread that implements all game logic. While that one central thread might someday be a bottleneck that limits scalability on large SMP systems, it does distribute the IO on any additional processors that might be present, and requires minimal coordination. In short, this model combines the logic simplicity of the non-blocking single process model with the coding simplicity of the threaded model, while separating the IO from the main logic. There will also be two other simple threads in this model. There will be a thread that listens for new connections and spawns the IO threads for each new connection. There will also be a thread that writes the data to disk periodically.

This is the approach that I intend to take for this project. The code will be written for both Linux and Mac OS X.

More info: I have set up an email address for programmers following this series to provide recommendations, bug reports, and other feedback. Send email about this project to spacetyrant [at] librenix.com.
mail this link | permapage | score:9174 | -Ray, March 18, 2005 (Updated: July 26, 2008)

Tutorial: UDP socket based client server C programs

Up
vote
Down

This short and sweet tutorial has the example C source code for both a UDP client and server.
This article explains how to write a simple UDP client/server system in C for the Linux or Unix platform. Writing client-server applications using UDP sockets is considered very easy, yet we sometimes miss some essential steps, which results in spending more time in intense debugging sessions. I went through one such experience. It is quite hard to remember all the socket API details quickly without having to wade through tons of man pages, so this page lists the standard code for UDP Client Server program. I hope it will be useful for others, too.
[The original article has gone offline and the link has been replaced by a working page. -Ed.] read more...
mail this link | permapage | score:9163 | -Ray, February 23, 2004 (Updated: August 24, 2008)

Space Tyrant: Multithreading lessons learned on SMP hardware

Up
vote
Down

There is much to report in this update of Space Tyrant. Before getting into the new features and functions, I’ll dispense with the crisis of The Bug.

For a couple of weeks, we had been noticing odd anomalies with Space Tyrant (ST) running on the virtual server at Ioresort.com (now offline -Ed.). We never saw the problem on any other box -- and it was tested on at least four other Linux boxes and a Mac OS X system. We did all manner of stress testing, locally and over the Internet, script based and even feeding the game the output of /dev/random. Nothing caused the anomaly on any other box.

At first, I suspected that it might just be an obscure problem with the virtual server itself; after all, I had been forced to modify the TLR code to get it to run properly there. That problem turned out to be merely a limitation of NFS, not a bug with the virtual server software. However, the environment was clearly different from any other system I had used which raised my suspicions -- and reduced my urgency about looking for the bug.

While the bug wasn’t frequent, it was persistent. The bug appeared to be related to corrupted buffers or corrupted buffer indexes. Out of idle curiosity, I lowered the number of buffers used by ST to see if that affected the bug. Somewhat counter-intuitively, it substantially raised the frequency of the problem.

Brian Estabrooks (the hero of this release) and I spent more and more of our efforts hunting this incredibly elusive bug until that was all we were doing. I implemented various diagnostic routines hunting for clues. The all seemed to point to buffer indexes being changed incorrectly. Both Brian and I audited the code. It seemed impossible for the indexes to be changed improperly. Brian even went so far as to replace the ring buffer scheme with a high watermark approach but to no avail.

While I continued to suspect it to be a simple logic error in the code, Brian turned his efforts elsewhere. What he came up with was quite interesting. It seems that on many hardware architectures (most? all?), modifying a bit field can temporarily modify other bit fields in the same word! Now, this isn’t a problem on a single-CPU system; it repairs the damage in the same operation, making it, effectively, atomic. On an SMP machine, however, two different CPU’s working on different bit fields of the same word simultaneously create havoc. The operation isn’t really atomic and it doesn’t work.

Did I mention that the virtual server is a 4-way Xeon system?

The ring buffer indexing in ST relies on unsigned integer bit fields to automate wrapping back around to the first buffer after using the last one. My parsimonious programming, of course, packed all the bit fields together, several to a word. Brian’s test version of ST added a pad after each buffer index to round it out so that each bit field lived alone in its own complete word. We abused the new version for nearly an hour before either of us would dare say it. The bug was gone.

Yay!

So, the moral of this story is: Operations on sub-word fields affect other bits in that word (at least on many hardware architectures). Tread very carefully if multiple threads are accessing different bits in shared words. It may appear to work perfectly, only to crumble into a pile of smoldering rubble the first time it's loaded on a multiple CPU system!

Other than the primary lesson, some other good things came out of (the search for) the bug. Several other latent bugs were found and fixed and Brian and I are both much more intimate with the code.

And, on to the enhancements. ST is starting to look like an actual playable game. The following functions implement the new major features.

players(): We now have player rankings. It works by adding all the players’ ship resources to an integer array. Then it scans the universe looking for deployed fighters and adds those to the array as well. Currently, those two items comprise the total strength of a player.

It then sorts the array with a recursive bit-plane sort that I wrote for Starship Traders in 1998. The qsort() function in the C library was plenty fast, but took too much memory for my taste. Memory was a bit scarcer in those days and, worse, the SST software model gave each player his own copy of the server.

The sort reorders the array in place as follows. It scans the high-order bit in each element of the array. It then moves all elements starting with ‘1’ bits to the top and all starting with ‘0’ bits to the bottom. Next, it calls itself twice to reorder the first and second chunks of the array on the second bit. Each of those two instances of the sort then call the sort twice again, now giving 4 new sorts for the third bit, and so on. When all 32 bits are accounted for, the array is in the correct order with the top player on top, etc.

Scanning the entire universe can be expensive with a large map. Therefore, the player rankings function keeps the result and time stamps it. If another player asks for a player ranking within five seconds, the system just gives them the old one. After five seconds, however, any new request triggers a fresh listing.

autopilot(): We’ve added an autopilot to let a player find a specific sector -- or to locate the nearest planet. If you type a ‘0’ (zero), you’ll be prompted for a sector number within 1000 of the sector you’re currently in. You then will have the option of pressing ‘/’ to automatically warp to the destination sector.

If you’re looking for a planet, type the ‘L’ command that you would normally use to land on a planet in your sector. In the absence of a planet, the L key will engage the autopilot which will search for the nearest planet and give you a ‘/’ command to autowarp to it.

The new autopilot function consists of two other functions in addition to autopilot(), which is merely a control function. I had intended to use the old shortest path algorithm function from TLR but it was big and complicated. I decided to try to write a simpler, recursive shortest path algorithm instead. The new recursive function is much simpler but not quite as efficient as the giant for loop in TLR.

The actual algorithm is implemented in two functions called pathdepth() and pathcalc(). The pathdepth() function repeatedly calls pathcalc() with an increasing ‘depth’ parameter. ‘Depth’ tells pathcalc() how many levels deep to search before giving up.

The pathcalc() function simply looks to see if the sector it is looking at is the target sector. If not, it calls itself for each new sector that the current sector connects to. If the current sector is the target sector, it starts filling in an array for the autowarp() function to follow to reach the target sector. As the previous recursive calls to the pathcalc() function exit, they fill in the remainder of the path array.

And, yes, I seem to like reinventing the wheel. ;-)

The other interesting addition to the code is the backup thread. It is implemented by a function called backupdata() and works as follows: It scans the player data, the map data, and the history data looking for ‘dirty’ flags. (Whenever any persistent data is changed anywhere in the game, a dirty flag is set to tell the backup thread to write it out to disk.) This process is quite fast for a small game, but for a game with millions of sectors, it’s a significant waste of resources to scan the dirty flag array frequently.

Therefore, for the map and history data, I’ve implemented a ‘dirty block’ scheme as well. When a dirty flag is set, its corresponding dirty block flag is set too. Then, the backup thread need only scan the dirty block arrays, typically only about one percent the size of the arrays it represents. When a dirty block is found, only the hundred or so records it points to are scanned to find the actual dirty records for backup.

The backup file, named ‘st.9999.dat’ -- where ‘9999’ varies with the port number you run the game on -- goes into the current working directory from where you start the daemon. If the file doesn’t exist, a new game is started. Also, if you’ve modified the game in a way that changes the size of the data -- by increasing the map size, for example -- it will start a new game upon startup.

The game can be shut down from the command line by sending a signal 15 (kill -15 pid) or by the admin with the ^ command. Note that the first player to create an account in a new game automatically becomes the admin of the game!

makehistory(): The storing of historical data is new as well. Whenever another player attacks your ship while you’re logged off, you’ll get a report of the action and any losses when you next log on. Also, for remote deployed fighters, you never get immediate notification, so that information is stored in the history log even if you're logged on when it happens. You can view any accumulated event information since your login time by pressing the ‘e’ key.

deploy(): This simple function allows a player to deploy, or retrieve, guard fighters in a sector. Those fighters will not let another player pass through or view any of the contents of that sector. Any ships parked under the fighters are automatically protected against all attacks except for an attack by the fighters’ owner. Once the fighters are destroyed, of course, all ships there are visible and can be attacked.

There is also a newly implemented time limit in the game to limit the total online time of a day’s sessions to 4 hours. Like most other parameters, it can be changed by modifying a #define statement near the top of the code.

command(): The help page, a menu of available commands that a player can perform, has been redesigned and rewritten. This menu is attached to the '?' key.

The old debugger thread is gone, replaced by an in-game command function called showdata(). Press the ‘z’ key to see information on buffers, buffer indexes, and the backup thread’s state and history. Only if you’re serious about modifying the code will this information be useful.

The section of the gameloop thread that broadcasts radio and news messages has been modified to show only one of each type of message per pass. That way, replaying a long radio history won’t flood the output buffers and longer radio and news histories can therefore be retained.

The old jumprtn() movement function has been consolidated into the warprtn() function. It’s only slightly more complicated than having them separate.

The current source code can be downloaded from http://librenix.com/st/st.158.c. and the original article in this series is here. As usual, the compile script is embedded in the comments at the top of the source file. You’ll have to rename the source st.c for the script to work unchanged.

[A Space Tyrant home page has been created as a central index to the various ST articles, links, and files.]
mail this link | permapage | score:9153 | -Ray, June 26, 2005 (Updated: July 26, 2008)
More articles...
Buy Large Wall Art Prints

Selected articles

Beneficial Computer Viruses

Space Tyrant: A multiplayer network game for Linux

Linux dominates Windows

Librenix T-Shirts and Coffee Mugs!

Scripting: A parallel Linux backup script

How to install Ubuntu Linux on the decTOP SFF computer

The life cycle of a programmer

Graffiti Server Download Page

Why software sucks

Programming Language Tradeoffs: 3GL vs 4GL

No, RMS, Linux is not GNU/Linux

Closed Source Linux Distribution Launched

The short life and hard times of a Linux virus

Mono-culture and the .NETwork effect

Testing the Digital Ocean $5 Cloud Servers with an MMORPG

The Supreme Court is wrong on Copyright Case

Why Programmers are not Software Engineers

Tutorial: Introduction to Linux files

Missing the point of the Mac Mini

Space Tyrant: A threaded game server project in C

Space Tyrant: Multithreading lessons learned on SMP hardware

Microsoft to push unlicensed users to Linux

Download: Linux 3D Client for Starship Traders

Apple to Intel move no threat to Linux

Linux vs. Windows: Why Linux will win

MiniLesson: An introduction to Linux in ten commands

Hacker Haiku

The Real Microsoft Monopoly

Space Tyrant: A threaded C game project: First Code

The Network Computer: An opportunity for Linux

VPS: Xen vs. OpenVZ

Apple DIY Repair

Shadow.sh: A simple directory shadowing script for Linux

 

Firefox sidebar

Site map

Site info

News feed

Features

Login
(to post)

Search

 
Articles are owned by their authors.   © 2000-2012 Ray Yeargin