Joined
·
5,970 Posts
This code appeared in my operating systems text, and their is a brief explanation along with some notes written in it, but I am only able to get an idea of what everything means. This won't be the last time this will show up in the text, so if somebody can explain these to me it will help a lot, so next time they pop up I might have a better idea (not to mention I want to go on to learn coding after I finish the text). They are copied straight from my text, as follows:
#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) ;
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.
The second equation below is supposed to represent how to create a child process function. How though is yet unclear to me:
#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);
}
Thanks a lot in advanced
#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) ;
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.
The second equation below is supposed to represent how to create a child process function. How though is yet unclear to me:
#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);
}
Thanks a lot in advanced
