Skip to content

Instantly share code, notes, and snippets.

@figengungor
Created October 8, 2013 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save figengungor/6889010 to your computer and use it in GitHub Desktop.
Save figengungor/6889010 to your computer and use it in GitHub Desktop.
Child Process in Windows Lab 2 -- creates threads
#include<stdio.h>
#include<Windows.h>
#include<stdlib.h> //required for malloc
#include<malloc.h> //required for malloc
//thread needs a function to execute, a parameter to this function if it has any, and a thread id
DWORD WINAPI threadwork(LPVOID param);
//DWORD is a data type for OS like int, WINAPI for OS to understand that it is a thread function
//Thread function only takes one parameter.
//To have several different data types, a struct should be used for thread attributes.
//LPVOID is a void pointer, so param should be casted to struct type before using in thread function.
//struct which keeps thread attributes
typedef struct
{
int start;
int end;
int thread;
} THREAD_PARAMETERS;
int main(int argc, char* argv[])
{
int i, thread_count;
HANDLE* handles; //WHY didn't we create an array instead of a pointer?
THREAD_PARAMETERS* param;
int* threadID;
if(argc!=2)
{
printf("error in child process.\n");
system("pause");
exit(0);
}
thread_count =atoi(argv[1]) //argument comes as string, convert it to int
//for handles, THREAD_PARAMETERS, threadID, only one pointer space is reserved.
//It won't be enough for thread_count number of threads.
//So we need to allocate this space for each of these with malloc.
/*
malloc returns a void pointer to the allocated space, or NULL if there is insufficient memory available.
To return a pointer to a type other than void, use a type cast on the return value.
The storage space pointed to by the return value is guaranteed to be suitably aligned
for storage of any type of object. If size is 0, malloc allocates a zero-length item in the heap
and returns a valid pointer to that item.
Always check the return from malloc, even if the amount of memory requested is small.
*/
handles = (HANDLE*) malloc(sizeof(HANDLE) * thread_count);
param = (THREAD_PARAMETERS*) malloc(sizeof(THREAD_PARAMETERS) * thread_count);
threadID = (int*) malloc(sizeof(int) * thread_count);
for(i=0; i<thread_count; i++)
{
param[i].start = 0;
param[i].end = 10;
param[i].thread = i+1;
handle[i]=CreateThread(NULL, 0, thread_work, &param[i], NULL, (LPWORD)&threadID[i]);
if(handle[i]==INVALID_HANDLE_VALUE)
{
printf("error in child: cannot create thread\n");
system("pause");
exit(0);
}
printf("%d - thread %d has started working.", threadID[i], param[i].thread);
}
WaitForMultipleObjects(thread_count, handles, TRUE, INFINITE)
for(i=0; i<thread_count; i++)
{
CloseHandle(handle[i]);
}
//HeapFree frees a memory block allocated from a heap
/*
BOOL WINAPI HeapFree(
_In_ HANDLE hHeap,
_In_ DWORD dwFlags,
_In_ LPVOID lpMem
);
hHeap[in]
A handle to the heap whose memory block is to be freed.
This handle is returned by either the HeapCreate or GetProcessHeap function.
dwFlags[in]
The heap free options.
lpMem[in]
A pointer to the memory block to be freed.This pointer is returned by the HeapAlloc or HeapReAlloc function.
If this pointer is NULL, the behavior is undefined.
*/
HeapFree(GetProcessHeap(), 0, handles);
HeapFree(GetProcessHeap(), 0, params);
HeapFree(GetProcessHeap(), 0, threadID);
return 1;
}
DWORD WINAPI threadwork(LPVOID param)
{
THREAD_PARAMETERS* parameter = (THREAD_PARAMETERS*) param;
int i;
for(i=parameter->start; i<parameter->end; i++)
{
printf("%d is working: counting %d", parameter->thread, i);
sleep(1000);
}
system("pause");
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment