Release 960225
[wine.git] / ipc / shm_semaph_test.c
blobfdceeb58f1dc2c19bc19eed266126f837ad63c9b
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 <stddebug.h>
20 #include <debug.h>
22 static volatile int * volatile data;
23 static int isparent=0;
24 #define DELAY (rand()%10)
25 shm_sem sem;
27 static void read_write(int num)
29 int i,j ;
30 volatile float dummy=0;
31 int val;
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);
37 shm_write_wait(sem);
38 *data= num;
39 for (j=DELAY ; j>=0;j--)
40 dummy*=2;
41 if (*data!=num) {
42 fprintf(stderr,"\nbad shm_write_wait(), num=%d\n",num);
43 shm_write_signal(sem);
44 return;
46 shm_write_signal(sem);
47 for (j=DELAY ; j>=0 ;j--)
48 dummy*=2;
49 shm_read_wait(sem);
50 val=*data;
51 for (j=DELAY; j>=0 ;j--)
52 dummy*=0.5;
53 if (*data!=val) {
54 fprintf(stderr,"\nbad shm_read_wait(), num=%d,val=%d,*data=%d\n",
55 num,val,*data);
56 shm_read_signal(sem);
57 return;
59 shm_read_signal(sem);
61 if (isparent)
62 fputc('\n',stderr);
64 static void child1()
66 read_write(2);
68 static void child2()
70 read_write(10);
72 static void parent()
74 isparent=1;
75 read_write(60);
78 int main()
80 int shmid;
81 int ret1, ret2;
82 int pid1, pid2;
83 int stat=0;
85 shm_sem_init(&sem);
86 shmid=shmget(IPC_PRIVATE, 0x100, IPC_CREAT | 0700);
87 data= (int *)shmat ( shmid, NULL, 0);
88 *data=0;
90 switch (pid1=fork()) {
91 case -1:
92 perror("fork 1");
93 return 1;
94 case 0:
95 fprintf(stderr,"child1\n");
96 child1();
97 fprintf(stderr,"child1 done\n");
98 return 0;
99 default :
101 switch (pid2=fork()) {
102 case -1:
103 perror("fork 2");
104 stat|=1;
105 break;
106 case 0:
107 fprintf(stderr,"child2\n");
108 child2();
109 fprintf(stderr,"child2 done\n");
110 return 0;
111 default :
113 fprintf(stderr,"parent\n");
114 if (pid2>0) { /* if second fork did not fail */
115 parent();
116 fprintf(stderr,"parent done, waiting for child2\n");
117 waitpid(pid2,&ret2,WUNTRACED);
118 stat|=ret2;
120 fprintf(stderr,"parent done, waiting for child1\n");
121 waitpid(pid1,&ret1,WUNTRACED);
122 stat|=ret1;
123 fprintf(stderr,"all done\n");
125 shmctl(shmid, IPC_RMID,NULL);
126 shm_sem_done(&sem);
127 return stat;