Learning Pthreads

February 17th, 2009 Comments off

I am beginning to enjoy the Prepare Future program on Linux internal and   driver development. Today’s class was about pthreads. I had some prior experience on pthreads library while at CUSAT.   It was refreshed today.

Pthreads is a user space library for  manipulating threads.

Here is what we did today.  First we wrote a small program which created a thread.

It is here.

#include<stdio.h>
#include <sys/types.h>
#include <pthread.h>

void * thread_function ( void *arg)
{
printf(“In Thread\n”);
pthread_exit(“Success”);
}

int main()
{
pthread_t tid;
void * ret_status;
pthread_create( &tid,NULL, &thread_function,NULL);

printf(“In Main\n”);

pthread_join(tid ,&ret_status);

return (0);

}

For compiling this I used.

$gcc -D_REENTRANT -o pt1 pt1.c -l pthread

Then  i tried out   semaphore and mutex in pthread library.

The important semaphore operations are

sem_init  –   int sem_init(sem_t *sem, int pshared, unsigned int value);

sem_ wait    int sem_wait(sem_t *sem);

sem_post         int sem_post(sem_t *sem);

sem_destroy   int sem_destroy(sem_t *sem);

I was forced to RTFM the manual. 😀

I wrote this then.

#include<stdio.h>
#include <sys/types.h>
#include <pthread.h>
#include <semaphore.h>

int y=0;
sem_t sigsem;

void * thread1 ( void *arg)
{
int x;
sem_wait(&sigsem);

x=y+3;

printf(“In Thread1\n”);
printf(” x is %d\n”,x);

pthread_exit(“Success thread1 “);
}

void * thread2 ( void *arg)
{
y=45;
sem_post(&sigsem);
printf(“In Thread2\n”);
printf(” y is %d\n”,y);
pthread_exit(“Success thread2”);
}

int main()
{
pthread_t tid1,tid2;
void * ret_status;

sem_init( &sigsem,0,0);

pthread_create( &tid1,NULL, &thread1,NULL);
pthread_create( &tid2,NULL, &thread2,NULL);
printf(“In Main\n”);

pthread_join(tid1 ,&ret_status);
pthread_join(tid2 ,&ret_status);

sem_destroy(&sigsem);

return (0);

}
This was followed by producer consumer problem as below.

#include<stdio.h>
#include <sys/types.h>
#include <pthread.h>

//sem_t sigsem;
int buffer[10];

void * producer ( void *arg)
{
int i,index;
index=0;

for (i =0;i<40 ;++i)
{

printf(“Producer  trying to produce an item……….\n”);

buffer[index] =i;

printf (“producer put  %d  at buffer [%d]\n”, i,index);

index++;
if (index==10)
index=0;
}

pthread_exit(“Producer ends”);
}

void * consumer ( void *arg)
{
int i,index=0;
for (i=0;i<40;++i)
{
printf(” Consumer  trying to consume  an item ……….\n”);
printf ( ” Consumer ate  %d from buffer[%d]\n”, buffer [index] ,index);

index++;
if (index==10)
index=0;
}

pthread_exit(“Consumer ends”);
}

int main()
{
pthread_t tid1,tid2;
void * ret_status;
pthread_create( &tid1,NULL, &producer,NULL);
pthread_create( &tid2,NULL, &consumer,NULL);

pthread_join(tid1 ,&ret_status);
pthread_join(tid2 ,&ret_status);

return (0);

}
The mutex in it  was then rewritten with pthread_mutex functions. Again lot of RTFM.

Then as an assignment we did the readers and writers problem.

#include<stdio.h>
#include <sys/types.h>
#include <pthread.h>
#include <semaphore.h>

sem_t  mutex,rcmutex;
int reader_count=0;
int buffer=10;

void reader_action(int r )
{

printf ( “\nReader %d  buffer is %d “,r, buffer);
}

void  write_action (int i)
{
int j;
buffer+=10;
for (j=0;j<10;++j) ; //Waste some time

printf ( “\n Writing for %d time buffer is %d “,i, buffer);

}

void * reader1 ( void *arg)
{

sem_wait(&rcmutex);
reader_count++;
if(reader_count==1)
sem_wait(&mutex);
sem_post(&rcmutex);

reader_action(1);

sem_wait(&rcmutex);

reader_count–;
if ( reader_count==0)
sem_post(&mutex);

sem_post(&rcmutex);

pthread_exit(“Success reader1 “);
}

void * reader2 ( void *arg)
{

sem_wait(&rcmutex);
reader_count++;
if(reader_count==1)
sem_wait(&mutex);
sem_post(&rcmutex);

reader_action(2);

sem_wait(&rcmutex);

reader_count–;
if ( reader_count==0)
sem_post(&mutex);

sem_post(&rcmutex);

pthread_exit(“Success reader2 “);
}

void * writer ( void *arg)
{
int i;
for (i=0 ; i<10;++i)
{

sem_wait (&mutex);
write_action(i);
sem_post(&mutex);
}
pthread_exit(“Success writer”);
}

int main()
{
pthread_t tid1,tid2,tid3,tid4,tid5,tid6;
void * ret_status;

sem_init( &mutex,0,1);
sem_init( &rcmutex,0,1);

pthread_create( &tid1,NULL, &reader1,NULL);
pthread_create( &tid2,NULL, &writer,NULL);
pthread_create( &tid3,NULL, &reader2,NULL);
pthread_create( &tid4,NULL, &reader1,NULL);
pthread_create( &tid5,NULL, &reader2,NULL);
pthread_create( &tid6,NULL, &reader1,NULL);

pthread_join(tid1 , &ret_status);
pthread_join(tid2 ,&ret_status);
pthread_join(tid3 ,&ret_status);
pthread_join(tid4 , &ret_status);
pthread_join(tid5 , &ret_status);
pthread_join(tid6 , &ret_status);

sem_destroy(&mutex);
sem_destroy(&rcmutex);
return (0);

}
The course is progressing smoothly. and looks interesting several fellow participants are also stimulating.

I forgot to take the card reader, so I am unable to post photos.  I had a walk around the JNTU campus in the morning and took several photos. May be I will beautify these blog enties with  photos and proper code snippets ( and build   proper pthread tutorial)   when I return.

How to create static and dynamic libraries with gcc

February 16th, 2009 Comments off

This is an exercise I did   today. I  am documenting it for my own reference.

The problem was simple.

Create a library with functions.

add(int,int) and sub (int,int)

and then use the library  in another program.

Here is the code

/*add.c*/

#include “a.h”

int add( int a,int b)

{
return (a+b);
}
int sub (int a, int b)
{
return( a-b);
}

Here is the header

/* a.h*/

int add(int,int);
int sub(int,int);

Compile add.c to get add.o

gcc -c add.c 

You will have an object file add.o .

Now  create a static library  with

ar rcs  libadd.a add.o

If you have a program which uses functions from your new library as below

/*  main.c */

#include “a.h”
int main()
{
printf (“%d\n”, add(5,10));
printf (“%d\n”, sub(10,5));
return 0;
}
You can link against  our new  library  like this.

gcc -o main main.c -L. -ladd

( Note the -L. and -ladd. -L. say that look for library in current directory and -ladd says that the name of the library is libadd.a)

For creating dynamic libraries, first compile the file containing library as below.

gcc -c -fPIC   add.c

and

 ld -shared -soname libadd.so.1 -o libadd.so.1.0 -lc add.o

 ln -sf libadd.so.1 libadd.so

/sbin/ldconfig -v -n .

( Dont forget .)

Now you are ready to use the library

gcc -o main  main.c -L. -ladd

Categories: Gnu/Linux Tags:

At JNTU Hyderabad for Prepare Future

February 16th, 2009 Comments off

I am presently attending a 2 weeks training program  conducted by  CDAC Hyderabad  at JNTU. This  is 2 weeks faculty updation program named Prepare future. I am attending Linux internals and driver development under system software track.

The course syllabus looks pretty impressive as given below.

  • Introduction to Linux System Programming
  • Linux Architecture
  • Linux Shell
  • GNU Tool Chain (GCC, GDB, MAKE, GPROF & GCONV)
  • System Calls and Working with Files
  • Linux Environment
  • Process Management & IPC
  • POSIX Compliant Thread Programming
  • Socket Programming
  • Linux Kernel Programming and Module Programming
  • Character Drivers
  • Concurrency, Race Conditions & IOCTL
  • Timers & Character Drivers
  • Block Drivers & Network Drivers
  • Introduction to USB Drivers

The course started today with an introduction to Linux architecture, system calls and programming interface. There  was nothing new to me, but I enjoyed the lecture by Ms Lakshmi  as  it refreshed my ideas about several concepts.   The second session was an introduction to shell,gcc and make.  It was not  a perfect lecture and the inexperience of the faculty was quite visible.

The afternoon was spent in the lab., with exercises on gcc make and gdb.  The lab sessions are held on  OpenSuse. In fact I am using it for the first time. As a long time debian user I have some discomfort.May be I will catch up by tomorrow.

The interesting thing about the program is that I have an option to sit in the lab as long as I like.  I feel like I was in CUSAT computer science department.

There are nearly 15 other participants. Most of them are from research organisations like CDAC , DRDO and DOE.  Even though the course is for faculty updation  I don’t find many faculty members attending the training. ( I am the only one from Kerala.)

Sample question papers for ASOC Examination

January 16th, 2009 9 comments

The Amateur Station Operators Certificate Examination is conducted   by Ministry of communication Government of India.  You have to pass this examination if you want to be a HAM radio operator in India. Several SWLs have asked me  about the nature of the examination and how the question papaers look like. I am posting some sample papers that I collected when I wrote the examination.
This Zip file has several sample papers that you can try before the actual examination.

Categories: Ham Radio Tags:

How to become a certified (Pseudo) Free software Activist

January 5th, 2009 7 comments

My recent posts on the state of free software in Kerala,  have drawn flak from several pseudo free software activists.  Some of them have even called me an agent of Microsoft.  I think this post is going to generate some more heat and the pun is intentional.

Recently Kerala State IT mission and  C-DIT have launched a new initiative called Centre for advanced  training in free and open source software(CATFOSS).  You can read about the objectives of the centre in this lenghty article.  ( Google  cache here)

Last week CATFOSS  announced admission to a six  month training program in advanced free and open source software.  As an academic, I was interested in this novel program. I looked at the website and discovered  several interesting information.

<Quote from CATFOSS>

In order to resist this process, two decades back, Mr. Richard Stallman founded the Free and Open Source Software (FOSS) movement in 1983. As the name FOSS itself implies, the ideology behind the FOSS movement has been that the fruits of the ICT revolution should flow freely among the users of this technology, the world over

<Quote from CATFOSS ends>

As far as I know, RMS has some objections to  Open Source  Software. Look at his article “why open source misses the point of free software” .

I was educated in a Government school and my English is not very good.  Still, I will recommend the website as an excellent model for precise writing . See this.

<Quote from CATFOSS>
One of the major problems the State government had to face in the process of to propogating FOSS was the lack of trained human reources and non-availability of FOSS based tools. Thus, the Centre for Advanced Training in Free and Open Source Software (CATFOSS), proposed to be initiated by the end of November 2008 at Ernakulam in the ITes Habitat in collaboration with Kerala State IT Mission (KSTIM) and Centre for Development of Imaging Technology (C-DIT) will unquestionably be a courageous step and perhaps the first of its kind in India to develop highly qualified manpower who can enrich the applications of FOSS based software tools in a time bound manner, thereby bridging a very crucial gap in the e-governance sector and IT industry in Kerala and elsewhere.

<Quote from CATFOSS ends>

Brochures and websites show you only the tip of the iceberg. Let us look at what else  CATFOSS offers.  If you are interested in the  recently announced  Advanced Training in Free and Open Source Software,   you can apply online here.   ( Google cache here). The course fee is only Rs50000/- and employment opportunities are very good  even during  the current recession.  The seats are limited,  so rush your application.

Let us see what you will learn. The prospectus is here.  (Google Cache here)

< Quote from Prospectus>

Curriculum
The curriculum at CATFOSS will have the following objectives :
a) Introduction to free software ecosystem, its philosophy and technical aspects.
b) Empowerment to spearhead free software projects.
c) Thorough knowledge in free software development work flow.
d) Indepth knowledge in designing, implementing, deploying and maintaining a free software
based solution for the society.
<< Quote from Prospectus ends>

Wow! Lot of advanced things to learn.  Is there any company recruiting  Free software activists? May be the  Kerala government can accommodate some. Who else ? God only knows . We, keralites  are living in God’s  own country.

There is a bonus. CATFOSS will teach you soft skills free of cost along with the course.  Look at the academic committe who formulated this crap.

Finally, how much public money is needed for this. Only  82 lakhs. 😀 .

Free software enthusiast of Kerala , wake up.  There is a new course designed for  you. You can become  a Free Software Activist in six months. Results assured. Rush the seats are limited.

Update: The  CATFOSS site has been take down. The google cache is here. I am adding relavant links from google cache.