ibm-information-center/dist/eclipse/plugins/i5OS.ic.apiref_5.4.0.1/apiexusmem.htm

387 lines
16 KiB
HTML

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-us" xml:lang="en-us">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="security" content="public" />
<meta name="Robots" content="index,follow" />
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
<meta name="DC.Type" content="reference" />
<meta name="DC.Title" content="Examples: Using semaphores and shared memory" />
<meta name="abstract" content="These two examples illustrate programs that support the client/server model." />
<meta name="description" content="These two examples illustrate programs that support the client/server model." />
<meta name="DC.Relation" scheme="URI" content="apiexmp.htm" />
<meta name="copyright" content="(C) Copyright IBM Corporation 1998, 2006" />
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 1998, 2006" />
<meta name="DC.Format" content="XHTML" />
<meta name="DC.Identifier" content="apiexusmem" />
<meta name="DC.Language" content="en-us" />
<!-- All rights reserved. Licensed Materials Property of IBM -->
<!-- US Government Users Restricted Rights -->
<!-- Use, duplication or disclosure restricted by -->
<!-- GSA ADP Schedule Contract with IBM Corp. -->
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
<link rel="stylesheet" type="text/css" href="./ic.css" />
<title>Examples: Using semaphores and shared memory</title>
</head>
<body id="apiexusmem"><a name="apiexusmem"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Examples: Using semaphores and shared memory</h1>
<div><p>These two examples illustrate programs that support the client/server
model.</p>
<div class="section"><div class="note"><span class="notetitle">Note:</span> Read the <a href="codedisclaimer.htm">Code license and disclaimer information</a> for important
legal information.</div>
</div>
<div class="section" id="apiexusmem__HDRIPCSRV"><a name="apiexusmem__HDRIPCSRV"><!-- --></a><h4 class="sectiontitle">Server program</h4><p>This program acts
as a server to the client program (see <a href="#apiexusmem__HDRIPCCLI">Client program</a>).
The buffer is a shared memory segment. The process synchronization is done
using semaphores.</p>
<p>Use the Create C Module (CRTCMOD) and the Create Program
(CRTPGM) commands to create this program.</p>
<p>Call this program with no
parameters before calling the client program.</p>
<pre>/*********************************************************************/
/*********************************************************************/
/* */
/* FUNCTION: This program acts as a server to the client program. */
/* */
/* LANGUAGE: ILE C */
/* */
/* APIs USED: semctl(), semget(), semop(), */
/* shmat(), shmctl(), shmdt(), shmget() */
/* ftok() */
/* */
/*********************************************************************/
/*********************************************************************/
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;sys/ipc.h&gt;
#include &lt;sys/sem.h&gt;
#include &lt;sys/shm.h&gt;
#define SEMKEYPATH "/dev/null" /* Path used on ftok for semget key */
#define SEMKEYID 1 /* Id used on ftok for semget key */
#define SHMKEYPATH "/dev/null" /* Path used on ftok for shmget key */
#define SHMKEYID 1 /* Id used on ftok for shmget key */
#define NUMSEMS 2 /* Num of sems in created sem set */
#define SIZEOFSHMSEG 50 /* Size of the shared mem segment */
#define NUMMSG 2 /* Server only doing two "receives"
on shm segment */
int main(int argc, char *argv[])
{
int rc, semid, shmid, i;
key_t semkey, shmkey;
void *shm_address;
struct sembuf operations[2];
struct shmid_ds shmid_struct;
short sarray[NUMSEMS];
/* Generate an IPC key for the semaphore set and the shared */
/* memory segment. Typically, an application specific path and */
/* id would be used to generate the IPC key. */
semkey = ftok(SEMKEYPATH,SEMKEYID);
if ( semkey == (key_t)-1 )
{
printf("main: ftok() for sem failed\n");
return -1;
}
shmkey = ftok(SHMKEYPATH,SHMKEYID);
if ( shmkey == (key_t)-1 )
{
printf("main: ftok() for shm failed\n");
return -1;
}
/* Create a semaphore set using the IPC key. The number of */
/* semaphores in the set is two. If a semaphore set already */
/* exists for the key, return an error. The specified permissions*/
/* give everyone read/write access to the semaphore set. */
semid = semget( semkey, NUMSEMS, 0666 | IPC_CREAT | IPC_EXCL );
if ( semid == -1 )
{
printf("main: semget() failed\n");
return -1;
}
/* Initialize the first semaphore in the set to 0 and the */
/* second semaphore in the set to 0. */
/* */
/* The first semaphore in the sem set means: */
/* '1' -- The shared memory segment is being used. */
/* '0' -- The shared memory segment is freed. */
/* The second semaphore in the sem set means: */
/* '1' -- The shared memory segment has been changed by */
/* the client. */
/* '0' -- The shared memory segment has not been */
/* changed by the client. */
sarray[0] = 0;
sarray[1] = 0;
/* The '1' on this command is a no-op, because the SETALL command*/
/* is used. */
rc = semctl( semid, 1, SETALL, sarray);
if(rc == -1)
{
printf("main: semctl() initialization failed\n");
return -1;
}
/* Create a shared memory segment using the IPC key. The */
/* size of the segment is a constant. The specified permissions */
/* give everyone read/write access to the shared memory segment. */
/* If a shared memory segment already exists for this key, */
/* return an error. */
shmid = shmget(shmkey, SIZEOFSHMSEG, 0666 | IPC_CREAT | IPC_EXCL);
if (shmid == -1)
{
printf("main: shmget() failed\n");
return -1;
}
/* Attach the shared memory segment to the server process. */
shm_address = shmat(shmid, NULL, 0);
if ( shm_address==NULL )
{
printf("main: shmat() failed\n");
return -1;
}
printf("Ready for client jobs\n");
/* Loop only a specified number of times for this example. */
for (i=0; i &lt; NUMMSG; i++)
{
/* Set the structure passed into the semop() to first wait */
/* for the second semval to equal 1, then decrement it to */
/* allow the next signal that the client writes to it. */
/* Next, set the first semaphore to equal 1, which means */
/* that the shared memory segment is busy. */
operations[0].sem_num = 1;
/* Operate on the second sem */
operations[0].sem_op = -1;
/* Decrement the semval by one */
operations[0].sem_flg = 0;
/* Allow a wait to occur */
operations[1].sem_num = 0;
/* Operate on the first sem */
operations[1].sem_op = 1;
/* Increment the semval by 1 */
operations[1].sem_flg = IPC_NOWAIT;
/* Do not allow to wait */
rc = semop( semid, operations, 2 );
if (rc == -1)
{
printf("main: semop() failed\n");
return -1;
}
/* Print the shared memory contents. */
printf("Server Received : \"%s\"\n", (char *) shm_address);
/* Signal the first semaphore to free the shared memory. */
operations[0].sem_num = 0;
operations[0].sem_op = -1;
operations[0].sem_flg = IPC_NOWAIT;
rc = semop( semid, operations, 1 );
if (rc == -1)
{
printf("main: semop() failed\n");
return -1;
}
} /* End of FOR LOOP */
/* Clean up the environment by removing the semid structure, */
/* detaching the shared memory segment, and then performing */
/* the delete on the shared memory segment ID. */
rc = semctl( semid, 1, IPC_RMID );
if (rc==-1)
{
printf("main: semctl() remove id failed\n");
return -1;
}
rc = shmdt(shm_address);
if (rc==-1)
{
printf("main: shmdt() failed\n");
return -1;
}
rc = shmctl(shmid, IPC_RMID, &amp;shmid_struct);
if (rc==-1)
{
printf("main: shmctl() failed\n");
return -1;
}
return 0;
}</pre>
</div>
<div class="section" id="apiexusmem__HDRIPCCLI"><a name="apiexusmem__HDRIPCCLI"><!-- --></a><h4 class="sectiontitle">Client program</h4><p>This program acts
as a client to the server program (see <a href="#apiexusmem__HDRIPCSRV">Server program</a>).
The program is run after the message <tt class="sysout">Ready for client jobs</tt> appears
from the server program.</p>
<p>Use the CRTCMOD and CRTPGM commands to create
this program.</p>
<p>Call this program with no parameters after calling the
server program.</p>
<pre>/*********************************************************************/
/*********************************************************************/
/* */
/* FUNCTION: This program acts as a client to the server program. */
/* */
/* LANGUAGE: ILE C */
/* */
/* APIs USED: semget(), semop(), */
/* shmget(), shmat(), shmdt() */
/* ftok() */
/* */
/*********************************************************************/
/*********************************************************************/
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;sys/ipc.h&gt;
#include &lt;sys/sem.h&gt;
#include &lt;sys/shm.h&gt;
#define SEMKEYPATH "/dev/null" /* Path used on ftok for semget key */
#define SEMKEYID 1 /* Id used on ftok for semget key */
#define SHMKEYPATH "/dev/null" /* Path used on ftok for shmget key */
#define SHMKEYID 1 /* Id used on ftok for shmget key */
#define NUMSEMS 2
#define SIZEOFSHMSEG 50
int main(int argc, char *argv[])
{
struct sembuf operations[2];
void *shm_address;
int semid, shmid, rc;
key_t semkey, shmkey;
/* Generate an IPC key for the semaphore set and the shared */
/* memory segment. Typically, an application specific path and */
/* id would be used to generate the IPC key. */
semkey = ftok(SEMKEYPATH,SEMKEYID);
if ( semkey == (key_t)-1 )
{
printf("main: ftok() for sem failed\n");
return -1;
}
shmkey = ftok(SHMKEYPATH,SHMKEYID);
if ( shmkey == (key_t)-1 )
{
printf("main: ftok() for shm failed\n");
return -1;
}
/* Get the already created semaphore ID associated with key. */
/* If the semaphore set does not exist, then it will not be */
/* created, and an error will occur. */
semid = semget( semkey, NUMSEMS, 0666);
if ( semid == -1 )
{
printf("main: semget() failed\n");
return -1;
}
/* Get the already created shared memory ID associated with key. */
/* If the shared memory ID does not exist, then it will not be */
/* created, and an error will occur. */
shmid = shmget(shmkey, SIZEOFSHMSEG, 0666);
if (shmid == -1)
{
printf("main: shmget() failed\n");
return -1;
}
/* Attach the shared memory segment to the client process. */
shm_address = shmat(shmid, NULL, 0);
if ( shm_address==NULL )
{
printf("main: shmat() failed\n");
return -1;
}
/* First, check to see if the first semaphore is a zero. If it */
/* is not, it is busy right now. The semop() command will wait */
/* for the semaphore to reach zero before running the semop(). */
/* When it is zero, increment the first semaphore to show that */
/* the shared memory segment is busy. */
operations[0].sem_num = 0;
/* Operate on the first sem */
operations[0].sem_op = 0;
/* Wait for the value to be=0 */
operations[0].sem_flg = 0;
/* Allow a wait to occur */
operations[1].sem_num = 0;
/* Operate on the first sem */
operations[1].sem_op = 1;
/* Increment the semval by one */
operations[1].sem_flg = 0;
/* Allow a wait to occur */
rc = semop( semid, operations, 2 );
if (rc == -1)
{
printf("main: semop() failed\n");
return -1;
}
strcpy((char *) shm_address, "Hello from Client");
/* Release the shared memory segment by decrementing the in-use */
/* semaphore (the first one). Increment the second semaphore to */
/* show that the client is finished with it. */
operations[0].sem_num = 0;
/* Operate on the first sem */
operations[0].sem_op = -1;
/* Decrement the semval by one */
operations[0].sem_flg = 0;
/* Allow a wait to occur */
operations[1].sem_num = 1;
/* Operate on the second sem */
operations[1].sem_op = 1;
/* Increment the semval by one */
operations[1].sem_flg = 0;
/* Allow a wait to occur */
rc = semop( semid, operations, 2 );
if (rc == -1)
{
printf("main: semop() failed\n");
return -1;
}
/* Detach the shared memory segment from the current process. */
rc = shmdt(shm_address);
if (rc==-1)
{
printf("main: shmdt() failed\n");
return -1;
}
return 0;
}</pre>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="apiexmp.htm" title="Contains example programs that use APIs and exit programs.">Examples: APIs</a></div>
</div>
</div>
</body>
</html>