In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
The knowledge of this article "how to solve the Linux Mini Program problem of inter-process communication" is not quite understood by most people, so the editor summarizes the following contents, detailed contents and clear steps, which can be used for reference. I hope you can get something after reading this article. Let's take a look at this "how to solve the Linux Mini Program problem of inter-process communication" article.
The topics are as follows:
"the parent process accepts 1000 pieces of data from the keyboard, sum1 it, and the child process sum2 the sum of 1000 squares. The result is passed to the parent process, and the parent process will sum1+sum2 and print the result."
Requirements: 1000 data are passed with a shared area of size 10; the child process uses a message mechanism to pass the sum2 to the parent process.
It mainly uses shared memory to achieve inter-process communication, uses pipes to achieve inter-process competition, and passes the test under freebsd. The code is as follows: time is limited, there may be some deficiencies, hope the master to give guidance.
# include # include const int key = 0x12345678 static int pfd1 [2], pfd2 [2]; # define shm_len (101024) # define val_num 5int 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; iTunes +) {printf ("please input a num:"); scanf ("% d", val + I);}} void show_val (int * val, int num) {int i; for (I = 0; I < num; iTunes +) {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 [_ 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 } the above is the content of this article entitled "how to solve the Linux Mini Program problem of Inter-process Communication". I believe you all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.