source: Sophya/trunk/SophyaProg/Tests/tpipe.cc@ 2440

Last change on this file since 2440 was 2440, checked in by ansari, 22 years ago

Correction de la logique de verification du flot de donnees ds tpipe.cc - Reza 02/10/2003

File size: 5.9 KB
Line 
1#include "zthread.h"
2
3#include <iostream>
4
5#include <stdlib.h>
6#include <stdio.h>
7#include <math.h>
8
9
10#include <time.h>
11#include <unistd.h>
12#include <sys/wait.h>
13
14#include "timing.h"
15
16/* --------------------------------------------------------- */
17/* ------ -Programme de test des tuyaux unix (pipe) ------ */
18/* Avec creation de threads (les ZThread de Sophya) */
19/* Ou avec creation de process (fork()) */
20/* Octobre 2003 - C. Magneville, R. Ansari */
21/* --------------------------------------------------------- */
22
23/* Identificateur de fichier de pipe */
24/* pipe_desc[0] : lecture pipe_desc[1] : ecriture */
25static int pipe_desc[2];
26
27/* fonction a executer dans le thread 1 ou process 1 */
28void write_to_pipe(void * arg);
29/* fonction a executer dans le thread 2 ou process 2 */
30void read_frpipe(void * arg);
31
32/* Tailles en nombre de double */
33static int NSample; /* Nombre total d'echantillons */
34static int NBlk1; /* Taille de paquet en ecriture (Thr1 ou Proc1) */
35static int NBlk2; /* Taille de paquet en lecture (Thr2 ou Proc2) */
36static double DeltaX; /* pas du calcul de cosinus */
37
38int main(int narg, char *arg[])
39
40{
41
42 if ((narg < 5) || ((narg>1)&&(strcmp(arg[1],"-h")==0)) ) {
43 cout << " Usage: tpipe T/P NSample Blk1 Blk2 \n "
44 << " T --> Threads , P --> 2 process \n"
45 << " NSample: Nb total d'echantillons \n"
46 << " NBlk1: Taille de paquet en ecriture \n"
47 << " NBlk2: Taille de paquet en lecture \n" << endl;
48 return(0);
49 }
50
51 if ((*arg[1] != 'T') && (*arg[1] != 'P')) {
52 cout << " tpipe: arg erreur / specifiez T ou P , tpipe -h pour aide \n" << endl;
53 return 1;
54 }
55
56 char proc_or_thread = *arg[1];
57 NSample = atoi(arg[2]);
58 NBlk1 = atoi(arg[3]);
59 NBlk2 = atoi(arg[4]);
60 double DeltaX = 50.*M_PI/(double)NSample;
61 cout << " =============== tpipe (Pipe Test Program) =================" << endl;
62 if (proc_or_thread == 'T') cout << " ----> avec Threads " << endl;
63 else cout << " ----> avec fork() / process " << endl;
64 cout << " -- NSample= " << NSample << " NBlk1=" << NBlk1
65 << " NBlk2=" << NBlk2 << endl;
66 cout << " ============================================================\n" << endl;
67
68 InitTim();
69
70 cout << ">> tpipe: Opening pipe ... " << endl;
71 int rc = pipe(pipe_desc);
72 if (rc != 0) {
73 cout << " ERREUR creation pipe !" << endl;
74 return 99;
75 }
76 if (proc_or_thread == 'T') {
77 cout << ">> tpipe: Creation des Threads ... " << endl;
78
79 ZThread zt1;
80 zt1.setAction(write_to_pipe, arg[1]);
81 ZThread zt2;
82 zt2.setAction(read_frpipe, arg[2]);
83 cout << ">> tpipe: Start des Threads Z1 et Z2 ... " << endl;
84 zt1.start();
85 zt2.start();
86 cout << ">> tpipe: Attente z1/2.join() ... " << endl;
87 zt1.join();
88 zt2.join();
89 cout << ">> tpipe: Thread Z1 and Z2 joined " << endl;
90 PrtTim("EndOftpipe_Threads ");
91 }
92 else {
93 cout << ">> tpipe: fork() - creation de process ... " << endl;
94 char * zzz = "toto";
95 pid_t rcf = fork();
96 if (rcf == 0) {
97 cout << ">> tpipe: Processus FILS - PID= " << getpid()
98 << " -> execution de read_frpipe()" << endl;
99 read_frpipe(zzz);
100 cout << ">> tpipe: FIN execution read_frpipe() ds FILS - PID= " << getpid() << endl;
101 PrtTim("EndOffun_Fils ");
102 }
103 else {
104 cout << ">> tpipe: Processus PERE - PID= " << getpid()
105 << " -> execution de read_frpipe()" << endl;
106 write_to_pipe(zzz);
107 cout << ">> tpipe: FIN execution write_to_pipe() ds PERE - PID= " << getpid() << endl;
108 PrtTim("EndOffun_Pere ");
109 cout << ">> tpipe: Attente processus fils PID=" << rcf << endl;
110 int status_procs[20];
111 waitpid(rcf, status_procs, 0);
112 cout << ">> tpipe: Status fils= " << status_procs[0] << endl;
113 }
114 }
115
116 char buff[128];
117 sprintf(buff,"EndOftpipe-PID=%d ",getpid());
118 PrtTim(buff);
119 cout << " ======= Fin du programme tpipe (PID= " << getpid()
120 << ") ======= \n" << endl;
121
122 return(0);
123
124}
125
126/* fonction a executer dans le thread 1 ou process 1 : write to pipe */
127void write_to_pipe(void *arg)
128{
129 double * d = new double[NBlk1];
130 int ns = 0;
131 cout << " ====> fonction write_to_pipe : START write to pipe ... " << endl;
132 for(int k=0; k<NSample; k+=NBlk1) {
133 // ---------------------
134 for(int i=0; i<NBlk1; i++) d[i] = cos((k+i)*DeltaX)+log((k+i)*DeltaX*100.+0.1);
135 // d[0] continet le numero d'un element particulier
136 // dont la valeur est multipliee par 3
137 int kkr = random()%NBlk1;
138 d[0] = kkr+0.1;
139 d[kkr] *= 3.;
140 // ---------------------
141 int nwrt = NBlk1;
142 if (k+NBlk1 > NSample) nwrt = NSample-k;
143 write(pipe_desc[1], d, nwrt*sizeof(double));
144 ns += nwrt;
145 }
146 cout << " ====> fonction write_to_pipe : END write to pipe NSample= " << ns << endl;
147 delete[] d;
148
149 return;
150}
151
152/* fonction a executer dans le thread 2 ou process 2 : read from pipe */
153void read_frpipe(void *arg)
154{
155 double * d = new double[NBlk2];
156 char * cd = (char *)d;
157 char * ccd = cd;
158 int ns = 0;
159 int nerr = 0;
160 int nwait = 0;
161 cout << " ====> fonction read_frpipe : START read from pipe ... " << endl;
162 int kkr = -1;
163 for(int k=0; k<NSample; k+=NBlk2) {
164 for(int ii=0; ii<NBlk2; ii++) d[ii] = -2.;
165 int nrd = NBlk2;
166 if (k+NBlk2 > NSample) nrd = NSample-k;
167 ssize_t nrdok = read(pipe_desc[0], d, nrd*sizeof(double));
168 if (nrdok != nrd*sizeof(double)) {
169 int nrdc = nrd*sizeof(double)-nrdok;
170 ccd = cd + nrdok;
171 while (nrdc != 0) {
172 nwait++;
173 usleep(1000);
174 nrdok = read(pipe_desc[0], ccd, nrdc);
175 nrdc -= nrdok;
176 ccd += nrdok;
177 }
178 }
179 ns += nrd;
180 for(int i=0; i<nrd; i++) {
181 if ((k+i)%NBlk1 == 0) { kkr = d[i]; continue; }
182 double xp = cos((k+i)*DeltaX)+log((k+i)*DeltaX*100.+0.1);
183 if (((k+i)%NBlk1) == kkr) xp *= 3.;
184 if (fabs(d[i]-xp) > 1.e-39) nerr++ ;
185 }
186 }
187 cout << " ====> fonction read_frpipe : END read from pipe NSample= "
188 << ns << " NErr=" << nerr << endl;
189 cout << " ====> fonction read_frpipe NWait= " << nwait << endl;
190 delete[] d;
191
192 return;
193}
Note: See TracBrowser for help on using the repository browser.