0% found this document useful (0 votes)
444 views

Counter 2

1. The document defines a process_func() function that accesses a shared memory segment. 2. It uses shmget() to locate the shared memory segment with a key of 5678 that was created by the server. 3. It attaches the located segment to its data space using shmat() and then prints the contents of the shared memory segment in a loop, incrementing a value each time.

Uploaded by

tom76925
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
444 views

Counter 2

1. The document defines a process_func() function that accesses a shared memory segment. 2. It uses shmget() to locate the shared memory segment with a key of 5678 that was created by the server. 3. It attaches the located segment to its data space using shmat() and then prints the contents of the shared memory segment in a loop, incrementing a value each time.

Uploaded by

tom76925
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

1 # 

 include 
  

counter 


"
2
3  oid 
v  counter 
:: 
process 

func 
()
4 {
 
5      
char 
 c 
;
6      
int 
 shmidct 

shmid 
;
7      
key 

t keyct 

key 
;
8      
char 
  

ct 
,  

shm 
;
9      
char 
 buffer 

SHMSZ 

;
10
11      
/*
12       
*  
We need to get the segment named
13       
* " 
5678 

,  
created by the server 
.
14       
*/
15      
keyct  
=  
5677 
;
16      
key  
=  
5678 
;
17
18      
/*
19       
*  
Locate the segment 
.
20       
*/
21      
if 
  
(( 
shmidct  
=  
shmget 

keyct 
,  

,  
0666 
))  
<  

)  
{
22          
perror 


shmget ct 


;
23      
}
24      
if 
  
(( 
shmid  
=  
shmget 

key 
,  
SHMSZ 
,  
0666 
))  
<  

)  
{
25          
perror 


shmget 


;
26      
}    
27
28      
/*
29       
*  
Now we attach the segment to our data space 
.
30       
*/
31      
if 
  
(( 
ct  
= ( 
char 
*) 
shmat 

shmidct 
,  
NULL 
,  

)) == ( 
char 
  
*) ‐ 

)  
{
32          
perror 


shmat ct 


;
33      
}
34      
if 
  
(( 
shm  
= ( 
char 
*) 
shmat 

shmid 
,  
NULL 
,  

)) == ( 
char 
  
*) ‐ 

)  
{
35          
perror 


shmat 


;
36      
}    
37  
38   val  
  =  

;
39  
40   while 
  ( 

)
41   {
 
42     while 
  (* 
ct 
!= 




;
43     printf 
  ( 
"% 



shm 

;
44     val  
  =  
val 


;
45     wait 
  ( 


;   
46     * 
 ct 




;
47   }
 
48 }
 
49

You might also like