LINUX 信号量共存 共享内存通信
编写两个程序Mysem.c(编译后为mysem)和Msginput.c(编译后为msginput),实现两个程序(进程)之间的通信。要求如下:1、mysem创建一个共享内...
编写两个程序Mysem.c(编译后为mysem)和Msginput.c(编译后为msginput),实现两个程序(进程)之间的通信。要求如下:
1、 mysem创建一个共享内存myshm(共享内存ID,内存大小为512字节),供mysem和myinput共享
2、 mysem创建一个子进程,该子进程加载myinput
3、 myinput从键盘以追加方式向myshm中输入信息(字符型),mysem接收信息并显示在屏幕上
4、 当myinput从键盘输入“end”并存入myshm后,myinput结束(退出),mysem收到“end”显示后,也退出,并释放所有的资源。
要运用到信号量的PV操作。
需要的效果是:输入一条,接收一条,打入END后,myinput退出,mysem输出END后也退出。
本人非常急,高分求答案。我要两个程序的源代码,若运行通过且在明早7点前,可加分。谢谢 展开
1、 mysem创建一个共享内存myshm(共享内存ID,内存大小为512字节),供mysem和myinput共享
2、 mysem创建一个子进程,该子进程加载myinput
3、 myinput从键盘以追加方式向myshm中输入信息(字符型),mysem接收信息并显示在屏幕上
4、 当myinput从键盘输入“end”并存入myshm后,myinput结束(退出),mysem收到“end”显示后,也退出,并释放所有的资源。
要运用到信号量的PV操作。
需要的效果是:输入一条,接收一条,打入END后,myinput退出,mysem输出END后也退出。
本人非常急,高分求答案。我要两个程序的源代码,若运行通过且在明早7点前,可加分。谢谢 展开
展开全部
/***Msginput.c***/
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/sem.h>
#include<unistd.h>
#include<stdio.h>
#include<errno.h>
#include<string.h>
#include"Mysem.h"
int main(){
FILE *fp;
int empty;
int full;
int mutex;
char * shm;
int shmid;
fp = fopen("shmid","r");
fread(&shmid,sizeof(int), 1, fp);
fread(&empty,sizeof(int), 1, fp);
fread(&full,sizeof(int), 1, fp);
fread(&mutex,sizeof(int), 1, fp);
fclose(fp);
shm = shmat(shmid, NULL, 0);
while(1){
P(&empty);
P(&mutex);
scanf("%s", shm);
if(strcmp(shm, "END") == 0){
V(&mutex);
V(&full);
break;
}
V(&mutex);
V(&full);
}
return 0;
}
/****Mysem.c*****/
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/sem.h>
#include<unistd.h>
#include<stdio.h>
#include<errno.h>
#include<string.h>
#include "Mysem.h"
#define BUFFER_SIZE 512
int main(){
char *shm;
int empty;
int full;
int mutex;
int shmid;
int pid;
int i;
FILE *fp;
// int init_sem_value = 0;
empty = semget(IPC_PRIVATE, 1, (0600|IPC_CREAT));
if(empty == -1){
perror("semget");
exit(1);
}
if(semctl(empty, 0, SETVAL, 1)<0){
perror("semctl");
exit(1);
}
full = semget(IPC_PRIVATE, 1, (0600|IPC_CREAT));
if(full == -1){
perror("semget");
exit(1);
}
if(semctl(full, 0, SETVAL, 0)<0){
perror("semctl");
exit(1);
}
mutex = semget(IPC_PRIVATE, 1, (0600|IPC_CREAT));
if(mutex == -1){
perror("semget");
exit(1);
}
if(semctl(mutex, 0, SETVAL, 1)<0){
perror("semctl");
exit(1);
}
shmid = shmget(IPC_PRIVATE, (BUFFER_SIZE*sizeof(char)),(IPC_CREAT|0600));
if(shmid == -1){
perror("shmget");
exit(1);
}
shm = shmat(shmid, NULL, 0);
if(shm == (char*)-1){
perror("shmat");
exit(1);
}
fp = fopen("shmid","w");
fwrite(&shmid, sizeof(int), 1, fp);
fwrite(&empty, sizeof(int), 1, fp);
fwrite(&full, sizeof(int), 1, fp);
fwrite(&mutex, sizeof(int), 1, fp);
fclose(fp);
pid = fork();
if(pid == 0){
execlp("./Msginput", "./Msginput",0);
perror("execlp");
exit(1);
}else{
while(1){
P(&full);
P(&mutex);
printf("%s\n", shm);
if(strcmp(shm,"END") == 0){
V(&mutex);
V(&empty);
break;
}
V(&mutex);
V(&empty);
}
}
wait(0);
if(semctl(full, 0, IPC_RMID, 1) == -1){
perror("semctl");
exit(1);
}
if(semctl(empty, 0, IPC_RMID, 1) == -1){
perror("semctl");
exit(1);
}
if(semctl(mutex, 0, IPC_RMID, 1) == -1){
perror("semctl");
exit(1);
}
if(shmctl(shmid, IPC_RMID, NULL) == -1){
perror("shmctl");
exit(1);
}
exit(0);
}
/****Mysem.h*****/
void P(int *s);
void V(int *s);
extern void *shmat (int __shmid, __const void *__shmaddr, int __shmflg);
void P(int *s){
struct sembuf sembuffer, *sops;
sops=&sembuffer;
sops->sem_num = 0;
sops->sem_op = -1;
sops->sem_flg = 0;
if(semop(*s, sops, 1)<0){
perror("semop");
exit(1);
}
return ;
}
void V(int *s){
struct sembuf sembuffer, *sops;
sops = &sembuffer;
sops->sem_num = 0;
sops->sem_op = 1;
sops->sem_flg = 0;
if(semop(*s, sops, 1)<0){
perror("semop");
exit(1);
}
return;
}
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/sem.h>
#include<unistd.h>
#include<stdio.h>
#include<errno.h>
#include<string.h>
#include"Mysem.h"
int main(){
FILE *fp;
int empty;
int full;
int mutex;
char * shm;
int shmid;
fp = fopen("shmid","r");
fread(&shmid,sizeof(int), 1, fp);
fread(&empty,sizeof(int), 1, fp);
fread(&full,sizeof(int), 1, fp);
fread(&mutex,sizeof(int), 1, fp);
fclose(fp);
shm = shmat(shmid, NULL, 0);
while(1){
P(&empty);
P(&mutex);
scanf("%s", shm);
if(strcmp(shm, "END") == 0){
V(&mutex);
V(&full);
break;
}
V(&mutex);
V(&full);
}
return 0;
}
/****Mysem.c*****/
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/sem.h>
#include<unistd.h>
#include<stdio.h>
#include<errno.h>
#include<string.h>
#include "Mysem.h"
#define BUFFER_SIZE 512
int main(){
char *shm;
int empty;
int full;
int mutex;
int shmid;
int pid;
int i;
FILE *fp;
// int init_sem_value = 0;
empty = semget(IPC_PRIVATE, 1, (0600|IPC_CREAT));
if(empty == -1){
perror("semget");
exit(1);
}
if(semctl(empty, 0, SETVAL, 1)<0){
perror("semctl");
exit(1);
}
full = semget(IPC_PRIVATE, 1, (0600|IPC_CREAT));
if(full == -1){
perror("semget");
exit(1);
}
if(semctl(full, 0, SETVAL, 0)<0){
perror("semctl");
exit(1);
}
mutex = semget(IPC_PRIVATE, 1, (0600|IPC_CREAT));
if(mutex == -1){
perror("semget");
exit(1);
}
if(semctl(mutex, 0, SETVAL, 1)<0){
perror("semctl");
exit(1);
}
shmid = shmget(IPC_PRIVATE, (BUFFER_SIZE*sizeof(char)),(IPC_CREAT|0600));
if(shmid == -1){
perror("shmget");
exit(1);
}
shm = shmat(shmid, NULL, 0);
if(shm == (char*)-1){
perror("shmat");
exit(1);
}
fp = fopen("shmid","w");
fwrite(&shmid, sizeof(int), 1, fp);
fwrite(&empty, sizeof(int), 1, fp);
fwrite(&full, sizeof(int), 1, fp);
fwrite(&mutex, sizeof(int), 1, fp);
fclose(fp);
pid = fork();
if(pid == 0){
execlp("./Msginput", "./Msginput",0);
perror("execlp");
exit(1);
}else{
while(1){
P(&full);
P(&mutex);
printf("%s\n", shm);
if(strcmp(shm,"END") == 0){
V(&mutex);
V(&empty);
break;
}
V(&mutex);
V(&empty);
}
}
wait(0);
if(semctl(full, 0, IPC_RMID, 1) == -1){
perror("semctl");
exit(1);
}
if(semctl(empty, 0, IPC_RMID, 1) == -1){
perror("semctl");
exit(1);
}
if(semctl(mutex, 0, IPC_RMID, 1) == -1){
perror("semctl");
exit(1);
}
if(shmctl(shmid, IPC_RMID, NULL) == -1){
perror("shmctl");
exit(1);
}
exit(0);
}
/****Mysem.h*****/
void P(int *s);
void V(int *s);
extern void *shmat (int __shmid, __const void *__shmaddr, int __shmflg);
void P(int *s){
struct sembuf sembuffer, *sops;
sops=&sembuffer;
sops->sem_num = 0;
sops->sem_op = -1;
sops->sem_flg = 0;
if(semop(*s, sops, 1)<0){
perror("semop");
exit(1);
}
return ;
}
void V(int *s){
struct sembuf sembuffer, *sops;
sops = &sembuffer;
sops->sem_num = 0;
sops->sem_op = 1;
sops->sem_flg = 0;
if(semop(*s, sops, 1)<0){
perror("semop");
exit(1);
}
return;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询