Overclock.net banner

1 - 3 of 3 Posts

Premium Member
5,970 Posts
Discussion Starter #1
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 */
else {/* parent process */
/* parent will wait for the child to complete */
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)
// 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
fprintf(stderr, "Create Process Failed");
return -1;
// parent will wait for the child to complete
WaitForSingleObject(pi.hProcess, INFINITE);
printf("Child Complete");
// close handles

Thanks a lot in advanced

2,461 Posts
For the Unix, and I'm taking a guess here, it looks like it randomly grabs a running process, and then shows three different control paths you could go down based off the returned Process ID.

For the Windows, it opens up MSPaint as a child process, shows you how to use the handles associated with it, waits for the user to close it, and then continues on with execution.

663 Posts
for #1

first call fork() this creates a copy of the running program, and it starts running at the next line.

check the return value from the fork() call if its a valid pid we are the proud parent and we can do stuff and then wait for the child to finish.

if the pid is 0, we are the child (in our process the fork() didnt happen pid is still 0)

in the child call exec() this executes 'ls' which lists the files in the current directory. note there is more to exec() than just running something, ls *is* the child process now, it overwrites everything in the memoryspace that was copied from the parent.

#2 initialize some process structures
call CreateProcess() to launch mspaint in the background, this is pretty much equivalent to fork+exec

then wait for paint to exit
clean up those process structures.
1 - 3 of 3 Posts