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 */
|
---|
25 | static int pipe_desc[2];
|
---|
26 |
|
---|
27 | /* fonction a executer dans le thread 1 ou process 1 */
|
---|
28 | void write_to_pipe(void * arg);
|
---|
29 | /* fonction a executer dans le thread 2 ou process 2 */
|
---|
30 | void read_frpipe(void * arg);
|
---|
31 |
|
---|
32 | /* Tailles en nombre de double */
|
---|
33 | static int NSample; /* Nombre total d'echantillons */
|
---|
34 | static int NBlk1; /* Taille de paquet en ecriture (Thr1 ou Proc1) */
|
---|
35 | static int NBlk2; /* Taille de paquet en lecture (Thr2 ou Proc2) */
|
---|
36 | static double DeltaX; /* pas du calcul de cosinus */
|
---|
37 |
|
---|
38 | int 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=" << NBlk2
|
---|
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 */
|
---|
127 | void 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 */
|
---|
153 | void 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 | for(int k=0; k<NSample; k+=NBlk2) {
|
---|
163 | for(int ii=0; ii<NBlk2; ii++) d[ii] = -2.;
|
---|
164 | int nrd = NBlk2;
|
---|
165 | if (k+NBlk2 > NSample) nrd = NSample-k;
|
---|
166 | ssize_t nrdok = read(pipe_desc[0], d, nrd*sizeof(double));
|
---|
167 | if (nrdok != nrd*sizeof(double)) {
|
---|
168 | int nrdc = nrd*sizeof(double)-nrdok;
|
---|
169 | ccd = cd + nrdok;
|
---|
170 | while (nrdc != 0) {
|
---|
171 | nwait++;
|
---|
172 | usleep(1000);
|
---|
173 | nrdok = read(pipe_desc[0], ccd, nrdc);
|
---|
174 | nrdc -= nrdok;
|
---|
175 | ccd += nrdok;
|
---|
176 | }
|
---|
177 | }
|
---|
178 | ns += nrd;
|
---|
179 | int kkr = d[0];
|
---|
180 | for(int i=1; i<nrd; i++) {
|
---|
181 | double xp = cos((k+i)*DeltaX)+log((k+i)*DeltaX*100.+0.1);
|
---|
182 | if (i == kkr) xp *= 3.;
|
---|
183 | if (fabs(d[i]-xp) > 1.e-39) nerr++ ;
|
---|
184 | }
|
---|
185 | }
|
---|
186 | cout << " ====> fonction read_frpipe : END read from pipe NSample= "
|
---|
187 | << ns << " NErr=" << nerr << endl;
|
---|
188 | cout << " ====> fonction read_frpipe NWait= " << nwait << endl;
|
---|
189 | delete[] d;
|
---|
190 |
|
---|
191 | return;
|
---|
192 | }
|
---|