I am reading a book on operating systems before I go on to try and learn C. The book often lists code to illustrate points, and while it is not mandatory I understand it, I would like to so I can better learn from it. Both of the following codes were presented:
This is supposed to represent how a child process that is running a copy of a program from the parent process looks in C coding for unix. I understand some things, like the idea of a fork statement, and the text tells me what the equation tries to accomplish. More direct and specific insight would be great though.Code:#include <sys/types.h> #include <stdio.h> #include <unistd.h> int main() { pid-t pid; /* fork a child process */ pid = fork(); if (pid < 0) {/* error occurred */ fprintf(stderr, "Fork Failed"); exit (-1) ; } else if (pid == 0} {/* child process */ execlpf"/bin/Is","Is",NULL); } else {/* parent process */ /* parent will wait for the child to complete */ wait(NULL); printf("Child Complete"); exit (0) ;
The second equation below is supposed to represent how to create a child process function. How though is yet unclear to me. I have been told that it has something to do with microsoft paint as a shared resource, but they were very vague:
Thanks a lot in advancedCode:#include <stdio.h> i #include <windows.h> int main(VOID) { STARTUPINFO si; PROCESS_INFORMATION pi; // allocate memory ZeroMemory(&si, sizeof (si)) ; si.cb = sizeof (si) ; ZeroMemory(&pi, sizeof(pi)); // create child process if (!CreateProcess(NULL, // use command line "C:\\WINDOWS\\system32\\mspaint.exe", // command line NULL, // don't inherit process handle NULL, // don't inherit thread handle FALSE, // disable handle inheritance 0, //no creation flags NULL, // use parent's environment block NULL, // use parent's existing directory &si, &pi}) { fprintf(stderr, "Create Process Failed"); return -1; } // parent will wait for the child to complete WaitForSingleObject(pi.hProcess, INFINITE); printf("Child Complete"); // close handles CloseHandle(pi.hProcess); CloseHandle(pi.hThread); }



LinkBack URL
About LinkBacks


