Advertisement
m027

snoopy

Jun 25th, 2022
626
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.58 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #include <time.h>
  5. #include <unistd.h>
  6. #include<sys/wait.h>
  7. #include <signal.h>
  8. #include <sys/ipc.h>
  9. #include <sys/msg.h>
  10. #include <stdbool.h>
  11.  
  12. #define MSGSIZE 100
  13. //creating structure for message queue
  14. struct message_queue
  15. {
  16.     long msg_type;
  17.     char msg_txt[100];
  18. }message;
  19.  
  20. bool loop = true;
  21.  
  22. void interrupt_handler(int signal) {
  23.    
  24.     if(signal == SIGINT)
  25.     {
  26.         loop = false;
  27.     }
  28. }
  29. //driver program
  30. int main() {
  31.    
  32.     //variables
  33.     char *ptr1;
  34.     char str[10];
  35.     FILE *ptr;
  36.     int returncode_msgrcv;
  37.    
  38.     //creating messsage queue
  39.     key_t key = ftok("number.txt", 65);
  40.     int msgid;
  41.    
  42.     msgid = msgget(key, 0666 | IPC_CREAT);
  43.     message.msg_type = 10;
  44.    
  45.     while (loop == true) {
  46.        
  47.         //opening the file
  48.         ptr = fopen("./numbers.txt","w+");
  49.        
  50.         //use of space after each number
  51.         char delim[] = " ";
  52.        
  53.         // CONV //
  54.        
  55.         //random number generations
  56.         srand(time(NULL));
  57.         int num = 0;
  58.         int sum = 0;
  59.         int mean = 0;
  60.        
  61.         //child process
  62.         int process1 = fork();
  63.         if(process1 == 0) {
  64.             wait(NULL);
  65.        
  66.             //getting ID
  67.        
  68. //            printf("Child process 1 ID:%d\n", getpid());
  69.        
  70.             //reading random numbers
  71.             srand(time(NULL));
  72.             for(int i=1; i<=10; i++) {
  73.                 fprintf(ptr, "%d ", rand()%11);
  74.             }
  75.             printf("\n");
  76.             printf("Generated numbers in CONV: %d", ptr);
  77.             printf("\n");
  78.             fclose(ptr);
  79.             fflush(NULL);
  80.             return 0;
  81.        
  82.             //checking if message was sent into MQ
  83.             if (msgsnd(msgid, &message, sizeof(message.msg_txt), 0) == -1){
  84.                 printf("message not sent\n");
  85.                 perror("msgsnd()");
  86.             }
  87.         }
  88.        
  89.             // LOG //
  90.        
  91.         //waiting queue
  92.         wait(NULL);
  93.            
  94.         //new process
  95.         int process2 = fork();
  96.         if(process2 == 0) {
  97.        
  98.             if ((msgid = msgget(key, 0666 | IPC_CREAT)) == -1) {
  99.                 printf("could not reach Message queue\n");
  100.                 perror("msgget()");
  101.             }
  102.        
  103.             returncode_msgrcv = msgrcv(msgid, &message, sizeof(message.msg_txt), message.msg_type, MSG_NOERROR|IPC_NOWAIT);
  104.             if (returncode_msgrcv == -1) {
  105.                 printf("message not received\n");
  106.                 perror("msgrcv()");
  107.            
  108.             }else{
  109.                 printf("Child process 2 ID:%d\n",getpid());
  110.                 printf("\nWrote 10 integers to numbers.txt\n");
  111.             }
  112.        
  113.             //opening the numbers generated in the file
  114.             ptr = fopen("./numbers.txt","rb");
  115.             if( fgets (str, 100, ptr)!=NULL ) {
  116.                 ptr1 = strtok(str, delim);
  117.             }
  118.         }
  119.        
  120.         // STAT //
  121.        
  122.         //    int process3 = fork();
  123.         //    if (process3 == 0) {
  124.        
  125.             printf("\n");
  126. //            printf("Child process 3 ID:%d\n",getpid());
  127. //
  128.             //addition and average
  129.             while(ptr1 != NULL) {
  130.                 num = atoi(ptr1);
  131.                 sum = sum + num;
  132.                 mean = sum/10;
  133.                 ptr1 = strtok(NULL, delim);
  134.             }
  135.        
  136.        
  137.         printf("Generated in REPORT: sum %d, mean %d \n", sum, mean);
  138.         printf("\n");
  139.    
  140.         //    }
  141.         return 0;
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement