Category Archives: C programming

Contains infromation related to c progamming.

Simulation of scoreboard

I have implemented scoreboard pipelining design simlation. The code of the program is attached with the link;

https://www.dropbox.com/sh/xhctryw74mgmvf2/AAB5SC-WOxq6wSlo-z13oNnHa?dl=0

 

Advertisements

Thread Synchronization

I have created a program for thread synchronization using thread and binary semaphore. The program prints ping pong in synchronized fashion. The code of the program is given below; The code is compiled in Linux using gcc.

#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
#include <semaphore.h>

sem_t mutex1;
sem_t mutex2;
void * testThread1(void *parg);
void * testThread2(void *parg);
int main(void){
pthread_t thread1,thread2;
int rc1,rc2;

sem_init(&mutex1, 0, 1);
sem_init(&mutex2, 0, 1);

//sem_wait(&mutex1);
sem_wait(&mutex2);

rc1 = pthread_create(&thread1,NULL,testThread1,NULL);
//sem_wait(&mutex2);
rc2 = pthread_create(&thread2,NULL,testThread2,NULL);

pthread_join(thread1, NULL);
pthread_join(thread2, NULL);

return 0;

}

void * testThread1(void *parg){
int count = 0;
//sem_post(&mutex2);
while(count <5){
sem_wait(&mutex1);
printf(“Ping \n”);
count = count + 1;
//sem_post(&mutex2);
sem_post(&mutex2);
}
}

void * testThread2(void *parg){
int count = 0;
//sem_post(&mutex1);
while(count<5){
sem_wait(&mutex2);
printf(“Pong \n”);
count = count + 1;
//sem_post(&mutex1);
sem_post(&mutex1);
}
}

Callback Timer

I have created a callback timer using windows API.  I have tested this program in VS studio 2010 as console application.The code of the program is as follow;

// testTimer.cpp : Defines the entry point for the console application.
//
#include<Windows.h>
#include<stdio.h>

#pragma comment(lib, “winmm.lib”)
#define MAX 5
#include <mmsystem.h>
void InitializeTimer();
void CALLBACK TimerFunction(UINT wTimerID, UINT msg,
DWORD dwUser, DWORD dw1, DWORD dw2);

int count = 1;

static MMRESULT m_id;
void stopTimer();

int main(void){
InitializeTimer();

getchar();
}

void CALLBACK TimerFunction(UINT wTimerID, UINT msg,
DWORD dwUser, DWORD dw1, DWORD dw2)
{
// This is used only to call MMTimerHandler

// Typically, this function is static member of CTimersDlg
printf(“TimerFunction is called :%d\n”,count);
if(count == MAX){
stopTimer();

count  = 1;
}

count++;

}

void InitializeTimer()
{
TIMECAPS tc;
int minRes = 5000;

timeGetDevCaps(&tc, sizeof(TIMECAPS));

timeBeginPeriod(minRes);

m_id = timeSetEvent(5000,minRes,TimerFunction,0,TIME_PERIODIC);
}

void stopTimer()
{
timeKillEvent(m_id);
timeEndPeriod (5000);

}

Circular Buffer

I have created a program using heap memory to implement circular buffer. The following is the code of the program.

 

// testCircularBuffer.cpp : Defines the entry point for the console application.
//

#include<iostream>

#include “stdio.h”
#include “stdlib.h”
#include “string.h”
#include<Windows.h>

#define LONG int
#define PBYTE char *

#define TRUE 1
#define FALSE 0

static LONG m_ReadHanlde;
static LONG m_WriteHanlle;
static PBYTE pMemBuffer;
static int memBufWrap ;// buffer wrap around

#define MIN_CHUNK_SIZE 1024 * 1024
int CIRCULAR_BUFFER_SIZE = 5 * MIN_CHUNK_SIZE; // size in MB

int InitCircularMemBuffer()
{

pMemBuffer = NULL;
m_ReadHanlde = 0;
m_WriteHanlle = 0;
memBufWrap = 0;

pMemBuffer = (char*) malloc(CIRCULAR_BUFFER_SIZE);
if(pMemBuffer == NULL){
return -1;
}

return 0;
}

int ReleaseCircularMemBuffer()
{

if(pMemBuffer != NULL){
free( pMemBuffer);
pMemBuffer = NULL;
}

return 0;
}

LONG GetCircularMemBufferSpace()
{
LONG lMemDist = 0;

if(pMemBuffer == NULL)
return 0;

//if(m_WritePtr == 0 && m_ReadPtr == 0)
if((m_WriteHanlle == m_ReadHanlde)&& (!memBufWrap))
{

return CIRCULAR_BUFFER_SIZE;
}

if(m_WriteHanlle > m_ReadHanlde)
{
lMemDist = (CIRCULAR_BUFFER_SIZE – m_WriteHanlle) + m_ReadHanlde;
}
else
{
lMemDist = m_ReadHanlde – m_WriteHanlle;
}

return lMemDist;
}

LONG GetCircularMemBufferArea()
{
LONG lmemArea = 0;

if(pMemBuffer == NULL)
return 0;

if(m_WriteHanlle == m_ReadHanlde && (!memBufWrap))
{

return 0;
}

if(m_WriteHanlle > m_ReadHanlde)
{
lmemArea = m_WriteHanlle – m_ReadHanlde;
}
else
{
lmemArea = (CIRCULAR_BUFFER_SIZE – m_ReadHanlde) + m_WriteHanlle;
}

return lmemArea;
}

int ResetCiruclarMemBuffer()
{
int MaxSecondCnt=10,i;

m_ReadHanlde = 0;
m_WriteHanlle = 0;
memBufWrap = 0;

return TRUE;
}

int ReadCircularMemBuffer(PBYTE pBuff, LONG lSize)
{
LONG lDist;

for(;;)
{
if(GetCircularMemBufferArea() >= lSize)
break;
else
{
Sleep(100);

}

}

if( ( m_ReadHanlde + lSize) > CIRCULAR_BUFFER_SIZE )
{
lDist = CIRCULAR_BUFFER_SIZE – m_ReadHanlde;
memcpy(pBuff, (pMemBuffer + m_ReadHanlde), lDist);
memcpy(pBuff + lDist, pMemBuffer, lSize – lDist);
m_ReadHanlde = lSize – lDist;
memBufWrap =0; // read also wrap around
}
else
{
memcpy(pBuff, (pMemBuffer + m_ReadHanlde), lSize);
m_ReadHanlde += lSize;
}

return lSize;
}

int WriteCircularMemBuffer(PBYTE pBuff, LONG lSize)
{
int dwDist;
//printf(“WriteBuffer+\n”);
if(pMemBuffer == NULL)
return FALSE;

//wait for enough space
for(;;)
{
if(GetCircularMemBufferSpace() >= lSize){
break;

}
else
{
Sleep(1);
}
}

if( ( m_WriteHanlle + lSize) > CIRCULAR_BUFFER_SIZE)
{

dwDist = CIRCULAR_BUFFER_SIZE – m_WriteHanlle;
memcpy( (pMemBuffer + m_WriteHanlle), pBuff,dwDist);

memcpy(pMemBuffer, (pBuff + dwDist), lSize – dwDist);
m_WriteHanlle = lSize – dwDist;
memBufWrap = 1;// the buffer just wrap around

return TRUE;
}

memcpy( (pMemBuffer + m_WriteHanlle), pBuff,lSize);
m_WriteHanlle += lSize;

return TRUE;
}

char testWriteBuf[5*MIN_CHUNK_SIZE+1];
char testReadBuf[5*MIN_CHUNK_SIZE+1];

int main()
{

InitCircularMemBuffer();

printf(“Free Mem %d and data size %d in buff m_WriteHandle %d m_ReadHanlde %d\n”,GetCircularMemBufferSpace(),GetCircularMemBufferArea(),m_WriteHanlle,m_ReadHanlde);
if(!WriteCircularMemBuffer(testWriteBuf, MIN_CHUNK_SIZE))
printf(“Write error\n”);
printf(“Free Mem %d and Data size %d avaliable in buff m_WritePtr %d m_ReadPtr %d\n”,GetCircularMemBufferSpace(),GetCircularMemBufferArea(),m_WriteHanlle,m_ReadHanlde);

if(!ReadCircularMemBuffer(testReadBuf, MIN_CHUNK_SIZE))
printf(“Read memory buffer error\n”);

}

Working with memory

I have created a program in which i have handled a memory allocated from heap. In this program i have created a memory of specific size form heap and storing data in it. When the memory is full then i reset it from start and storing data again in the memory. The following is the complete code of the program. 

 

// testMemory.cpp : Defines the entry point for the console application.
//

#include<stdio.h>
#include<iostream>

using namespace std;

char *startOfMemory;
char *pMemory;

int main(void){

pMemory = (char *)malloc(256);
startOfMemory = pMemory;
int countMem = 0;
char *str = strdup(“This is just test msg”);
int len = strlen(str);
int count = 0;
while(count<1000){

if (countMem + len > 256){
pMemory = startOfMemory;
printf(“Mem is full now set to start\n”);
countMem = 0;
}
memcpy(pMemory,str,len);
pMemory = pMemory + len;
countMem = countMem + len;
printf(“Mem Size:%d\n”,countMem);

count = count + 1;
}
pMemory = startOfMemory;

free(pMemory);
pMemory = NULL;

getchar();
return 0;
}

Encoding and decoding of Struct

I have created a program in which i have encode and decode the struct. This is required when sending data using network. The following is the code of the program.

// testStructEnDec.cpp : Defines the entry point for the console application.
//

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

using namespace std;

typedef struct ipV4{
char *strIP;
}tIpV4;

typedef struct IPList{
int num;
int port;
tIpV4 objIPList[20];
}tRemoteIPList;

size_t encodeRemoteIP(tRemoteIPList* objIPList, unsigned char** buf);
tRemoteIPList* decodeIP(const unsigned char* buf, const size_t bufSize);

int main(void){
tRemoteIPList objList;

objList.num = 3;
objList.port = 80;
objList.objIPList[0].strIP = strdup(“192.168.1.1”);
objList.objIPList[1].strIP = strdup(“192.168.1.2”);
objList.objIPList[2].strIP = strdup(“192.168.1.10”);
unsigned char *buf;
size_t bufSize = encodeRemoteIP(&objList,&buf);

tRemoteIPList* objIP = decodeIP(buf,bufSize);
printf(“Port:%d”,objIP->port);
for(int count = 0;count<objIP->num;count++){
printf(“IP:%s\n”,objIP->objIPList[count].strIP);

}

getchar();
return 0;
}

size_t encodeRemoteIP(tRemoteIPList* objIPList, unsigned char** buf)
{
int count;
size_t total_size = 0;
if (!objIPList)
{
return 0;
}
total_size = total_size + sizeof(size_t) + sizeof(size_t); // no of ips.

for(count = 0;count<objIPList->num;count++){
total_size = total_size + strlen(objIPList->objIPList[count].strIP) + sizeof(size_t)+sizeof(size_t);
}

*buf = (unsigned char *)calloc(1,total_size);
if (*buf)
{
size_t offset = 0;
size_t ipLen;
size_t numIp = htonl(objIPList->num);
size_t port = htonl(objIPList->port);

memcpy(*buf + offset, &numIp,sizeof(numIp)); offset += sizeof(numIp);
memcpy(*buf + offset, &port,sizeof(port)); offset += sizeof(port);
for(count = 0;count<objIPList->num;count++){
ipLen = strlen(objIPList->objIPList[count].strIP);
ipLen = htonl(ipLen);
memcpy(*buf + offset, &ipLen,sizeof(size_t)); offset += sizeof(ipLen);
memcpy(*buf + offset, objIPList->objIPList[count].strIP,ipLen); offset += ipLen;
}
}

return total_size;

}

tRemoteIPList* decodeIP(const unsigned char* buf, const size_t bufSize)
{
tRemoteIPList *objIP;

if(buf!= NULL){
objIP = (tRemoteIPList *)calloc(1, sizeof(tRemoteIPList));

if(objIP !=NULL){
size_t numIP = 0;
size_t port;
size_t offset = 0;
size_t ipLen;
unsigned int count = 0;
memcpy(&numIP, buf + offset, sizeof(numIP));
numIP = ntohl(numIP);
offset += sizeof(numIP);
objIP->num = numIP;

memcpy(&port, buf + offset, sizeof(port));
port = ntohl(port);
offset += sizeof(port);
objIP->port = port;

for(count = 0;count<numIP;count++){
memcpy(&ipLen, buf + offset, sizeof(ipLen));
ipLen = ntohl(ipLen);
offset += sizeof(ipLen);
objIP->objIPList[count].strIP = (char *)calloc(1,ipLen + 1);
memcpy(objIP->objIPList[count].strIP,buf+offset,ipLen);
offset +=ipLen;

}

}
}

return objIP;
}

Array of Pointer to Pointer

I have created a program in which i have used array of pointer to pointers. The following is the complete code. 

// testArrayOfPointers.cpp : Defines the entry point for the console application.
//

#include<conio.h>
#include<iostream>

using namespace std;

typedef unsigned char ** arrayofPointerToPointer[5];

arrayofPointerToPointer pointerToPointerList;
unsigned char **pPointerArray;

int main(void){

for(int count = 0;count<5;count++){
pointerToPointerList[count] = NULL;
}
pPointerArray = (unsigned char **)malloc(5 * sizeof(unsigned char **));
pointerToPointerList[0] = pPointerArray;
char *strName1 = strdup(“Jawad1”);
char *strName2 = strdup(“Jawad2”);
char *strName3 = strdup(“Jawad3”);
char *strName4 = strdup(“Jawad4”);
pPointerArray[0] = (unsigned char*)strName1;
pPointerArray[1] = (unsigned char*)strName2;
pPointerArray[2] = (unsigned char*)strName3;
pPointerArray[3] = (unsigned char*)strName4;

unsigned char **pTempPointerArray = pointerToPointerList[0];

for(int i=0;i<4;i++){
printf(“My name:%s\n”,pTempPointerArray[i]);
}

getchar();
return 0;
}