Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

CS609- SYSTEM

PROGRAMMING
ASSIGNEMN-2
JUNAID MALIK

BC190202640@vu.edu.pk For More Visit: vulmshelp.com JUNAID MALIK


Junaidfazal08@gmail.com
(0304-1659294)
AL-JUNAID TECH INSTITUT
Question:
Write a program in C/C++ to create and execute a thread within a virtual address space of a
calling process using the CreateThread() API. Set the parameters as:
1. The security attributes for a thread should be NULL,
2. Stack size should be default,
3. name of thread function should be “VUFunc”,
4. no parameter should be passed to the thread,
5. the thread should be run immediately after the creation, and
6. Thread Id should be received in a pointer to a variable.

Display the message if thread is successfully created, otherwise display the error code using
GetLastError() function. Also, display the created thread ID in a main function. Inside the
“VUFunc”, display the string “Virtual University” ten times.

ANSWER:
#include <windows.h>
#include <iostream>

void VUFunc() {
for (int i = 0; i < 10; i++) {
std::cout << "Virtual University" << std::endl;
}
}

int main() {
DWORD threadId;
AL-JUNAID TECH INSTITUT
HANDLE hThread = CreateThread(NULL, 0,
(LPTHREAD_START_ROUTINE)VUFunc, NULL, 0, &threadId);

if (hThread == NULL) {
std::cout << "Error creating thread: " << GetLastError() << std::endl;
return 1;
}

std::cout << "Thread successfully created with ID: " << threadId <<
std::endl;
WaitForSingleObject(hThread, INFINITE);
CloseHandle(hThread);
return 0;
}

You might also like