-
Notifications
You must be signed in to change notification settings - Fork 0
/
sharennumber.c
54 lines (48 loc) · 900 Bytes
/
sharennumber.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
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<stdlib.h>
int main()
{
int shm_size,n,i,lar,smal;
long shmid;
key_t key;
int *shm,*s;
printf("\nEnter The limit :");
scanf("%d",&n);
shm_size=n*sizeof(int);
key=123452;
if((shmid=shmget(key,shm_size,IPC_CREAT|0666))< 0)
{printf("\nError in creating s.m. ");exit(0);}
/* if((shm = shmat(shmid, NULL, 0)) == (int *)-1){
printf("Error in attach");
exit(0);}*/
if ((shm = shmat(shmid, NULL, 0)) == (int *) -1) {
perror("shmat");
exit(1);
}
s=shm;
for(i=0;i<n;i++,s++)
scanf("%d",s);
printf("reached here");
if(fork()==0)
{
s=shm;
lar=*s;
smal=*s;
for(i=1;i<n;i++){
if(*(s+i)>lar)
lar=*(s+i);
else if(*(s+i)<smal)
smal=*(s+i);
}
}
else
{ wait(NULL);
return 0;
}
printf("\nLargest=%d \t Smallest=%d",lar,smal);
return 0;
}