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

📄 test11~1.txt

📁 人民邮电出版社的经典著作《Linux下的C编程》配套源码!初学者的必备!共14章
💻 TXT
字号:
/*服务器端*/
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <stdarg.h>
#include <fcntl.h>

#define BUF_SIZE 1024
#define MYKEY 24

int main(void)
{
	int shmid;
	char *shmptr;
	int fdin,fdout;
	char ch='w';
   
	if(mkfifo("fifo1",0600)==-1|| mkfifo("fifo2", 0600)==-1)
	{
		printf("error, failed to creat my-fifo!\n");
		exit(254);
	}

	if((fdout=open("fifo1", O_WRONLY)==-1)|| (fdin=open("fifo2", O_RDONLY)==-1))
	{
		printf("error, failed to open my-fifo!\n");
		exit(254);
	}

	if(shmid=shmget(MYKEY, BUF_SIZE, IPC_CREAT)==-1)
	{
        	printf("shmget error! \n");
	        exit(1);
	}
    
	if(shmptr=shmat(shmid,0,0)==( void*)-1)
	{
    		fprintf(stderr, "shmat error!\n");
		exit(1);
	}

	while(1)
	{
	    	while(ch!='s')
    		{
		        read(fdin, &ch, 1);
		}
		printf("string: %s \n", shmptr);
		if(write(fdout, "p", 1)!=1)
		{
		        fprintf(stderr, "write error.\n");
		        exit(1);
		}
		ch='w';
	}

}

/*客户端*/

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#define BUF_SIZE 1024
#define MYKEY 24

int main(void)
{
    	int shmid;
    	char *shmptr;
    	int fdin, fdout;
    	char ch='w';
   
	if((fdout=open("fifo2", O_WRONLY)==-1)|| (fdin=open("fifo1", O_RDONLY)==-1))
	{
		printf("error, failed to open my-fifo!\n");
		exit(254);
	}

    	if(shmid=shmget(MYKEY, BUF_SIZE, IPC_CREAT)==-1)
    	{
        	printf("shmget error! \n");
        	exit(1);
    	}
    
	if(shmptr=shmat(shmid,0,0)==( void*)-1)
	{
    		fprintf(stderr, "shmat error!\n");
    		exit(1);
	}

	scanf("input string: %s \n", shmptr);

	while(1)
	{
    		while(ch!='p')
    		{
        		read(fdin, &ch, 1);
     		}
    		scanf("\ninput string: %s \n", shmptr);
    		if(write(fdout, "s", 1)!=1)
    		{
        		fprintf(stderr,"write error.\n");
        		exit(1);
    		}
    		ch='w';
    
	}

	exit(0);
}

⌨️ 快捷键说明

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