-
Notifications
You must be signed in to change notification settings - Fork 12
/
_4.sharedmemory.c
50 lines (50 loc) · 1.42 KB
/
_4.sharedmemory.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
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/wait.h>
struct shared{
int a_b;
int c_d;
};
const key_t key = 1;
pid_t pid;
void* shared_mem;
int main()
{
int a,b,c,d;
printf("Enter the values of a: ");
scanf("%d",&a);
printf("Enter the values of b: ");
scanf("%d",&b);
printf("Enter the values of c: ");
scanf("%d",&c);
printf("Enter the values of d: ");
scanf("%d",&d);
int shmid = shmget(key,sizeof(struct shared), 0666|IPC_CREAT);
if(shmid == -1) {
printf("\nUnable to create shared memory");
exit(0);
}
pid = fork();
if(pid == 0){
shared_mem = shmat(shmid,(void*) 0,0);
struct shared* shared_data = (struct shared*) shared_mem;
shared_data->a_b = a * b;
exit(EXIT_SUCCESS);
}
else{
wait(NULL);
shared_mem = shmat(shmid,(void*) 0,0);
if(shared_mem == (void*) -1) exit (EXIT_FAILURE);
struct shared* shared_data = (struct shared*) shared_mem;
shared_data->c_d = c*d;
int result = (shared_data->a_b)+(shared_data->c_d);
printf("Result is %d\n", result);
if(shmdt(shared_mem) == -1) exit (EXIT_FAILURE);
if(shmctl(shmid,IPC_RMID,0) == -1) exit(EXIT_FAILURE);
return EXIT_SUCCESS;
}
}