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

📄 bw_svmsg.c

📁 经典的linux源码
💻 C
字号:
/* include bw_svmsg1 */#include	"unpipc.h"void	reader(int, int, int);void	writer(int, int);struct msgbuf	*buf;int		totalnbytes, xfersize;intmain(int argc, char **argv){	int		i, nloop, contpipe[2], msqid;	pid_t	childpid;	if (argc != 4)		err_quit("usage: bw_svmsg <#loops> <#mbytes> <#bytes/write>");	nloop = atoi(argv[1]);	totalnbytes = atoi(argv[2]) * 1024 * 1024;	xfersize = atoi(argv[3]);	buf = Valloc(xfersize);	Touch(buf, xfersize);	buf->mtype = 1;	Pipe(contpipe);	msqid = Msgget(IPC_PRIVATE, IPC_CREAT | SVMSG_MODE);	if ( (childpid = Fork()) == 0) {		writer(contpipe[0], msqid);		/* child */		exit(0);	}	Start_time();	for (i = 0; i < nloop; i++)		reader(contpipe[1], msqid, totalnbytes);	printf("bandwidth: %.3f MB/sec\n",		   totalnbytes / Stop_time() * nloop);	kill(childpid, SIGTERM);	Msgctl(msqid, IPC_RMID, NULL);	exit(0);}/* end bw_svmsg1 *//* include bw_svmsg2 */voidwriter(int contfd, int msqid){	int		ntowrite;	for ( ; ; ) {		Read(contfd, &ntowrite, sizeof(ntowrite));		while (ntowrite > 0) {			Msgsnd(msqid, buf, xfersize - sizeof(long), 0);			ntowrite -= xfersize;		}	}}voidreader(int contfd, int msqid, int nbytes){	ssize_t	n;	Write(contfd, &nbytes, sizeof(nbytes));	while ((nbytes > 0) &&		   ( (n = Msgrcv(msqid, buf, xfersize - sizeof(long), 0, 0)) > 0)) {		nbytes -= n + sizeof(long);	}}/* end bw_svmsg2 */

⌨️ 快捷键说明

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