b.c

来自「Linux网络编程。通过建立共享内存」· C语言 代码 · 共 44 行

C
44
字号
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>

#include "shm_com.h"

int main()
{
	int running=1;
	shm_data* shm_buff;
	int shmid;
	char buff[BUFSIZ];
	
	shmid = shmget((key_t)1234, sizeof(shm_data), 0666|IPC_CREAT);
	
	shm_buff = (shm_data*)shmat(shmid, (void*)0, 0);
	
	while (running){
		while (shm_buff->is_written==1){
			sleep(1);
			printf ("waitting for client...\n");
		}
		printf ("Enter some text :   ");
		fgets(buff, BUFSIZ, stdin);
		strncpy(shm_buff->data, buff, TEXT_SZ);
		shm_buff->is_written=1;
		
		if (strncmp(buff, "end", 3)==0)
			running=0;
	}
	
	shmdt(shm_buff);
	
	exit(EXIT_SUCCESS);
}



⌨️ 快捷键说明

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