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