-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path030.c
More file actions
66 lines (55 loc) · 2.49 KB
/
030.c
File metadata and controls
66 lines (55 loc) · 2.49 KB
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
/*
..........................................................................................................................................
Name : 030.c
Author : SHRUTI VERMA
Description : Write a program to create a shared memory.
a. write some data to the shared memory
b. attach with O_RDONLY and check whether you are able to overwrite.
c. detach the shared memory
d. remove the shared memory
Date : 16 Sep 2025
..........................................................................................................................................
*/
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/shm.h>
#include<sys/ipc.h>
int main() {
int key, shmid;
char* data;
key = ftok("file.txt",34);
shmid = shmget(key, 1024, IPC_CREAT | 0744);
data = shmat(shmid, NULL, 0);
scanf("%[^\n]", data);
printf("data from shared memory : %s\n", data);
shmdt(data);
data = shmat(shmid, NULL, SHM_RDONLY);
printf("trying to overwrite\n");
data[0] = 'X';
printf("%s", data);
shmdt(data);
}
/*----------------------------------OUTPUT--------------------------------------------
vumma@vumma-VivoBook-15-ASUS-Laptop-X507UF:~/Desktop/SS/HOL2$ gcc 030.c -o 030.out
vumma@vumma-VivoBook-15-ASUS-Laptop-X507UF:~/Desktop/SS/HOL2$ ./030.out
hello guys, it's shruti here
data from shared memory : hello guys, it's shruti here
trying to overwrite
Segmentation fault (core dumped)
vumma@vumma-VivoBook-15-ASUS-Laptop-X507UF:~/Desktop/SS/HOL2$ ipcs -m
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x00000000 0 vumma 606 11657952 2 dest
0x00000000 1 vumma 606 11657952 2 dest
0x00000000 8 vumma 600 524288 2 dest
0x22056ed5 11 vumma 744 1024 0
vumma@vumma-VivoBook-15-ASUS-Laptop-X507UF:~/Desktop/SS/HOL2$ ipcrm -m 11
vumma@vumma-VivoBook-15-ASUS-Laptop-X507UF:~/Desktop/SS/HOL2$ ipcs -m
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x00000000 0 vumma 606 11657952 2 dest
0x00000000 1 vumma 606 11657952 2 dest
0x00000000 8 vumma 600 524288 2 dest
*/