Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
What will be the output..??
Will child process is still be able to read the file..??

C++
#include"stdio.h"
int main()
{
   FILE* fd;
   char buf[20];
   fd = fopen("myOS", "r"); /* Checks for open success */
   if((fork()) != 0)
   {
      fread(buf, 1, 10, fd);
      buf[5]='\0';
      printf("\nParent : %s\n",buf);
      fclose(fd);
   }
   else
   {
      sleep(1);
      fread(buf, 1, 10, fd);
      buf[5]='\0';
      printf("\nChild : %s\n",buf);
   }
}


Please let me know your valuable inputs on the same..

Faithfully,
kplohit
Posted
Updated 27-Jul-14 23:53pm
v2
Comments
Sergey Alexandrovich Kryukov 28-Jul-14 13:40pm    
As to the output, just build the program and find out...
—SA

1 solution

Output is : Parent : xxxxx

The fork command actually creates a child process, and returns the PID of the process to the parent, and a value of zero to the child. In this example, the first block of code is executed by the parent, while the second block is executed by the child. The one thing you have to note is that the child process gets a copy of all the variables and subroutines that are available to the parent. However, if the child process makes any modifications at all, they are simply discarded when it exits; they do not affect the parent process.
But if u want to retain the modifications use pipes to send data back to parent

If the call to fork() is executed successfully, Unix will

make two identical copies of address spaces, one for the parent and the other for the child.
Both processes will start their execution at the next statement following the fork() call. In this case, both processes will start their execution at the if statement as shown below:

Both processes start their execution right after the system call fork(). Since both processes have identical but separate address spaces, those variables initialized before the fork() call have the same values in both address spaces. Since every process has its own address space, any modifications will be independent of the others. In other words, if the parent changes the value of its variable, the modification will only affect the variable in the parent process's address space. Other address spaces created by fork() calls will not be affected even though they have identical variable names.

Due to the fact that the CPU scheduler will assign a time quantum to each process, the parent or the child process will run for some time before the control is switched to the other and the running process will print some lines before you can see any line printed by the other process. Therefore, the value of MAX_COUNT should be large enough so that both processes will run for at least two or more time quanta. If the value of MAX_COUNT is so small that a process can finish in one time quantum, you will see two groups of lines, each of which contains all lines printed by the same process.(#define MAX_COUNT 200)
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 28-Jul-14 13:41pm    
5ed.
—SA
pavanreddy61 30-Jul-14 0:27am    
Thanks :D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900