⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 client2.c

📁 这是unix网络编程一书的源代码希望能对大家的学习提供一种便利
💻 C
字号:
#include	"cliserv2.h"

int
main(int argc, char **argv)
{
	int		fd, i, nloop, nusec;
	pid_t	pid;
	char	mesg[MESGSIZE];
	long	offset;
	struct shmstruct	*ptr;

	if (argc != 4)
		err_quit("usage: client2 <name> <#loops> <#usec>");
	nloop = atoi(argv[2]);
	nusec = atoi(argv[3]);

		/* 4open and map shared memory that server must create */
	fd = Shm_open(Px_ipc_name(argv[1]), O_RDWR, FILE_MODE);
	ptr = Mmap(NULL, sizeof(struct shmstruct), PROT_READ | PROT_WRITE,
			   MAP_SHARED, fd, 0);
	Close(fd);

	pid = getpid();
	for (i = 0; i < nloop; i++) {
		Sleep_us(nusec);
		snprintf(mesg, MESGSIZE, "pid %ld: message %d", (long) pid, i);

		if (sem_trywait(&ptr->nempty) == -1) {
			if (errno == EAGAIN) {
				Sem_wait(&ptr->noverflowmutex);
				ptr->noverflow++;
				Sem_post(&ptr->noverflowmutex);
				continue;
			} else
				err_sys("sem_trywait error");
		}
		Sem_wait(&ptr->mutex);
		offset = ptr->msgoff[ptr->nput];
		if (++(ptr->nput) >= NMESG)
			ptr->nput = 0;		/* circular buffer */
		Sem_post(&ptr->mutex);
		strcpy(&ptr->msgdata[offset], mesg);
		Sem_post(&ptr->nstored);
	}
	exit(0);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -