Fork - C中的父子关系: [英] Fork - parent child relationship in C:

查看:132
本文介绍了Fork - C中的父子关系:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

输出是什么.. ??

子进程是否仍然可以读取文件.. ??



What will be the output..??
Will child process is still be able to read the file..??

#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);
   }
}





请相信我们的宝贵意见..



忠实地,

kplohit



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

Faithfully,
kplohit

推荐答案

输出为:父:xxxxx



fork命令实际上创建了一个子进程,并将进程的PID返回给父进程,并将值0返回给子进程。在该示例中,第一代码块由父代执行,而第二代码块由子代执行。您必须注意的一件事是子进程获取父进程可用的所有变量和子例程的副本。但是,如果子进程完全进行了任何修改,它们会在退出时被丢弃;它们不会影响父进程。

但是如果你想保留修改使用管道将数据发送回父母



如果对fork()的调用成功执行,Unix将



制作两个相同的地址空间副本,一个用于父级,另一个用于孩子。

两个进程将在fork()调用之后的下一个语句处开始执行。在这种情况下,两个进程将在if语句处开始执行,如下所示:



两个进程在系统调用fork()之后立即开始执行。由于两个进程具有相同但独立的地址空间,因此在fork()调用之前初始化的那些变量在两个地址空间中具有相同的值。由于每个进程都有自己的地址空间,因此任何修改都将独立于其他进程。换句话说,如果父级更改其变量的值,则修改将仅影响父进程的地址空间中的变量。 fork()调用创建的其他地址空间即使具有相同的变量名也不会受到影响。



由于CPU调度程序将分配时间量对于每个进程,父进程或子进程将在控件切换到另一进程之前运行一段时间,并且正在运行的进程将打印一些行,然后才能看到其他进程打印的任何行。因此,MAX_COUNT的值应足够大,以便两个进程至少运行两个或更多时间量程。如果MAX_COUNT的值太小以至于进程可以在一个时间段内完成,您将看到两组线,每组包含由同一进程打印的所有行。(#defined MAX_COUNT 200)
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)


这篇关于Fork - C中的父子关系:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆