实验4
fifo_write.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#define FIFO "myfifo"
#define BUFF_SIZE 1024
int main(int argc, char* argv[]) {
char buff[BUFF_SIZE];
int real_write;
int fd;
int rw = 1;
if (access(FIFO, F_OK) == -1) {
if ((mkfifo(FIFO, 0666) < 0) && (errno != EEXIST)) {
printf("Can NOT create fifo file!\n");
exit(1);
}
}
if ((fd = open(FIFO, O_WRONLY)) == -1) {
printf("Open fifo error!\n");
exit(1);
}
do {
printf("请输入要写入管道的内容:\n");
fgets(buff, BUFF_SIZE, stdin);
if ((real_write = write(fd, buff, strlen(buff))) > 0)
printf("第%d次写入管道:'%s'.\n", rw++, buff);
} while (strlen(buff) != 0);
close(fd);
exit(0);
}
fifo_read.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#define FIFO "myfifo"
#define BUFF_SIZE 1024
int main() {
char buff[BUFF_SIZE];
int real_read;
int fd;
int rc = 1;
if (access(FIFO, F_OK) == -1) {
if ((mkfifo(FIFO, 0666) < 0) && (errno != EEXIST)) {
printf("Can NOT create fifo file!\n");
exit(1);
}
}
if ((fd = open(FIFO, O_RDONLY)) == -1) {
printf("Open fifo error!\n");
exit(1);
}
while (1) {
memset(buff, 0, BUFF_SIZE);
if ((real_read = read(fd, buff, BUFF_SIZE)) > 0)
printf("第%d次读取管道: '%s'.\n", rc++, buff);
else
break;
}
close(fd);
exit(0);
}
shmmutexwrite.c
#include<semaphore.h>
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#include<sys/shm.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
#define BUFFER_SIZE 10
#define sem_name"mysem"
int main()
{
struct People
{
char name[10];
int age;
};
int shmid;
sem_t*sem;
int age=10,i=1;
char buff[BUFFER_SIZE];
key_t shmkey;
shmkey=ftok("shmmutexread.c",0);
sem=sem_open(sem_name,O_CREAT,0644,1);
if(sem==SEM_FAILED)
{
printf("unable to creat semaphore!");
sem_unlink(sem_name);
exit(-1);
}
shmid=shmget(shmkey,1024,0666|IPC_CREAT);
if(shmid==-1)
printf("creat shm is fail\n");
struct People * addr;
addr=(struct People*)shmat(shmid,0,0);
if(addr==(struct People*)-1)
printf("shm shmat is fail\n");
addr->age=0;
printf("写进程映射的共享内存地质=%p\n",addr);
do
{
sem_wait(sem);
memset(buff,0,BUFFER_SIZE);
memset((addr+i)->name,0,BUFFER_SIZE);
printf("写进程:输入一些姓名(不超过10个字符)到共享内存(输入'quit'退出):\n");
if(fgets(buff,BUFFER_SIZE,stdin)==NULL)
{
perror("fgets");
sem_post(sem);
break;
}
strncpy((addr+i)->name,buff,strlen(buff)-1);
(addr+i)->age=++age;
addr->age++;
i++;
sem_post(sem);
sleep(1);
}while(strncmp(buff,"quit",4)!=0);
if(shmdt(addr)==-1)
printf("shmdt is fail\n");
sem_close(sem);
sem_unlink(sem_name);
}
shmmutexread.c
#include<semaphore.h>
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#include<sys/shm.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
#define sem_name"mysem"
int main()
{
int shmid;
sem_t*sem;
int i=1;
key_t shmkey;
shmkey=ftok("shmmutexread.c",0);
struct People
{
char name[10];
int age;
};
sem=sem_open(sem_name,0,0644,0);
if(sem==SEM_FAILED)
{
printf("unable to open semaphore!");
sem_close(sem);
exit(-1);
}
shmid=shmget(shmkey,0,0666);
if(shmid==-1)
{
printf("creat shm is fail\n");
exit(0);
}
struct People*addr;
addr=(struct People*)shmat(shmid,0,0);
if(addr==(struct People*)-1)
{
printf("shm shmat is fail\n");
exit(0);
}
printf("读进程映射的共享内存地址=%p\n",addr);
do
{
sem_wait(sem);
if(addr->age>0)
{
printf("\n读进程:绑定到共享内存%p:姓名%d %s,年龄%d \n", addr,1,(addr+i)->name,(addr+i)->age);
addr->age--;
if(strncmp((addr+i)->name,"quit",4)==0) break;
i++;
}
sem_post(sem);
}while(1);
sem_close(sem);
if(shmdt(addr)==-1) printf("shmdt is fail\n");
if(shmctl(shmid,IPC_RMID,NULL)==-1)
printf("shmctl delete error\n");
}