導航:首頁 > 源碼編譯 > 編譯一段程序實現進程的管道通信

編譯一段程序實現進程的管道通信

發布時間:2023-05-30 16:59:35

『壹』 編寫一個利用管道流,實現線程之間的通信,實現文件傳輸功能java程序

importjava.io.BufferedReader;
importjava.io.BufferedWriter;
importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.io.PipedInputStream;
importjava.io.PipedOutputStream;
importjava.io.PrintWriter;
publicclassDay10A{
privateBufferedReaderbr;//字元效率讀取流
privateBufferedWriterbw;//字元效率寫出流
privatePipedInputStreampis;//管道讀取流
privatePipedOutputStreampos;//管道輸出流,
privatebyte[]readBytes;//讀取緩沖區
privatebyte[]writeBytes;//寫出緩沖區
privateStringpath="C:\Users\Administrator\Desktop";
privateFilefile=null;
privateFileOutputStreamfos=null;

Day10A(){
writeBytes=newbyte[1024];
file=newFile(path,"測試.txt");
try{
if(!file.exists()){
file.createNewFile();
fos=newFileOutputStream(file);
}else{
fos=newFileOutputStream(file,true);
}
br=newBufferedReader(newInputStreamReader(System.in));
bw=newBufferedWriter(newPrintWriter(fos));
pis=newPipedInputStream();
pos=newPipedOutputStream();
pis.connect(pos);
}catch(IOExceptione){
e.printStackTrace();
}
}
publicstaticvoidmain(String[]args){
Day10Ad=newDay10A();
d.readFunction();
d.writerFunction();
}
privatevoidreadFunction(){
newThread(newRunnable(){
publicvoidrun(){
while(true){
try{
for(Stringstr=br.readLine();!str.contentEquals("over");str=br.readLine()){
readBytes=str.getBytes();
pos.write(readBytes);
}
}catch(Exceptione){
}
break;
}
try{
pos.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}).start();
}
privatevoidwriterFunction(){
newThread(newRunnable(){
@Override
publicvoidrun(){
try{
Stringstr="";
for(intlen=pis.read(writeBytes);len!=-1;len=pis.read(writeBytes)){
str=newString(writeBytes,0,len);
bw.write(str+" ");
bw.flush();
}
}catch(Exceptione){
}
}
}).start();
}
}

『貳』 linux管道實驗題

<pre t="code" l="cpp">#include <unistd.h>
#include <stdio.h>

//警告: 該程序未做錯誤驗證, 未關閉管道(由系統自動關閉)

int main()
{
int p2c[2]; // 該管道父進程寫,子進程讀
int c2p[2]; // 該管道子進程寫,父進程讀

// 創建2條管道
pipe(p2c);
pipe(c2p);

int pid = fork();
int fd_read, fd_write; // 這兩個描述符用於保存某進程讀端和寫端
int pid_my; // 保存某進程自身的pid
int pid_other; // 另一進程的pid,通過

if ( pid == 0 ) { // 子進程
fd_read = p2c[0];
fd_write= c2p[1];
// 通過getpid取得自身pid,寫到管道里
pid_my = getpid();

write(fd_write, pid_my, sizeof(int));
// 從另一管道讀取另一進程的pid
read(fd_read, pid_other, sizeof(int));
// 列印讀取到的pid
printf("Recive pid : %d\n", pid_other);
} else { // p
fd_read = c2p[0];
fd_write= p2c[1];
pid_my = getpid();
// 由於子進程是先寫自身pid,父進程最好先讀取子進程的pid
read(fd_read, pid_other, sizeof(int));
write(fd_write, pid_my, sizeof(int));

printf("Recive pid : %d\n", pid_other);
}

return 0;
}

『叄』 關於linux系統下管道 通信的問題(高手回答)

不是高爛殲手,沒事寫了一下b,請大家批評指正:
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include<sys/基嘩types.h>

int main()
{
int n, fd[2];
pid_t pid, pid1;
char buffer[BUFSIZ+1];
if(pipe(fd) < 0)
{
printf("pipe failed!\n");
exit(1);
}
if((pid = fork()) < 0)
{
printf("fork failed");
exit(1);
}
else if(pid > 0)
{
if((pid1 = fork()) < 0)
{
printf("fork failed");
exit(1);
}
else if(pid1 >0)
{
close(fd[0]);
write(fd[1], "Hello World!", 12);
}
else
{
close(fd[1]);
n = read(fd[0], buffer, 6);
write(STDOUT_FILENO,"\np1 get:\n", 8);
write(STDOUT_FILENO, buffer, n);
}
}
else
{
close(fd[1]);
n = read(fd[0], buffer, 6);
write(STDOUT_FILENO,"搏歷行\np0 get:\n", 8);
write(STDOUT_FILENO, buffer, n);
}
exit(0);
}

『肆』 編寫一個程序實現以下功能: (1)使用fork()創建進程。 (2)使用管道實現子進程和父進程之間的通信。

編寫一段程序,使用系統調用fork( )創建兩個子進程。當此程序運行時,在系統中有一個父進程和兩個子進程活動。讓每一個進程在屏幕上顯示一個字元;父進程顯示字元「a」,子進程分別顯示字元「b」和「c」。試觀察記錄屏幕上的顯示結果,並分析原因。
〈程序〉
#include<stdio.h>
main()
{
int p1,p2;
if(p1=fork()) /*子進程創建成功*/
putchar('b');
else
{
if(p2=fork()) /*子進程創建成功*/
putchar('c');
else putchar('a'); /*父進程執行*/
}
}

<運行結果>
bca(有時會出現abc的任意的排列)

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

編制一段程序,實現進程的管道通信。使用系統調用pipe()建立一條管道線。兩個子進程p1和p2分別向通道個寫一句話:
child1 process is sending message!
child2 process is sending message!
而父進程則從管道中讀出來自兩個進程的信息,顯示在屏幕上。

〈程序〉
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
int pid1,pid2;

main( )
{
int fd[2];
char outpipe[100],inpipe[100];
pipe(fd); /*創建一個管道*/
while ((pid1=fork( ))==-1);
if(pid1==0)
{
lockf(fd[1],1,0);
sprintf(outpipe,"child 1 process is sending message!");
/*把串放入數組outpipe中*/
write(fd[1],outpipe,50); /*向管道寫長為50位元組的串*/
sleep(5); /*自我阻塞5秒*/
lockf(fd[1],0,0);
exit(0);
}
else
{
while((pid2=fork( ))==-1);
if(pid2==0)
{
lockf(fd[1],1,0); /*互斥*/
sprintf(outpipe,"child 2 process is sending message!");
write(fd[1],outpipe,50);
sleep(5);
lockf(fd[1],0,0);
exit(0);
}
else
{
wait(0); /*同步*/
read(fd[0],inpipe,50); /*從管道中讀長為50位元組的串*/
printf("%s\n",inpipe);
wait(0);
read(fd[0],inpipe,50);
printf("%s\n",inpipe);
exit(0);
}
}
}
〈運行結果〉
延遲5秒後顯示:
child1 process is sending message!
再延遲5秒:
child2 process is sending message!

附:我承認我是復制的 不過很符合題意~

『伍』 用C語言編寫一個簡單的管道通信程序 需用到fock() 在Linux下可以運行

一仔並亂個利用pipe,進行父子進程間通信的小念檔例子
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>

int main(void)
{
int fds[2];
pid_t pid;

if(pipe(fds) == -1)
{
perror("pipe error");
exit(1);
}
pid=fork();
if(pid == -1)
{
perror("fork error");
exit(1);
}
if(pid == 0)//parent
{
char buf[1024];
int n;
close(fds[1]);
n = read(fds[0], buf, 1024);//從管道讀端讀出數據到buf
write(STDOUT_FILENO, "child:",6);
write(STDOUT_FILENO, buf, n);//將讀到的數據列印到終端
}
else//child
{
close(fds[0]);
write(fds[1], "hello world\蔽掘n", 12);//向管道寫端寫入數據「hello world\n」
wait(NULL);
}

return 0;
}

『陸』 c++ 進程間通信(管道和共享內存分別寫),給個簡單代碼,包注釋

#include<stdio.h>
#include<windows.h>
intmain(intargc,char*argv[])
{
if(argv[1]==0){//如果是主進程
HANDLEhPipeW,hPipeR;//讀管道和寫管道
STARTUPINFOAsi;
PROCESS_INFORMATIONpi;
charstr[128];
charparam[1024];
CreatePipe(&hPipeR,&hPipeW,NULL,0);
SetHandleInformation(hPipeW,HANDLE_FLAG_INHERIT,HANDLE_FLAG_INHERIT);//使得子進程可以繼承這個句柄
ZeroMemory(&si,sizeof(si));
si.cb=sizeof(si);
sprintf(param,""%s"%x",argv[0],hPipeW);//傳給子進程的參數
if(CreateProcessA(argv[0],param,0,0,TRUE,0,0,0,&si,&pi)!=FALSE){
char*pstr=str;
CloseHandle(hPipeW);//關閉管道的輸入端,因為此時已經由子進程使用輸入端了
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
for(;;){
DWORDr;
ReadFile(hPipeR,pstr,128,&r,0);//從管道讀取數據
if(r>0)
pstr+=r;
else
break;
}
CloseHandle(hPipeR);
puts(str);
}
return0;
}else{//如果是子進程
charstr[]="Hello!";
HANDLEhPipeW;
DWORDr;
sscanf(argv[1],"%x",&hPipeW);//從參數獲取管道的寫句柄
WriteFile(hPipeW,str,sizeof(str),&r,0);//往管道寫入數據
CloseHandle(hPipeW);
return0;
}
}#include<stdio.h>
#include<string.h>
#include<windows.h>
intmain(intargc,char*argv[])
{
if(argv[1]==0){//如果是作為主進程運行
HANDLEhShmem;
charparam[1024];
STARTUPINFOAsi;
PROCESS_INFORMATIONpi;
char*pstr;
hShmem=CreateFileMapping(INVALID_HANDLE_VALUE,0,PAGE_READWRITE,0,256,0);//創建共享內存對象
SetHandleInformation(hShmem,HANDLE_FLAG_INHERIT,HANDLE_FLAG_INHERIT);//使得句柄可以繼承到子進程

sprintf(param,""%s"%x",argv[0],hShmem);
ZeroMemory(&si,sizeof(si));
si.cb=sizeof(si);
CreateProcessA(argv[0],param,0,0,TRUE,0,0,0,&si,&pi);
WaitForSingleObject(pi.hProcess,INFINITE);//等待子進程運行結束
pstr=(char*)MapViewOfFile(hShmem,FILE_MAP_WRITE,0,0,0);//將共享內存對象中的內存塊映射到當前進程
puts(pstr);
UnmapViewOfFile(pstr);
CloseHandle(hShmem);
return0;
}else{//如果是作為子進程運行
HANDLEhShmem;
char*pstr;
sscanf(argv[1],"%x",&hShmem);
pstr=(char*)MapViewOfFile(hShmem,FILE_MAP_WRITE,0,0,0);//將共享內存對象中的對象映射到當前進程
strcpy(pstr,"Hello~!");//往共享內存里寫入字元串
UnmapViewOfFile(hShmem);
CloseHandle(hShmem);

return0;
}
}

『柒』 利用C語言寫一個程序實現兩個進程間進行管道通信

#include <stdio.h>

#include <stdlib.h>

#include <errno.h>頃告帶

#include <雀蘆string.h>

#define N 10

#define MAX 100

int child_read_pipe(int fd)

{

char buf[N];

int n = 0;

while(1)

{

n = read(fd,buf,sizeof(buf));

buf[n] = '\0';

printf("Read %d bytes : %s.\n",n,buf);

if(strncmp(buf,"quit",4) == 0)

break;

}

return 0;

}

int father_write_pipe(int fd)

{

char buf[MAX] = {0};

while(1)

{

printf(">");

fgets(buf,sizeof(buf),stdin);

buf[strlen(buf)-1] = '\0';

write(fd,buf,strlen(buf));

usleep(500);

if(strncmp(buf,"quit"友悶,4) == 0)

break;

}

return 0;

}

int main()

{

int pid;

int fd[2];

if(pipe(fd) < 0)

{

perror("Fail to pipe");

exit(EXIT_FAILURE);

}

if((pid = fork()) < 0)

{

perror("Fail to fork");

exit(EXIT_FAILURE);

}else if(pid == 0){

close(fd[1]);

child_read_pipe(fd[0]);

}else{

close(fd[0]);

father_write_pipe(fd[1]);

}

exit(EXIT_SUCCESS);

}

閱讀全文

與編譯一段程序實現進程的管道通信相關的資料

熱點內容
mdk編譯後目標文件 瀏覽:613
老人動手解壓 瀏覽:720
小米sd卡解壓 瀏覽:996
程序員那麼可愛陸漓替老袁說情 瀏覽:28
當女程序員遇見問題 瀏覽:746
32位編譯器什麼意思 瀏覽:355
php多參數函數 瀏覽:17
通達信板塊動作源碼 瀏覽:751
matlab完全自學一本通pdf 瀏覽:250
php源碼本地安裝 瀏覽:961
伺服器怎麼用不會斷電 瀏覽:301
主從伺服器有什麼用 瀏覽:213
jstlpdf 瀏覽:15
安卓原神在哪個app下載 瀏覽:808
單片機編程技術什麼意思 瀏覽:104
e點課堂源碼 瀏覽:46
免費打擊墊app哪個好 瀏覽:532
程序員必裝的6款軟體 瀏覽:750
基於單片機的遙控器設計 瀏覽:521
安卓如何取消圓圖標 瀏覽:11