예제 #1
0
파일: locktests.c 프로젝트: cephpp/test
int initTest(struct donneesPub *dp){
   
    P("Maitre ouvre %s\n",dp->fname);
    dp->fd = open(dp->fname, OPENFLAGS, MANDMODES);
    if (dp->fd < 0) {
        perror("lock test : can't open test file :");
        terminer(1);
    }
    P("fd=%d\n", dp->fd);
    return 0;
}
예제 #2
0
파일: locktests.c 프로젝트: cephpp/test
void * esclave(void *donnees)
{
    struct donneesFils *df;
    int i, cmd,a, resultat,ftest;
    int maitre;
    struct flock request;
    char tmp[16];
    char *phraseTest="L'écriture a réussi";
    int len;
    enum etat_t etat;
    
    len=strlen(phraseTest);
    df=(struct donneesFils *)donnees; 
    i=df->dpr->whoami;
    P("Escalve n=%d\n", i);
    cmd=df->dp->lclnt[i][0];
    maitre=df->dp->maitre[1];
    etat=SYNC;
    errno=0;
    memset(tmp,0,16);
    while(1){
        switch(etat){
            case SELECT: /* NOT reached */
            case SYNC:
                if(read(cmd,&etat,sizeof(int))<0){
                    perror("Lecture du pipe \n");
                    exit(1);
                }
                P("State :%d\n", etat);
                continue;
            case RDONLY:
                /* On tente de lire un fichier */
                P("TEST READ ONLY %d\n", RDONLY);
                if((ftest=open(df->dp->fname, O_RDONLY|O_NONBLOCK))<0){
                    resultat=ECHEC;
                    etat=RESULTAT;
                    if(df->dp->verbose)
                        perror("Open:");
                    continue;
                }
                P("fd=%d\n",ftest);
                a=read(ftest, tmp, 16);
                if(a<16)
                    resultat=ECHEC;
                else
                    resultat=SUCCES;
                etat=RESULTAT;
                continue;
                        
            case WRONLY:
                /* On tente de lire un fichier */
                P("TEST WRITE ONLY %d\n", WRONLY);
                if((ftest=open(df->dp->fname, O_WRONLY|O_NONBLOCK))<0){
                    resultat=ECHEC;
                    etat=RESULTAT;
                    if(df->dp->verbose)
                        perror("Open read only:");
                    continue;
                }
                P("fd=%d\n",ftest);
                if(write(ftest, phraseTest, len)<len)
                    resultat=ECHEC;
                else
                    resultat=SUCCES;
                etat=RESULTAT;
                continue;

            case LOCK: /* NOT reached */
                
                
            case READLOCK:
                /* On essaie de lire une section lockée en lecture sur le fichier */
                P("READ LOCK %d\n", F_RDLCK);
                if((ftest=open(df->dp->fname, O_RDONLY|O_NONBLOCK))<0){
                    resultat=ECHEC;
                    etat=RESULTAT;
                    if(df->dp->verbose)
                        perror("Open read-write:");
                    continue;
                }

                P("fd=%d\n",ftest);
                /* Lock de l'ensemble du fichier*/
                request.l_type=F_RDLCK;
                request.l_whence = SEEK_SET;
                request.l_start =  0;
                request.l_len = 0;
                
                if(fcntl(ftest, F_SETLK, &request)<0){
                    if(df->dp->verbose || errno != EAGAIN)
                        perror("RDONLY: fcntl");
                    resultat=ECHEC;
                }
                else
                    resultat=SUCCES;
                etat=RESULTAT;
                continue;

            case WRITELOCK:
                /* On essaie d'écrire  le fichier */
                P("WRITE LOCK %d\n", F_WRLCK);
                if((ftest=open(df->dp->fname, O_WRONLY|O_NONBLOCK))<0){
                    resultat=ECHEC;
                    etat=RESULTAT;
                    if(df->dp->verbose)
                        perror("\nOpen:");
                    continue;
                }
                /* Lock de l'ensemble du fichier*/
                P("fd=%d\n",ftest);
                request.l_type=F_WRLCK;
                request.l_whence = SEEK_SET;
                request.l_start =  0;
                request.l_len = 0;
                
                if(fcntl(ftest, F_SETLK, &request)<0){
                    if(df->dp->verbose || errno != EAGAIN)
                        perror("\nWRONLY: fcntl");
                    resultat=ECHEC;
                }
                else                    
                    resultat=SUCCES;
                etat=RESULTAT;
                continue;

            case BYTELOCK_READ:
                etat=BYTELOCK;
                continue;

            case BYTELOCK_WRITE:
                etat=BYTELOCK;
                continue;
                
            case BYTELOCK:
                /* On se met en attente de la section à locker. L'ensemble de la structure est 
                 * donnée par le maitre et transmise par le tube.
                 */
                P("WRITE LOCK %d\n", etat);
                if(read(cmd, &request, sizeof(struct flock))<0){
                    perror("Pipe : read error");
                    terminer(1);
                }

                if((ftest=open(df->dp->fname, O_RDWR|O_NONBLOCK))<0){
                    resultat=ECHEC;
                    etat=RESULTAT;
                    if(df->dp->verbose)
                        perror("\nOpen:");
                    continue;
                }
                if(fcntl(ftest, F_SETLK, &request)<0){
                    if(df->dp->verbose || errno != EAGAIN)
                        perror("\nBTLOCK: fcntl");
                    resultat=ECHEC;
                    etat=RESULTAT;
                    continue;
                }
                /* On change le caractère à la place donnée pour vérification */
                a=lseek(ftest, request.l_start,SEEK_SET);
                write(ftest,"=",1);
                resultat=SUCCES;
                etat=RESULTAT;
                continue;
                 
            case RESULTAT:
                if(resultat==SUCCES)
                    write(0,"=",1);
                else
                    write(0,"x",1);
                P("RESULTAT: %d\n", resultat);
                if(write(maitre,&resultat,sizeof(int))<0){
                    P("Slave num %d\n", df->dpr->whoami);
                    perror("Slave\n");
                }
                
                etat=SYNC;
                continue;

            case CLEAN:
                close(ftest);
                /* Send CLEAN Ack */
                if(write(maitre,&resultat,sizeof(int))<0){
                    P("Slave num %d\n", df->dpr->whoami);
                    perror("Slave\n");
                }
                etat=SYNC;
                continue;
            
            
            case FIN:
                E("(End)\n");
                terminer(0);
            printf("Unknown command\n");
            terminer(1);

        }
    }



}
예제 #3
0
파일: locktests.c 프로젝트: 1587/ltp
void *esclave(void *donnees)
{
	struct donneesFils *df;
	int i, a, resultat, ftest;
	struct s_test tLock;
	struct flock request;
	char tmp[16];
#ifdef DEBUG
	char dbg[16];
#endif
	char *phraseTest = "L'ecriture a reussi";
	int len;
	enum etat_t etat;

	resultat = -1;
	ftest = -1;
	len = strlen(phraseTest);
	df = (struct donneesFils *)donnees;
	i = df->dpr->whoami;
	P("Esclave n=%d\n", i);
	esclaveLecteur = dp.lclnt[i][0];
	esclaveEcrivain = dp.maitre[1];
	etat = SYNC;
	errno = 0;
	memset(tmp, 0, 16);
	while (1) {
		switch (etat) {
		case SELECT:
		case SYNC:
			getLockTest(&tLock);
			etat = tLock.test;
			P("Esclave Etat=%d\n", etat);

			continue;
		case RDONLY:
			/* On tente de lire un fichier */
			/* Try to read a file */
			P("TEST READ ONLY %d\n", RDONLY);
			if ((ftest = open(dp.fname, O_RDONLY | O_NONBLOCK)) < 0) {
				resultat = ECHEC;
				etat = RESULTAT;
				if (dp.verbose)
					perror("Open:");
				continue;
			}
			P("fd=%d\n", ftest);
			a = read(ftest, tmp, 16);
			if (a < 16)
				resultat = ECHEC;
			else
				resultat = SUCCES;
			etat = RESULTAT;
			continue;

		case WRONLY:
			/* On tente d'ecrire un fichier */
			/* Try to write a file */
			P("TEST WRITE ONLY %d\n", WRONLY);
			if ((ftest = open(dp.fname, O_WRONLY | O_NONBLOCK)) < 0) {
				resultat = ECHEC;
				etat = RESULTAT;
				if (dp.verbose)
					perror("Open read only:");
				continue;
			}
			P("fd=%d\n", ftest);
			if (write(ftest, phraseTest, len) < len)
				resultat = ECHEC;
			else
				resultat = SUCCES;
			etat = RESULTAT;
			continue;

		case LOCK:

		case READLOCK:
			/* On essaie de lire une section lockee en lecture sur le fichier */
			/* Try to read a read-locked section */
			P("READ LOCK %d\n", F_RDLCK);
			if ((ftest = open(dp.fname, O_RDONLY | O_NONBLOCK)) < 0) {
				resultat = ECHEC;
				etat = RESULTAT;
				if (dp.verbose)
					perror("Open read-write:");
				continue;
			}

			P("fd=%d\n", ftest);
			/* Lock de l'ensemble du fichier */
			/* Lock the whole file */
			request.l_type = F_RDLCK;
			request.l_whence = SEEK_SET;
			request.l_start = 0;
			request.l_len = 0;

			if (fcntl(ftest, F_SETLK, &request) < 0) {
				if (dp.verbose || errno != EAGAIN)
					perror("RDONLY: fcntl");
				resultat = ECHEC;
			} else
				resultat = SUCCES;
			etat = RESULTAT;
			continue;

		case WRITELOCK:
			/* On essaie d'ecrire le fichier */
			/* Try to write a file */
			P("WRITE LOCK %d\n", F_WRLCK);
			if ((ftest = open(dp.fname, O_WRONLY | O_NONBLOCK)) < 0) {
				resultat = ECHEC;
				etat = RESULTAT;
				if (dp.verbose)
					perror("\nOpen:");
				continue;
			}
			/* Lock de l'ensemble du fichier */
			/* Lock the whole file */
			P("fd=%d\n", ftest);
			request.l_type = F_WRLCK;
			request.l_whence = SEEK_SET;
			request.l_start = 0;
			request.l_len = 0;

			if (fcntl(ftest, F_SETLK, &request) < 0) {
				if (dp.verbose || errno != EAGAIN)
					perror("\nWRONLY: fcntl");
				resultat = ECHEC;
			} else
				resultat = SUCCES;
			etat = RESULTAT;
			continue;

		case BYTELOCK_READ:
			P("BYTE LOCK READ: %d\n", etat);
			etat = BYTELOCK;
			continue;

		case BYTELOCK_WRITE:
			P("BYTE LOCK WRITE: %d\n", etat);
			etat = BYTELOCK;
			continue;

		case BYTELOCK:
			/* On se met en attente de la section a locker. L'ensemble de la structure est
			 * donnee par le maitre et transmise par le tube.
			 */
			/* Wait for the exact section to lock. The whole file is sent by the master */

			P("BYTE LOCK %d\n", etat);
			getLockSection(&request);
			if ((ftest = open(dp.fname, O_RDWR | O_NONBLOCK)) < 0) {
				resultat = ECHEC;
				etat = RESULTAT;
				if (dp.verbose)
					perror("\nOpen:");
				continue;
			}

			if (fcntl(ftest, F_SETLK, &request) < 0) {
				if (dp.verbose || errno != EAGAIN)
					perror("\nBTLOCK: fcntl");
				resultat = ECHEC;
				etat = RESULTAT;
				continue;
			}
			/* On change le caractere a la place donnee pour verification */
			/* Change the character at the given position for an easier verification */
			a = lseek(ftest, request.l_start, SEEK_SET);
			write(ftest, "=", 1);
			resultat = SUCCES;
			etat = RESULTAT;
			continue;

		case RESULTAT:
			if (resultat == SUCCES)
				write(0, "=", 1);
			else
				write(0, "x", 1);
			P("RESULTAT: %d\n", resultat);
			sendResult(resultat);
			etat = SYNC;
			continue;

		case CLEAN:
			close(ftest);
			/* Send CLEAN Ack */
			sendResult(resultat);
			etat = SYNC;
			continue;

		case FIN:
			E("(End)\n");
			terminer(0);
			printf("Unknown command\n");
			terminer(1);

		}
	}

}