incr2private.c

来自「UNIX网络编程 第2卷 进程间通信 代码」· C语言 代码 · 共 44 行

C
44
字号
#include	"unpipc.h"#define	SEM_NAME	"mysem"intmain(int argc, char **argv){	int		fd, i, nloop, zero = 0;	int		*ptr;	sem_t	*mutex;	if (argc != 3)		err_quit("usage: incr2 <pathname> <#loops>");	nloop = atoi(argv[2]);		/* 4open file, initialize to 0, map into memory */	fd = Open(argv[1], O_RDWR | O_CREAT, FILE_MODE);	Write(fd, &zero, sizeof(int));	ptr = Mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);	Close(fd);		/* 4create, initialize, and unlink semaphore */	mutex = Sem_open(Px_ipc_name(SEM_NAME), O_CREAT | O_EXCL, FILE_MODE, 1);	Sem_unlink(Px_ipc_name(SEM_NAME));	setbuf(stdout, NULL);	/* stdout is unbuffered */	if (Fork() == 0) {		/* child */		for (i = 0; i < nloop; i++) {			Sem_wait(mutex);			printf("child: %d\n", (*ptr)++);			Sem_post(mutex);		}		exit(0);	}		/* 4parent */	for (i = 0; i < nloop; i++) {		Sem_wait(mutex);		printf("parent: %d\n", (*ptr)++);		Sem_post(mutex);	}	exit(0);}

⌨️ 快捷键说明

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