/** * * @File : exo_07.cxx * * @Authors : A. B. Dragut * * @Date : 2011 * * * @Synopsis : lectures/ecritures --- des acces concurrents * **/ #include #include #include // atoi() #include // sleep() #include // oflags de open() #include // struct stat #include "CExc.h" #include "nsSysteme.h" // Stat() #include using namespace nsSysteme; // wrappers using namespace std; namespace { string quelleHeureEstIl() { time_t cadran; time(&cadran); string heure (ctime(&cadran)); heure . erase(heure.size() - 1); return heure; } } int main (int argc, char * argv []) { try { if ((6 != argc && 8 != argc) || (6 == argc && string("lire") != argv [1]) || (8 == argc && string("ecrire") != argv [1])) throw CExc ("main()", string ("Usage : ") + argv [0] + " {lire |ecrire <1erChar> " + " } "); if(string("lire") == argv [1]) { const unsigned int CstCombien = atoi (argv [2]); const unsigned int CstDelaiIni = atoi (argv [3]); const unsigned int CstDelai = atoi (argv [4]); const int fd = Open (argv[5], O_RDONLY); cout << "Debut lecture toutes les " << CstDelai << "s par " << CstCombien << " apres " << CstDelaiIni << "\n"; ::sleep(CstDelaiIni); for(bool qContinue(true);qContinue;) { char c; // TRAVAIL A FAIRE // --------------- // boucle pour // lire depuis le descripteur fd, un caractere a la fois (utilisez sizeof()) // afficher aussitot chaque caractere lu, rappelant aussi les parametres de lancement // mettre qContinue a faux lorsqu'on a atteint la fin du fichier ::sleep (CstDelai); } Close(fd); } else if(string("ecrire") == argv [1]) { unsigned int NbCarac = atoi (argv [3]); const unsigned int CstDelaiFin = atoi (argv [4]); const unsigned int CstDelaiIni = atoi(argv [5]); const unsigned int CstDelai = atoi (argv [6]); const int fd = Open (argv[7], O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, 0700); cout << "Debut ecriture toutes les " << CstDelai << "s de " << NbCarac << " apres " << CstDelaiIni << "\n"; ::sleep(CstDelaiIni); for (char Deb = argv [2][0]; NbCarac--; ++Deb) { Write (fd, & Deb, 1); cout << " Ecriture a " << quelleHeureEstIl() << " du caractere '" << Deb << "'\n"; ::sleep (CstDelai); } ::sleep(CstDelaiFin); Close(fd); cout << "Ecriture toutes les " << CstDelai << " apres " << CstDelaiIni << " terminee a " << quelleHeureEstIl() << ".\n"; } else { throw CExc ("main()",string ("Usage : ") + argv [0] + " {lire |ecrire <1erChar> " + "} "); } return 0; } catch (const CExc & Exc) { cerr <