作业
1> 使用三个程序来完成输出 ABCABCABCABCABC
#include<myhead.h>
#include"sem.h"
#define PAGE_SIZE 4096
int main(int argc, const char *argv[])
{
int semid=create_sem(3);
printf("semid=%d\n",semid);
if(semid==-1)
{
perror("create_sem error");
return -1;
}
key_t key=ftok("/",'k');
if(key==-1)
{
perror("ftok error!\n");
return -1;
}
int shmid=shmget(key,PAGE_SIZE,IPC_CREAT|0664);
if(shmid==-1)
{
perror("shmget error!\n");
return -1;
}
char *shmaddress=(char *)shmat(shmid,NULL,0);
if(shmaddress==(void *)-1)
{
perror("shmat error!\n");
return -1;
}
//输出
while(1)
{
P(semid,2);
*shmaddress='C';
printf("%c\n",*shmaddress);
V(semid,0);
sleep(1);
}
if(shmdt(shmaddress)==-1)
{
perror("shmdt error!\n");
return -1;
}
if(shmctl(shmid,IPC_RMID,NULL)==-1)
{
perror("shmctl error!\n");
return -1;
}
del_sem(semid);
return 0;
}
#include<myhead.h>
#include"sem.h"
#define PAGE_SIZE 4096
int main(int argc, const char *argv[])
{
int semid=create_sem(3);
printf("semid=%d\n",semid);
if(semid==-1)
{
perror("create_sem error");
return -1;
}
key_t key=ftok("/",'k');
if(key==-1)
{
perror("ftok error!\n");
return -1;
}
int shmid=shmget(key,PAGE_SIZE,IPC_CREAT|0664);
if(shmid==-1)
{
perror("shmget error!\n");
return -1;
}
char *shmaddress=(char *)shmat(shmid,NULL,0);
if(shmaddress==(void *)-1)
{
perror("shmat error!\n");
return -1;
}
//输出
while(1)
{
P(semid,1);
*shmaddress='B';
printf("%c\n",*shmaddress);
V(semid,2);
sleep(1);
}
if(shmdt(shmaddress)==-1)
{
perror("shmdt error!\n");
return -1;
}
if(shmctl(shmid,IPC_RMID,NULL)==-1)
{
perror("shmctl error!\n");
return -1;
}
del_sem(semid);
return 0;
}
#include<myhead.h>
#include"sem.h"
#define PAGE_SIZE 4096
int main(int argc, const char *argv[])
{
int semid=create_sem(3);
printf("semid=%d\n",semid);
if(semid==-1)
{
perror("create_sem error");
return -1;
}
key_t key=ftok("/",'k');
if(key==-1)
{
perror("ftok error!\n");
return -1;
}
int shmid=shmget(key,PAGE_SIZE,IPC_CREAT|0664);
if(shmid==-1)
{
perror("shmget error!\n");
return -1;
}
char *shmaddress=(char *)shmat(shmid,NULL,0);
if(shmaddress==(void *)-1)
{
perror("shmat error!\n");
return -1;
}
//输出
while(1)
{
P(semid,0);
*shmaddress='A';
printf("%c\n",*shmaddress);
V(semid,1);
sleep(3);
}
if(shmdt(shmaddress)==-1)
{
perror("shmdt error!\n");
return -1;
}
if(shmctl(shmid,IPC_RMID,NULL)==-1)
{
perror("shmctl error!\n");
return -1;
}
del_sem(semid);
return 0;
}
2> 将共享内存和信号灯集完成两个进程间通信程序重新实现一下
#include<myhead.h>
#include"sem.h"
#define PAGE_SIZE 4096
int main(int argc, const char *argv[])
{
//1.---信号灯集创建并初始化
int semid=create_sem(2);
//创建key
key_t key=ftok("/",'k');
if(key ==-1)
{
perror("ftok error!\n");
return -1;
}
//使用key来创建共享内存段
int shmid=shmget(key, PAGE_SIZE , IPC_CREAT|0664);
if(shmid==-1)
{
perror("shmget error");
return -1;
}
printf("shmid %d\n",shmid);
//映射共享内存段到用户空间
char *addr=(char *)shmat(shmid,NULL,0);
if(addr==(void*)-1)
{
perror("shmat error");
return -1;
}
//使用共享内存段
while(1)
{
//2.---P操作
P(semid,1);
printf("请输入》》》");
fgets(addr,PAGE_SIZE,stdin);
addr[strlen(addr)-1]='\0';
//3.---v操作
V(semid,0);
if(strcmp(addr,"quit")==0)
{
break;
}
}
//撤销映射
if(shmdt(addr)==-1)
{
perror("shmdt error\n");
return -1;
}
//删除共享内存段
if(shmctl(shmid,IPC_RMID,NULL)==-1)
{
perror("shctl error\n");
return -1;
}
//4.---信号灯集删除
del_sem(semid);
return 0;
}
#include<myhead.h>
#include"sem.h"
#define PAGE_SIZE 4096
int main(int argc, const char *argv[])
{
//1.---信号灯集创建并初始化
int semid=create_sem(2);
//创建key
key_t key=ftok("/",'k');
if(key ==-1)
{
perror("ftok error!\n");
return -1;
}
//使用key来打开共享内存段
int shmid=shmget(key, PAGE_SIZE , IPC_CREAT|0664);
if(shmid==-1)
{
perror("shmget error");
return -1;
}
printf("shmid %d\n",shmid);
//映射共享内存段到用户空间
char *addr=(char *)shmat(shmid,NULL,0);
if(addr==(void*)-1)
{
perror("shmat error");
return -1;
}
//使用共享内存段-读取
while(1)
{
//2.---P操作
P(semid,0);
printf("共享内存段内内容->%s\n",addr);
//3.---v操作
V(semid,1);
if(strcmp(addr,"quit")==0)
{
break;
}
}
//撤销映射
if(shmdt(addr)==-1)
{
perror("shmdt error\n");
return -1;
}
//删除共享内存段
if(shmctl(shmid,IPC_RMID,NULL)==-1)
{
perror("shctl error\n");
return -1;
}
//4.---信号灯集删除
del_sem(semid);
return 0;
}