Scripting
C++ Scripting is the concept of C++ with no usage of new operator Easy way of sort integer with C++
Scripting
#include <iostream>
#include <vector>
#include <algorithm>
 
 
int main(int argc, char **argv) {
 
    std::vector<int> data;
    int n;
    while (std::cin >> n)
    {
        data.push_back(n);
    }
 
    std::sort (data.begin(), data.end());
 
    std::cout << data.size() << std::endl;
    for (std::vector<int>::iterator it=data.begin(); it!=data.end(); ++it)
        std::cout << ' ' << *it;
 
    std::cout << '\n';
    return 0;
}





Eigen Lib
Eigen Lib Example
Eigen Lib
#include <iostream>#include <Eigen/Dense>using namespace Eigen;int main(){  Matrix2d mat;  mat << 1, 2,         3, 4;  Vector2d u(-1,1), v(2,0);  std::cout << "Here is mat*mat:\n" << mat*mat << std::endl;  std::cout << "Here is mat*u:\n" << mat*u << std::endl;  std::cout << "Here is u^T*mat:\n" << u.transpose()*mat << std::endl;  std::cout << "Here is u^T*v:\n" << u.transpose()*v << std::endl;  std::cout << "Here is u*v^T:\n" << u*v.transpose() << std::endl;  std::cout << "Let's multiply mat by itself" << std::endl;  mat = mat*mat*mat*mat;  std::cout << "Now mat is mat:\n" << mat << std::endl;}





UDP Client
UDP Client Example
UDP Client
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
 
int main(int argc, char**argv)
{
   int sockfd,n;
   struct hostent *hp; /* host information */
   struct sockaddr_in servaddr,cliaddr;
   char sendline[1000];
   char recvline[1000];
 
   if (argc != 2)
   {
      printf("usage:  Addr\n");
      return 1;
   }
 
   sockfd=socket(AF_INET,SOCK_DGRAM,0);
 
   bzero(&servaddr,sizeof(servaddr));
   servaddr.sin_family = AF_INET;
 
 
   hp = gethostbyname(argv[1]);
    /* put the host's address into the server address structure */
   memcpy((void *)&servaddr.sin_addr, hp->h_addr_list[0], hp->h_length);
 
   servaddr.sin_port=htons(8080);
 
   while (fgets(sendline, 10000,stdin) != NULL)
   {
 
      sendto(sockfd,sendline,strlen(sendline),0,(struct sockaddr *)&servaddr,sizeof(servaddr));
      printf("sendto\n");
      n=recvfrom(sockfd,recvline,10000,0,NULL,NULL);
      recvline[n]=0;
      fputs(recvline,stdout);
   }
}





UDP Server
Udp server example
UDP Server
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
 
int main(int argc, char**argv)
{
   int sockfd,n;
   struct sockaddr_in servaddr,cliaddr;
   socklen_t len;
   char mesg[1000];
 
 
    if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) 
       { perror("cannot create socket"); return 0; }
 
 
 
   bzero(&servaddr,sizeof(servaddr));
   servaddr.sin_family = AF_INET;
   servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
   servaddr.sin_port=htons(8080);
   if (bind(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr))< 0) 
       { perror("bind failed"); return 0; }
 
   for (;;)
   {
      len = sizeof(cliaddr);
      n = recvfrom(sockfd,mesg,1000,0,(struct sockaddr *)&cliaddr,&len);
      printf("-------------------------------------------------------\n");
      sendto(sockfd,mesg,n,0,(struct sockaddr *)&cliaddr,sizeof(cliaddr));
      printf("-------------------------------------------------------\n");
      mesg[n] = 0;
      printf("Received the following:\n");
      printf("%s",mesg);
      printf("-------------------------------------------------------\n");
   }
}





fork
fork example
fork
#include <unistd.h>
#include <stdio.h>
 
int main(int argc, char *argv[])
{
    printf("-->start\n");
    pid_t   pid;
    pid = fork ();
    if (pid > 0) {
        printf("parent\n");
    }
    else if (pid == 0) {
       printf("child\n");
       pid_t   pid1;
       pid1 = fork ();
       if  (pid1 > 0)
          printf("child parent\n");
       else
          printf("child child\n");
 
    } else {
         printf("error\n");
    }
 
    printf("<--stop\n");
  return 0;
}





dup2
dup2 Example
dup2
#include <unistd.h>
#include <stdio.h>
 
int main(int argc, char *argv[])
{
    FILE * pFile1 = fopen ("myfile1.txt","w");
    FILE * pFile2 = fopen ("myfile2.txt","w");
    if (pFile1!=NULL && pFile2!=NULL)
    {
 
        int fd1=fileno (pFile1);
        int fd2=fileno (pFile2);
 
        if (dup2(fd1, fd2) == -1 )
        {
            printf("error dup2");
        }
        // here is a normal write
        fputs ("write on file 1",pFile1);
        // here is a redirected write
        fputs ("write on file 1 because of dup2",pFile2);
        fclose (pFile1);
    }
 
    return 0;
}





Cpu Usage
/proc
Cpu Usage
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <ctype.h>
#include <vector>
 
#define PROCSTATFILE  "/proc/stat"
 
 
class CpuUsage
{
 
public:
 
    unsigned int id;
 
    unsigned long usertime;
    unsigned long nicetime;
    unsigned long systemtime;
    unsigned long idletime;
    unsigned long ioWait;
    unsigned long irq;
    unsigned long softIrq;
    unsigned long steal;
    unsigned long guest;
 
    CpuUsage()
    {
    }
 
    CpuUsage( const CpuUsage & src)
    {
        this->id=src.id;
        this->usertime=src.usertime;
        this->nicetime=src.nicetime;
        this->systemtime=src.systemtime;
        this->idletime=src.idletime;
        this->ioWait=src.ioWait;
        this->irq=src.irq;
        this->softIrq=src.softIrq;
        this->steal=src.steal;
        this->guest=src.guest;
    }
 
    void set(CpuUsage * src)
    {
        this->id=src->id;
        this->usertime=src->usertime;
        this->nicetime=src->nicetime;
        this->systemtime=src->systemtime;
        this->idletime=src->idletime;
        this->ioWait=src->ioWait;
        this->irq=src->irq;
        this->softIrq=src->softIrq;
        this->steal=src->steal;
        this->guest=src->guest;
    }
 
    int readCpu()
    {
        FILE * file = fopen(PROCSTATFILE, "r");
        if (file == NULL) {
            printf("Cannot open " PROCSTATFILE);
            return -1;
        }
        this->id=-1;
        fscanf(file, "cpu %llu %llu %llu %llu %llu %llu %llu %llu %llu", &this->usertime, &this->nicetime,&this->systemtime, &this->idletime, &this->ioWait, &this->irq, &this->softIrq, &this->steal, &this->guest);
        fclose (file);
        return 0;
    }
 
 
    int readCpu(int cpu_id)
    {
        FILE * file = fopen(PROCSTATFILE, "r");
        if (file == NULL) {
            printf("Cannot open " PROCSTATFILE);
            return -1;
        }
        char buffer[256];
        int cpu=0;
        int result=-1;
        while(true)
        {
            fgets(buffer, 255, file);
            if (buffer[0]=='c' && buffer[1]=='p' && buffer[2]=='u')
            {
                if (isdigit(buffer[3]))
                {
                    if (cpu==cpu_id)
                    {
                        sscanf(buffer, "cpu%d %llu %llu %llu %llu %llu %llu %llu %llu %llu",&this->id, &this->usertime, &this->nicetime,&this->systemtime, &this->idletime, &this->ioWait, &this->irq, &this->softIrq, &this->steal, &this->guest);
                        result=0;
                        break;
                    }
                    cpu++;
                }
            }
            else
                break;
        }
 
        fclose (file);
        return result;
    }
 
 
};
 
 
class DeltaCpuUsage
{
 
public:
 
    unsigned int id;
 
    unsigned long usertime;
    unsigned long nicetime;
    unsigned long systemtime;
    unsigned long idletime;
    unsigned long ioWait;
    unsigned long irq;
    unsigned long softIrq;
    unsigned long steal;
    unsigned long guest;
 
    DeltaCpuUsage(CpuUsage  * CpuUsageT0, CpuUsage  * CpuUsageT1)
    {
 
        if (CpuUsageT0->id==CpuUsageT1->id)
            this->id=CpuUsageT0->id;
 
        this->usertime=CpuUsageT1->usertime-CpuUsageT0->usertime;
        this->nicetime=CpuUsageT1->nicetime-CpuUsageT0->nicetime;
        this->systemtime=CpuUsageT1->systemtime-CpuUsageT0->systemtime;
        this->idletime=CpuUsageT1->idletime-CpuUsageT0->idletime;
        this->ioWait=CpuUsageT1->ioWait-CpuUsageT0->ioWait;
        this->irq=CpuUsageT1->irq-CpuUsageT0->irq;
        this->softIrq=CpuUsageT1->softIrq-CpuUsageT0->softIrq;
        this->steal=CpuUsageT1->steal-CpuUsageT0->steal;
        this->guest=CpuUsageT1->guest-CpuUsageT0->guest;
    }
 
    void print()
    {
        if (this->id==-1)
            printf("cpu  %llu %llu %llu %llu %llu %llu %llu %llu %llu\n", this->usertime, this->nicetime,this->systemtime, this->idletime, this->ioWait, this->irq, this->softIrq, this->steal, this->guest);
        else
            printf("cpu%d  %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",this->id, this->usertime, this->nicetime,this->systemtime, this->idletime, this->ioWait, this->irq, this->softIrq, this->steal, this->guest);
    }
 
 
};
 
 
int getCpuNumber()
{
    FILE* file = fopen(PROCSTATFILE, "r");
    if (file == NULL) {
        printf("Cannot open " PROCSTATFILE);
    }
    char buffer[256];
    int cpu = 0;
    while(true)
    {
        fgets(buffer, 255, file);
        if (buffer[0]=='c' && buffer[1]=='p' && buffer[2]=='u')
        {
            if (isdigit(buffer[3]))
                cpu++;
        }
        else
            break;
    }
 
    fclose(file);
    return cpu;
}
 
int trace_cpu(std::vector<int>  & selectedCpu )
{
    std::vector<CpuUsage*> cpuUsage1List;
    std::vector<CpuUsage*> cpuUsage2List;
 
    for (int i=0; i<selectedCpu.size(); i++)
    {
        int cpuid=selectedCpu.at(i);
        CpuUsage * cpuUsage1= new CpuUsage();
        cpuUsage1->readCpu(cpuid);
        cpuUsage1List.push_back(cpuUsage1);
 
        CpuUsage * cpuUsage2= new CpuUsage();
        cpuUsage2List.push_back(cpuUsage2);
    }
    while (1)
    {
        usleep(1000000);
        for (int i=0; i<selectedCpu.size(); i++)
        {
            int cpuid=selectedCpu.at(i);
            cpuUsage2List.at(i)->readCpu(cpuid);
        }
 
        for (int i=0; i<selectedCpu.size(); i++)
        {
            DeltaCpuUsage delta(cpuUsage1List.at(i) ,cpuUsage2List.at(i));
            delta.print();
            cpuUsage1List.at(i)->set(cpuUsage2List.at(i));
        }
    }
 
}
 
 
int main(int argc, char** argv) {
 
    int cpu=getCpuNumber();
    //printf("Cpu Nb : %d\n",cpu);
 
 
    std::vector<int> selectedCpu;
 
    if (argc>1)
    {
        for (int i=1; i<argc; i++) {
            int cpuid=atoi(argv[i]);
            if ( 0<=cpuid && cpuid<cpu)
                selectedCpu.push_back(atoi(argv[i]));
            else
                printf("selected cpu is out of range\n");
        }
    }
    else
    {
        for (int i=0; i<cpu; i++)
            selectedCpu.push_back(i);
    }
 
    if (selectedCpu.size()>0)
        trace_cpu(selectedCpu);
 
 
 
}





sleep
sleep
sleep
 
 
// *************************
//      nanosleep
// *************************
#include <time.h>
 
struct timespec ts;
 
ts.tv_sec = 0;
ts.tv_nsec = atoi( 1 ); // 1 nsec --> 1e-9 sec
 
nanosleep(&ts, NULL);
 
// *************************
//        usleep
// *************************
 
#include <unistd.h>
 
usleep(1);   // 1 usec --> 1e-6 sec
 
 
 
// *************************
//        sleep
// *************************
 
#include <unistd.h>
 
sleep(1);   // 1 sec
 
 
 





popen
example of popen use
popen
#include <unistd.h>
#include <stdio.h>
 
int main(int argc, char *argv[])
{
 
    char buf[1024];
 
    FILE * popenfile =popen("/bin/ls", "r");
 
    while (fgets(buf, 1024, popenfile) != NULL)
        (void) printf("%s", buf);
 
    pclose(popenfile);
    return 0;
}