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

BC190408018

STUDENT NAME: RAHMEEN SHAHBAZ


SUBJECT: CS609

Question: (20)

Write a program in C/C++ :

1. To create Parent Process and Child Process using the Windows


CreateProcess() API. Set the parameters as:
a. Set the path to the executable program written in C/C++ that the parent
process wants to run. The name of the C/C++ file should be the compiled
version “PrintText.exe” and it should be placed in the “Document” folder
of your desktop or laptop system. You have to put the code in
“PrintText.cpp” file to print your “Name” and “Roll Number” five times,
each time with a newline.
b. No command-line parameters are being passed to the child process.
c. Security attributes for the child process and thread should default
d. Handles for the child process and thread are not inheritable.
e. No creation flags are specified.
f. The child process uses the environment block of the parent process.
g. The child process uses the starting directory of the parent process.
h. A pointer to the STARTUPINFO structure
i. A pointer to the PROCESS_INFORMATION structure.

2. The program should clear the memory of the “STARTUPINFO” and


“PROCESS_INFORMATION” structures.
3. Display the message if child process is successfully created, otherwise display
the error code using GetLastError() function.
4. Also, display the Child Process ID and Child Process Thread ID in main
function.
5. The parent process waits for the child process to complete using
WaitForSingleObject() API with INFINITE as the timeout value and then closes
the process and thread handles with CloseHandle() API.

Note:
1. Include <iostream >, <windows.h>, and < tchar.h> header files in your
program.
2. Focus only what has been asked in the assignment. Marks will be deducted
on including extra or irrelevant code.
3. Question requires a complete program.
4. After the main code, you should place the code of “PrintText.cpp”. The
sample pattern of your assignment solution should be as:

Main code:
#include <iostream>
#include <windows.h>
#include <tchar.h>

int main() {
_TCHAR szPath[] = _T("C:\\Users\\Public\\Documents\\PrintText.exe");
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si); // Corrected typo: si.cb = sizeof(si);

ZeroMemory(&pi, sizeof(pi));

if (CreateProcess(NULL, szPath, NULL, NULL, FALSE, 0, NULL, NULL, &si,


&pi)) {
std::cout << "Child process successfully created." << std::endl;
std::cout << "Child Process ID: " << pi.dwProcessId << std::endl;
std::cout << "Child Process Thread ID: " << pi.dwThreadId << std::endl;

WaitForSingleObject(pi.hProcess, INFINITE);

CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
else {
std::cout << "Error Code: " << GetLastError() << std::endl;
}
return 0;
}//main code ended

PrintText code:
#include <iostream>
int main(){
for(int i = 0; i<5; i++){
std::cout <<"NAME: RAHMEEN SHAHBAZ" << std::endl;
std::cout <<"Roll Number: BC190408018" << std::endl;
}
} //End

5. Your solution should be a single Microsoft Word file (doc or docx). File in
any other format will be awarded zero marks.

You might also like