Ejemplo n.º 1
0
void my_lock(int fd)
{
	int oflag, i;
	union semun arg;
	struct semid_ds seminfo;

	if (initflag == 0) {
		oflag = IPC_CREAT | IPC_EXCL | SVSEM_MODE;
		if ((semid = semget(Ftok(LOCK_PATH, 0), 1, oflag)) >= 0) {
			arg.val = 1;
			Semctl(semid, 0, SETVAL, arg);
		} else if (errno == EEXIST) {
			semid = Semget(Ftok(LOCK_PATH, 0), 1, SVSEM_MODE);
			arg.buf = &seminfo;
			for (i = 0; i < MAX_TRIES; i++) {
				Semctl(semid, 0, IPC_STAT, arg);
				if (arg.buf->sem_otime != 0)
					goto init;
				sleep(1);
			}
			err_quit("semget OK, but semaphore not initalized");
		} else
			err_sys("semget error");

init:
		initflag = 1;
		postop.sem_num = 0;
		postop.sem_op = 1;
		postop.sem_flg = SEM_UNDO;
		waitop.semnum = 0;
		waitop.semop = -1;
		waitop.semflg = SEM_UNDO;
	}
	Semop(semid, &waitop, 1);
}
Ejemplo n.º 2
0
int main(int argc, char *argv[])
{
	int readid, writeid;
	key_t Rkey, Wkey;
	
	Rkey = Ftok("/tmp/msg", 0);
	Wkey = Ftok("/tmp/msg2", 0);

	readid = Msgget(Rkey, 0644 | IPC_CREAT);
	writeid = Msgget(Wkey, 0644 | IPC_CREAT);

	server(readid, writeid);


	exit(EXIT_FAILURE);
}
Ejemplo n.º 3
0
int
main(int argc, char **argv)
{
	int		c, flag, mqid;
	long	type;
	ssize_t	n;
	struct msgbuf	*buff;

	type = flag = 0;
	while ( (c = Getopt(argc, argv, "nt:")) != -1) {
		switch (c) {
		case 'n':
			flag |= IPC_NOWAIT;
			break;

		case 't':
			type = atol(optarg);
			break;
		}
	}
	if (optind != argc - 1)
		err_quit("usage: msgrcv [ -n ] [ -t type ] <pathname>");

	mqid = Msgget(Ftok(argv[optind], 0), MSG_R);

	buff = Malloc(MAXMSG);

	n = Msgrcv(mqid, buff, MAXMSG, type, flag);
	printf("read %d bytes, type = %ld\n", n, buff->mtype);

	exit(0);
}
Ejemplo n.º 4
0
int
main(int argc, char **argv)
{
	int		c, id, oflag;
	char	*ptr;
	size_t	length;

	oflag = SVSHM_MODE | IPC_CREAT;
	while ( (c = Getopt(argc, argv, "e")) != -1) {
		switch (c) {
		case 'e':
			oflag |= IPC_EXCL;
			break;
		}
	}
	if (optind != argc - 2)
		err_quit("usage: shmget [ -e ] <pathname> <length>");
	length = atoi(argv[optind + 1]);

	id = Shmget(Ftok(argv[optind], 0), length, oflag);
	ptr = Shmat(id, NULL, 0);

	printf("shm addr : %p\n", ptr);

	exit(0);
}
Ejemplo n.º 5
0
int 
main(int argc,char **argv)
{
	int id;

	if(argc!=2)
		err_quit("ysage: shmrmid <pathname>");

	id=Shmget(Ftok(argv[1],0),0,SVSHM_MODE);
	Shmctl(id,IPC_RMID,NULL);
	exit(0);
}
Ejemplo n.º 6
0
int main(int argc, char **argv)
{
	int mqid;

	if (argc != 2)
		err_quit("Usage: rmid <pathname>");

	mqid = Msgget(Ftok(argv[1], 0), 0);
	Msgctl(mqid, IPC_RMID, NULL);

	exit(0);
}
Ejemplo n.º 7
0
void
my_lock(int fd)
{
	int		oflag, i;
	union semun	arg;
	struct semid_ds	seminfo;

	if (initflag == 0) {
		oflag = IPC_CREAT | IPC_EXCL | SVSEM_MODE;
		if ( (semid = semget(Ftok(LOCK_PATH, 0), 1, oflag)) >= 0) {
				/* 4success, we're the first so initialize */
			arg.val = 1;
			Semctl(semid, 0, SETVAL, arg);

		} else if (errno == EEXIST) {
				/* 4someone else has created; make sure it's initialized */
			semid = Semget(Ftok(LOCK_PATH, 0), 1, SVSEM_MODE);
			arg.buf = &seminfo;
			for (i = 0; i < MAX_TRIES; i++) {
				Semctl(semid, 0, IPC_STAT, arg);
				if (arg.buf->sem_otime != 0)
					goto init;
				Write(pipefd[1], "", 1);	/* tell parent */
				sleep(1);
			}
			err_quit("semget OK, but semaphore not initialized");

		} else
			err_sys("semget error");
init:
		initflag = 1;
		postop.sem_num = 0;		/* and init the two semop() structures */
		postop.sem_op  = 1;
		postop.sem_flg = SEM_UNDO;
		waitop.sem_num = 0;
		waitop.sem_op  = -1;
		waitop.sem_flg = SEM_UNDO;
	}
	Semop(semid, &waitop, 1);		/* down by 1 */
}
Ejemplo n.º 8
0
int main(int argc, char **argv)
{
	int i, id;
	struct shmid_ds buff;
	unsigned char *ptr;

	if (argc != 2)
		err_quit("Usage: write <pathname>");

	id = Shmget(Ftok(argv[1], 0), 0, SVSHM_MODE);
	ptr = Shmat(id, NULL, 0);
	Shmctl(id, IPC_STAT, &buff);

	for (i = 0; i < buff.shm_segsz; i++)
		*ptr++ = i % 256;
	exit(0);
}
Ejemplo n.º 9
0
int
main(int argc, char **argv)
{
    int		c, oflag, mqid;

    oflag = SVMSG_MODE | IPC_CREAT;
    while ( (c = Getopt(argc, argv, "e")) != -1) {
        switch (c) {
        case 'e':
            oflag |= IPC_EXCL;
            break;
        }
    }
    if (optind != argc - 1)
        err_quit("usage: msgcreate [ -e ] <pathname>");

    mqid = Msgget(Ftok(argv[optind], 0), oflag);
    exit(0);
}
Ejemplo n.º 10
0
int main(int argc, char **argv)
{
	int mqid;
	size_t len;
	long type;
	struct msgbuf *ptr;

	if (argc != 4)
		err_quit("Usage: msgsnd <pathname> <#bytes> <type>");
	len = atoi(argv[2]);
	type = atoi(argv[3]);

	mqid = Msgget(Ftok(argv[1], 0), MSG_W);

	ptr = (struct msgbuf *)Calloc(sizeof(long) + len, sizeof(char));
	ptr->mtype = type;

	Msgsnd(mqid, ptr, len, 0);

	exit(0);
}
Ejemplo n.º 11
0
int main(int argc, char **argv)
{
	int semid, nsems, i;
	struct semid_ds seminfo;
	unsigned short *ptr;
	union semun arg;

	if (argc < 2)
		err_quit("Usage: setvalue <pathname> [ value ...]");

	//first get the number of semaphore in the set
	semid = Semget(Ftok(argv[1], 0), 0, 0);
	arg.buf = &seminfo;
	Semctl(semid, 0, IPC_STAT, arg);
	nsems = arg.buf->sem_nsems;

	//now get the values from the command line
	ptr = Calloc(nsems, sizeof(unsigned short));
	arg.array = ptr;
	for (i = 0; i < nsems; i++)
		ptr[i] = atoi(argv[i + 2]);
	Semctl(semid, 0, SETALL, arg);
	exit(0);
}