红联Linux门户
Linux帮助

求高手相助----vfork,execl函数相关问题

发布时间:2012-12-07 15:44:23来源:红联作者:
我用vfork建立了两个任务,想在两个任务之间用消息队列进行通信,但是在建立进程时执行到execl函数时出现permission denied的错误!源代码如下,请高手指导一下我这个菜鸟啊!
/*************************************************************
*
*学习uClinux消息队列的例子
*
**************************************************************/
#include "ucdemo.h"


int main(void)
{
pid_t msgspro,msgrpro; /*进程创建返回值*/
struct msqid_ds msg_info; /*消息队列变量*/

/*创建了一个消息队列,0666指定了访问权限,所有进程都可以访问*/
msgQid = msgget(IPC_PRIVATE, IPC_CREAT|IPC_EXCL|0666);
if(msgQid == -1){
perror("msg creat error");
exit(1);
}
printf("open the queue %d\n",msgQid);


/*获取消息队列的属性*/
if (msgctl(msgQid,IPC_STAT,&msg_info) == ERROR){
msgctl(msgQid, IPC_RMID, NULL);/*删除队列*/
perror("msg ipc_stat access error");
exit(1);
}
/*根据入参调整消息队列的属性*/
msg_info.msg_qbytes = sizeof(struct msgbuf)*maxMsgs;/*消息队列的最大长度*/
if (msgctl(msgQid, IPC_SET, &msg_info) == ERROR){/*调整队列属性失败*/
msgctl(msgQid, IPC_RMID, NULL);/*删除队列*/
perror("msg ipc_set call error");
exit(1);
}



/*创建发送消息的进程*/
msgspro = vfork();
printf("new creat sendmsg process id: %d\n",msgspro);
if(msgspro == -1){
perror("msgspro vfork error");
exit(1);
}else if(msgspro == 0){
if(execl("./","msgspro",NULL)<0){
perror("Process msgspro execl error");
}
}

/*创建接收消息的进程*/
msgrpro = vfork();
printf("new creat recvmsg process id: %d\n",msgrpro);
if(msgrpro == -1){
perror("msgrpro vfork error\n");
exit(1);
}else if(msgrpro == 0){
if(execl("./","msgrpro",NULL)<0){
perror("Process msgrpro execl error");
}
}
}
文章评论

共有 0 条评论