ÎÒÓÃ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");
}
}
}