NetApp Interview Question
Software Engineer / Developers#include <stdio.h>
#include <sys/types.h>
#define MAX_COUNT 200
void ChildProcess(void); /* child process prototype */
void ParentProcess(void); /* parent process prototype */
void main(void)
{
pid_t pid;
pid = fork();
if (pid == 0)
ChildProcess();
else
ParentProcess();
}
void ChildProcess(void)
{
int i;
for (i = 1; i <= MAX_COUNT; i++)
printf(" This line is from child, value = %d\n", i);
printf(" *** Child process is done ***\n");
}
void ParentProcess(void)
{
int i;
for (i = 1; i <= MAX_COUNT; i++)
printf("This line is from parent, value = %d\n", i);
printf("*** Parent is done ***\n");
}
The fork system call will create the child process by copying the address space of the parent. It returns twice by returning the non-zero value to the parent process (the non-zero value is the child process id) and returns zero to the child process. Basically, it create a new process control block by copying from the parent process. The process control block (PCB) can be implemented as a structure which contains various information like process id, parent process id, process state and others. Once it creates a child process, it puts one process in the ready queue and other in the running state.
- Srinivasan March 24, 2010Data structures involved here are PCB structure, ready queue.
This answer is based on my knowledge. Please confirm.