0% found this document useful (0 votes)
137 views4 pages

CreateProcess() in Win32 API Example

C program to repeat the preceding exercise using the CreateProcess() function in Win32 API. You need to display the new process id or the process identifier of the created process before quitting.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
137 views4 pages

CreateProcess() in Win32 API Example

C program to repeat the preceding exercise using the CreateProcess() function in Win32 API. You need to display the new process id or the process identifier of the created process before quitting.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
  • Task Description
  • Program Script
  • Conclusion
  • Difference between fork() and CreateProcess()

Task2

Description of the tasks: Write a C program to repeat the preceding exercise using the CreateProcess() function in Win32 API. You need to display the new process id or the process identifier of the created process before quitting. Note: You are expected to write a separate program that will run as a child process outputting the Fibonacci sequence. Perform necessary error checking to ensure that a non-negative number is entered by the user.

Objectives: Create process using the CreateProcess() function in Win32 API

Methodology: #include <sys/types.h> #include <windows.h> #include <stdio.h> //#define _WIN32_WINNT 0x0501

int main() {

STARTUPINFO si; PROCESS_INFORMATION pi; int a=0, b=1, n=a+b,i,ii;

ZeroMemory(&si, sizeof(si));

[Link] = sizeof(si);

if(! CreateProcess(L"C:\\WINDOWS\\system32\\[Link]",NULL,NULL,NULL,FALSE,0, NULL,NULL,&si,&pi)) printf("\nSorry! CreateProcess() failed.\n\n"); else{ printf("Enter the number of a Fibonacci Sequence:\n"); scanf_s("%d", &ii);

if (ii < 0) printf("Please enter a non-negative integer!\n"); else { { printf("Child is producing the Fibonacci Sequence...\n"); printf("%d %d",a,b); for (i=1;i<ii;i++) { n=a+b; printf("%d ", n); a=b; b=n; } printf("Child ends\n"); }

{ printf("Parent is waiting for child to complete...\n"); printf("Parent ends\n"); } } }

WaitForSingleObject([Link], 5000); printf("\n");

// Close process and thread handles. CloseHandle([Link]); CloseHandle([Link]);

return 0;

Conclusion: Processes are created in the Win32 API using the CreateProcess () function. It is similar to fork () in Linux. In here also parent creates a new child process. Difference between fork() and CreatProcess()

The fork() is passed no parameters but in CreateProcess () expects no fewer than ten parameters. The system call fork() has the child process inheriting the address space of its parent but in CreateProcess () requires loading a specified program into the address space of the child process at process creation.

Common questions

Powered by AI

The child process first checks if the input number is non-negative. If valid, it outputs the initial numbers of the Fibonacci sequence, then iteratively calculates subsequent values by adding the last two numbers in the sequence until the user-specified count is reached .

The fork() system call in Linux is simpler as it requires no parameters and the child process inherits the address space of its parent directly. In contrast, CreateProcess() in the Win32 API is more complex, requiring at least ten parameters and necessitating loading a specified program into the child's address space at the time of creation .

The parent process uses WaitForSingleObject() to wait for the child process to terminate. This function waits for the specified process to signal it has terminated, ensuring the parent process does not end before the child process completes, thereby managing correct synchronization and sequence of termination .

ZeroMemory ensures that structures like STARTUPINFO are cleared of residual or garbage values that could cause undefined behavior. By initializing memory to zero, the program avoids unpredictable outcomes due to leftover data, thus promoting stability and reliable operation .

Explicitly closing process and thread handles is vital for resource management, preventing resource leaks. If handles remain open, they occupy system resources unnecessarily, potentially leading to resource exhaustion and performance degradation .

Error checking is crucial to ensure that the user inputs a non-negative integer for calculating the Fibonacci sequence. This prevents potential runtime errors or logic failures in the program output, as negative numbers do not conceptually fit within the Fibonacci sequence .

If invalid input is not correctly handled, the program could attempt to compute an impractical Fibonacci sequence, leading to logic errors, infinite loops, or crashes. Such oversight could also result in security vulnerabilities if incorrect inputs are executed without proper validation .

CreateProcess() explicitly loads a specified program into the address space of the child process, which is separate from the parent's. This contrasts with fork() on Linux, where the child automatically inherits the parent's address space, ensuring a distinct separation through manual specification of the program .

The STARTUPINFO structure is used to specify window properties during the startup of a new process. It is initialized with the ZeroMemory function to ensure all members are zeroed out, with the exception of the cb member, which is set to the size of the structure. This setup is necessary for successful execution of CreateProcess().

In the Win32 API, child processes do not naturally inherit attributes or environment of their parent as they do in Linux using fork(). This necessitates specifying all execution specifics at creation with CreateProcess(). In Linux, processes form a natural hierarchy, with the child always derived directly maintaining parent attributes unless explicitly changed .

Task2 
 
Description of the tasks: 
Write a C program to repeat the preceding exercise using the CreateProcess() function in
si.cb = sizeof(si); 
 
  if(! CreateProcess(L"C:WINDOWSsystem32cmd.exe",NULL,NULL,NULL,FALSE,0,
{ 
         printf("Parent is waiting for child to complete...
"); 
         printf("Parent ends
"); 
      }
 
The fork() is passed no parameters but in CreateProcess () expects no fewer than ten 
parameters.  
 
The system call for

You might also like