-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFdTest.cpp
More file actions
48 lines (47 loc) · 682 Bytes
/
Copy pathFdTest.cpp
File metadata and controls
48 lines (47 loc) · 682 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/wait.h>
#include <fcntl.h>
#define BLOCK 15
using namespace std;
int main()
{
int fd = open("Text.txt",O_RDONLY);
if(fd<0)
{
perror("File not openend");
exit(0);
}
char buffer[16];
int i,sz;
cout<<"Parent reading = \n";
for(i=0;i<5;i++)
{
sz = read(fd,buffer,15);
buffer[sz] = '\0';
cout<<buffer<<endl;
}
pid_t p = fork();
if(p<0)
{
perror("Child not created");
exit(0);
}
else if(p==0)
{
cout<<"Child reading = \n";
for(i=0;i<5;i++)
{
sz = read(fd,buffer,15);
buffer[sz] = '\0';
cout<<buffer<<endl;
}
}
else
{
wait(NULL);
}
return 0;
}