Tuesday, February 2, 2010

Pthread Part-1

A Simple Pthread Program:
    0 #include <stdio.h>
1 #include <unistd.h>
2 #include <pthread.h>
3
4 void *fun(void *args) {
5 int i;
6
7 for (i=0; i<10; i++) {
8 printf("hello world\n");
9 sleep(1);
10 }
11 pthread_exit(NULL);
12 }
13
14 int main() {
15 pthread_t pt_id;
16
17 printf("Main: Before Create\n");
18 pthread_create(&pt_id, NULL, fun, NULL);
19 printf("Main: After Create\n");
20 pthread_join(pt_id, NULL);
21 printf("Main: After Join\n");
22 pthread_exit(NULL);
23 return 0;
24 }
25


Output:
  
$./pthread1
Main: Before Create
Main: After Create
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
Main: After Join
$

Thursday, January 7, 2010

Generic Makefile

Here is a Generic Makefile to compile xxx.c++ to xxx executable:
    0 CC = g++
1 CFLAG = -Wall -g -ansi
2
3 %: %.c++
4 $(CC) $(CFLAG) -o $@ $^
5
It will be very useful for all our sample codes.
For example:
hello.c++:
    0 #include <iostream>
1
2 using namespace std;
3
4 int main() {
5 cout<<"hello world"<<endl;
6 return 0;
7 }
$ make hello
g++ -Wall -g -ansi -o hello hello.c++
$ ./hello
hello world
$

Welcome to Programmer's Quest

Why did I create Prog Heaven ? What can you find in this blog ?

I started this blog as part of my New Year Resolution to start a Blog for Start-up Programmers. I'm going to post a variety of Programming Blog-posts which include:

* C Programming
* Unix/Linux System Programming
* Unix/Linux Network Programming
* C++ Programming
* Object Oriented Analysis & Design
* Unified Modeling Language - UML
* Functional Programming (Scheme, LISP, Haskel)
* Logic Programming (Prolog)
* Programming Problem Solving
* Algorithms & Data Structures
* Software Engineering (mostly Construction)
* and Other Programming Related Articles ...

I will also try to answer any question & comments related to programming. Enjoy the ride with me.