Search within this blog

Tuesday, June 29, 2010

First day Second Show

From here let me take you one month back. After a tedious end semester examination we all were tired. In engineering end semesters are always tedious, as you hardly study throughout the semester and the whole load concentrates. So on the day of the last exam we decided to hang out.

We needed a place to go. First of all we made lots of debates and discussion. And as time passed in discussion and view - counter-views, the schedule of most of the movie halls became unsuitable. Except one, Hind. We decided to go there. Although the movie was not of choice for most of us.

The six of us went outside the college and weighted for the bus. Now, I have a theory in this. Whenever you weight for a bus say S-12, all the bus except that would pass you. So, it is no wonder that we saw two back to back S-9s and some of the rarest buses we never get in the days of requirement. After waiting for (say,) 30 minutes, we did some calculation. Suman pointed out if we share the taxi fare among six of us, then it is almost comparable to the bus fare. We agreed and took a taxi. It is no wonder that as soon as we got inside, an S-12 passed by.

The movie was Jhoom Barabar Jhoom (first day second show). Whoever have watched the movie knows how it is. It is one of the most Codopyrine movies i have ever seen. No story, no screen play, no acting and of course, no ticket to hollywood. Believe me, it is really hard to undergo Bobby Deol in the latter half of the movie. The songs were in the same category except Bol Na Halke Halke.

But none of us could possibly think of for what I would remember the day later on. That was the first day I suspected the entry of someone else in my life.

From here the post takes an acute angle turn.

We sat side by side in the movie hall. Could I concentrate on the movie fully? When we came out (all six of us, if people wants disambiguation) it was raining. We stopped for a roll. Egg roll mixed with Calcutta rain is a kind of irresistible taste. Now one after another the copy-book cliches started happening around me. We both became deserted near the foot-over bridge near Sealdah. It started raining heavily and we found there was only one umbrella (that too a small, fragile, sealdah market special). This was the beginning of a long story.

Thursday, June 17, 2010

Creating Multiple Processes under Windows (repost)

The following code illustrates how multiple processes can be spawned run in parallel under Windows. Here Windows API is used for system calls (like fork in Linux). The compiler I used is Microsoft Visual C++ 6.0.


There are three C source files:

  1. Win32Process1.c for Win32Process1.exe
  2. Win32Process2.c for Win32Process2.exe
  3. Win32MulProc.c for Win32MulProc.exe

Win32Process1 & Win32Process2 are similar. They both print integers 0 onwards with a specific delay between two integers. The delay can be specified by command line. If no delay is specified in command line then they goes for default delay which is set to 500ms.

Win32MulProc creates and runs Win32Process1 & Win32Process2 as child processes. It uses CreateProcess API call. Win32MulProc also has a loop that prints integers as before with specific delay. The delays for all 3 processes can be set by command line like
> Win32MulProc 500 250 250

The three processes the main one, Win32Process1 and Win32Process2 runs simultaneously.

The main process waits until the children completes.

CODES:

Win32Process1.c 1/27/2007

/**

* Win32Process1.c

* Code for Win32Process1.exe

*

* This is the code for the child process Win32Process1 which will be

* created by parent process Win32MulProc

*

* Author: Joydip Datta

* Date: 3:09 PM Saturday, January 27, 2007

*/

#include stdio.h

#include stdlib.h

#include windows.h

// a simple process thats counts 1 2 3 4 etc until Esc is hit

// a specific delay is imposed between each count

#define DEFAULT_DELAY 500 //ms

int main(int argc, char * argv[]) {

int delay, i, ch;

printf("\nProcess Win32Process1 started...\n");

if(argc>=2) //delay specified

delay = atoi(argv[1]);

else //delay not specified

delay = DEFAULT_DELAY;

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

if(kbhit())

if((ch=getch())==27)

break;

printf("Win32Process1: %d\n",i);

Sleep(delay);

}

printf("\nProcess Win32Process1 ended...\n");

}

Win32Process2.c 1/27/2007

/**

* Win32Process2.c

* Code for Win32Process2.exe

*

* This is the code for the child process Win32Process2 which will be

* created by parent process Win32MulProc

*

* Author: Joydip Datta

* Date: 3:09 PM Saturday, January 27, 2007

*/

#include stdio.h

#include stdlib.h

#include windows.h

// a simple process thats counts 1 2 3 4 etc until Esc is hit

// a specific delay is imposed between each count

#define DEFAULT_DELAY 500 //ms

int main(int argc, char * argv[]) {

int delay, i, ch;

printf("\nProcess Win32Process2 started...\n");

if(argc>=2) //delay specified

delay = atoi(argv[1]);

else //delay not specified

delay = DEFAULT_DELAY;

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

if(kbhit())

if((ch=getch())==27)

break;

printf("Win32Process2: %d\n",i);

Sleep(delay);

}

printf("\nProcess Win32Process2 ended...\n");

}

Win32MulProc.c 1/27/2007

/**

* Win32MulProc.c

* This is the parent process that calls and run two child processes

* Win32Process1.exe and Win32Process2.exe

* (and of course there is the main process)

* (To check how Windows handles multi programming)

*

* Author: Joydip Datta

* Date: 3:41 PM Saturday, January 27, 2007

*/

#include stdio.h

#include windows.h

#define DEFAULT_DELAY 500

#define PROCESS1 "Win32Process1"

#define PROCESS2 "Win32Process2"

int main(int argc, char* argv[])

{

STARTUPINFO si1,si2;

PROCESS_INFORMATION pi1,pi2;

int delayMain,delayP1,delayP2, i, ch;

char cmd1[50],cmd2[50]; //commands for two child processes

printf("\nProcess Main started...\n");

//delay calculation

if(argc>=4) {//delay specified

delayMain = atoi(argv[1]);

delayP1 = atoi(argv[2]);

delayP2 = atoi(argv[3]);

}

else //delay not specified

delayMain = delayP1 = delayP2 = DEFAULT_DELAY;

//setup commands

sprintf(cmd1,"%s %d",PROCESS1,delayP1);

sprintf(cmd2,"%s %d",PROCESS2,delayP2);

printf("\nPreparing to open two child processes...\n");

//CREATE 1ST PROCESS

//ALLOCATE MEMORY

ZeroMemory(&si1,sizeof(si1));

si1.cb=sizeof(si1);

ZeroMemory(&pi1,sizeof(pi1));

//create child process

if(! CreateProcess(NULL,

cmd1,

NULL,

NULL,

FALSE,

0,

NULL,

NULL,

&si1,

&pi1))

{

fprintf(stderr,"err: Win32Process1 creation failed");

return -1;

}

//CREATE 2ND PROCESS

//ALLOCATE MEMORY

ZeroMemory(&si2,sizeof(si2));

si2.cb=sizeof(si2);

ZeroMemory(&pi2,sizeof(pi2));

//create child process

if(! CreateProcess(NULL,

cmd2,

NULL,

NULL,

FALSE,

0,

NULL,

NULL,

&si2,

&pi2))

{

fprintf(stderr,"err: Win32Process2 creation failed");

return -1;

}

//loop for main process

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

if(kbhit())

if((ch=getch())==27)

break;

printf("Main: %d\n",i);

Sleep(delayMain);

}

printf("\nLoop of Process Main ended.\nBut Process Main will wait for her children...\n");

//parent will wait till child completes

WaitForSingleObject(pi1.hProcess,INFINITE);

printf("1st child complete\n");

WaitForSingleObject(pi2.hProcess,INFINITE);

printf("2nd child complete\n");

//close handles

CloseHandle(pi1.hProcess);

CloseHandle(pi1.hThread);

printf("\nProcess Main ended...\n");

return 0;

}

References:

- Galvin

- MSDN

Joydip Datta
1/27/2007 4:41:36 PM

Creating Threads in Windows

Like process you can also create threads in windows at ease. The thing is similar to pthread_create in Linux. This code is mostly taken from Galvin. You use the CreateThread API call and pass the thread function to it. The following code illustrates this. The code is taken from Silberschatz, Galvin, Gagne: Operating System Concepts, 7th Edition: Figure 4.7. But the code was not working for some reason, so, i made a few changes and post it here. The changes are in bold. I also added a few comments to describe the code more elaborately.

File: Win32ThrdCreat.c
---------------------------------------

/*
MULTITHREADED C PROGRAM USING WIN32 API

CREATES A CHILD THREAD TO CARRY OUT 'thread_func' FUNC.

MAIN THREAD AND CHILD THREAD RUNS CONCUURENTLY

1/2 S DELAY IS IMPOSED IN EACH THREAD FOR DEMONSTRATION PURPOSE

Galvin p 31
Modified by: Joydip Datta
Date: 9:56 AM Thursday, December 14, 2006
*/

#include
#include

DWORD Sum=0; //shared data

//the thread runs in this separate function
DWORD WINAPI thread_function(LPVOID Param)
{
DWORD Upper = * (DWORD*) Param;
long i;

for(i= 0; i<= Upper; i++){
printf("Child Thread: %ld\n",i);
Sum+=i;
Sleep(500);//pause for 1/2 s
}

return 0;
}

int main(int argc, char * argv[])
{
DWORD ThreadId;
HANDLE ThreadHandle;
long Param,i;

//perform basic err chk
if(argc != 2){
fprintf(stderr, "An Integer parameter is requirred");
return -1;
}

Param = atoi(argv[1]);

if(Param <>
fprintf(stderr,"An Integer param >=0 is requirred");
return -1;
}

//create the thread
ThreadHandle = CreateThread(
NULL, //default security attribs
0, //default stack size
thread_function, //pointer to thread func
&Param, //parameter to Thread func
0, //default cration flags
&ThreadId//returns the thread Id
);


for(i=0;i
printf("Main Thread: %ld\n",i);
Sleep(500); //pause for 1/2 s
}

if (ThreadHandle != 0) {
//wait for the thread object to finish
WaitForSingleObject(ThreadHandle, INFINITE);

//close the thread handle
CloseHandle(ThreadHandle);

printf("Sum: %ld",Sum);
}
return 0;
}

Monday, June 14, 2010

Being mad in Mumbai rain.. Childhood returns

I was inside lab. My lab doesn't have a window. So, I had no idea about what was going on outside. I came out and saw the earth getting soaked under Mumbai rain. I wrapped my pen, copy, earphone and mobile in a polythene bag, took my bi-cycle and went outside. Rolling through the slippery downhill road, I felt the raindrops all over my face, washing away the salts. Within a few moments I was all drenched. The rain is so heavy in Mumbai, that you can not even see objects that are meters away. While coming down I saw images of my childhood floating around me. The days when I intentionally kept the umbrella at bag. The days when I preferred cycling through those portions of the road where there were water standing. The childish joy while pushing myself through the waterlogged roads. Sometimes ice-stones fell during nor' westers. I remembered how I ran in the rooftop of my house to pick them up. I used to put some of them in my mouth when my mom was not watching.