您好,登錄后才能下訂單哦!
這篇文章主要介紹“Linux進程間如何通信”,在日常操作中,相信很多人在Linux進程間如何通信問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Linux進程間如何通信”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
題目如下:
“父進程從鍵盤上接受1000個數據,對其求和sum1,子進程對這1000個數平方和sum2,結果傳給父進程,父進程將sum1+sum2后,打印結果。”
要求:用大小為10的共享區傳遞1000個數據;子進程用消息機制將sum2傳給父進程。
主要利用共享內存實現進程間通信,使用管道實現進程間競爭關系,freebsd下測試通過。代碼如下:時間有限,有可能有些不足,希望高手給予指點。
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/shm.h> #include <signal.h> const int key = 0x12345678; static int pfd1[2], pfd2[2]; #define shm_len (10*1024) #define val_num 5 int init_shm() { int shmid = -1; shmid = shmget((key_t)key, shm_len, 0666 | ipc_creat); if (shmid < 0) { printf("shmget failed!\n"); exit(-1); } return shmid; } void cancel_shm(int shmid) { if (shmctl(shmid, ipc_rmid, 0) == -1) { printf("shmctl failed!\n"); exit(-1); } printf("cancel_shm successfully!\n"); } void *shm_get(int shmid) { void *mem = null; mem = shmat(shmid, 0, 0); if (mem == (void *)-1) { printf("shmat failed!\n"); exit(-1); } return mem; } int get_val(int *val, int num) { int i; for (i = 0; i < num; i++) { printf("please input a num:"); scanf("%d", val + i); } } void show_val (int *val, int num) { int i; for (i = 0; i < num; i++) { printf("%d\t", *(val + i)); } printf("\n"); } int add_val (int *val, int num) { int result = 0; int i; for (i = 0; i < num; i++) { result += *(val + i); } return result; } int square_val (int *val, int num) { int result = 0; int i, tmp; for (i = 0; i < num; i++) { tmp = *(val + i); result += (tmp * tmp); } return result; } void tell_wait (void) { if (pipe(pfd1) < 0 || pipe(pfd2) < 0) { printf("pipe error!\n"); exit(-1); } } void tell_parent (pid_t pid) { if (write(pfd2[1], "c", 1) != 1) { printf("write error!\n"); exit(-1); } } void wait_parent (void) { char c; if (read(pfd1[0], &c, 1) != 1) { printf("read error!\n"); exit(-1); } } void tell_child (pid_t pid) { if (write(pfd1[1], "p", 1) != 1) { printf("write error!\n"); exit(-1); } } void wait_child (void) { char c; if (read(pfd2[0], &c, 1) != 1) { printf("read error!\n"); exit(-1); } } int main(int argc, char *argv[]) { void *mem = null; int shmid = -1; pid_t pid = -1; int val[val_num]; int result = 0; shmid = init_shm(); tell_wait(); if ((pid = fork()) < 0) { //error printf("fork error!\n"); exit(-1); } else if (pid == 0) { //child int result = 0; wait_parent(); mem = shm_get(shmid); //get share memery memcpy(val, mem, sizeof(int) * val_num); result = square_val(val, val_num); *(int *)((void *)mem + shm_len - 4) = result; tell_parent(pid); exit(1); } else { //parent int child_result = 0; mem = shm_get(shmid); //get share memery get_val(val, val_num); //get user input memcpy(mem, val, sizeof(int) * val_num); //copy user input to share memery tell_child(pid); result = add_val(val, val_num); wait_child(); child_result = *(int *)((void *)mem + shm_len - 4); printf("result:%d, child_result:%d, all:%d\n", result, child_result, result + child_result); } cancel_shm(shmid); return 0; }
到此,關于“Linux進程間如何通信”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。