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