- New implementation of SendMessage, ReceiveMessage, ReplyMessage functions
[wine] / ipc / shm_semaph_test.c
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  ***************************************************************************
9  */
10 #include <time.h>
11 #include <stdio.h>
12 #include <unistd.h>
13 #include <errno.h>
14 #include <stdlib.h>
15 #include <sys/wait.h>
16 #include "shm_semaph.h"
17 #include <sys/shm.h>
18 #define DEBUG_DEFINE_VARIABLES
19 #include <debug.h>
20
21 static volatile int * volatile data;
22 static int isparent=0;
23 #define DELAY (rand()%10)
24 shm_sem sem;
25
26 static void read_write(int num)
27 {
28   int i,j ;
29   volatile float dummy=0;
30   int val;
31
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);
36      shm_write_wait(sem);
37      *data= num;
38      for (j=DELAY ; j>=0;j--)
39         dummy*=2;
40      if (*data!=num) {
41         fprintf(stderr,"\nbad shm_write_wait(), num=%d\n",num);
42         shm_write_signal(sem);
43         return;
44      }
45      shm_write_signal(sem);
46      for (j=DELAY ; j>=0 ;j--)
47         dummy*=2;
48      shm_read_wait(sem);
49      val=*data;
50      for (j=DELAY; j>=0 ;j--)
51         dummy*=0.5;
52      if (*data!=val) {
53         fprintf(stderr,"\nbad shm_read_wait(), num=%d,val=%d,*data=%d\n",
54                 num,val,*data);
55         shm_read_signal(sem);
56         return;
57      }
58      shm_read_signal(sem);
59   }
60   if (isparent)
61      fputc('\n',stderr);
62 }
63 static void child1()
64 {
65   read_write(2);
66 }
67 static void child2()
68 {
69   read_write(10);
70 }
71 static void parent()
72 {
73   isparent=1;
74   read_write(60);
75 }
76
77 int main()
78 {
79   int shmid;
80   int ret1, ret2;
81   int pid1, pid2;
82   int stat=0;
83   
84   shm_sem_init(&sem);
85   shmid=shmget(IPC_PRIVATE, 0x100, IPC_CREAT | 0700);
86   data= (int *)shmat ( shmid, NULL, 0);
87   *data=0;
88   
89   switch (pid1=fork()) {
90     case -1:
91       perror("fork 1");
92       return 1;
93     case 0:
94       fprintf(stderr,"child1\n");
95       child1();
96       fprintf(stderr,"child1 done\n");
97       return 0;
98     default :
99   }
100   switch (pid2=fork()) {
101     case -1:
102       perror("fork 2");
103       stat|=1;
104       break;
105     case 0:
106       fprintf(stderr,"child2\n");
107       child2();
108       fprintf(stderr,"child2 done\n");
109       return 0;
110     default :
111   }
112   fprintf(stderr,"parent\n");
113   if (pid2>0) {                    /* if second fork did not fail */
114      parent();
115      fprintf(stderr,"parent done, waiting for child2\n");
116      waitpid(pid2,&ret2,WUNTRACED);
117      stat|=ret2;
118   }
119   fprintf(stderr,"parent done, waiting for child1\n");
120   waitpid(pid1,&ret1,WUNTRACED);
121   stat|=ret1;
122   fprintf(stderr,"all done\n");
123
124   shmctl(shmid, IPC_RMID,NULL);
125   shm_sem_done(&sem);
126   return stat;
127 }