source: branches/libc-0.6/src/emx/testcase/ibm/sem_srv.c@ 2442

Last change on this file since 2442 was 1978, checked in by bird, 21 years ago

http://www-1.ibm.com/servers/enable/site/porting/iseries/overview/examples/semaphore.txt

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 8.3 KB
Line 
1/*
2
3Client/Server programs that use semaphores and shared memory
4
5The server program acts as a server to the client program. The buffer
6is a shared memory segment. The process synchronization is done using
7semaphores.
8
9The client program acts as a client to the server program. The program
10is run after a message appears from the server program.
11
12Choose your browser's option to save to local disk and then reload this
13document to download this code example. Send the program to your AS/400
14and compile it using the development facilities supplied there. This
15program was developed on a V3R1 system and tested on V3R1, V3R2 and
16V3R6, and V4R4 systems.
17
18This small program that is furnished by IBM is a simple example to
19provide an illustration. This example has not been thoroughly tested
20under all conditions. IBM, therefore, cannot guarantee or imply
21reliability, serviceability, or function of this program. All programs
22contained herein are provided to you "AS IS". THE IMPLIED WARRANTIES
23OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY
24DISCLAIMED.
25
26 */
27/*********************************************************************/
28/* */
29/* test case: sem_srv.c */
30/* */
31/* objective: semaphores and shared memory example - server pgm */
32/* */
33/* description: This program shows all of the semaphore and */
34/* shared memory API's. Basically this example */
35/* serves as a client server model, with the buffer */
36/* being a shared memory segment, and the process */
37/* synchronization being done by semaphores. This */
38/* function is the "server" job. This program is to */
39/* be executed before the client program is run. */
40/* */
41/* external routines: */
42/* semctl */
43/* semget */
44/* semop */
45/* shmget */
46/* shmat */
47/* shmdt */
48/* shmctl */
49/* */
50/* usage notes: Compile this program using CRTBNDC */
51/* Call it with no parameters before running client */
52/* program. The server program will end after the */
53/* client program has been called twice. */
54/*********************************************************************/
55#include <stdio.h>
56#include <string.h>
57#include <sys/ipc.h>
58#include <sys/sem.h>
59#include <sys/shm.h>
60
61#define SEMKEY 8888 /* Key passed into semget operation */
62#define SHMKEY 9999 /* Key passed into shmget operation */
63
64#define NUMSEMS 2 /* Num of sems in created sem set */
65#define SIZEOFSHMSEG 50 /* Size of the shared mem segment */
66
67#define NUMMSG 2 /* Server thread only doing two
68 "receives" on shm segment */
69
70int main(int argc, char *argv[])
71{
72 int rc, semid, shmid, i;
73 void *shm_address;
74 struct sembuf operations[2];
75 struct shmid_ds shmid_struct;
76 unsigned short init_vals[NUMSEMS];
77 unsigned short *sem_array;
78
79 /* Create a semaphore set with the constant key. The number of */
80 /* semaphores in the set is two. If a semaphore set already */
81 /* exists for the key, give an error. The permissions are given */
82 /* as everyone has read/write access to the semaphore set. */
83
84 semid = semget( SEMKEY, NUMSEMS, 0666 | IPC_CREAT | IPC_EXCL );
85 if ( semid == -1 )
86 {
87 printf("main: semget() failed\n");
88 return ( -1 );
89 }
90
91 /* Initialize the semaphores in the set to 0 for the first one, */
92 /* and 0 for the second one. */
93 /* */
94 /* The first semaphore in the sem set means: */
95 /* '1' -- The shared memory segment is being used */
96 /* '0' -- The shared memory segment is freed */
97 /* The second semaphore in the sem set means: */
98 /* '1' -- The shared memory segment has been modified by */
99 /* the client. */
100 /* '0' -- The shared memory segment has not been */
101 /* modified by the client. */
102
103 init_vals[0] = 0;
104 init_vals[1] = 0;
105
106 sem_array = init_vals;
107
108 /* The '1' on this command is a no op, since the SETALL command */
109 /* is used */
110 rc = semctl( semid, 1, SETALL, sem_array);
111 if(rc == -1)
112 {
113 printf("main: semctl() initialization failed\n");
114 return ( -1 );
115 }
116
117 /* Create a shared memory segment with the constant key. The */
118 /* size of the segment is a constant. The permissions are given */
119 /* as everyone has read/write access to the semaphore set. If a */
120 /* shared memory segment already exists for this key, give an */
121 /* error */
122 shmid = shmget(SHMKEY, SIZEOFSHMSEG, 0666 | IPC_CREAT | IPC_EXCL);
123 if (shmid == -1)
124 {
125 printf("main: shmget() failed\n");
126 return ( -1 );
127 }
128
129 /* Next, attach the shared memory segment to the server process. */
130 shm_address = shmat(shmid, NULL, 0);
131 if ( shm_address==NULL )
132 {
133 printf("main: shmat() failed\n");
134 return ( -1 );
135 }
136 printf("Ready for client jobs\n");
137
138 /* Only loop a specified number of times for this example */
139 for (i=0; i < NUMMSG; i++)
140 {
141 /* Set the structure passed into the semop() to first wait */
142 /* for the second semval to equal 1, then decrement it to */
143 /* allow the next signal that the client has wrote to it. */
144 /* Next, set the first semaphore to equal 1, which means */
145 /* that the shared memory segment is busy. */
146 operations[0].sem_num = 1; /* Operate on the second sem */
147 operations[0].sem_op = -1; /* Decrement the semval by one */
148 operations[0].sem_flg = 0; /* Allow a wait to occur */
149
150 operations[1].sem_num = 0; /* Operate on the first sem */
151 operations[1].sem_op = 1; /* Increment the semval by 1 */
152 operations[1].sem_flg = IPC_NOWAIT; /* Do not allow to wait */
153
154 rc = semop( semid, operations, 2 );
155 if (rc == -1)
156 {
157 printf("main: semop() failed\n");
158 return ( -1 );
159 }
160
161 /* Print the shared memory contents */
162 printf("Server Thread Received : \"%s\"\n", (char *) shm_address);
163
164 /* Signal the first semaphore to free the shared memory */
165 operations[0].sem_num = 0;
166 operations[0].sem_op = -1;
167 operations[0].sem_flg = IPC_NOWAIT;
168
169 rc = semop( semid, operations, 1 );
170 if (rc == -1)
171 {
172 printf("main: semop() failed\n");
173 return ( -1 );
174 }
175
176 } /* End of FOR LOOP */
177
178 /* Cleanup the environment by removing the semid structure, */
179 /* detatching the shared memory segment, then performing the */
180
181 /* delete on the shared memory segment id. */