Linux applet for interprocess communication

  • 2020-05-10 23:19:54
  • OfStack

After work, I wrote an assignment for a friend about interprocess communication. The topic is as follows:

"The parent process takes 1000 pieces of data from the keyboard, sums them up to sum1, the child process squares the 1000 Numbers and sum2, and the result is passed to the parent process, which prints the result after sum1+sum2."

Requirements: use a Shared area of size 10 to pass 1000 data; The child process passes sum2 to the parent process using the message mechanism.  

It mainly USES Shared memory to realize inter-process communication, and USES pipes to realize inter-process competition. FreeBSD passed the test. The code is as follows: time is limited, there may be some deficiencies, hope the master to give advice.


#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;
}

The above is the entire content of this article, I hope to help you with your study.


Related articles: