C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 6th, 2013, 07:24 PM
MatanBarLev MatanBarLev is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 3 MatanBarLev User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 m 59 sec
Reputation Power: 0
Events in C and problems in the program

When I write the code:

Code:
 #define _CRT_SECURE_NO_WARNINGS /* to suppress Visual Studio 2010 compiler warning */

#include "MoreTypes.h"
#include <stdio.h>
#include <Windows.h>
#include "Bmp24LineReader.h"
#include "Dynamic2DCharArray.h"
#include "Hw3Types.h"
#include "IspImage.h"
#include "IspImageAnalysis.h"
#include <string.h>
	
/**
 * LoadPicture() change picture from colors to gray.
 * 
 * accepts:
 * -------
 * Picture - the color picture.
 * Pitch - the size of line in bytes
 * Height - the height of the picture in pixeles
 * Width - the width of the picture in pixles
 *
 * Returns:
 * -------
 * This function doesn't return anything - it change a global variables:
 * PicGrayArr - the gray picture
 * ArrOfEvents - array that signals everytime row has been changed to gray
 */ 

void LoadPicture(ScandAPictureRowStruct* ThreadData) 
{
	EReadRowRes res;
	Byte_t* LineBuffer;
	Byte_t Red=0, Green=0, Blue=0, Gray=0;
	int j, RowNum=0;
	char num=0;
	DWORD dwWaitResult=0, ReleaseRes; 
	int Pitch=0, Width=0, Height=0;
	
	printf("Enter Thread Function\n");


	Height = ThreadData->Height;
	Width = ThreadData->Width;
	Pitch= ThreadData->Pitch;


	//creating space for the line
	LineBuffer = (Byte_t*) malloc (sizeof(Byte_t) * Pitch);
	if (LineBuffer==NULL)
	{
		//handle error
		printf("Memory Allocation Failed\n");
		//handle error
	}
	//reading the picture and making in gray
	for (RowNum=0; RowNum < Height; RowNum++)
	{
		//reading color line
		res = ReadRow (picture, LineBuffer);
		if (res==FAILURE)
		{
			CloseBmp24LineReader( &picture );
			//handle error
		}
		if (res != END_OF_FILE)
		{
			//convert pixel from color to gray
			for (j=0; j<Width; j=j+3)
			{
				Blue = *(LineBuffer+j);
				Green = *(LineBuffer+j+1);
				Red = *(LineBuffer+j+2);
				num=ConvertPixelColorToGrayscale(Blue, Green, Red);  	 // Convert Color Pixel to Gray Pixel
				PicGrayArr[RowNum][j]=num;
			}
		//signal that the line was loaded 
		SetEvent(ArrOfEvents[RowNum]);
		}
	}
	ReleaseRes = ReleaseSemaphore(semap, 1, /* Signal that exactly one cell was emptied */NULL );	/* We don't care what the previous count was */
	if ( ReleaseRes == FALSE ) 
	{
		//handle error
	}
}


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

{
	// ******* Variables *********////////
	int Pitch=0, i=0, j=0;
	char num=0; 
	char* PicFileName=NULL, *OutputFileName=NULL, *str=NULL;
	int Height=0, Width=0;
	int FaceWidth=0, FaceHeight =0, NumOfAreasVertical = 0, NumOfAreasHorizontal = 0, NumOfMaxThreads;
	DWORD threadID, WaitRes;
	Byte_t *LineBuffer=NULL,  **LineBufferPointerArr=NULL;
	ScandAPictureRowStruct ThreadData;
	HANDLE ReadPicThread;
//	// ***** Variables ********///////
//
//
////	printf("hello\n"); 
//
//	// ****** Input Handling  ***********// 
//	if (argc!=8)
//	{
//		printf ("Input Error\n");
//		//handle error
//	}

	PicFileName = argv[1];
	OutputFileName = argv[2];
	FaceWidth = atoi(argv [3]);
	FaceHeight = atoi(argv[4]);
	NumOfAreasVertical = atoi(argv[5]); 
	NumOfAreasHorizontal = atoi(argv [6]);
	NumOfMaxThreads = atoi(argv [7]); 
//
//	// ****** Input Handling ************//
//
	//create semaphore
	semap = CreateSemaphore( NULL, // default sec. attr.
	NumOfMaxThreads, // initial count
	NumOfMaxThreads, // max count
	NULL ); // no name

//	
//	//create events. We create an event for each row. Once the event is signaled it means that we have already loaded the current row
	ArrOfEvents = (HANDLE*) malloc ( sizeof(HANDLE) * Height); 
	if (ArrOfEvents == NULL)
	{
		printf("error!\n");
		//handle error
	}

	//read the picture
	picture= OpenBmp24LineReader(PicFileName);
	if ( !IsInitialized( picture ) ) 
	{
		printf("error!\n");
	//	handle error
	}
	Pitch = GetPitch(picture); // Get number of bytes in a row
	Width = GetWidth (picture);
	Height = GetHeight (picture);

	PicGrayArr = CreateDynamic2DCharArr (Height, Width);

	
	//initial event array
	for(i=0; i < Height; i++)
	{
		ArrOfEvents[i] = CreateEvent(NULL, TRUE /* manual reset*/, FALSE /*initial state is not signaled*/ , NULL /* Object not named*/ ); 
		if (ArrOfEvents[i]==NULL)
		{
			printf("Create Event Error\n"); 
		//	handle error
		}
	}

	////creating thread to read the picture
	ThreadData.picture = picture;
	ThreadData.Pitch = Pitch;
	ThreadData.Height = Height;
	ThreadData.Width = Width;

	
	printf("Before Sema\n");
	printf("Before Thread\n");

	ReadPicThread=CreateThread( NULL, 0 /*use default stack size*/, (LPTHREAD_START_ROUTINE) LoadPicture /* thread function*/,
	&ThreadData /* argument to thread function */,0 ,&threadID	/* returns the thread identifier */  );


	WaitRes = WaitForSingleObject(ReadPicThread, INFINITE );
	if ( WaitRes != WAIT_OBJECT_0 ) 
	{
		//hanle error
	}


	CloseBmp24LineReader( &picture );
	free (LineBufferPointerArr);
	FreeDynamic2DCharArr(PicGrayArr);
}


I find out that when the program reaches the line of:

printf("Before Sema\n");
it's stuck, and exits without printing the line.

Howeven, once I omit the lines:

Code:
for(i=0; i < Height; i++)
{
ArrOfEvents[i] = CreateEvent(NULL, TRUE /* manual reset*/, FALSE /*initial state is not signaled*/ , NULL /* Object not named*/ ); 
if (ArrOfEvents[i]==NULL)
{
printf("Create Event Error\n"); 
//
handle error
}
}


The problem disappears, meaning the program doesn't get stuck at all and continues to run.

What may be the problem with the Create Events?

Reply With Quote
  #2  
Old January 7th, 2013, 09:22 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,458 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 33 m 33 sec
Reputation Power: 403
Unlikely to be the problem but easy to verify, what's the value of Height in that loop?
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Events in C and problems in the program

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap