Tags:
create new tag
view all tags

#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/times.h>
#include <sys/time.h>
#include <sched.h>
#include <sys/wait.h>

struct shared {
	volatile int status[2];
	volatile int turn;
	volatile char data[256];
};

#define FREE  0
#define BUSY  1

#define CHILD  0
#define PARENT 1

void myexit(int ret);
void shared_init(struct shared *s);
int cs_entry(struct shared *s);
void cs_exit(struct shared *s);

int shm_id = -1;
struct shared *s = (void *)-1;

int this_proc,other_proc;
long proc_hit = 0;
int nprove = -1;

int main(int argc,char *argv[])
{
	char value = 0;
	volatile char *p;
	pid_t pid;
	int i,j,count = 0;

	if (argc != 2) {
		printf("Uso: %s <numero di prove>\n",argv[0]);
		myexit(5);
	}
	if (sscanf(argv[1],"%d",&nprove) != 1 || nprove <= 0) {
		printf("%s non va bene come <numero di prove>\n",argv[1]);
		myexit(5);
	}

	shm_id = shmget(IPC_PRIVATE, sizeof(struct shared), 0777);
	if (shm_id == -1) {
		perror("shmget");
		myexit(5);
	}	
	if ( (s = shmat(shm_id, NULL , 0)) == (void *)-1) {
		perror("shmat");
		myexit(5);
	}

	shared_init(s);

	if ( (pid = fork()) == -1 ) {
		perror("fork");
		myexit(5);
	}
	if (pid == 0) { /* child */
		this_proc = CHILD;
		other_proc = PARENT;

		for(j=0; j<nprove ; j++) {
			value = ++value % 2;
			p = s->data;
			proc_hit += cs_entry(s);
			for (i=0;i<sizeof(s->data);i++) {
				*p++ = value;
			}
			cs_exit(s);
			count++;
		}

		printf("Figlio: Eseguiti %d controlli\n",count);
		printf("Figlio: collisioni col padre %ld \n",proc_hit);

		myexit(0);

	} else { /* parent */
		this_proc = PARENT;
		other_proc = CHILD;

		for(j=0; j< nprove ; j++) {
			p = s->data;

			proc_hit += cs_entry(s);
			value = s->data[0];
			for (i=0;i<sizeof(s->data);i++)
				if (*p++ != value) { printf("Problema\n"); }
			cs_exit(s);
			count++;
		}

		printf("Padre: Eseguiti %d controlli\n",count);
		printf("Padre: collisioni col figlio %ld \n",proc_hit);

		wait(NULL);
		myexit(0);
	}
}

void myexit(int ret)
{
	struct shmid_ds shds;

	if (s != (void *)-1) shmdt(s);
	if (shm_id != -1) shmctl(shm_id,IPC_RMID,&shds);
	exit(ret);
}

void shared_init(struct shared *s)
{
	int i;

	s->status[CHILD] = FREE;
	s->status[PARENT] = FREE;
	s->turn = CHILD;
	for (i=0 ; i<sizeof(s->data) ; i++) s->data[i] = 0;
}

int cs_entry(struct shared *s)
{
	int ret = 0;

	s->status[this_proc] = BUSY;

	s->turn = other_proc;
	while ((s->status[other_proc] == BUSY) && (s->turn != this_proc)) {
		ret = 1;
	}
	return ret;
}

void cs_exit(struct shared *s)
{
	s->status[this_proc] = FREE;
}




-- AntonioValletta - 14 Dec 2001

Topic revision: r1 - 2001-12-14 - AntonioValletta






 
Questo sito usa cookies, usandolo ne accettate la presenza. (CookiePolicy)
Torna al Dipartimento di Informatica
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2021 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback