一.
實現pipe/msgqueue/sems/shm
相關**
1.實現pipe,從鍵盤讀取資料寫入管道,讀取資料,寫到螢幕
(1)** #include
#include
#include
#include
intmain()
memset(buf,0x00,sizeof(buf));
if((len=read(fds[0],buf,100))==-1)
if(write(1,buf,len)!=len)
} }(2)結果
2.實現msgqueue
(1)comm.h
#include
#include
#include
#include
# definepathname"."
# defineproj_id 0x6666
# definesever_type 1
# defineclient_type 2
structmsgbuf;
intcreatmsgqueue();
intgetmsgqueue();
intdestroymsgqueue(int msgid);
intsendmsg(int msgid,int who,char *msg);
intrecvmsg(int msgid,int recvtype,char out);
# endif
(2)comm.c
#include"comm.h"
staticint commmsgqueue(int flags)
//intmsgid=msgget(_key,ipc_creat|ipc_excl|0666);
int msgid=msgget(_key,flags);
if(msgid<0)
return msgid;
}intcreatemsgqueue()
intgetmsgqueue()
intdestroymsgqueue(int msgid)
return 0;
}intsendmsg(int msgid,int who,char *msg)
return 0;
}intrecvmsg(int msgid,int recvtype,char out)
strcpy(out,buf.mtext);
return 0;
}(3)
server.c
# include"comm.h"
intmain()
}destroymsgqueue(msgid);
return 0;
}//法2
intmsgid=createqueue();
printf("msgid:%d\n",msgid);
while(1);
intret=readqueue(msgid,client_type,buf,sizeof(buf)-1);
if(ret<0)
printf("client:%s\n",buf);
if(ret<0)
}(4)
client.c
#include"comm.h"
intmain()
//buf[read_size]='\0';
//intret=writequeue(msgid,client_type,buf,strlen(buf));
//if(ret<0)
//ints=readqueue(msgid,client_type,buf,strlen(buf));
//if(s<0)
ssize_t s=read(0,buf,sizeof(buf));
if(s>0)
recvmsg(msgid,server_type,buf);
printf("server# %s\n",buf);
} return 0;
}3.實現sems
(1)comm.h
# define__comm_h__
# include
#include
#include
#include
# definepathname "."
# defineproj_id 0x6666
unionsemun;
intcreatesemset(int nums);
intinitsem(int semid,int nums,int initval);
intgetsemset(int nums);
int p(intsemid,int who);
int v(intsemid,int who);
intdestroysemset(int semid);
#endif
(2)comm.c
#include"comm.h"
staticint commsemset(int nums,int flags)
int semid=semget(key,nums,flags);
if(semid<0)
return semid;
}intcreatesemset(int nums)
intgetsemset(int nums)
intinitsem(int semid,int nums,int initval)
return 0;
}staticint commpv(int semid,int who,int op)
return 0;
}int p(intsemid,int who)
int v(intsemid,int who)
intdestroysemset(int semid)
}(3)
test.c
#include"comm.h"
intmain()
}else
wait(null);
} destroysemset(semid);
return 0;
}(4)
結果截圖,起初能正常執行,而後產生錯誤,如下
4.實現shm
(1)comm.h
# ifndef __comm_h__
# define__comm_h__
#include
#include
#include
#include
# definepathname "."
# define proj_id0x6666
intcreateshm(int size);
intdestroyshm(int shmid);
int getshm(intsize);
# endif
(2)comm.c
#include"comm.h"
staticint commshm(int size,int flags)
int shmid=0;
if(shmid=shmget(key,size,flags))<0)
return shmid;
}intdestroyshm(int shmid)
return 0;
}int createshm(int size)
intgetshm(int size)
(3)server.c
# include"comm.h"
intmain()
shmdt(addr);
sleep(2);
destroyshm(shmid);
return 0;
}(4)
client.c
#include"comm.h"
intmain()
shmdt(addr);
sleep(2);
return 0;
}(5)
結果截圖
二.ipcs顯示ipc資源
(1)ipcs –m:檢視共享記憶體
(2)ipcs –a:檢視所有資源
ipcrm:手動刪除ipc資源
簡單了解程序間通訊
from multiprocessing import process,queue import os,time def write q print 啟動寫子程序 s os.getpid for chr in a b c d q.put chr time.sleep 1 print 結束寫子程序 s...
SendMessage程序間簡單通訊問題
主要api findwindow lpctstrlpclassname,lpctstrlpwindowname 通過程序名得到程序handle sendmessage uintmessage,wparamwparam 0,lparamlparam 0 the result of the messag...
SendMessage程序間簡單通訊問題
本例以兩個vc dialog 主要api findwindow lpc tstrlpclassname,lpctstrlpwindowname 通過程序名得到程序handle sendmessage uintmessage,wparamwparam 0,lparamlparam 0 the resu...