Showing posts with label C. Show all posts
Showing posts with label C. Show all posts

Monday, May 23, 2011

Hello world in Visual studio of Windows

// Test Project.cpp : Defines the entry point for the console application.
//
E.g 1:
#include "stdafx.h"
#include <stdio.h>

int _tmain(int argc, _TCHAR* argv[])
{
 printf("hello world\n");
 printf("Press Enter to continue: ");
 fflush(stdout);
 while (getchar() != '\n' );
 return 0;
}


E.g 2:
// TrustZoneTest.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <process.h>
int _tmain(int argc, _TCHAR* argv[])
{
 double x = 1.1;
 double y = 2.2;
 printf("hello world");
 system("pause");

 return 0;
}

Tuesday, April 26, 2011

pthread

pthread_create(): create a thread, and thread starts to run
phtread_join(): suspend current execution, and wait until thread terminate

pthread_create() is similar with fork(); pthread_join() is similar with wait()

pthread_mutex_lock()
pthread_mutex_trylock()
pthread_mutex_unlock()

Socket Programming in C (3): Socket to Address structure Translation

// Get address structure by sock on current side, E.g. you want to know the client port number which randomly assigned
int getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen);

// Get address structure by sock on the other side. E.g. server wants to know client's structure.
int getpeername(int sockfd, struct sockaddr *addr, socklen_t *addrlen);

Socket Programming in C (2): IP address/Hostname Translation

****************************************************************************
//Convert IP addresses from a dots-and-number string to a struct in_addr and back
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

(1) Old way, not support IPv6

char *inet_ntoa(struct in_addr in); // convert binary in network to string
int inet_aton(const char *cp, struct in_addr *inp); // convert dot-number string cp to struct in_addr pointer inp
in_addr_t inet_addr(const char *cp); // convert do-number string cp to in_addr_t

// ntoa: network to ASCII (readable)
// aton: ASCII to network
// atoi: ASCII to integer

(2) Use inet_pton() or inet_ntop(), support for IPv6 
#include <arpa/inet.h>
 

int inet_pton(int af, const char *src, void *dst); // convert string src to network dst. if af is AF_INET(IPv4), dst must be struct addr_in* type.
const char *inet_ntop(int af, const void *src, char *dst, socklen_t size); //convert network src to string dst. if af is AF_INET, src pints to a struct addr_in, and dst must be at least INET_ADDRSTRLEN bytes long

*******************************************************************************
//Convert IP addresses from host name string to a struct in_addr and back

#include <netdb.h>
#include <sys/socket.h>       /* for AF_INET */
struct hostent *gethostbyname(const char *name); // convert hostname/dotIP to a host struture, which includes network addr_in
struct hostent *gethostbyaddr(const void *addr, socklen_t len, int type); // convert a network addr_in to host structure
struct hostent {
               char  *h_name;            /* full host name*/
               char **h_aliases;         /* alias list */
               int    h_addrtype;        /* host address type */
               int    h_length;           /* length of address */
               char **h_addr_list;      /* list of addresses, often use (struct addr_in*) h_addr */
}
#define h_addr h_addr_list[0]  /* often use this one, need cast to struct addr_in*  */

*******************************************************************************
Example:
/*
 * Converts ascii text to in_addr struct.
 * NULL is returned if the address can not be found.
 * */
struct in_addr *atoaddr(char *address)
{
  struct hostent *host;
  static struct in_addr saddr;

  /* First try it as aaa.bbb.ccc.ddd. */
  saddr.s_addr = inet_addr(address);
  if (saddr.s_addr != -1)
    return &saddr;

  host = gethostbyname(address);
  if (host != NULL)
    return (struct in_addr *) *host->h_addr_list;

  return NULL;
}

Socket Programming in C (1): Address Structure

// this is all the address structure.
// first one is general one, which often declares, and the cast to a specific one
// second part is for IPv4
// third part is for IPv6
// last part is a general structure to store internet address.

include <netinet/in.h>

// All pointers to socket address structures are often cast to pointers
// to this type before use in various functions and system calls:

struct sockaddr {
    unsigned short    sa_family;     // address family, AF_xxx
    char                    sa_data[14];  // 14 bytes of protocol address
};


// IPv4 AF_INET sockets:

struct sockaddr_in {
    short                 sin_family;   // e.g. AF_INET, AF_INET6
    unsigned short   sin_port;     // e.g. htons(3490)
    struct in_addr    sin_addr;     // see struct in_addr, below
    char                  sin_zero[8];  // zero this if you want to
};

struct in_addr {
    unsigned long s_addr;          // load with inet_pton()
};


// IPv6 AF_INET6 sockets:

struct sockaddr_in6 {
    u_int16_t       sin6_family;   // address family, AF_INET6
    u_int16_t       sin6_port;     // port number, Network Byte Order
    u_int32_t       sin6_flowinfo; // IPv6 flow information
    struct in6_addr sin6_addr;     // IPv6 address
    u_int32_t       sin6_scope_id; // Scope ID
};

struct in6_addr {
    unsigned char   s6_addr[16];   // load with inet_pton()
};


// General socket address holding structure, big enough to hold either
// struct sockaddr_in or struct sockaddr_in6 data:

struct sockaddr_storage {
    sa_family_t  ss_family;     // address family
    // all this is padding, implementation specific, ignore it:
    char      __ss_pad1[_SS_PAD1SIZE];
    int64_t   __ss_align;
    char      __ss_pad2[_SS_PAD2SIZE];
};

Wednesday, March 30, 2011

How to run 100 apps

1. Using shell script, and run all the apps by background

#!/bin/bash
i=1
while [ $i -le 5 ]
do
    ./while1&
    (( i++ ))
done


2. Using fork and exec
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>

#define TIMES 100
#define PROGRAM "./while1"

int main()
{
    int pid;
    int i;
    for(i = 0; i< TIMES; i++)
    {
    pid = fork();
    if(pid == -1)
    {
        fprintf(stderr, "fork() failed\n");
        exit(1);
    }
    else if(pid == 0)
    {
        // child process
        execv(PROGRAM, NULL);
    }
    else
    {
        // parent process
        // wait(0); //comment out this line, you only can fork 1 program
    }
    }
//    wait(0); // comment out this line if you want to this process to wait 100 while1 program
    return 1;
}

Friday, March 25, 2011

rdtsc in Windows and Linux

Code in Windows, you have to compile it by VC++

#include <intrin.h>
unsigned __int64 rdtsc()
{
  return __rdtsc();
}


Code in Linux 

#include <stdint.h>
#include <stdio.h>

  __inline__ uint64_t rdtsc() {
    uint32_t lo, hi;
    __asm__ __volatile__ (      // serialize
    "xorl %%eax,%%eax \n        cpuid"
    ::: "%rax", "%rbx", "%rcx", "%rdx");
    /* We cannot use "=A", since this would use %rax on x86_64 */
    __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
    return (uint64_t)hi << 32 | lo;
  }
int main()
{
    printf("rdtsc is: %llu\n",rdtsc());
    return 1;
}

more detailed information: http://en.wikipedia.org/wiki/Time_Stamp_Counter

Another version in Linux:
/************ Record TSC to SMRAM offset 0xA9000 ****************/
movl $0xA9000, %ebx
rdtsc
movl %eax, (%ebx)
movl %edx, 4(%ebx)

unsigned int lo = *(unsigned int*)(0xA9000);
unsigned int hi = *(unsigned int*)(0xA9004);
unsigned long long beforeConfig = (unsigned long long)hi << 32 | lo;

Thursday, March 24, 2011

Print out current time in C on Windows

  typedef struct _SYSTEMTIME {
       WORD wYear;
       WORD wMonth;
       WORD wDayOfWeek;
       WORD wDay;
       WORD wHour;
       WORD wMinute;
       WORD wSecond;
       WORD wMilliseconds;
    } SYSTEMTIME;

     #include <Windows.h>
     #include <stdio.h>

     void main()
     {
         SYSTEMTIME st;
         GetSystemTime(&st);
         printf("Year:%d\nMonth:%d\nDate:%d\nHour:%d\nMin:%d\nSecond:% d\n" ,st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond);
     }


more info:
http://www.codersource.net/c/c-tutorials/c-date-and-time.aspx
 

Thursday, November 11, 2010

global, static variables in C

In this post, I am going to explain the global, static variables in C

********
main.c
********
int a[2];             // this is the non-static global variable, could be used at other files
int static b;        // this is the static global variable, could only be used in this file

void main()
{
 int static c;       // this is the static variable like in java. associated with class.
                          // it only has one copy,
 swap();
}

********
swap.c
********
extern int a[2];

void swap()
{
int tmp;
tmp = a[0];
a[0] = a[1];
a[1] = a[0]
}

deals please see slides 14
http://www.cs.gmu.edu/~setia/cs367/slides/index.html