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
22 static volatile int * volatile data;
23 static int isparent=0;
24 #define DELAY (rand()%10)
27 static void read_write(int num)
30 volatile float dummy=0;
33 srand(num+time(NULL));
34 for (i=0x3fff;i>=0;i--) {
35 if((i&0x7ff)==0 && isparent)
36 fprintf(stderr,"0x%06x\r",i);
39 for (j=DELAY ; j>=0;j--)
42 fprintf(stderr,"\nbad shm_write_wait(), num=%d\n",num);
43 shm_write_signal(sem);
46 shm_write_signal(sem);
47 for (j=DELAY ; j>=0 ;j--)
51 for (j=DELAY; j>=0 ;j--)
54 fprintf(stderr,"\nbad shm_read_wait(), num=%d,val=%d,*data=%d\n",
86 shmid=shmget(IPC_PRIVATE, 0x100, IPC_CREAT | 0700);
87 data= (int *)shmat ( shmid, NULL, 0);
90 switch (pid1=fork()) {
95 fprintf(stderr,"child1\n");
97 fprintf(stderr,"child1 done\n");
101 switch (pid2=fork()) {
107 fprintf(stderr,"child2\n");
109 fprintf(stderr,"child2 done\n");
113 fprintf(stderr,"parent\n");
114 if (pid2>0) { /* if second fork did not fail */
116 fprintf(stderr,"parent done, waiting for child2\n");
117 waitpid(pid2,&ret2,WUNTRACED);
120 fprintf(stderr,"parent done, waiting for child1\n");
121 waitpid(pid1,&ret1,WUNTRACED);
123 fprintf(stderr,"all done\n");
125 shmctl(shmid, IPC_RMID,NULL);