我必须说我知道信号量,但我还不知道如何使用它们。所以问题是当获取特定值时我将控件传递给我的数据段,我int lock
怎么能让我的代码工作,因为此时它冻结了,我不明白为什么......
sc(server) - 首先运行
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/wait.h>
#include "st.h"
int main(){
int shmid,i;
int w =1;
struct msg* m;
shmid = shmget(1271,sizeof(struct msg), IPC_CREAT|IPC_EXCL|0600);
if(shmid == -1){
perror("~~~Shmid");
exit(1);
}
m = shmat(shmid,0,0);
printf("segment attached to structure");
do{
printf("waiting...");
sleep(1);
}while(m->lock != 1);
if(m->lock == 1)
printf("lock open!");
shmdt(m);
return 0;
}
抄送(客户)
int main(int argc, char *argv[]){
if(argc != 2)
perror("~~~ ./c [file name]");
exit(1);
int shmid;
struct msg* m;
shmid = shmget(1271,0,0);
if(shmid == -1){
perror("~~~Shmid");
exit(1);
}
m = shmat(shmid,0,0);
m->f = *argv[1];
m->lock = 1;
shmdt(m);
return 0;
}
st.h
struct msg{
char f[50];
int lock;
};