Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.75 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/stat.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <sys/wait.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <ctype.h>
  9. #include <time.h>
  10.  
  11. static int p2cpipe[2]; /* This descriptor is used for parent to child comm. */
  12. static int c2ppipe[2]; /* This descriptor is used for child to parenpipt comm. */
  13. static char text[255];
  14. struct tm *info;
  15. char buff[255];
  16.  
  17. /**
  18.  * This is the parent process.  It will act as the parent when executing.
  19.  * @param pid This is the process ID of the spawned child process.
  20.  */
  21. void parentProcess(pid_t pid, int numRuns) {
  22.     int index;
  23.     // Initialize the buffer.
  24.     memset(&text[0], 0, 255);
  25.     // CLose the pipes we will not be needing.
  26.     close(p2cpipe[0]);
  27.     close(c2ppipe[1]);
  28.     /* Must be the master process. */
  29.     for (index = 0; index < numRuns; index++) {
  30.         time_t rawtime;
  31.         time(&rawtime);
  32.         info = gmtime(&rawtime);   
  33.         strftime(buff, sizeof(buff), "GMT: %a %b %d %T %Y", info);
  34.         printf("%s\n", buff);
  35.         /* Write to the pipe. */
  36.         write(p2cpipe[1], &info, sizeof(info));
  37.         // Wait for the child process to respond to our message.
  38.         read(c2ppipe[0], &text[0], 255);
  39.         printf("%s\n", &text[0]);
  40.         sleep(1);
  41.     }
  42. }
  43.  
  44. /**
  45.  * This is the child process.  It will perform the role of the child in the program.
  46.  */
  47. void childProcess(int numRuns, int offset) {
  48.     /* PID = 0.  Must be the slave process. */
  49.     // Initialize the buffer to be empty.
  50.     memset(&text[0], 0, 255);
  51.     // Close the pipes this process will not need.
  52.     close(p2cpipe[1]);
  53.     close(c2ppipe[0]);
  54.  
  55.     for (int i = 0; i < numRuns; i++) {
  56.         /* Read from the pipe. */
  57.         read(p2cpipe[0], &info, sizeof(info));
  58.         info->tm_hour += offset;
  59.         mktime(info);
  60.         /* Now that there is text, print it out. */
  61.         strftime(buff, sizeof(buff), "%a %b %d %T %Y", info);
  62.         char offset_formatted[255];
  63.         if(offset < 0) {
  64.             sprintf(offset_formatted, "- %i", (offset*-1));
  65.         } else {
  66.             sprintf(offset_formatted, "+ %i", offset);
  67.         }
  68.         printf("GMT %s: %s\n", offset_formatted, buff);
  69.         // Write the data into the pipe so that the parent receives it.
  70.         write(c2ppipe[1], "OK", 255);
  71.     }
  72. }
  73.  
  74. int main(int argc, char*argv[]) {
  75.     /* the process id for the given set of processes. */
  76.     pid_t pid;
  77.  
  78.     /* Initialize the pipes between processes. Note: We need two pipes because a pipe is unidirectional when defined this way.*/
  79.     pipe(p2cpipe);
  80.     pipe(c2ppipe);
  81.  
  82.     /* Fork the new processes. */
  83.     pid = fork();
  84.  
  85.     if (pid < 0) {
  86.         printf("Fork failed.\n");
  87.         exit(-1);
  88.     } else if (pid == 0) {
  89.         /* PID = 0.  Must be the slave process. */
  90.       childProcess(atoi(argv[1]), atoi(argv[2]));
  91.     } else {
  92.       parentProcess(pid, atoi(argv[1]));
  93.         /* Wait for the child process to complete. */
  94.         waitpid(pid, NULL, 0);
  95.     }
  96.     return EXIT_SUCCESS;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement