Professional Documents
Culture Documents
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.
Methodology: #include <sys/types.h> #include <windows.h> #include <stdio.h> //#define _WIN32_WINNT 0x0501
int main() {
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
if(! CreateProcess(L"C:\\WINDOWS\\system32\\cmd.exe",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"); }
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.