Category Archives: Quant Development

Quant Development

Great tips for those taking Bloomberg Brainbench Previsor c++ online test

Find the middle element of a singly linked list
What are your favorite coding features about C/C++?
Answer Question
How would you form an index for a book? i.e. finding the words specified, ordering the page numbers per word (needed to discuss data structures a bit)
More tips from Graham at Velocity Reviews dot com.
I know there a good few companies out there that won’t let you in the
door without an A score in brainbench. www.brainbench.com

It’s easy to get a high score if you study. Be prepared to get a lousy
score first time (unless you’re name is victor bazarov), then you’ll
get a feel for the type of questions they ask (you’ll be disgusted the
first time you take the test). The second time round you’ll be prepared
and will score ok. things that they ask include order of construction
for classes that inherit virtually, using set_new_handler correctly,
being able to parse the syntax for pointers to functions e.g. this type
of stuff (except uglier);

int (* minus)(int,int)

other stuff includes the unnamed namespace, ugly pointer arithmetic,
assumptions about the size of various datatypes (whats platform
specific, what isn’t), exceptions, new style casts (with pointers,
references, unrelated types etc, exceptions thrown when casting
pointers versus bad_cast with references, They offer a sample test at;

http://www.brainbench.com/xml/bb/com….xml?testId=54

as Mister Bazarov says it won’t make you a good programmer but it might
get you to interview stage.

For the record I managed to get the top score of all the candidates
interviewed however the test was in no way a good benchmark of my
ability…. I definitely ain’t that good and struggle a lot of the time
(fook I hope that doesn’t come back to haunt me). Start by taking their
sample test if you’re interested. Don’t pay them any money though. If a
company needs a brainbench score before they interview you, they
usually pay. Victor Bazarovs advice is sound, just program if you want
to be profecient.

more resources:
Brainbench have free C++ tests (and quite a few others)
http://www.brainbench.com/

Loads of interview questions here
http://www.techinterviews.com/interview-questions/c
http://www.techinterviews.com/c-interview-questions-and-answers-3

Online C++ test here
http://www.cprogramming.com/cgi-bin/quiz.cgi

Look at C/C++ Program Perfometer
http://lists.sourceforge.net/lists/l…rfometer-users
that contains various testsuites ( http://lists.sourceforge.net/lists/l…rfometer-users )

Yes have a c++ compiler on hand to retype questions. You have three minutes for each question asked. Do note some questions may be an actual image so that they cannot be copied in text format.
From: http://www.anomalous.net/brainbench.html
int + char = ?
If you care, you cast. In other words, if this is a significant issue in your code, you’re doing something wrong. You shouldn’t assume that the next person to come along will understand type promotion as well as you do. Incidentally, the result is an int.
How do you declare a pointer to a class method?
Seems like a reasonable question, right? Except then you remember the concept behind encapsulation: you really don’t want to look at the implementation of any method in a class hierarchy, since that can really mess you up if you’re handling an object of the wrong type. In other words, this is just something that’s conceptually impure…and likely to lead to problems when you need to maintain the code. If you really, really need to do this, remember that it’s not void (*class::method)(int, int), the way you might expect — it’s void (class::*method)(int, int). To me, in addition to asking about a questionable practice, it’s also a trick syntax question.
What does the “mutable” modifier do?
This is actually a reasonable, but somewhat obscure, question. It’s used on two ways; one that strikes me as poor practice, and one that is important but uncommon. The mutable modifier makes a member variable non-const under all circumstances; it forms an exception even when the surrounding object has been declared const. This is, in my opinion, a Bad Thing To Do; it changes the state of an object that is supposed to be stable. On a positive note, this can be very important as an optimizer hint when dealing with hardware registers. In such a circumstance, you’re really telling the optimizer to make sure it reads the memory location each time it’s accessed, and not to make the assumption that the value hasn’t changed. So 50/50 here, in my book; the sample in the test is an example of misleading coding practices, but the modifier does have value in specific circumstances.
What is a null namespace and how is it used?
This is an obscure question. Null namespaces, or namespaces that haven’t been given a name, are recent additions to the C++ specification and are unfamiliar to many C++ developers. The functionality wasn’t even supported prior to version 4 of g++. Since gcc 3.4.6 is distributed with RedHat Enterprise Linux version 4, this functionality is not widely available to UNIX/Linux developers yet. It’s also one of those very strange things that periodically make it into the language; I’m sure there’s a good reason that it’s there, but I certainly can’t think of it. Null namespaces provide another mechanism for variable scoping; any variables declared in the un-named namespace are local to the file where they were declared, as if they were static. They are accessed as if they weren’t in a namespace at all.

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!

The connection options available within Active Quant, Open Tick, Activ, Interactive Brokers, Onanda, Tenfore, Google, Yahoo

The connection options available within Active Quant, Open Tick, Activ, Interactive Brokers, Onanda, Tenfore, Google, Yahoo
So those are the options. Downloading the source code for Active Quant is straight forward seems to load within Eclipse fine. Be aware there is no front end graphical user interface for this Java based framework. I have posted enough info on this.
Why Active Quant? I would ask more with Marketcetera or QuantLib. From my research, Active Quant is the only sample Java source code I have seen thus far. I have broken down the code and it seems straightforward by analyzing. It has straightforward configuration XML files, testing client code, and the code itself. The next question is how does it perform or how easy is it to work. That is another story.
Let’s hope that Active Quant will be smart in their online documentation. I have not seemed to install and successfully get the market feed downloading from Yahoo. I have had some success with Marketcetera which it all came down to their You Tube channel videos. I just wish Active Quant had the same type of facilities in order for developers like me to get up and running fairly quickly. We shall see once I find the time to do this.
If you can afford the eighteen hundred dollar a month Activ wants, I would imagine you might be more interested in paying your mortgage. I do understand they are fairly professional as well but I would expect that as they are even more expensive than Bloomberg.
So, my next option is Interactive Brokers as they are fairly affordable (I think) for access to their APIs and data feeds. I also have heard it can be fairly slow but that would not be a concern as this is just at a development stage for now. Let me know what you think thus far.

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!

Does anyone know of an affordable Open Tick alternative provider for proper backtesting?

Does anyone know of an affordable Open Tick alternative provider for proper backtesting?

So here we are resuming my research and investigation on the capabilities of a Quant based Java framework like Active Quant. It seems that connection facility into Open Tick is no good as Open Tick is no longer in business. The reason people have decided to go with Open Tick is because of their vast data feed and being affordable. It seems Open Tick are now out of business since Feb 2010. That is a shame so I am on a hunt for alternative.
As being any other dude with a budget of one thousand to two thousand dollars per month, I clearly cannot afford something like a Bloomberg or Active Financial market feed. It has been recommended to look at a few sites which list some Open Tick alternatives. These include:
http://www.bearcave.com/software/market_trading/resources_and_notes/Market%20Tick%20Data.html
or
http://blog.fosstrading.com/2009/11/opentick-alternatives.html
Based on the first link, there seems to be somewhat affordable market data feed providers. One is listed at sixty dollars a month but goes up to four hundred. I say ouch. None the less, without real market I cannot really forward on any research. Does anyone have any affordable recommendation to go with who might be an Open Tick alternative? Let me know by commenting below.
The other question I have is why did Open Tick go out of business. As according to the second link, a message from Open Tick was sent out. It went something like:

To opentick subscribers, friends, supporters, contributors and the rest of the community…

It has been quite a journey for opentick, and for those of you who have been with us for the ride we cannot thank you enough for the support, contributions and guidance you have given us over the course of the last 5 years. We could not have come as far as we have without you.

However, we are sad to say that the time has come for us to close the doors for opentick. However, this isn’t goodbye. In fact, it’s a new beginning. We will be introducing a fresh service with all the bells and whistles we’ve been slaving on over the course of the last year under a new name, a new website and a new level of service. Check back here in the near future for more information about the forthcoming new company and service. Of course amongst all these changes, there are some things that will remain the same – we still aim to provide a reliable free market data service, with an open architecture for a wide range of software platform support.

If you are a current paying subscriber, this March billing cycle will be your final billing cycle; at your next billing date service will be terminated. If you are a delayed or historical data user not currently paying for service, your account will be deactivated as of Friday, March 20th.

Sincerely,
opentick Staff

This is why you don’t go with Mickey Mouse operations. But surprisingly, I was surprised Open Tick would have been classified as this. Too bad I say. But stick with the larger operators who you know won’t go out of business. It would be a shame to develop an infrastructure and your broker feed provider goes bust. That would be a mess for your systems you develop. I just wish they were more affordable. Still looking. As said, if you know an affordable one, let me know.
Thanks

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!

Know your data structures, algorithms, design patterns and test case uses for any technical job interview!!

Know your data structures, algorithms, design patterns and test case uses for any technical job interview!!
This might not be quant related but if you are applying for any development type roles (name me a quant based position that is not), ensure you know the notes below.
Let me stress this (once again right), know these topics really well. I have done at least seven interviews which involve one hundred percent rate of design patterns. I was kind of dumbfounded on the level of detail interviewers go into on this. There are some great resources out there but it might be wise to pick up a copy of Gang of Four in Design Patterns. This seems to be to the bible in this arena. They also started the concept of this.
We were all taught data structures in our early years of computer science courses. You need to know these areas well too. Understood the logic and reasoning of why these data structures are used. Focus on the basic ones including stack, heap, singly and double linked list, binary search tree, and even graphs. Also understand the concept of the Big O algorithms which involved time and space complexity. A great site to learn this from is algolist.net. It has some great tutorials to have you understand this data structures with code snippets in both Java and C++.
Let’s get into the less talked about algorithms. Depending on the type of company you are applying to, I would recommend to learn the basics including Fibonacci, prime, factorial, and all the basic sorting algorithms. These include your typical sorting like bubble, selection, insertion, merge, and quick sort. Again know the time complexity of each sorting algorithm. A good explanation of this is found at Wikipedia.
Regardless of what level of position you are applying, be expected to answer questions on these above concepts. You should consider emphasizing your knowledge in design patterns if you are going for a senior role or even architect level.

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!

Easiest and best XML parser in C++ or C tutorial with source code

Easiest and best XML parser in C++ or C tutorial with source code

Under Windows, when I have to debug a software that is using the XMLParser Library, it’s usually a nightmare because the library is sooOOOoooo slow in debug mode. To solve this problem, during all the debugging session, I use a very fast DLL version of the XMLParser Library (the DLL is compiled in release mode). Using the DLL version of the XMLParser Library allows me to have lightening XML parsing speed, even in debug mode! Other than that, the DLL version is useless: In the release version of my tool, I always use the normal, “.cpp”-based, XMLParser Library.
• V2.21: Mars 1, 2007: 1 minor change, 1 bug fix
• V2.22: Mars 6, 2007: 1 bug fix
• V2.23: Mars 13, 2007: 1 bug fix
• V2.24: April 24, 2007: 1 bug fix, 1 addition
• V2.25: May 18, 2007: 1 bug fix
• V2.26: May 22, 2007: 1 bug fix
• V2.27: May 28, 2007: 2 additions, 1 minor change, 2 bug fixes
• V2.28: June 27, 2007: 2 additions, 2 minor changes
• v2.29: July 3,2007: 1 bug fix
• v2.30: July 31,2007: 2 bug fixes, 1 addition
• v2.31: August 29,2007: 1 fix
• v2.32: October 4,2007: 1 addition
• v2.33: October 11, 2007: 1 addition
• v2.34: January 25, 2008, 2 additions
• v2.35: February 2, 2008: 1 minor change
• v2.36: March 9, 2008: 2 bug fixes, 2 additions, 4 minor changes
• v2.37: March 24, 2008: 1 bux fix
• v2.38: June 2, 2008: 3 additions
• v2.39: August 9, 2008: 4 additions, 2 bug fixes
• v2.40: December 19, 2008: 1 minor change
• v2.41: June 25, 2009: 1 minor change
o modified the functions ToXMLStringTool::toXML() and XMLParserBase64Tool::decode() to accept null string as input
A small tutorial
Let’s assume that you want to parse the XML file “PMMLModel.xml” that contains:

Hello World!
















Let’s analyse line by line the following small example program:
#include // to get “printf” function
#include // to get “free” function
#include “xmlParser.h”

int main(int argc, char **argv)
{
// this open and parse the XML file:
XMLNode xMainNode=XMLNode::openFileHelper(“PMMLModel.xml”,”PMML”);

// this prints ““:
XMLNode xNode=xMainNode.getChildNode(“Header”);
printf(“Application Name is: ‘%s’\n”, xNode.getChildNode(“Application”).getAttribute(“name”));

// this prints “Hello world!”:
printf(“Text inside Header tag is :’%s’\n”, xNode.getText());

// this gets the number of “NumericPredictor” tags:
xNode=xMainNode.getChildNode(“RegressionModel”).getChildNode(“RegressionTable”);
int n=xNode.nChildNode(“NumericPredictor”);

// this prints the “coefficient” value for all the “NumericPredictor” tags:
for (int i=0; i” (note that the “<” character entity has been replaced by “<"): printf("Application Name is: '%S'\n", xNode.getChildNode("Application").getAttribute("name")); The following command prints on the screen "Hello World!": printf("Text inside Header tag is :'%s'\n", xNode.getText()); Let's assume you want to "go to" the tag named "RegressionTable": xNode=xMainNode.getChildNode("RegressionModel").getChildNode("RegressionTable"); Note that the previous value of the object named xNode has been "garbage collected" so that no memory leak occurs. If you want to know how many tags named "NumericPredictor" are contained inside the tag named "RegressionTable": int n=xNode.nChildNode("NumericPredictor"); The variable n now contains the value 3. If you want to print the value of the coefficient attribute for all the NumericPredictor tags: for (int i=0; i


You can use:
char *t=xMainNode.getChildNode(“Extension”).createXMLString(true);
printf(“%s\n”,t);
free(t);
Note that you must free the memory yourself (using the “free(t);” function) : only the XMLNode objects
and their contents are “garbage collected”. The parameter true to the function createXMLString means that we want formatted output.

The XML Parser library contains many more other small usefull methods that are not described here (The zip file contains some additional examples to explain other functionalities). These methods allows you to:
• navigate easily inside the structure of the XML document
• create, update & save your own XML structure of XMLNode’s.
That’s all folks! With this basic knowledge, you should be able to retreive easily any data from any XML file!

http://www.applied-mathematics.net/tools/xmlParser.html

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!

This is easily the best and simplest C or C++ Posix compliant Linux Unix multi thread pthread tutorial I have found

This is easily the best and simplest C or C++ Posix compliant Linux Unix multi thread pthread tutorial I have found

http://www.mobydisk.com/softdev/techinfo/pthreads_tutorial/

POSIX threads tutorial
This is a short tutorial on how to code POSIX threads in C++ on GNU/Linux. For up-to-date information on the Linux implementation of pthreads called LinuxThreads, check out Xavier Leroy’s page.
What you need
Any modern Linux should be able to do this. For specific requirements, check outthe link above. This tutorial does not explain what threads are, how they work, or how to synchronize multiple threads. It assumes you know what you plan to use threads for, and that you have a good reference on synchronizing threads. If you don’t have any of this information, then visit the Links section at the bottom of this page.
Compiling this demo
At your Linux prompt type:
g++ threaddemo.cpp -o threaddemo -lpthread -D_REENTRANT
then:
./threaddemo
Program description
This sample program will create 2 threads. One thread displays the word “ping” and the other displays “pong.” The resulting output will look something like this:
ping,pong,ping,pong,ping,pong,ping,pong,ping,pong,ping,pong,ping,pong,ping,pong,
ping,pong,ping,pong,ping,pong,ping,pong,ping,pong,ping,pong,ping,pong,ping,pong,
ping,ping,pong,ping,pong,ping,pong,ping,pong,ping,pong,ping,pong,ping,pong,ping,
pong,ping,pong,ping,pong,ping,pong,ping,pong,ping,pong,ping,pong,ping,pong,ping,
pong,ping,pong,ping,pong,ping,pong,ping,pong,ping,pong,ping,pong,ping,pong,ping,
pong,ping,pong,ping,pong,ping,pong,ping,pong,ping,pong,ping,pong,ping,pong,ping,
Done!
Sample source
The sample source code is threaddemo.cpp. You will also need Morgan McGuire’s great kbhit.cpp which makes things a little easier.
The how-to
The pthreads library hides out in the pthread.h file, as you can see below.
#include #include
#include “kbhit.cpp”

// On Linux, you must compile with the -D_REENTRANT option. This tells
// the C/C++ libraries that the functions must be thread-safe
#ifndef _REENTRANT
#error ACK! You need to compile with _REENTRANT defined since this uses threads
#endif
Whenever compiling an application for pthreads, you must use the -D_REENTRANT option to inform the C/C++ libraries that they are in multithread mode. The #ifndef _REENTRANT line verifies that this is the case before allowing the compile to continue. Further, you should only make calls to other libraries that were compiled with -D_REENTRANT. If you need to know if your favorite library is compiled with this code, seek the author or the documentation. For more information about this subject see the links at the bottom of this page.
volatile int bStop = false;
The bStop variable is used to tell the threads when to stop. If the variable is not declared volatile, then the compiler may think that the value is always false, and may not even pay attention to it! More later.
////////////////////////////////////////////////////////////////////////////////
// Starting point for a thread
////////////////////////////////////////////////////////////////////////////////
void * runMe(void *generic_pointer)
{
// Do this until we are told to stop
while (!bStop)
{
// Output some text
cout << static_cast(generic_pointer) << ","; // Relenquish the CPU for another thread pthread_yield(); } return NULL; } This routine will be executed in each of the 2 threads that we will create. The function takes a void pointer, and returns a void pointer. This is necessary, since it is a requirement of the pthread_create function that we are about to use. The parameter, and the return value are arbitrary; that is, they can be used for whatever purpose you wish. In this demo, the parameter is assumed to be a char * that holds a string for the thread to display. In a real application, this parameter may be a pointer to a structure with information about what the thread needs to do. For example, imagine a multithreaded game, with a thread controlling each of the enemy characters. This parameter may be a pointer to the character's information, with the character position, intelligence, weapons, etc. The return value is not used here. It could be treated similarly to the parameter, in that it can be used to return some arbitrary data to the parent thread. It could be a pointer to an integer return value indicating success or failure, or an exception structure, or error message. The basic function of this thread is to display a message over and over, while not using up the entire CPU. The thread avoids hogging the CPU by calling pthread_yield() after displaying the message. This function tells the OS that the thread isn't busy and something else can go happen now. Note that the OS may not wait until the thread calls pthread_yield to switch threads. It may happen at any point. This call just tells the OS that this would be a good time to do so. More on this later. The thread will stop displaying the message once the value of bStop becomes true. At this point, the thread will exit with a return value of NULL. This return statement does not return control to the caller: it stops the thread from executing entirely. This is the equivalent to calling pthread_exit(NULL). pthread_t nThreadID1, nThreadID2; Every thread has an ID. Nothing special there. pthread_create(&nThreadID1, NULL, runMe, szMessage1); pthread_create(&nThreadID2, NULL, runMe, szMessage2); This code is kinda like doing: runMe(szMessage1); runMe(szMessage2); Except that the code executes in the background, instead of sequentially. After this code, there are 3 threads: the parent, and 2 children. Let's check out the pthread_create call. The first parameter is the address of nThreadID. pthread_create will stuff the thread ID into this. The second parameter has all sorts of nifty stuff about the thread: priority, scheduling, stack size, whatever. We don't care about any of this so it is NULL. Next is the name of the function to use as the starting point for the thread. The function must take a void * and return a void *. Lots of tutorials do this incorrectly, and must cast the function with some crazy C-style weirdness. If you declare the function exactly right, you can just place the name there and nothing else. The last function is the generic parameter, which we use for the message. while (!_kbhit()) { pthread_yield(); } At this point the child threads are running. The parent thread now loops until the user presses a key. It is crucial that this loop call pthread_yield() or the child threads may not execute. This is because the parent thread would spend all its time checking for a key, and never giving anyone else a chance to work. This behavior of threads varies with operating system, kernel, processor, phase of the moon, etc. Pthreads makes no guarantee that each thread executes "fairly." Threads may be interrupted at any time, for any length of time. If this isn't appropriate, then you may need some thread synchronization code, or to provide a hint during the pthread_create call about how you want the threads to behave. bStop = true; This changes the value of the bStop variable. When the threads check this value they will exit. If this value were not declared volatile, then it is possible that the threads may not notice it, and continue running forever! pthread_join(nThreadID1, NULL); pthread_join(nThreadID2, NULL); The pthread_join call tells a thread to wait until the specified thread has exited. This is why we saved the thread IDs. What happens if we remove this code? In this demo, nothing. The child threads will stop anyway, once the parent thread exits. In more complex code, you may need to wait for your threads to free their memory, close open files, etc. before your application exits. The pthread_join call also allows you to see the return value of the thread. Remember the void * return value? The second parameter to pthread_join() tells you where to put this value. For this application, we don't care what they returned. Further improvements A nifty improvement (left to the reader, of course) would be to have each thread return the number of times it displayed the message. In a perfect world, it would be the same for each thread. But as we know, threads aren't guaranteed fair excecution time! In theory, this code is POSIX compliant (except for the kbhit.cpp) and should run on other operating systems. You may wish to compile this code and run it on Coolix or Niftyux or FreeBVD or whatever and see if it works. Links Most of these are pretty much the first things you find in a search for threads, linux, and POSIX or some combination thereof. • Xavier Leroy is the author of LinuxThreads and his page has lotsa info. • posix threads programming • DMOZ links on Linux threads programming

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!

Simple C or C++ tutorial on how to socket program or build tcp client server technologies

Simple C or C++ tutorial on how to socket program or build tcp client server technologies

http://www.linuxhowtos.org/C++/socket.htm
This is a simple tutorial on using sockets for interprocess communication.

The client server model
Most interprocess communication uses the client server model. These terms refer to the two processes which will be communicating with each other. One of the two processes, the client, connects to the other process, the server, typically to make a request for information. A good analogy is a person who makes a phone call to another person.
Notice that the client needs to know of the existence of and the address of the server, but the server does not need to know the address of (or even the existence of) the client prior to the connection being established.
Notice also that once a connection is established, both sides can send and receive information.
The system calls for establishing a connection are somewhat different for the client and the server, but both involve the basic construct of a socket.
A socket is one end of an interprocess communication channel. The two processes
each establish their own socket.
The steps involved in establishing a socket on the client side are as follows:
1. Create a socket with the socket() system call
2. Connect the socket to the address of the server using the connect() system call
3. Send and receive data. There are a number of ways to do this, but the simplest is to use the read()and write() system calls.
The steps involved in establishing a socket on the server side are as follows:
1. Create a socket with the socket() system call
2. Bind the socket to an address using the bind() system call. For a server socket on the Internet, an address consists of a port number on the host machine.
3. Listen for connections with the listen() system call
4. Accept a connection with the accept() system call. This call typically blocks until a client connects with the server.
5. Send and receive data

Socket Types
When a socket is created, the program has to specify the address domain and the socket type. Two processes can communicate with each other only if their sockets are of the same type and in the same domain.
There are two widely used address domains, the unix domain, in which two processes which share a common file system communicate, and the Internet domain, in which two processes running on any two hosts on the Internet communicate. Each of these has its own address format.
The address of a socket in the Unix domain is a character string which is basically an entry in the file system.
The address of a socket in the Internet domain consists of the Internet address of the host machine (every computer on the Internet has a unique 32 bit address, often referred to as its IP address).
In addition, each socket needs a port number on that host.
Port numbers are 16 bit unsigned integers.
The lower numbers are reserved in Unix for standard services. For example, the port number for the FTP server is 21. It is important that standard services be at the same port on all computers so that clients will know their addresses.
However, port numbers above 2000 are generally available.
There are two widely used socket types, stream sockets, and datagram sockets. Stream sockets treat communications as a continuous stream of characters, while datagram sockets have to read entire messages at once. Each uses its own communciations protocol.
Stream sockets use TCP (Transmission Control Protocol), which is a reliable, stream oriented protocol, and datagram sockets use UDP (Unix Datagram Protocol), which is unreliable and message oriented.
The examples in this tutorial will use sockets in the Internet domain using the TCP protocol.

Sample code
C code for a very simple client and server are provided for you. These communicate using stream sockets in the Internet domain. The code is described in detail below. However, before you read the descriptions and look at the code, you should compile and run the two programs to see what they do.
server.c
client.c
Download these into files called server.c and client.c and compile them separately into two executables called server and client.
They probably won’t require any special compiling flags, but on some solaris systems you may need to link to the socket library by appending -lsocket to your compile command.
Ideally, you should run the client and the server on separate hosts on the Internet. Start the server first. Suppose the server is running on a machine called cheerios. When you run the server, you need to pass the port number in as an argument. You can choose any number between 2000 and 65535. If this port is already in use on that machine, the server will tell you this and exit. If this happens, just choose another port and try again. If the port is available, the server will block until it receives a connection from the client. Don’t be alarmed if the server doesn’t do anything;
It’s not supposed to do anything until a connection is made.

Here is a typical command line:
server 51717
To run the client you need to pass in two arguments, the name of the host on which the server is running and the port number on which the server is listening for connections.
Here is the command line to connect to the server described above:
client cheerios 51717

The client will prompt you to enter a message.
If everything works correctly, the server will display your message on stdout, send an acknowledgement message to the client and terminate.
The client will print the acknowledgement message from the server and then terminate.
You can simulate this on a single machine by running the server in one window and the client in another. In this case, you can use the keyword localhost as the first argument to the client.
The server code uses a number of ugly programming constructs, and so we will go through it line by line.
________________________________________

#include

This header file contains declarations used in most input and output and is typically included in all C programs.
________________________________________

#include

This header file contains definitions of a number of data types used in system calls. These types are used in the next two include files.
________________________________________

#include

The header file socket.h includes a number of definitions of structures needed for sockets.
________________________________________

#include

The header file in.h contains constants and structures needed for internet domain addresses.
________________________________________
void error(char *msg)
{
perror(msg);
exit(1);
}

This function is called when a system call fails. It displays a message about the error on stderr and then aborts the program. The perror man page gives more information.
________________________________________
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno, clilen, n;

sockfd and newsockfd are file descriptors, i.e. array subscripts into the file descriptor table . These two variables store the values returned by the socket system call and the accept system call.
portno stores the port number on which the server accepts connections.
clilen stores the size of the address of the client. This is needed for the accept system call.
n is the return value for the read() and write() calls; i.e. it contains the number of characters read or written.
________________________________________
char buffer[256];

The server reads characters from the socket connection into this buffer.
________________________________________
struct sockaddr_in serv_addr, cli_addr;

A sockaddr_in is a structure containing an internet address. This structure is defined in netinet/in.h.

Here is the definition:
struct sockaddr_in
{
short sin_family; /* must be AF_INET */
u_short sin_port;
struct in_addr sin_addr;
char sin_zero[8]; /* Not used, must be zero */
};

An in_addr structure, defined in the same header file, contains only one field, a unsigned long calleds_addr.
The variable serv_addr will contain the address of the server, and cli_addr will contain the address of the client which connects to the server.
________________________________________
if (argc < 2) { fprintf(stderr,"ERROR, no port provided "); exit(1); } The user needs to pass in the port number on which the server will accept connections as an argument. This code displays an error message if the user fails to do this. ________________________________________ sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); The socket() system call creates a new socket. It takes three arguments. The first is the address domain of the socket. Recall that there are two possible address domains, the unix domain for two processes which share a common file system, and the Internet domain for any two hosts on the Internet. The symbol constantAF_UNIX is used for the former, and AF_INET for the latter (there are actually many other options which can be used here for specialized purposes). The second argument is the type of socket. Recall that there are two choices here, a stream socket in which characters are read in a continuous stream as if from a file or pipe, and a datagram socket, in which messages are read in chunks. The two symbolic constants are SOCK_STREAM and SOCK_DGRAM. The third argument is the protocol. If this argument is zero (and it always should be except for unusual circumstances), the operating system will choose the most appropriate protocol. It will choose TCP for stream sockets and UDP for datagram sockets. The socket system call returns an entry into the file descriptor table (i.e. a small integer). This value is used for all subsequent references to this socket. If the socket call fails, it returns -1. In this case the program displays and error message and exits. However, this system call is unlikely to fail. This is a simplified description of the socket call; there are numerous other choices for domains and types, but these are the most common. The socket() man page has more information. ________________________________________ bzero((char *) &serv_addr, sizeof(serv_addr)); The function bzero() sets all values in a buffer to zero. It takes two arguments, the first is a pointer to the buffer and the second is the size of the buffer. Thus, this line initializes serv_addr to zeros. ---- portno = atoi(argv[1]); The port number on which the server will listen for connections is passed in as an argument, and this statement uses the atoi() function to convert this from a string of digits to an integer. ________________________________________ serv_addr.sin_family = AF_INET; The variable serv_addr is a structure of type struct sockaddr_in. This structure has four fields. The first field is short sin_family, which contains a code for the address family. It should always be set to the symbolic constant AF_INET. ________________________________________ serv_addr.sin_port = htons(portno); The second field of serv_addr is unsigned short sin_port, which contain the port number. However, instead of simply copying the port number to this field, it is necessary to convert this to network byte order using the function htons() which converts a port number in host byte order to a port number in network byte order. ________________________________________ serv_addr.sin_addr.s_addr = INADDR_ANY; The third field of sockaddr_in is a structure of type struct in_addr which contains only a single field unsigned long s_addr. This field contains the IP address of the host. For server code, this will always be the IP address of the machine on which the server is running, and there is a symbolic constant INADDR_ANY which gets this address. ________________________________________ if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error("ERROR on binding"); The bind() system call binds a socket to an address, in this case the address of the current host and port number on which the server will run. It takes three arguments, the socket file descriptor, the address to which is bound, and the size of the address to which it is bound. The second argument is a pointer to a structure of type sockaddr, but what is passed in is a structure of type sockaddr_in, and so this must be cast to the correct type. This can fail for a number of reasons, the most obvious being that this socket is already in use on this machine. The bind() manual has more information. ________________________________________ listen(sockfd,5); The listen system call allows the process to listen on the socket for connections. The first argument is the socket file descriptor, and the second is the size of the backlog queue, i.e., the number of connections that can be waiting while the process is handling a particular connection. This should be set to 5, the maximum size permitted by most systems. If the first argument is a valid socket, this call cannot fail, and so the code doesn't check for errors. The listen() man page has more information. ________________________________________ clilen = sizeof(cli_addr); newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) error("ERROR on accept"); The accept() system call causes the process to block until a client connects to the server. Thus, it wakes up the process when a connection from a client has been successfully established. It returns a new file descriptor, and all communication on this connection should be done using the new file descriptor. The second argument is a reference pointer to the address of the client on the other end of the connection, and the third argument is the size of this structure. The accept() man page has more information. ________________________________________ bzero(buffer,256); n = read(newsockfd,buffer,255); if (n < 0) error("ERROR reading from socket"); printf("Here is the message: %s ",buffer); Note that we would only get to this point after a client has successfully connected to our server. This code initializes the buffer using the bzero() function, and then reads from the socket. Note that the read call uses the new file descriptor, the one returned by accept(), not the original file descriptor returned bysocket(). Note also that the read() will block until there is something for it to read in the socket, i.e. after the client has executed a write(). It will read either the total number of characters in the socket or 255, whichever is less, and return the number of characters read. The read() man page has more information. ________________________________________ n = write(newsockfd,"I got your message",18); if (n < 0) error("ERROR writing to socket"); Once a connection has been established, both ends can both read and write to the connection. Naturally, everything written by the client will be read by the server, and everything written by the server will be read by the client. This code simply writes a short message to the client. The last argument of write is the size of the message. The write() man page has more information. ________________________________________ return 0; } This terminates main and thus the program. Since main was declared to be of type int as specified by the ascii standard, some compilers complain if it does not return anything. Client code As before, we will go through the program client.c line by line. #include
#include
#include
#include
#include

The header files are the same as for the server with one addition. The file netdb.h defines the structurehostent, which will be used below.
________________________________________
void error(char *msg)
{
perror(msg);
exit(0);
}
int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;

The error() function is identical to that in the server, as are the variables sockfd, portno, and n. The variable serv_addr will contain the address of the server to which we want to connect. It is of type struct sockaddr_in.
The variable server is a pointer to a structure of type hostent. This structure is defined in the header filenetdb.h as follows:
struct hostent
{
char *h_name; /* official name of host */
char **h_aliases; /* alias list */
int h_addrtype; /* host address type */
int h_length; /* length of address */
char **h_addr_list; /* list of addresses from name server */
#define h_addr h_addr_list[0] /* address, for backward compatiblity */
};

It defines a host computer on the Internet. The members of this structure are:
h_name Official name of the host.
h_aliases A zero terminated array of alternate
names for the host.
h_addrtype The type of address being returned;
currently always AF_INET.
h_length The length, in bytes, of the address.
h_addr_list A pointer to a list of network addresses
for the named host. Host addresses are
returned in network byte order.

Note that h_addr is an alias for the first address in the array of network addresses.
________________________________________
char buffer[256];
if (argc < 3) { fprintf(stderr,"usage %s hostname port ", argv[0]); exit(0); } portno = atoi(argv[2]); sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); All of this code is the same as that in the server. ________________________________________ server = gethostbyname(argv[1]); if (server == NULL) { fprintf(stderr,"ERROR, no such host "); exit(0); } The variable argv[1] contains the name of a host on the Internet, e.g. cs.rpi.edu. The function: struct hostent *gethostbyname(char *name) Takes such a name as an argument and returns a pointer to a hostent containing information about that host. The field char *h_addr contains the IP address. If this structure is NULL, the system could not locate a host with this name. In the old days, this function worked by searching a system file called /etc/hosts but with the explosive growth of the Internet, it became impossible for system administrators to keep this file current. Thus, the mechanism by which this function works is complex, often involves querying large databases all around the country. The gethostbyname() man page has more information. ________________________________________ bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);

This code sets the fields in serv_addr. Much of it is the same as in the server. However, because the fieldserver->h_addr is a character string, we use the function:
void bcopy(char *s1, char *s2, int length)

which copies length bytes from s1 to s2. —-
if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0) error("ERROR connecting"); The connect function is called by the client to establish a connection to the server. It takes three arguments, the socket file descriptor, the address of the host to which it wants to connect (including the port number), and the size of this address. This function returns 0 on success and -1 if it fails. Theconnect() man page has more information. Notice that the client needs to know the port number of the server, but it does not need to know its own port number. This is typically assigned by the system when connect is called. ________________________________________ printf("Please enter the message: "); bzero(buffer,256); fgets(buffer,255,stdin); n = write(sockfd,buffer,strlen(buffer)); if (n < 0) error("ERROR writing to socket"); bzero(buffer,256); n = read(sockfd,buffer,255); if (n < 0) error("ERROR reading from socket"); printf("%s ",buffer); return 0; } The remaining code should be fairly clear. It prompts the user to enter a message, uses fgets to read the message from stdin, writes the message to the socket, reads the reply from the socket, and displays this reply on the screen. Enhancements to the server code The sample server code above has the limitation that it only handles one connection, and then dies. A "real world" server should run indefinitely and should have the capability of handling a number of simultaneous connections, each in its own process. This is typically done by forking off a new process to handle each new connection. The following code has a dummy function called dostuff(int sockfd). This function will handle the connection after it has been established and provide whatever services the client requests. As we saw above, once a connection is established, both ends can use read and write to send information to the other end, and the details of the information passed back and forth do not concern us here. To write a "real world" server, you would make essentially no changes to the main() function, and all of the code which provided the service would be in dostuff(). To allow the server to handle multiple simultaneous connections, we make the following changes to the code: 1. Put the accept statement and the following code in an infinite loop. 2. After a connection is established, call fork()#### to create a new process. 3. The child process will close sockfd#### and call #dostuff#####, passing the new socket file descriptor as an argument. When the two processes have completed their conversation, as indicated by dostuff()#### returning, this process simply exits. 4. The parent process closes newsockfd####. Because all of this code is in an infinite loop, it will return to the accept statement to wait for the next connection. Here is the code. while (1) { newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) error("ERROR on accept"); pid = fork(); if (pid < 0) error("ERROR on fork"); if (pid == 0) { close(sockfd); dostuff(newsockfd); exit(0); } else close(newsockfd); } /* end of while */ Click here for a complete server program which includes this change. This will run with the program client.c. The zombie problem The above code has a problem; if the parent runs for a long time and accepts many connections, each of these connections will create a zombie when the connection is terminated. A zombie is a process which has terminated but but cannot be permitted to fully die because at some point in the future, the parent of the process might execute a wait and would want information about the death of the child. Zombies clog up the process table in the kernel, and so they should be prevented. Unfortunately, the code which prevents zombies is not consistent across different architectures. When a child dies, it sends a SIGCHLD signal to its parent. On systems such as AIX, the following code in main() is all that is needed. signal(SIGCHLD,SIG_IGN); This says to ignore the SIGCHLD signal. However, on systems running SunOS, you have to use the following code: void *SigCatcher(int n) { wait3(NULL,WNOHANG,NULL); } ... int main() { ... signal(SIGCHLD,SigCatcher); ... The function SigCatcher() will be called whenever the parent receives a SIGCHLD signal (i.e. whenever a child dies). This will in turn call wait3 which will receive the signal. The WNOHANG flag is set, which causes this to be a non-blocking wait (one of my favorite oxymorons). Alternative types of sockets This example showed a stream socket in the Internet domain. This is the most common type of connection. A second type of connection is a datagram socket. You might want to use a datagram socket in cases where there is only one message being sent from the client to the server, and only one message being sent back. There are several differences between a datagram socket and a stream socket. 1. Datagrams are unreliable, which means that if a packet of information gets lost somewhere in the Internet, the sender is not told (and of course the receiver does not know about the existence of the message). In contrast, with a stream socket, the underlying TCP protocol will detect that a message was lost because it was not acknowledged, and it will be retransmitted without the process at either end knowing about this. 2. Message boundaries are preserved in datagram sockets. If the sender sends a datagram of 100 bytes, the receiver must read all 100 bytes at once. This can be contrasted with a stream socket, where if the sender wrote a 100 byte message, the receiver could read it in two chunks of 50 bytes or 100 chunks of one byte. 3. The communication is done using special system calls sendto()#### and receivefrom()#### rather than the more generic read()#### and write()####. 4. There is a lot less overhead associated with a datagram socket because connections do not need to be established and broken down, and packets do not need to be acknowledged. This is why datagram sockets are often used when the service to be provided is short, such as a time-of-day service. Click here for the server code using a datagram socket. Click here for the client code using a datagram socket. These two programs can be compiled and run in exactly the same way as the server and client using a stream socket. Most of the server code is similar to the stream socket code. Here are the differences. sock=socket(AF_INET, SOCK_DGRAM, 0); Note that when the socket is created, the second argument is the symbolic constant SOCK_DGRAM instead of SOCK_STREAM. The protocol will be UDP, not TCP. ---- fromlen = sizeof(struct sockaddr_in); while (1) { n = recvfrom(sock,buf,1024,0,(struct sockaddr *)&from,&fromlen); if (n < 0) error("recvfrom"); Servers using datagram sockets do not use the listen() or the accept() system calls. After a socket has been bound to an address, the program calls recvfrom() to read a message. This call will block until a message is received. The recvfrom() system call takes six arguments. The first three are the same as those for the read() call, the socket file descriptor, the buffer into which the message will be read, and the maximum number of bytes. The fourth argument is an integer argument for flags. This is ordinarily set to zero. The fifth argument is a pointer to a sockaddr_in structure. When the call returns, the values of this structure will have been filled in for the other end of the connection (the client). The size of this structure will be in the last argument, a pointer to an integer. This call returns the number of bytes in the message. (or -1 on an error condition). The recfrom() man page has more information. ________________________________________ n = sendto(sock,"Got your message ",17, 0,(struct sockaddr *) &from,fromlen); if (n < 0) error("sendto"); } } To send a datagram, the function sendto() is used. This also takes six arguments. The first three are the same as for a write() call, the socket file descriptor, the buffer from which the message will be written, and the number of bytes to write. The fourth argument is an int argument called flags, which is normally zero. The fifth argument is a pointer to a sockadd_in structure. This will contain the address to which the message will be sent. Notice that in this case, since the server is replying to a message, the values of this structure were provided by the recvfrom call. The last argument is the size of this structure. Note that this is not a pointer to an int, but an int value itself. The sendto() man page has more information. The client code for a datagram socket client is the same as that for a stream socket with the following differences. • the socket system call has SOCK_DGRAM instead of SOCK_STREAM as its second argument. • there is no connect()**** system call • instead of read**** and write****, the client uses recvfrom**** and sendto **** which are described in detail above. Sockets in the Unix Domain Here is the code for a client and server which communicate using a stream socket in the Unix domain. U_server.c U_client The only difference between a socket in the Unix domain and a socket in the Internet domain is the form of the address. Here is the address structure for a Unix Domain address, defined in the header file. struct sockaddr_un { short sun_family; /* AF_UNIX */ char sun_path[108]; /* path name (gag) */ }; The field sun_path has the form of a path name in the Unix file system. This means that both client and server have to be running the same file system. Once a socket has been created, it remain until it is explicitly deleted, and its name will appear with the ls command, always with a size of zero. Sockets in the Unix domain are virtually identical to named pipes (FIFOs). Designing servers There are a number of different ways to design servers. These models are discussed in detail in a book by Douglas E. Comer and David L. Stevens entiteld Internetworking with TCP/IP Volume III:Client Server Programming and Applications published by Prentice Hall in 1996. These are summarized here. Concurrent, connection oriented servers The typical server in the Internet domain creates a stream socket and forks off a process to handle each new connection that it receives. This model is appropriate for services which will do a good deal of reading and writing over an extended period of time, such as a telnet server or an ftp server. This model has relatively high overhead, because forking off a new process is a time consuming operation, and because a stream socket which uses the TCP protocol has high kernel overhead, not only in establishing the connection but also in transmitting information. However, once the connection has been established, data transmission is reliable in both directions. Iterative, connectionless servers Servers which provide only a single message to the client often do not involve forking, and often use a datagram socket rather than a stream socket. Examples include a finger daemon or a timeofday server or an echo server (a server which merely echoes a message sent by the client). These servers handle each message as it receives them in the same process. There is much less overhead with this type of server, but the communication is unreliable. A request or a reply may get lost in the Internet, and there is no built-in mechanism to detect and handle this. Single Process concurrent servers A server which needs the capability of handling several clients simultaneous, but where each connection is I/O dominated (i.e. the server spends most of its time blocked waiting for a message from the client) is a candidate for a single process, concurrent server. In this model, one process maintains a number of open connections, and listens at each for a message. Whenever it gets a message from a client, it replies quickly and then listens for the next one. This type of service can be done with the select system call.

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!

How to statically link or dynamically link libraries in your c++ program?

Static and Dynamic Linking and GDB
ENEL 315 Lab Tip, Winter 1997
This is NOT a course handout – to save paper, no paper copies have been printed.
Author: Steve Norman
First published: 20 January 1997
Last modified: Tue Jan 21 14:27:49 MST 1997
Contents
• Introduction
• Static and dynamic linking – concepts
• What kind of linking is done in ENEL 315 Labs?
• What does all this have to do with GDB?
________________________________________
Introduction
The main point of this handout is to explain how to work around a problem that may occasionally arise when you use gdb on the ECE Linux system. (If you don’t know what gdb is, don’t worry. ENEL 315 Lab[2] has several exercises designed to introduce you to gdb.)
Before I talk about the problem with gdb, I have to explain the difference between static linking and dynamic linking.
[back to top of document]
________________________________________
Static and dynamic linking – concepts
Linking is the process of combining the machine code in object files (.o files on a Unix system) with machine code from library files to create an executable file. (If this concept is unfamiliar to you, see the ENGG 333 handout called Multi-file Projects and Makefiles for an explanation.)
When an executable is produced using static linking, all of the needed machine code from the library is copied into the executable file when the link command is run. For example, suppose prog.c is the source code for a simple C program that makes calls to scanf and printf. You compile the program with the command
gccw -c prog.c
and then you link it with the command
gccw -o prog prog.o
Under static linking, the complete machine code translations of scanf and printf are incorporated into the executable file prog.
When dynamic linking is used to create an executable, machine code from the library is not copied into the executable file. Instead, a little bit of information about where to find the library machine code is put in the executable file. When the executable is actually run, the operating system finds the needed library machine code and loads it into computer memory along with the machine code from the executable. Returning to our example, if the sequence of commands
gccw -c prog.c
gccw -o prog prog.o
is executed in an environment with dynamic linking, the file prog will contain the machine code translation of prog.c and information about where to find machine code for functions like scanf and printf, but it won’tcontain the machine code translations of scanf and printf.
Dynamic linking has many advantages over static linking. Here are two:
• Reduced use of disk space. Dynamically linked executables tend to be much smaller than statically linked executables.
• More efficient use of memory at run time. Dynamically linked executables allow the operating system to play clever tricks that may reduce total memory use when many programs are running simultaneously. (Ask me in a lab period if you would like a less vague explanation.)
There are several other advantages.
Here are some disadvantages of dynamic linking:
• Sometimes the operating system can’t find the library machine code that is needed when a program is run. Microsoft Windows users may have experienced this – the error message is something like “Can’t find DLL file.”
• It’s a lot more complicated than static linking – implementing dynamic linking is a relatively tough job for the developers of the operating system.
[back to top of document]
________________________________________
What kind of linking is done in ENEL 315 Labs?
On the DECstation system, only static linking is available. That’s one reason why executables are relatively big on the DECstations.
On the ECE Linux systems, the default is dynamic linking, which explains why the Linux executables are generally pretty small. However, you can ask for static linking if you want it, by adding the option -static to the command that invokes the linker. For example, this command, extracted from a Makefile, would result in dynamic linking on one of our Linux boxes:
g++w -o foo $(OBJS)
To get static linking instead, you would change the command to this:
g++w -static -o foo $(OBJS)
Use of -static on the DECstations is harmless – it has no effect, since linking is always static.
[back to top of document]
________________________________________
What does all this have to do with GDB?
Here’s the problem:
On the ECE Linux boxes, when dynamically linked executables crash, gdb sometimes presents bogus information about the function call stack.
This is a pretty serious problem, because if you’re not aware of it, you may get misleading or nonsensical information about where a program was when it crashed.
The problem does not seem to occur with statically linked executables. So if you’re planning to use gdb to examine the behaviour of a program on an ECE Linux box, it is a good idea to make sure that the executable is statically linked.
Note that this is a problem with our specific versions of Linux and gdb. In general, gdb works very well with dynamically linked executables on Unix systems.
http://enel.ucalgary.ca/People/Norman/enel315_winter1997/linking_and_gdb/

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!

Job interview question: How to mix c and c++ source code together in the same file

[32.1] What do I need to know when mixing C and C++ code?
Here are some high points (though some compiler-vendors might not require all these; check with your compiler-vendor’s documentation):
• You must use your C++ compiler when compiling main() (e.g., for static initialization)
• Your C++ compiler should direct the linking process (e.g., so it can get its special libraries)
• Your C and C++ compilers probably need to come from same vendor and have compatible versions (e.g., so they have the same calling conventions)
In addition, you’ll need to read the rest of this section to find out how to make your C functions callable by C++ and/or your C++ functions callable by C.
BTW there is another way to handle this whole thing: compile all your code (even your C-style code) using a C++ compiler. That pretty much eliminates the need to mix C and C++, plus it will cause you to be more careful (and possibly —hopefully!— discover some bugs) in your C-style code. The down-side is that you’ll need to update your C-style code in certain ways, basically because the C++ compiler is more careful/picky than your C compiler. The point is that the effort required to clean up your C-style code may be less than the effort required to mix C and C++, and as a bonus you get cleaned up C-style code. Obviously you don’t have much of a choice if you’re not able to alter your C-style code (e.g., if it’s from a third-party).
[ Top | Bottom | Previous section | Next section | Search the FAQ ]
________________________________________
[32.2] How can I include a standard C header file in my C++ code?
To #include a standard header file (such as ), you don’t have to do anything unusual. E.g.,
// This is C++ code

#include // Nothing unusual in #include line

int main()
{
std::printf(“Hello world\n”); // Nothing unusual in the call either

}
If you think the std:: part of the std::printf() call is unusual, then the best thing to do is “get over it.” In other words, it’s the standard way to use names in the standard library, so you might as well start getting used to it now.
However if you are compiling C code using your C++ compiler, you don’t want to have to tweak all these calls from printf() to std::printf(). Fortunately in this case the C code will use the old-style header rather than the new-style header , and the magic of namespaces will take care of everything else:
/* This is C code that I’m compiling using a C++ compiler */

#include /* Nothing unusual in #include line */

int main()
{
printf(“Hello world\n”); /* Nothing unusual in the call either */

}
Final comment: if you have C headers that are not part of the standard library, we have somewhat different guidelines for you. There are two cases: either you can’t change the header, or you canchange the header.
[ Top | Bottom | Previous section | Next section | Search the FAQ ]
________________________________________
[32.3] How can I include a non-system C header file in my C++ code?
If you are including a C header file that isn’t provided by the system, you may need to wrap the #include line in an extern “C” { /*…*/ } construct. This tells the C++ compiler that the functions declared in the header file are C functions.
// This is C++ code

extern “C” {
// Get declaration for f(int i, char c, float x)
#include “my-C-code.h”
}

int main()
{
f(7, ‘x’, 3.14); // Note: nothing unusual in the call

}
Note: Somewhat different guidelines apply for C headers provided by the system (such as ) and for C headers that you can change.
[ Top | Bottom | Previous section | Next section | Search the FAQ ]
________________________________________
[32.4] How can I modify my own C header files so it’s easier to #include them in C++ code?
If you are including a C header file that isn’t provided by the system, and if you are able to change the C header, you should strongly consider adding the extern “C” {…} logic inside the header to make it easier for C++ users to #include it into their C++ code. Since a C compiler won’t understand the extern “C” construct, you must wrap the extern “C” { and } lines in an #ifdef so they won’t be seen by normal C compilers.
Step #1: Put the following lines at the very top of your C header file (note: the symbol __cplusplus is #defined if/only-if the compiler is a C++ compiler):
#ifdef __cplusplus
extern “C” {
#endif
Step #2: Put the following lines at the very bottom of your C header file:
#ifdef __cplusplus
}
#endif
Now you can #include your C header without any extern “C” nonsense in your C++ code:
// This is C++ code

// Get declaration for f(int i, char c, float x)
#include “my-C-code.h” // Note: nothing unusual in #include line

int main()
{
f(7, ‘x’, 3.14); // Note: nothing unusual in the call

}
Note: Somewhat different guidelines apply for C headers provided by the system (such as ) and for C headers that you can’t change.
Note: #define macros are evil in 4 different ways: evil#1, evil#2, evil#3, and evil#4. But they’re still useful sometimes. Just wash your hands after using them.
[ Top | Bottom | Previous section | Next section | Search the FAQ ]
________________________________________
[32.5] How can I call a non-system C function f(int,char,float) from my C++ code?
If you have an individual C function that you want to call, and for some reason you don’t have or don’t want to #include a C header file in which that function is declared, you can declare the individual C function in your C++ code using the extern “C” syntax. Naturally you need to use the full function prototype:
extern “C” void f(int i, char c, float x);
A block of several C functions can be grouped via braces:
extern “C” {
void f(int i, char c, float x);
int g(char* s, char const* s2);
double sqrtOfSumOfSquares(double a, double b);
}
After this you simply call the function just as if it were a C++ function:
int main()
{
f(7, ‘x’, 3.14); // Note: nothing unusual in the call

}
[ Top | Bottom | Previous section | Next section | Search the FAQ ]
________________________________________
[32.6] How can I create a C++ function f(int,char,float) that is callable by my C code?
The C++ compiler must know that f(int,char,float) is to be called by a C compiler using the extern “C” construct:
// This is C++ code

// Declare f(int,char,float) using extern “C”:
extern “C” void f(int i, char c, float x);

// Define f(int,char,float) in some C++ module:
void f(int i, char c, float x)
{

}
The extern “C” line tells the compiler that the external information sent to the linker should use C calling conventions and name mangling (e.g., preceded by a single underscore). Since name overloading isn’t supported by C, you can’t make several overloaded functions simultaneously callable by a C program.
[ Top | Bottom | Previous section | Next section | Search the FAQ ]
________________________________________
[32.7] Why is the linker giving errors for C/C++ functions being called from C++/C functions?
If you didn’t get your extern “C” right, you’ll sometimes get linker errors rather than compiler errors. This is due to the fact that C++ compilers usually “mangle” function names (e.g., to support function overloading) differently than C compilers.
See the previous two FAQs on how to use extern “C”.
[ Top | Bottom | Previous section | Next section | Search the FAQ ]
________________________________________
[32.8] How can I pass an object of a C++ class to/from a C function?
Here’s an example (for info on extern “C”, see the previous two FAQs).
Fred.h:
/* This header can be read by both C and C++ compilers */
#ifndef FRED_H
#define FRED_H

#ifdef __cplusplus
class Fred {
public:
Fred();
void wilma(int);
private:
int a_;
};
#else
typedef
struct Fred
Fred;
#endif

#ifdef __cplusplus
extern “C” {
#endif

#if defined(__STDC__) || defined(__cplusplus)
extern void c_function(Fred*); /* ANSI C prototypes */
extern Fred* cplusplus_callback_function(Fred*);
#else
extern void c_function(); /* K&R style */
extern Fred* cplusplus_callback_function();
#endif

#ifdef __cplusplus
}
#endif

#endif /*FRED_H*/
Fred.cpp:
// This is C++ code

#include “Fred.h”

Fred::Fred() : a_(0) { }

void Fred::wilma(int a) { }

Fred* cplusplus_callback_function(Fred* fred)
{
fred->wilma(123);
return fred;
}
main.cpp:
// This is C++ code

#include “Fred.h”

int main()
{
Fred fred;
c_function(&fred);

}
c-function.c:
/* This is C code */

#include “Fred.h”

void c_function(Fred* fred)
{
cplusplus_callback_function(fred);
}
Unlike your C++ code, your C code will not be able to tell that two pointers point at the same object unless the pointers are exactly the same type. For example, in C++ it is easy to check if aDerived* called dp points to the same object as is pointed to by a Base* called bp: just say if (dp == bp) …. The C++ compiler automatically converts both pointers to the same type, in this case to Base*, then compares them. Depending on the C++ compiler’s implementation details, this conversion sometimes changes the bits of a pointer’s value. However your C compiler will not know how to do that pointer conversion, so the conversion from Derived* to Base*, for example, must take place in code compiled with a C++ compiler, not in code compiled with a C compiler.
NOTE: you must be especially careful when converting both to void* since that conversion will not allow either the C or C++ compiler to do the proper pointer adjustments! For example (continuing from the previous paragraph), if you assigned dp and bp into two void* pointers, say dpv and bpv, it might be the case that dpv != bpv even if dp == bp. You have been warned.
[ Top | Bottom | Previous section | Next section | Search the FAQ ]
________________________________________
[32.9] Can my C function directly access data in an object of a C++ class?
Sometimes.
(For basic info on passing C++ objects to/from C functions, read the previous FAQ).
You can safely access a C++ object’s data from a C function if the C++ class:
• Has no virtual functions (including inherited virtual functions)
• Has all its data in the same access-level section (private/protected/public)
• Has no fully-contained subobjects with virtual functions
If the C++ class has any base classes at all (or if any fully contained subobjects have base classes), accessing the data will technically be non-portable, since class layout under inheritance isn’t imposed by the language. However in practice, all C++ compilers do it the same way: the base class object appears first (in left-to-right order in the event of multiple inheritance), and member objects follow.
Furthermore, if the class (or any base class) contains any virtual functions, almost all C++ compliers put a void* into the object either at the location of the first virtual function or at the very beginning of the object. Again, this is not required by the language, but it is the way “everyone” does it.
If the class has any virtual base classes, it is even more complicated and less portable. One common implementation technique is for objects to contain an object of the virtual base class (V) last (regardless of where V shows up as a virtual base class in the inheritance hierarchy). The rest of the object’s parts appear in the normal order. Every derived class that has V as a virtualbase class actually has a pointer to the V part of the final object.
[ Top | Bottom | Previous section | Next section | Search the FAQ ]
________________________________________
[32.10] Why do I feel like I’m “further from the machine” in C++ as opposed to C?
Because you are.
As an OO programming language, C++ allows you to model the problem domain itself, which allows you to program in the language of the problem domain rather than in the language of the solution domain.
One of C’s great strengths is the fact that it has “no hidden mechanism”: what you see is what you get. You can read a C program and “see” every clock cycle. This is not the case in C++; old line C programmers (such as many of us once were) are often ambivalent (can you say, “hostile”?) about this feature. However after they’ve made the transition to OO thinking, they often realize that although C++ hides some mechanism from the programmer, it also provides a level of abstraction and economy of expression which lowers maintenance costs without destroying run-time performance.
Naturally you can write bad code in any language; C++ doesn’t guarantee any particular level of quality, reusability, abstraction, or any other measure of “goodness.”
C++ doesn’t try to make it impossible for bad programmers to write bad programs; it enables reasonable developers to create superior software.
http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!

Know both Linux or Unix top command and ps command to clear Bloomberg interview question

Know both Linux or Unix top command and ps command to clear Bloomberg interview question

This is for questions on how to diagnose for memory leaks or run away processes

top
Process viewer, find the CPU-intensive programs currently running. See ps for explanations of the field descriptors.
Syntax
top options

Options
-b Run in batch mode; don’t accept command-line input.
Useful for sending output to another command or to a file.
-c Show command line in display instead of just command name.
-d delay
Specify delay between refreshes.
-i Suppress display of idle and zombie processes.
-n num
Update display num times, then exit.
-p pid
Monitor only processes with the specified process ID.
-q Refresh without any delay.
If user is privileged, run with highest priority.
-s Secure mode. Disable some (dangerous) interactive commands.
-S Cumulative mode. Print total CPU time of each process,
including dead child processes.

Interactive commands

space Update display immediately.
A Sort by age, with newest first.
c Toggle display of command name or full command line.
f, F Add fields to display or remove fields from the display.
h, ? Display help about commands and the status of secure and cumulative modes.
k Prompt for process ID to kill and signal to send (default is 15) to kill it.
i Toggle suppression of idle and zombie processes.
l Toggle display of load average and uptime information.
^L Redraw screen.
m Toggle display of memory information.
M Sort tasks by resident memory usage.
n, # Prompt for number of processes to show.
If 0 is entered, show as many as will fit on the screen
(default).
N Sort numerically by process ID.
o, O Change order of displayed fields.
P Sort tasks by CPU usage (default).
q Exit.
r Apply renice to a process. Prompt for PID and renice value. Suppressed in secure mode.
s Change delay between refreshes.
Prompt for new delay time, which should be in seconds.
(Suppressed in secure mode.)
S Toggle cumulative mode. (See the -S option.)
t Toggle display of processes and CPU states information.
T Sort tasks by time/cumulative time.
W Write current setup to ~/.toprc. This is the recommended way to write a top configuration file.
Top displays per-process CPU usage (not total server load) it is useful for seeing how much work the machine is doing now compared to some point in the past.
At the top of the display output there are three numbers representing the number of processes waiting for the CPU now, an average for the past five minutes, and an average for the past fifteen minutes. These three numbers are the “load average”
Top should only be used to compare the load average on two different machines if they have an identical configuration (both hardware and software.)
ps
Process status, information about processes running in memory. If you want a repetitive update of this status, use top.
Syntax

ps option(s)
ps [-L]

Options
-L List all the keyword options
This version of ps accepts 3 kinds of option:

-Unix98 options may be grouped and must be preceeded by a dash.
BSD options may be grouped and must not be used with a dash.
–GNU long options are preceeded by two dashes.

Options of different types may be freely mixed. The PS_PERSONALITY environment variable provides more detailed control of ps behavior.

The Options below are listed side-by-side (unless there are differences).
Simple Process Selection:
-A a select all processes (including those of other users)
-a select all with a tty except session leaders
-d select all, but omit session leaders
-e select all processes
g really all, even group leaders (does nothing w/o SunOS settings)
-N negate selection
r restrict output to running processes
T select all processes on this terminal
x select processes without controlling ttys
–deselect negate selection

Process Selection by List:

-C select by command name
-G select by RGID (supports names)
-g select by session leader OR by group name
–Group select by real group name or ID
–group select by effective group name or ID
-p p –pid select by process ID (PID)
-s –sid select by session ID
-t –tty select by terminal (tty)
-u U select by effective user ID (supports names)
-U select by RUID (supports names)
–User select by real user name or ID
–user select by effective user name or ID

-123 implied –sid
123 implied –pid

Output Format Control:

-c Different scheduler info for -l option
-f Full listing
-j j Jobs format
-l l Long format
-O O Add the information associated with the space or comma separated
list of keywords specified, after the process ID, in the default
information display.

-o o Display information associated with the space or comma separated
list of keywords specified.
–format user-defined format
s display signal format
u display user-oriented format
v display virtual memory format
X old Linux i386 register format
-y do not show flags; show rss in place of addr

Output Modifiers:
C use raw CPU time for %CPU instead of decaying average
c true command name
e show environment after the command
f ASCII-art process hierarchy (forest)
-H show process hierarchy (forest)
h do not print header lines (repeat header lines in BSD personality)
-m m show all threads
-n set namelist file
n numeric output for WCHAN and USER
N specify namelist file
O sorting order (overloaded)
S include some dead child process data (as a sum with the parent)
-w w wide output
–cols set screen width
–columns set screen width
–forest ASCII art process tree
–html HTML escaped output
–headers repeat header lines
–no-headers print no header line at all
–lines set screen height
–nul unjustified output with NULs
–null unjustified output with NULs
–rows set screen height
–sort specify sorting order
–width set screen width
–zero unjustified output with NULs

Information:
-V V print version
L list all format specifiers
–help print help message
–info print debugging info
–version print version

Obsolete:
A increase the argument space (DecUnix)
M use alternate core (try -n or N instead)
W get swap info from … not /dev/drum (try -n or N instead)
k use /vmcore as c-dumpfile (try -n or N instead)
NOTES
The “-g” option can select by session leader OR by group name. Selection by session leader is specified by many standards, but selection by group is the logical behavior that several other operating systems use. This ps will select by session leader when the list is completely numeric (as sessions are). Group ID numbers will work only when some group names are also specified.

The “m” option should not be used. Use “-m” or “-o” with a list. (“m” displays memory info, shows threads, or sorts by memory use)

The “h” option varies between BSD personality and Linux usage (not printing the header) Regardless of the current personality, you can use the long options –headers and –no-headers

Terminals (ttys, or screens of text output) can be specified in several forms: /dev/ttyS1, ttyS1, S1. Obsolete “ps t” (your own terminal) and “ps t?” (processes without a terminal) syntax is supported, but modern options (“T”,”-t” with list, “x”, “t” with list) should be used instead.

The BSD “O” option can act like “-O” (user-defined output format with some common fields predefined) or can be used to specify sort order. Heuristics are used to determine the behavior of this option. To ensure that the desired behavior is obtained, specify the other option (sorting or formatting) in some other way.

For sorting, BSD “O” option syntax is O[+|-]k1[,[+|-]k2[,…]] Order the process listing according to the multilevel sort specified by the sequence of short keys from SORT KEYS, k1, k2, … The `+’ is quite optional, merely re-iterating the default direction on a key. `-‘ reverses direction only on the key it precedes.
The O option must be the last option in a single command argument, but specifications in successive arguments are catenated.

GNU sorting syntax is –sortX[+|-]key[,[+|-]key[,…]]
Choose a multi-letter key from the SORT KEYS section. X may be any convenient separator character. To be GNU-ish use `=’. The `+’ is really optional since default direction is increasing numerical or lexicographic order. For example, ps jax –sort=uid,-ppid,+pid

This ps works by reading the virtual files in /proc. This ps does not need to be suid kmem or have any privileges to run. Do not give this ps any special permissions.

This ps needs access to a namelist file for proper WCHAN display. The namelist file must match the current Linux kernel exactly for correct output.

To produce the WCHAN field, ps needs to read the System.map file created when the kernel is compiled. The search path is:
$PS_SYSTEM_MAP
/boot/System.map-`uname -r`
/boot/System.map
/lib/modules/`uname -r`/System.map
/usr/src/linux/System.map
/System.map
The member used_math of task_struct is not shown, since crt0.s checks to see if math is present. This causes the math flag to be set for all processes, and so it is Programs swapped out to disk will be shown without command line arguments, and unless the c option is given, in brackets.

%CPU shows the cputime/realtime percentage. It will not add up to 100% unless you are lucky. It is time used divided by the time the process has been running.

The SIZE and RSS fields don’t count the page tables and the task_struct of a proc; this is at least 12k of memory that is always resident. SIZE is the virtual size of the proc (code+data+stack).

Processes marked are dead processes (so-called”zombies”) that remain because their parent has not destroyed them properly. These processes will be destroyed by init(8) if the parent process exits.
PROCESS FLAGS
ALIGNWARN 001 print alignment warning msgs
STARTING 002 being created
EXITING 004 getting shut down
PTRACED 010 set if ptrace (0) has been called
TRACESYS 020 tracing system calls
FORKNOEXEC 040 forked but didn’t exec
SUPERPRIV 100 used super-user privileges
DUMPCORE 200 dumped core
SIGNALED 400 killed by a signal

PROCESS STATE CODES
D uninterruptible sleep (usually IO)
R runnable (on run queue)
S sleeping
T traced or stopped
Z a defunct (“zombie”) process

For BSD formats and when the “stat” keyword is used,
additional letters may be displayed:
W has no resident pages
< high-priority process N low-priority task L has pages locked into memory (for real-time and custom IO) ENVIRONMENT VARIABLES and PERSONALITY (posix,linux,bsd,sun) Examples: List every process on the system using standard syntax: ps -e List every process on the system using BSD syntax: ps ax List the top 10 CPU users. ps -e -o pcpu -o pid -o user -o args | sort -k 1 | tail -21r List every process except those running as root (real & effective ID) ps -U root -u root -N List every process with a user-defined format: ps -eo pid,tt,user,fname,tmout,f,wchan Odd display with AIX field descriptors: ps -o "%u : %U : %p : %a" Print only the process IDs of syslogd: ps -C syslogd -o pid= When displaying multiple fields, part of the output may be truncated, to avoid this supply a width to the arguments: ps -e -o user:20,args. Since ps cannot run faster than the system and is run as any other scheduled process, the informa

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!