Can someone help me with this computer science quesiton? I have attached my code
ID: 3756557 • Letter: C
Question
Can someone help me with this computer science quesiton? I have attached my code, I just need to see if im doing this right, Thank you
#include <windows.h>
#include <stdio.h>
DWORD Sum;
DWORD WINAPI Summation (LPVOID Param)
{
DWORD Upper = *(DWORD*) Param;
for (DWORD i = 0; i <= Upper; i++)
Sum +i;
return 0;
}
int main (int argc, char *argv[])
{
DWORD ThreadID;
HANDLE ThreadHandle;
int Param;
if (argc != 2) {
fprintf (stderr, “An integer parameter is required ”);
return -1;
}
Param = atoi(argv[1]);
if(Param < 0) {
fprintf(stderr, “An integer >= 0 is required ”);
return -1;
}
ThreadHandle = CreateThread(
Null,
0,
Summation,
&Param,
&ThreadId);
if (ThreadHandle != NULL) {
WaitForSingleObject(ThreadHandle, INFINITE);
CloseHandle(ThreadHandle);
printf(“sum = %d ”, Sum);
}
}
Objectives: 1. Work with threads on Windows OS 2. Gain experience with an industry standard OS Assignment: (1) Study Win32 APls for thread operations on Windows; (2) Design the function of a thread and write a program to create this thread on Windows OS.Explanation / Answer
Answer :
#define _WIN32_WINNT 0x0501
#include <windows.h>
#include <stdio.h>
#include <conio.h>
DWORD WINAPI MyThreadFunction(LPVOID lpParam)
{
printf("The parameter: %u. ", *(DWORD*)lpParam);
return 0;
}
int main(void)
{
DWORD dwThreadId, dwThrdParam = 1;
HANDLE hThread;
hThread = CreateThread(
NULL,
0,
MyThreadFunction,
&dwThrdParam,
0,
&dwThreadId);
printf("The thread ID: %d. ", dwThreadId);
if (hThread == NULL)
printf("CreateThread() failed, error: %d. ", GetLastError());
else
printf("It seems the CreateThread() is OK lol! ");
if (CloseHandle(hThread) != 0)
printf("Handle to thread closed successfully. ");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.