linux怎么选择消息队列共享内存
答案:2 悬赏:20 手机版
解决时间 2021-02-22 01:05
- 提问者网友:容嬷嬷拿针来
- 2021-02-21 20:07
linux怎么选择消息队列共享内存
最佳答案
- 五星知识达人网友:你哪知我潦倒为你
- 2021-02-21 21:14
#include
#include
#include
#include
#include
#include
#include
void msg_show_attr(int msg_id, struct msqid_ds msg_info)
{
int ret = -1;
sleep(1);
ret = msgctl(msg_id, IPC_STAT, &msg_info);
if( -1 == ret)
{
printf(“获得消息信息失败\n”);
return ;
}
printf(“\n”);
printf(“现在队列中的字节数:%d\n”,msg_info.msg_cbytes);
printf(“队列中消息数:%d\n”,msg_info.msg_qnum);
printf(“队列中最大字节数:%d\n”,msg_info.msg_qbytes);
printf(“最后发送消息的进程pid:%d\n”,msg_info.msg_lspid);
printf(“最后接收消息的进程pid:%d\n”,msg_info.msg_lrpid);
printf(“最后发送消息的时间:%s”,ctime(&(msg_info.msg_stime)));
printf(“最后接收消息的时间:%s”,ctime(&(msg_info.msg_rtime)));
printf(“最后变化时间:%s”,ctime(&(msg_info.msg_ctime)));
printf(“消息UID是:%d\n”,msg_info.msg_perm.uid);
printf(“消息GID是:%d\n”,msg_info.msg_perm.gid);
}
int main(void)
{
int ret = -1;
int msg_flags, msg_id;
key_t key;
struct msgmbuf{
int mtype;
char mtext[10];
};
struct msqid_ds msg_info;
struct msgmbuf msg_mbuf;
int msg_sflags,msg_rflags;
char *msgpath = “/ipc/msg/”;
key = ftok(msgpath,’a');
if(key != -1)
{
printf(“成功建立KEY\n”);
}
else
{
printf(“建立KEY失败\n”);
}
msg_flags = IPC_CREAT;
msg_id = msgget(key, msg_flags|0666);
if( -1 == msg_id)
{
printf(“消息建立失败\n”);
return 0;
}
msg_show_attr(msg_id, msg_info);
msg_sflags = IPC_NOWAIT;
msg_mbuf.mtype = 10;
memcpy(msg_mbuf.mtext,”测试消息”,sizeof(“测试消息”));
ret = msgsnd(msg_id, &msg_mbuf, sizeof(“测试消息”), msg_sflags);
if( -1 == ret)
{
printf(“发送消息失败\n”);
}
msg_show_attr(msg_id, msg_info);
msg_rflags = IPC_NOWAIT|MSG_NOERROR;
ret = msgrcv(msg_id, &msg_mbuf, 10,10,msg_rfla
共享内存示例代码如下:
#include
#include
#include
#include
typedef int sem_t;
union semun {
int val;
struct semid_ds *buf;
unsigned short *array;
} arg;
sem_t CreateSem(key_t key, int value)
{
union semun sem;
sem_t semid;
sem.val = value;
semid = semget(key,value,IPC_CREAT|0666);
if (-1 == semid)
{
printf(“create semaphore error\n”);
return -1;
}
semctl(semid,0,SETVAL,sem);
return semid;
}
void SetvalueSem(sem_t semid, int value)
{
union semun sem;
sem.val = value;
semctl(semid,0,SETVAL,sem);
return ;
}
int GetvalueSem(sem_t semid)
{
union semun sem;
return semctl(semid,0,GETVAL,sem);
return sem.val;
}
void DestroySem(sem_t semid)
{
union semun sem;
sem.val = 0;
semctl(semid,0,IPC_RMID,sem);
}
int Sem_P(sem_t semid)
{
struct sembuf sops={0,+1,IPC_NOWAIT};
return (semop(semid,&sops,1));
}
int Sem_V(sem_t semid)
{
struct sembuf sops={0,-1,IPC_NOWAIT};
return (semop(semid,&sops,1));
}
static char msg[]=”你好,共享内存\n”;
int main(void)
{
key_t key;
int semid,shmid;
char i,*shms,*shmc;
struct semid_ds buf;
int value = 0;
char buffer[80];
pid_t p;
key = ftok(“/ipc/sem/”,’a');
shmid = shmget(key,1024,IPC_CREAT|0604);
semid = CreateSem(key,1);
p = fork();
if(p > 0)
{
shms = (char *)shmat(shmid,0,0);
memcpy(shms, msg, strlen(msg)+1);
sleep(10);
Sem_P(semid);
shmdt(shms);
DestroySem(semid);
}
else if(p == 0)
{
shmc = (char *)shmat(shmid,0,0);
Sem_V(semid);
printf(“共享内存的值为:%s\n”,shmc);
shmdt(sg_
#include
#include
#include
#include
#include
#include
void msg_show_attr(int msg_id, struct msqid_ds msg_info)
{
int ret = -1;
sleep(1);
ret = msgctl(msg_id, IPC_STAT, &msg_info);
if( -1 == ret)
{
printf(“获得消息信息失败\n”);
return ;
}
printf(“\n”);
printf(“现在队列中的字节数:%d\n”,msg_info.msg_cbytes);
printf(“队列中消息数:%d\n”,msg_info.msg_qnum);
printf(“队列中最大字节数:%d\n”,msg_info.msg_qbytes);
printf(“最后发送消息的进程pid:%d\n”,msg_info.msg_lspid);
printf(“最后接收消息的进程pid:%d\n”,msg_info.msg_lrpid);
printf(“最后发送消息的时间:%s”,ctime(&(msg_info.msg_stime)));
printf(“最后接收消息的时间:%s”,ctime(&(msg_info.msg_rtime)));
printf(“最后变化时间:%s”,ctime(&(msg_info.msg_ctime)));
printf(“消息UID是:%d\n”,msg_info.msg_perm.uid);
printf(“消息GID是:%d\n”,msg_info.msg_perm.gid);
}
int main(void)
{
int ret = -1;
int msg_flags, msg_id;
key_t key;
struct msgmbuf{
int mtype;
char mtext[10];
};
struct msqid_ds msg_info;
struct msgmbuf msg_mbuf;
int msg_sflags,msg_rflags;
char *msgpath = “/ipc/msg/”;
key = ftok(msgpath,’a');
if(key != -1)
{
printf(“成功建立KEY\n”);
}
else
{
printf(“建立KEY失败\n”);
}
msg_flags = IPC_CREAT;
msg_id = msgget(key, msg_flags|0666);
if( -1 == msg_id)
{
printf(“消息建立失败\n”);
return 0;
}
msg_show_attr(msg_id, msg_info);
msg_sflags = IPC_NOWAIT;
msg_mbuf.mtype = 10;
memcpy(msg_mbuf.mtext,”测试消息”,sizeof(“测试消息”));
ret = msgsnd(msg_id, &msg_mbuf, sizeof(“测试消息”), msg_sflags);
if( -1 == ret)
{
printf(“发送消息失败\n”);
}
msg_show_attr(msg_id, msg_info);
msg_rflags = IPC_NOWAIT|MSG_NOERROR;
ret = msgrcv(msg_id, &msg_mbuf, 10,10,msg_rfla
共享内存示例代码如下:
#include
#include
#include
#include
typedef int sem_t;
union semun {
int val;
struct semid_ds *buf;
unsigned short *array;
} arg;
sem_t CreateSem(key_t key, int value)
{
union semun sem;
sem_t semid;
sem.val = value;
semid = semget(key,value,IPC_CREAT|0666);
if (-1 == semid)
{
printf(“create semaphore error\n”);
return -1;
}
semctl(semid,0,SETVAL,sem);
return semid;
}
void SetvalueSem(sem_t semid, int value)
{
union semun sem;
sem.val = value;
semctl(semid,0,SETVAL,sem);
return ;
}
int GetvalueSem(sem_t semid)
{
union semun sem;
return semctl(semid,0,GETVAL,sem);
return sem.val;
}
void DestroySem(sem_t semid)
{
union semun sem;
sem.val = 0;
semctl(semid,0,IPC_RMID,sem);
}
int Sem_P(sem_t semid)
{
struct sembuf sops={0,+1,IPC_NOWAIT};
return (semop(semid,&sops,1));
}
int Sem_V(sem_t semid)
{
struct sembuf sops={0,-1,IPC_NOWAIT};
return (semop(semid,&sops,1));
}
static char msg[]=”你好,共享内存\n”;
int main(void)
{
key_t key;
int semid,shmid;
char i,*shms,*shmc;
struct semid_ds buf;
int value = 0;
char buffer[80];
pid_t p;
key = ftok(“/ipc/sem/”,’a');
shmid = shmget(key,1024,IPC_CREAT|0604);
semid = CreateSem(key,1);
p = fork();
if(p > 0)
{
shms = (char *)shmat(shmid,0,0);
memcpy(shms, msg, strlen(msg)+1);
sleep(10);
Sem_P(semid);
shmdt(shms);
DestroySem(semid);
}
else if(p == 0)
{
shmc = (char *)shmat(shmid,0,0);
Sem_V(semid);
printf(“共享内存的值为:%s\n”,shmc);
shmdt(sg_
全部回答
- 1楼网友:春色三分
- 2021-02-21 21:33
我用的linux as4 的操作系统,重装了oracle数据库后,以前的老数据文件和里面的文件都不能删除,并且读写和执行的权限都赋权给了oracle和root这两个,删除时提示为只读系统文件,请高手指点如何修改文件,能给出相应的命令最好!
msgrcv.c 内容如下:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define n 10
typedef struct student
{
int sno;
char sname[15];
}stu;
typedef struct msgbuf
{
long mtype;
stu stu[n];
}msgbuf;
main(int argc, char* argv[])
{
int i = 0;
msgbuf mbf = {0};
mbf.mtype = 1;
int msqid ;
errno = 0;
char *name = "./msgaaa";
key_t key = ftok(name,0);
msqid = msgget(key, ipc_creat);
while(i < n)
{
mbf.stu[i].sno = i;
strcpy(mbf.stu[i].sname, "hello");
if (i == 1)
{
memset(mbf.stu[i].sname, 0, strlen(mbf.stu
[i].sname));
strcpy(mbf.stu[i].sname, "aaa");
}
if (i == n-1)
{
memset(mbf.stu[i].sname, 0, strlen(mbf.stu
[i].sname));
strcpy(mbf.stu[i].sname, "end");
}
printf("------- %s ------", mbf.stu[i].sname);
msgsnd(msqid, mbf.stu[i], sizeof(mbf.stu[i]), 0);
if (-1 == errno)
{
perror("msgsnd");
exit(-1);
}
printf(" %d -- %s\n", mbf.stu[i].sno, mbf.stu[i].sname);
i++;
sleep(1);
}//while
msgctl(msqid, ipc_rmid, null);
}
msgsnd内容如下:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define n 10
typedef struct student
{
int sno;
char sname[15];
}stu;
typedef struct msgbuf
{
long mtype;
stu stu[n];
}msgbuf;
main(int argc, char* argv[])
{
int i = 0;
msgbuf mbf = {0};
mbf.mtype = 1;
int msqid ;
errno = 0;
char *name = "./msgaaa";
key_t key = ftok(name,0);
msqid = msgget(key, ipc_creat);
while(i < n)
{
mbf.stu[i].sno = i;
strcpy(mbf.stu[i].sname, "hello");
if (i == 1)
{
memset(mbf.stu[i].sname, 0, strlen(mbf.stu[i].sname));
strcpy(mbf.stu[i].sname, "aaa");
}
if (i == n-1)
{
memset(mbf.stu[i].sname, 0, strlen(mbf.stu[i].sname));
strcpy(mbf.stu[i].sname, "end");
}
// printf("------- %s ------", mbf.stu[i].sname);
msgsnd(msqid, mbf.stu[i], sizeof(mbf.stu[i]), 0);
if (-1 == errno)
{
perror("msgsnd");
exit(-1);
}
printf(" %d -- %s\n", mbf.stu[i].sno, mbf.stu[i].sname);
i++;
sleep(1);
}//while
msgctl(msqid, ipc_rmid, null);
}
msgsnd 显示结果:
0 -- hello
1 -- aaa
2 -- hello
3 -- hello
4 -- hello
5 -- hello
6 -- hello
7 -- hello
8 -- hello
9 -- end
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯