-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path8b_fib.c
106 lines (100 loc) · 2.14 KB
/
8b_fib.c
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//using shm_open and mmap
//parent.c
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
int main(int argc,char *argv[])
{
int i;
pid_t pid;
int k;
int n1,n2,n3;
const int SIZE = 4096;
int shm_fd;
void *ptr;
if (argc > 1)
{
sscanf(argv[1], "%d", &i);
if (i < 1)
{
printf("Error input: %d\n", i);
return 0;
}
}
else
{
printf("Error: not passing N in command line\n");
exit(0);
}
pid = fork();
if (pid == 0)// CHILD
{
execlp("./fib","fib",argv[1],NULL);
}
else if (pid > 0)
{
wait(NULL);
printf("\nPARENT: child completed\n");
shm_fd = shm_open("VSS", O_RDONLY, 0666);
ptr = mmap(0, SIZE, PROT_READ, MAP_SHARED, shm_fd, 0);
printf("Parent printing:\n");
printf("%s ", (char *)ptr);
shm_unlink("VSS");
}
return 0;
}
//child.c
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include<stdlib.h>
int main(int argc,char *argv[])
{
int k=2,n1,n2,n3;
void*ptr;
int shm_fd = shm_open("VSS", O_CREAT | O_RDWR, 0666);
ftruncate(shm_fd,4096);
ptr = mmap(0,4096,PROT_WRITE, MAP_SHARED, shm_fd, 0);
printf("CHILD:\n");
int i=atoi(argv[1]);
n1=0;
n2=1;
sprintf(ptr,"%d ",n1);
ptr+=strlen(ptr);
printf("%d ",n1);
sprintf(ptr,"%d ",n2);
ptr+=strlen(ptr);
printf("%d ",n2);
while (k!=i)
{
n3=n1+n2;
sprintf(ptr,"%d ", n3);
printf("%d ", n3);
n1=n2;
n2=n3;
ptr += strlen(ptr);
k++;
}
}
Output:
$ gcc parent.c -lrt
$ gcc child.c -o fib -lrt
$ ./a.out 5
CHILD:
0 1 1 2 3
PARENT: child completed
Parent printing:
0 1 1 2 3