Tuesday, December 21, 2010

C code: A small program to convert decimal number to binary digits.

Hi,

Here its a small recursive function, which will convert a decimal value to binary digits.

#include <stdio.h>

void convert_binary(int a)
{
  if(a!=0)
    {
        convert_binary(a/2);
        printf("%d",a%2);
    }
    else
    {
        return ;
    }
}

int main()
{
    printf("Hello world!\n");
    convert_binary(130); // Here this recursive function will convert 130 to binary digits.
    return 0;
}

If you feel its good, give me a thanks !!!

 

C Threads : Two synchronized threads to print alternate numbers.

Here, i was trying to print alternate numbers starting from 0 to 11. I have two threads, one will always prints odd numbers and other will print only even number.

My intension is to print odd and even alternately. To ensure the synchronization, i am using MUTEXES.

Sample code :
pthread_mutex_lock (&mutexsum);
gCount++;
std::cout<<gCount<<endl;
pthread_mutex_unlock (&mutexsum);

Full version of the code is as below. Here I use pthread libraries to create the threads.
My reference  : https://computing.llnl.gov/tutorials/pthreads/.

#include <iostream>
#include<stdio.h>
#include <pthread.h>
using namespace std;
pthread_mutex_t mutexsum;
int gCount=0;

void *odd(void* ptr)
{
while(gCount<=10)
{
   if(gCount%2==1) //If even
   {
        pthread_mutex_lock (&mutexsum);
        gCount++;
        std::cout<<gCount<<endl;
        pthread_mutex_unlock (&mutexsum);
   }
}
}

void *even(void* ptr)
{
while(gCount<10)
{
   if(gCount%2==0) //If even
   {
        pthread_mutex_lock (&mutexsum);
        gCount++;
        std::cout<<gCount<<endl;
        pthread_mutex_unlock (&mutexsum);
   }
}
}

int main()
{

    pthread_t T_even, T_odd;
    int T_rt1,T_rt2;
    void *status;
    pthread_mutex_init(&mutexsum, NULL);
    std::cout << "Hello world!" << endl;
    int i=0; // this variable has no significance
    T_rt1= pthread_create(&T_odd,NULL,odd,&i); // here variable i has no significance. it can be null also. I am passing it as a trial
    T_rt2=pthread_create(&T_even,NULL,even,&i);

    pthread_join(T_odd,&status);
    pthread_join(T_even,&status);

    pthread_exit(NULL);
    return 0;
}

Send me your comments.

Tuesday, December 7, 2010

C Code: Functions to read a line from file, removing leading and trailing spaces from a string token and get token string.

/******************************************************************************
*
* Function Name: read_line
*
* Description: get one line from /etc/passwd file. If there is a line contains
* more than LINE_MAX characters, it will ignore it and read the
* next line. It also handles continuation character '\'. It always
* leaves (or inserts) \n\0 at the end of the line.
*
* Parameters:
* FILE *f: file ptr points to /etc/passwd file
* char *buffer: char array
* size_t bufeln: length of buffer
*
* Return Value:
* int : It returns the length of the line read, excluding the \n\0.
*
******************************************************************************/
static int read_line(
FILE *f,
char *buffer,
size_t buflen)
{
int linelen = 0;
char c;
int curlen = 0;
char *ret, *tempbuf;

tempbuf = buffer;
while (1) {
tempbuf += (unsigned)curlen;
/* buffer over flow, skip this long line */
if (tempbuf >= (buffer + buflen - 1))
{
do {
c = getc(f);
if (feof(f)) {
return (-1);
}
} while (c != '\n');
linelen = 0;
tempbuf = buffer;
curlen = 0;
}
ret = fgets(tempbuf, linelen == 0 ?
buflen : buflen - (unsigned)linelen, f);
curlen = strlen(ret);
linelen += curlen;
switch (ret == NULL ? EOF : ret[curlen - 1]) {
case EOF:
if (linelen == 0 ) {
/* The file was empty */
linelen = -1;
} else if (buffer[linelen] == '\\') {
/* We expected another line to append to previous
line as indicated by a trailing backslash, but the
file ended */
linelen = -1;
} else {
/* The file contained one line, and all is well */
buffer[linelen ] = '\n';
buffer[linelen + 1 ] = '\0';
}
return (linelen);
case '\n':
if (curlen > 1 &&
ret[curlen - 2] == '\\') {
curlen -= 2;
linelen -= 2;
} else {
ret[curlen - 1] = '\n';
ret[curlen ] = '\0';
return (linelen - 1);
}
break;
default:
break;
} /* case */
}
/*NOTREACHED*/
}

/******************************************************************************
*
* Function Name: gettok
*
* Description: parse one line read from /etc/passwd file, return pointer to the
* next passwd entry field
* Parameters:
* char **line_addr: address of the pointer that point to an array
*
* Return Value:
* char *p : pointer to an array
*
*****************************************************************************/
static char *gettok (char **line_addr)
{
char *q = *line_addr;
char *p = q;
while (*p && *p != ':' && *p != '\n')
p++;
if (*p == '\n')
*p = '\0';
else if (*p != '\0')
*p++ = '\0';
*line_addr = p;
return (q);
}

/******************************************************************************
*
* Function Name: rm_leading_and_trailing_blank
*
* Description: Removes all the leading and trialing blanks in a string token.
* A string " This is a test " will be formatted to
* "This is a test"
*
* Cases like empty character string and character string with
* just a space or spaces are also covered. A string " "
* will be reduced to ""
*
* Parameters:
* target_string: string token.
*
* Return Value: None
*
*****************************************************************************/
static void rm_leading_and_trailing_blank(char *target_string)
{

char * mover = target_string;
char * current = target_string;

while (*mover != '\0' && (*mover == ' ' || *mover == '\t'))
mover++;

while(*mover)
{
*current = *mover;
current++, mover++;
}
*current = '\0';
current--;
while ( current >= target_string &&
(*current == ' ' || *current == '\t' || *current == '\n'))
{
*current='\0';
current--;
}

}