1 /***************************************************************************
2 * Copyright 1995, Technion, Israel Institute of Technology
3 * Electrical Eng, Software Lab.
4 * Author: Michael Veksler.
5 ***************************************************************************
6 * File: shm_semaph_test.c
7 * Purpose: Test semaphores handleingr shared memory operations.
8 ***************************************************************************
16 #include "shm_semaph.h"
18 #define DEBUG_DEFINE_VARIABLES
21 static volatile int * volatile data;
22 static int isparent=0;
23 #define DELAY (rand()%10)
26 static void read_write(int num)
29 volatile float dummy=0;
32 srand(num+time(NULL));
33 for (i=0x3fff;i>=0;i--) {
34 if((i&0x7ff)==0 && isparent)
35 fprintf(stderr,"0x%06x\r",i);
38 for (j=DELAY ; j>=0;j--)
41 fprintf(stderr,"\nbad shm_write_wait(), num=%d\n",num);
42 shm_write_signal(sem);
45 shm_write_signal(sem);
46 for (j=DELAY ; j>=0 ;j--)
50 for (j=DELAY; j>=0 ;j--)
53 fprintf(stderr,"\nbad shm_read_wait(), num=%d,val=%d,*data=%d\n",
85 shmid=shmget(IPC_PRIVATE, 0x100, IPC_CREAT | 0700);
86 data= (int *)shmat ( shmid, NULL, 0);
89 switch (pid1=fork()) {
94 fprintf(stderr,"child1\n");
96 fprintf(stderr,"child1 done\n");
100 switch (pid2=fork()) {
106 fprintf(stderr,"child2\n");
108 fprintf(stderr,"child2 done\n");
112 fprintf(stderr,"parent\n");
113 if (pid2>0) { /* if second fork did not fail */
115 fprintf(stderr,"parent done, waiting for child2\n");
116 waitpid(pid2,&ret2,WUNTRACED);
119 fprintf(stderr,"parent done, waiting for child1\n");
120 waitpid(pid1,&ret1,WUNTRACED);
122 fprintf(stderr,"all done\n");
124 shmctl(shmid, IPC_RMID,NULL);