This repository has been archived by the owner on Dec 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
os5 iii.c
86 lines (69 loc) · 1.75 KB
/
os5 iii.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
#define _GNU_SOURCE
#include<sched.h>
#include<unistd.h>
#include <sys/ipc.h>
#include <stdio.h>
#include <sys/shm.h>
#include<semaphore.h>
#include<string.h>
#include <stdlib.h>
static sem_t * mutex;
int create_shared_memory(){
key_t key;
int shmID;
key = ftok("threadSemaphore.c", 'a');
shmID = shmget(key, 1024, IPC_CREAT | 0666);
return shmID;
}
void write_to_shm(int x){
int shmID = create_shared_memory();
char *str = shmat(shmID, NULL, 0);
char num[100];
sprintf(num,"%d",x);
strcpy(str,num);
shmdt(str);
}
int read_from_shm(){
int shmID = create_shared_memory();
char *str = shmat(shmID, NULL, 0);
int num = (int) strtol(str,(char **)NULL,10);
return num;
shmdt(str);
}
int main(){
pid_t childPID;
int x= 10,r;
int shmid=0;
write_to_shm(10);
read_from_shm();
sem_t key;
//sem_init(&key,1,1);
shmid=shmget(0,sizeof(mutex)*2 + SHMLBA, IPC_CREAT | SHM_R | SHM_W);
mutex=shmat(shmid, NULL, 0);
r=sem_init(&mutex[0],1,1);
r=sem_init(&mutex[1],1,0);
childPID =fork();
if(childPID==0){
int a,b;
sem_wait(&mutex[0]);
a =read_from_shm();
printf("x: %d in child core %d before Addition\n",a,sched_getcpu());
a=a+1;
write_to_shm(a);
b =read_from_shm();
printf("x: %d in child core %d after Addition\n",a,sched_getcpu());
sem_post(&mutex[1]);
}
else{
int a,b;
sem_wait(&mutex[1]);
a =read_from_shm();
printf("x: %d in parent core %d before subtraction\n",a,sched_getcpu());
a=a-1;
write_to_shm(a);
b =read_from_shm();
printf("x: %d in parent core %d after subtraction\n",a,sched_getcpu());
sem_post(&mutex[0]);
}
return 0;
}