Thursday, August 01, 2019

Program - Observer Pattern

#include <iostream>
#include <forward_list>
using namespace std;

class abstractObs
{
    public:
        virtual void update(int, int, int) = 0;
};

class abstractSubject
{
    virtual void notify()=0;
   
    protected:
    forward_list<abstractObs *> obsList;
   
    public:
    void registerObs(abstractObs *);
    void unregisterObs(abstractObs *);
};

void abstractSubject::registerObs(abstractObs *inObs)
{
    obsList.push_front(inObs);
}

void abstractSubject::unregisterObs(abstractObs *inObs)
{
    obsList.remove(inObs);
}

class actualSubject : public abstractSubject
{
    private:
        int runs, wickets, overs;
        actualSubject();
        virtual void notify();
    public:
        static actualSubject * actualSubjectPtr;
        static actualSubject * getActualSubject();
        void setData(int inRuns, int inWickets, int inOvers)
        {
            runs=inRuns;
            wickets=inWickets;
            overs=inOvers;
            notify();
        }
};

actualSubject * actualSubject::actualSubjectPtr = NULL;

actualSubject::actualSubject()
:runs(0),wickets(0),overs(0){}

void actualSubject::notify()
{
    for(auto it = begin(obsList); it != end(obsList); it++)
        (*it)->update(runs, wickets, overs);
}

actualSubject * actualSubject::getActualSubject()
{
    if(NULL == actualSubject::actualSubjectPtr)
        actualSubject::actualSubjectPtr = new actualSubject();
    return actualSubject::actualSubjectPtr;
}

class displayRuns:public abstractObs
{
    public:
        void update(int, int, int);
};

void displayRuns::update(int runs, int wickets, int overs)
{
    cout<<"Runs are:"<< runs << endl;
}

class displayWickets:public abstractObs
{
    public:
        void update(int, int, int);
};

void displayWickets::update(int runs, int wickets, int overs)
{
    cout<<"Wickets are:"<<wickets<<endl;
}

int main()
{
    actualSubject *sub = actualSubject::getActualSubject();
    displayRuns runObj;
    displayWickets wicObj;
    sub->registerObs(&runObj);
    sub->setData(1,2,3);
    sub->registerObs(&wicObj);
    sub->setData(1,4,3);
    sub->unregisterObs(&runObj);
    sub->setData(1,2,6);
    return 0;
}

Friday, July 26, 2019

Some questions

1. Find if there is a junction in two linked lists.
2. A number is stored in an Array. Add 1 to that number.
3. There are n steps. one can walk 1 or 2 steps at a time. What are the number of possible ways to climb the steps. Then make it 1 or 2 or...n steps at a time.
4. Reverse the order of words in a sentence.
5. Find the repeated characters in a string.

Monday, May 16, 2016

Multi-Threading

  • refer to this pthread program to print even and odd numbers in different threads.
http://dhinu-programs.blogspot.in/2016/05/pthread.html
  •  Good article about user space threads and kerneal space threads
http://www.evanjones.ca/software/threading.html


Thursday, May 05, 2016

GPS related

  • Assistace data
    • reference time
    • ref position
    • ionospheric model
    • UTC model
    • acquisition assistance

Monday, May 02, 2016

Client-server

  • 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.
  • reference link 
  • Program

Wednesday, April 27, 2016

C++

Monday, April 25, 2016

program, assembly, simple1.c

simple1.c

int add(int i, int j);

int main()
{
    int i, j;
    i++;
    j = 10;
    j = i;
    j = add(i,j);
    return 0;
}

int add(int i, int j)
{
    int result;
    result = i+j;
    return result;
}

simple1.s

    .file    "simple1.c"
    .text
    .globl    main
    .type    main, @function
main:
.LFB0:
    .cfi_startproc
    pushq    %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    movq    %rsp, %rbp
    .cfi_def_cfa_register 6
    subq    $16, %rsp
    addl    $1, -8(%rbp)
    movl    $10, -4(%rbp)
    movl    -8(%rbp), %eax
    movl    %eax, -4(%rbp)
    movl    -4(%rbp), %edx
    movl    -8(%rbp), %eax
    movl    %edx, %esi
    movl    %eax, %edi
    call    add
    movl    %eax, -4(%rbp)
    movl    $0, %eax
    leave
    .cfi_def_cfa 7, 8
    ret
    .cfi_endproc
.LFE0:
    .size    main, .-main
    .globl    add
    .type    add, @function
add:
.LFB1:
    .cfi_startproc
    pushq    %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    movq    %rsp, %rbp
    .cfi_def_cfa_register 6
    movl    %edi, -20(%rbp)
    movl    %esi, -24(%rbp)
    movl    -24(%rbp), %eax
    movl    -20(%rbp), %edx
    addl    %edx, %eax
    movl    %eax, -4(%rbp)
    movl    -4(%rbp), %eax
    popq    %rbp
    .cfi_def_cfa 7, 8
    ret
    .cfi_endproc
.LFE1:
    .size    add, .-add
    .ident    "GCC: (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4"
    .section    .note.GNU-stack,"",@progbits

nm simple1.o

0000000000000032 T add
0000000000000000 T main