新聞中心
在操作系統(tǒng)中,進(jìn)程通信是非常重要的,而Linux系統(tǒng)的進(jìn)程通信方式有很多種,其中一種非常高效的方式就是通過(guò)管道進(jìn)行進(jìn)程間的數(shù)據(jù)交換。

站在用戶的角度思考問(wèn)題,與客戶深入溝通,找到呼中網(wǎng)站設(shè)計(jì)與呼中網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都做網(wǎng)站、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名注冊(cè)、網(wǎng)頁(yè)空間、企業(yè)郵箱。業(yè)務(wù)覆蓋呼中地區(qū)。
那么什么是管道呢?管道是一種進(jìn)程間通信方式,它只能用于有親緣關(guān)系的進(jìn)程之間的通信,即父子進(jìn)程或兄弟進(jìn)程之間的通信,管道將一個(gè)進(jìn)程的輸出和另一個(gè)進(jìn)程的輸入連接起來(lái),形成一個(gè)管道傳輸數(shù)據(jù),數(shù)據(jù)在管道中是一個(gè)流式的過(guò)程。
Linux系統(tǒng)中管道的創(chuàng)建和使用非常方便,使用系統(tǒng)調(diào)用pipe可以創(chuàng)建一個(gè)無(wú)名管道,它返回兩個(gè)文件描述符,一個(gè)代表管道的讀端,一個(gè)代表寫端,通過(guò)這兩個(gè)文件描述符,父子進(jìn)程或兄弟進(jìn)程可以進(jìn)行數(shù)據(jù)的讀寫。
使用管道進(jìn)行進(jìn)程通信的一個(gè)典型的例子是shell的管道符“|”,在shell中,一個(gè)命令的輸出通??梢酝ㄟ^(guò)管道符“|”連接到另一個(gè)命令的輸入,這時(shí)候之一個(gè)命令的輸出就會(huì)被傳遞給第二個(gè)命令進(jìn)行處理。
使用管道進(jìn)行進(jìn)程通信的好處是非常明顯的,首先它非常高效,因?yàn)楣艿朗腔趦?nèi)存的,數(shù)據(jù)在進(jìn)程間的傳遞非??焖?,其次管道可以支持單向流動(dòng)的數(shù)據(jù)傳輸,這就可以很方便地將一個(gè)進(jìn)程的輸出傳遞給另一個(gè)進(jìn)程進(jìn)行處理,極大地降低了進(jìn)程之間的耦合性。
在Linux系統(tǒng)中,管道不僅可以用于父子進(jìn)程或兄弟進(jìn)程之間的通信,還可以用于線程之間的通信,而且管道還可以進(jìn)行多路復(fù)用,這極大地方便了進(jìn)程之間的多路數(shù)據(jù)交換。
但是,使用管道進(jìn)行進(jìn)程通信也存在一些問(wèn)題,例如管道的容量有限,過(guò)大的數(shù)據(jù)無(wú)法進(jìn)行傳輸,而且當(dāng)寫入管道的數(shù)據(jù)量超過(guò)管道的容量時(shí),進(jìn)程會(huì)被阻塞,直到管道中的數(shù)據(jù)被消耗掉。
另外,管道的雙方必須同時(shí)存在,否則數(shù)據(jù)無(wú)法進(jìn)行傳輸,而且管道一旦被創(chuàng)建就無(wú)法刪除,這會(huì)導(dǎo)致一些占用管道資源的進(jìn)程退出之后,管道資源還是被占用的問(wèn)題。
總體來(lái)說(shuō),Linux系統(tǒng)中的管道是一種非常高效的進(jìn)程間通信方式,它具有快速、輕量、靈活等優(yōu)點(diǎn),但是也存在一些局限性,使用過(guò)程中需要注意管道容量的限制,以及管道資源無(wú)法被釋放的問(wèn)題。針對(duì)以上問(wèn)題,Linux系統(tǒng)還提供了其他的進(jìn)程通信方式,如共享內(nèi)存、消息隊(duì)列等,根據(jù)實(shí)際需求選擇適當(dāng)?shù)倪M(jìn)程通信方式,可以極大地提高程序的性能和可靠性。
相關(guān)問(wèn)題拓展閱讀:
- linux下c的兩個(gè)進(jìn)程如何實(shí)現(xiàn)通信?一個(gè)進(jìn)程給另一個(gè)進(jìn)程發(fā)送消息,另一個(gè)接受并顯示出來(lái)。求大神啊
linux下c的兩個(gè)進(jìn)程如何實(shí)現(xiàn)通信?一個(gè)進(jìn)程給另一個(gè)進(jìn)程發(fā)送消息,另一個(gè)接受并顯示出來(lái)。求大神啊
linux中的進(jìn)程通信分為三個(gè)部分:低級(jí)通信,管道通信和進(jìn)程間通信IPC(inter process communication)。linux的低級(jí)通信主要用來(lái)傳遞進(jìn)程的控制信號(hào)——文件鎖和軟中斷信號(hào)機(jī)制。linux的進(jìn)程間通信IPC有三個(gè)部分——①信號(hào)量,②共享內(nèi)存和③消息隊(duì)列。以下是我編寫的linux進(jìn)程通信的C語(yǔ)言實(shí)現(xiàn)代碼。操作系統(tǒng)為redhat9.0,編輯器為vi,編譯器采用gcc。下面所有實(shí)現(xiàn)代碼均已經(jīng)通過(guò)測(cè)試,運(yùn)行無(wú)誤。
一.低級(jí)通信–信號(hào)通信
signal.c
#include 鎮(zhèn)碰
#include
#include 御笑談
/*捕捉到信號(hào)sig之后,執(zhí)行預(yù)先預(yù)定的動(dòng)作函數(shù)*/
void sig_alarm(int sig)
{
printf(“—the signal received is %d. /n”, sig);
signal(SIGINT, SIG_DFL); //SIGINT終端中斷信號(hào),SIG_DFL:恢復(fù)默認(rèn)行為,SIN_IGN:忽略信號(hào)
}
int main()
{
signal(SIGINT, sig_alarm);//捕升賣捉終端中斷信號(hào)
while(1)
{
printf(“waiting here!/n”);
sleep(1);
}
return 0;
}
二.管道通信
pipe.c
#include
#define BUFFER_SIZE 30
int main()
{
int x;
int fd;
char buf;
char s;
pipe(fd);//創(chuàng)建管道
while((x=fork())==-1);//創(chuàng)建管道失敗時(shí),進(jìn)入循環(huán)
/*進(jìn)入子進(jìn)程,子進(jìn)程向管道中寫入一個(gè)字符串*/
if(x==0)
{
sprintf(buf,”This is an example of pipe!/n”);
write(fd,buf,BUFFER_SIZE);
exit(0);
}
/*進(jìn)入父進(jìn)程,父進(jìn)程從管道的另一端讀出剛才寫入的字符串*/
else
{
wait(0);//等待子進(jìn)程結(jié)束
read(fd,s,BUFFER_SIZE);//讀出字符串,并將其儲(chǔ)存在char s中
printf(“%s”,s);//打印字符串
}
return 0;
}
三.進(jìn)程間通信——IPC
①信號(hào)量通信
sem.c
#include
#include
#include
#include
#include
#include
/*聯(lián)合體變量*/
union semun
{
int val; //信號(hào)量初始值
struct semid_ds *buf;
unsigned short int *array;
struct seminfo *__buf;
};
/*函數(shù)聲明,信號(hào)量定義*/
static int set_semvalue(void); //設(shè)置信號(hào)量
static void del_semvalue(void);//刪除信號(hào)量
static int semaphore_p(void); //執(zhí)行P操作
static int semaphore_v(void); //執(zhí)行V操作
static int sem_id;//信號(hào)量標(biāo)識(shí)符
int main(int argc, char *argv)
{
int i;
int pause_time;
char op_char = ‘O’;
srand((unsigned int)getpid());
sem_id = semget((key_t)1234, 1, 0666 | IPC_CREAT);//創(chuàng)建一個(gè)信號(hào)量,IPC_CREAT表示創(chuàng)建一個(gè)新的信號(hào)量
/*如果有參數(shù),設(shè)置信號(hào)量,修改字符*/
if (argc > 1)
{
if (!set_semvalue())
{
fprintf(stderr, “Failed to initialize semaphore/n”);
exit(EXIT_FAILURE);
}
op_char = ‘X’;
sleep(5);
}
for(i = 0; i 1)
{
sleep(10);
del_semvalue(); //刪除信號(hào)量
}
exit(EXIT_SUCCESS);
}
/*設(shè)置信號(hào)量*/
static int set_semvalue(void)
{
union semun sem_union;
sem_union.val = 1;
if (semctl(sem_id, 0, SETVAL, sem_union) == -1)
return(0);
return(1);
}
/*刪除信號(hào)量*/
static void del_semvalue(void)
{
union semun sem_union;
if (semctl(sem_id, 0, IPC_RMID, sem_union) == -1)
fprintf(stderr, “Failed to delete semaphore/n”);
}
/*執(zhí)行P操作*/
static int semaphore_p(void)
{
struct sembuf sem_b;
sem_b.sem_num = 0;
sem_b.sem_op = -1; /* P() */
sem_b.sem_ = SEM_UNDO;
if (semop(sem_id, &sem_b, 1) == -1)
{
fprintf(stderr, “semaphore_p failed/n”);
return(0);
}
return(1);
}
/*執(zhí)行V操作*/
static int semaphore_v(void)
{
struct sembuf sem_b;
sem_b.sem_num = 0;
sem_b.sem_op = 1; /* V() */
sem_b.sem_ = SEM_UNDO;
if (semop(sem_id, &sem_b, 1) == -1)
{
fprintf(stderr, “semaphore_v failed/n”);
return(0);
}
return(1);
}
②消息隊(duì)列通信
send.c
#include
#include
#include
#include
#include
#include
#include
#include
#define MAX_TEXT 512
/*用于消息收發(fā)的結(jié)構(gòu)體–my_msg_type:消息類型,some_text:消息正文*/
struct my_msg_st
{
long int my_msg_type;
char some_text;
};
int main()
{
int running = 1;//程序運(yùn)行標(biāo)識(shí)符
struct my_msg_st some_data;
int msgid;//消息隊(duì)列標(biāo)識(shí)符
char buffer;
/*創(chuàng)建與接受者相同的消息隊(duì)列*/
msgid = msgget((key_t)1234, 0666 | IPC_CREAT);
if (msgid == -1)
{
fprintf(stderr, “msgget failed with error: %d/n”, errno);
exit(EXIT_FAILURE);
}
/*向消息隊(duì)列中發(fā)送消息*/
while(running)
{
printf(“Enter some text: “);
fgets(buffer, BUFSIZ, stdin);
some_data.my_msg_type = 1;
strcpy(some_data.some_text, buffer);
if (msgsnd(msgid, (void *)&some_data, MAX_TEXT, 0) == -1)
{
fprintf(stderr, “msgsnd failed/n”);
exit(EXIT_FAILURE);
}
if (strncmp(buffer, “end”, 3) == 0)
{
running = 0;
}
}
exit(EXIT_SUCCESS);
}
receive.c
#include
#include
#include
#include
#include
#include
#include
#include
/*用于消息收發(fā)的結(jié)構(gòu)體–my_msg_type:消息類型,some_text:消息正文*/
struct my_msg_st
{
long int my_msg_type;
char some_text;
};
int main()
{
int running = 1;//程序運(yùn)行標(biāo)識(shí)符
int msgid; //消息隊(duì)列標(biāo)識(shí)符
struct my_msg_st some_data;
long int msg_to_receive = 0;//接收消息的類型–0表示msgid隊(duì)列上的之一個(gè)消息
/*創(chuàng)建消息隊(duì)列*/
msgid = msgget((key_t)1234, 0666 | IPC_CREAT);
if (msgid == -1)
{
fprintf(stderr, “msgget failed with error: %d/n”, errno);
exit(EXIT_FAILURE);
}
/*接收消息*/
while(running)
{
if (msgrcv(msgid, (void *)&some_data, BUFSIZ,msg_to_receive, 0) == -1)
{
fprintf(stderr, “msgrcv failed with error: %d/n”, errno);
exit(EXIT_FAILURE);
}
printf(“You wrote: %s”, some_data.some_text);
if (strncmp(some_data.some_text, “end”, 3) == 0)
{
running = 0;
}
}
/*刪除消息隊(duì)列*/
if (msgctl(msgid, IPC_RMID, 0) == -1)
{
fprintf(stderr, “msgctl(IPC_RMID) failed/n”);
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
③共享內(nèi)存通信
share.h
#define TEXT_SZ 2023 //申請(qǐng)共享內(nèi)存大小
struct shared_use_st
{
int written_by_you; //written_by_you為1時(shí)表示有數(shù)據(jù)寫入,為0時(shí)表示數(shù)據(jù)已經(jīng)被消費(fèi)者提走
char some_text;
};
producer.c
#include
#include
#include
#include
#include
#include
#include
#include “share.h”
int main()
{
int running = 1; //程序運(yùn)行標(biāo)志位
void *shared_memory = (void *)0;
struct shared_use_st *shared_stuff;
char buffer;
int shmid; //共享內(nèi)存標(biāo)識(shí)符
/*創(chuàng)建共享內(nèi)存*/
shmid = shmget((key_t)1234, sizeof(struct shared_use_st), 0666 | IPC_CREAT);
if (shmid == -1)
{
fprintf(stderr, “shmget failed/n”);
exit(EXIT_FAILURE);
}
/*將共享內(nèi)存連接到一個(gè)進(jìn)程的地址空間中*/
shared_memory = shmat(shmid, (void *)0, 0);//指向共享內(nèi)存之一個(gè)字節(jié)的指針
if (shared_memory == (void *)-1)
{
fprintf(stderr, “shmat failed/n”);
exit(EXIT_FAILURE);
}
printf(“Memory attached at %X/n”, (int)shared_memory);
shared_stuff = (struct shared_use_st *)shared_memory;
/*生產(chǎn)者寫入數(shù)據(jù)*/
while(running)
{
while(shared_stuff->written_by_you == 1)
{
sleep(1);
printf(“waiting for client…/n”);
}
printf(“Enter some text: “);
fgets(buffer, BUFSIZ, stdin);
strncpy(shared_stuff->some_text, buffer, TEXT_SZ);
shared_stuff->written_by_you = 1;
if (strncmp(buffer, “end”, 3) == 0)
{
running = 0;
}
}
/*該函數(shù)用來(lái)將共享內(nèi)存從當(dāng)前進(jìn)程中分離,僅使得當(dāng)前進(jìn)程不再能使用該共享內(nèi)存*/
if (shmdt(shared_memory) == -1)
{
fprintf(stderr, “shmdt failed/n”);
exit(EXIT_FAILURE);
}
printf(“producer exit./n”);
exit(EXIT_SUCCESS);
}
customer.c
#include
#include
#include
#include
#include
#include
#include
#include “share.h”
int main()
{
int running = 1;//程序運(yùn)行標(biāo)志位
void *shared_memory = (void *)0;
struct shared_use_st *shared_stuff;
int shmid; //共享內(nèi)存標(biāo)識(shí)符
srand((unsigned int)getpid());
/*創(chuàng)建共享內(nèi)存*/
shmid = shmget((key_t)1234, sizeof(struct shared_use_st), 0666 | IPC_CREAT);
if (shmid == -1)
{
fprintf(stderr, “shmget failed/n”);
exit(EXIT_FAILURE);
}
/*將共享內(nèi)存連接到一個(gè)進(jìn)程的地址空間中*/
shared_memory = shmat(shmid, (void *)0, 0);//指向共享內(nèi)存之一個(gè)字節(jié)的指針
if (shared_memory == (void *)-1)
{
fprintf(stderr, “shmat failed/n”);
exit(EXIT_FAILURE);
}
printf(“Memory attached at %X/n”, (int)shared_memory);
shared_stuff = (struct shared_use_st *)shared_memory;
shared_stuff->written_by_you = 0;
/*消費(fèi)者讀取數(shù)據(jù)*/
while(running)
{
if (shared_stuff->written_by_you)
{
printf(“You wrote: %s”, shared_stuff->some_text);
sleep( rand() % 4 );
shared_stuff->written_by_you = 0;
if (strncmp(shared_stuff->some_text, “end”, 3) == 0)
{
running = 0;
}
}
}
/*該函數(shù)用來(lái)將共享內(nèi)存從當(dāng)前進(jìn)程中分離,僅使得當(dāng)前進(jìn)程不再能使用該共享內(nèi)存*/
if (shmdt(shared_memory) == -1)
{
fprintf(stderr, “shmdt failed/n”);
exit(EXIT_FAILURE);
}
/*將共享內(nèi)存刪除,所有進(jìn)程均不能再訪問(wèn)該共享內(nèi)存*/
if (shmctl(shmid, IPC_RMID, 0) == -1)
{
fprintf(stderr, “shmctl(IPC_RMID) failed/n”);
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
摘自:
關(guān)于linux進(jìn)程管道通信的介紹到此就結(jié)束了,不知道你從中找到你需要的信息了嗎 ?如果你還想了解更多這方面的信息,記得收藏關(guān)注本站。
香港服務(wù)器選創(chuàng)新互聯(lián),2H2G首月10元開(kāi)通。
創(chuàng)新互聯(lián)(www.cdcxhl.com)互聯(lián)網(wǎng)服務(wù)提供商,擁有超過(guò)10年的服務(wù)器租用、服務(wù)器托管、云服務(wù)器、虛擬主機(jī)、網(wǎng)站系統(tǒng)開(kāi)發(fā)經(jīng)驗(yàn)。專業(yè)提供云主機(jī)、虛擬主機(jī)、域名注冊(cè)、VPS主機(jī)、云服務(wù)器、香港云服務(wù)器、免備案服務(wù)器等。
分享題目:Linux進(jìn)程管道通信:高效實(shí)現(xiàn)進(jìn)程間數(shù)據(jù)交換(linux進(jìn)程管道通信)
轉(zhuǎn)載來(lái)于:http://fisionsoft.com.cn/article/djsgsge.html


咨詢
建站咨詢
