#include<signal.h>
#include<errno.h>
#include<stdio.h>
#include<unistd.h>
int main(void)
{
pid_t pid;
signal (SIGCHLD,SIG_IGN);
printf("before fork pid: %d \n",getpid());
/*
*/
fd=open("./1.txt",O_RDWR);
if(fd==-1)
{
perror("the error tips");
return -1;
}
pid=fork();
if(pid==-1)
{
//printf("")
perror("title");
return -1;
}
if(pid>0)
{
abc++;
printf("parent: pid :%d \n",getpid());
/*
下面开始往父进程里面写东西:
即:使用write() system call to write something to the file
ssize_t write(int fd, const void *buf,size_t count);
*/
write(fd,"parent",6);
//sleep(20);
/*printf("abc:%d \n",abc);*/
}
else if(pid==0)
{
/*
下面开始在子进程里面也写入文件内容
*/
printf("child :%d ,parent: %d \n",getpid(),getppid());
write(fd,"child",5);
//printf("abc: %d \n",abc);
}
printf("fork after ....\n");
return 0;
}
/*let's to annalysis the prom ,the parent process and the child process write to the same fd in the same moment,
so who will write finally ???
*/
in the first ,we have to build the
首先,我们应当明确:
1.子进程确实复制了父进程的打开的文件描述符
2.那么,问题来了,如果父子进程同时向同一个文件描述符写入,那么会出现什么情况???
是先让父进程写入???还是先让子进程写入??
3.还有一点:因为子进程复制了父进程的文件描述符,所以不能忘记父进程关闭该文件描述符,子进程使用完文件后也要关闭这个文件描述符。
因为:子进程和父进程都有自己各自的进程空间。
父子进程的有同一个文件描述符,但该文件描述符在内核中只存在一个文件表。
所以,父子进程的读写操作都会造成文件表中的当前文件偏移量的值发生变化,因为他俩是同一个的文件偏移量
4. 记住:文件表是归Linux内核管理。就是VFS来管理 文件表。
5.
#include<errno.h>
#include<stdio.h>
#include<unistd.h>
int main(void)
{
pid_t pid;
signal (SIGCHLD,SIG_IGN);
printf("before fork pid: %d \n",getpid());
/*
*/
fd=open("./1.txt",O_RDWR);
if(fd==-1)
{
perror("the error tips");
return -1;
}
pid=fork();
if(pid==-1)
{
//printf("")
perror("title");
return -1;
}
if(pid>0)
{
abc++;
printf("parent: pid :%d \n",getpid());
/*
下面开始往父进程里面写东西:
即:使用write() system call to write something to the file
ssize_t write(int fd, const void *buf,size_t count);
*/
write(fd,"parent",6);
//sleep(20);
/*printf("abc:%d \n",abc);*/
}
else if(pid==0)
{
/*
下面开始在子进程里面也写入文件内容
*/
printf("child :%d ,parent: %d \n",getpid(),getppid());
write(fd,"child",5);
//printf("abc: %d \n",abc);
}
printf("fork after ....\n");
return 0;
}
/*let's to annalysis the prom ,the parent process and the child process write to the same fd in the same moment,
so who will write finally ???
*/
in the first ,we have to build the
首先,我们应当明确:
1.子进程确实复制了父进程的打开的文件描述符
2.那么,问题来了,如果父子进程同时向同一个文件描述符写入,那么会出现什么情况???
是先让父进程写入???还是先让子进程写入??
3.还有一点:因为子进程复制了父进程的文件描述符,所以不能忘记父进程关闭该文件描述符,子进程使用完文件后也要关闭这个文件描述符。
因为:子进程和父进程都有自己各自的进程空间。
父子进程的有同一个文件描述符,但该文件描述符在内核中只存在一个文件表。
所以,父子进程的读写操作都会造成文件表中的当前文件偏移量的值发生变化,因为他俩是同一个的文件偏移量
4. 记住:文件表是归Linux内核管理。就是VFS来管理 文件表。
5.