Question

In: Computer Science

Write a program in C or C++ that spawns three child processes. The first child sends...

Write a program in C or C++ that spawns three child processes. The first child sends a signal to the parent (you'll need the parent's pid!), which the parent shall catch. The second child waits for 10 seconds and then terminates. Once the parent detects that both of these has happened, it should signal the third child to terminate.

Solutions

Expert Solution

#include <stdio.h>    /* printf                       */
#include <stdlib.h>   /* exit                         */
#include <string.h>   /* strcpy                       */
#include <unistd.h>   /* sleep, fork                  */
#include <sys/shm.h>  /* shmget, shmat, shmdt, shmctl */
#include <sys/wait.h> /* wait                         */

int main(void) 
{
  int pid1,pid2,pid3;
  int shmid;       /* return value from fork/shmget */
  char *buffer;    /* shared buffer                 */
  key_t key = 123; /* arbitrary key                 */
  
  shmid = shmget(key, 1024, 0600 | IPC_CREAT);
  buffer = (char *) shmat(shmid, NULL, 0);
  strcpy(buffer,"");
  
  pid1 = fork();
 

  
  if (pid1 == 0) /* child */
  {
    printf("child: putting message in buffer\n");
    strcpy(buffer, "type any message here.");
    shmdt(buffer); /* detach memory from child process */

    printf("child: sleeping for 5 seconds...\n");
    sleep(5);    
    printf("child: exiting\n");
    exit(0); 
  }
  else if (pid1 > 0) /* parent */
  {
     
    printf("parent: waiting for child to exit...\n");
    wait(NULL);
    printf("parent: message from child is %s\n", buffer);
       pid2 = fork();
     if (pid2 == 0) /*  2nd child */
     {
        printf(" second child: sleeping for 1o seconds...\n");
               sleep(10);
           printf(" second child: exiting\n");       
           exit(0); 
     }
     else 
     {
       pid3 = fork();
        if (pid3 == 0) /* 3rd child */
           {
  printf("child 3: sleeping for 15 seconds...\n");
  sleep(15);
  printf("child 3: exiting\n");
  exit(0);
  }

     
      
    shmdt(buffer);              /* detach memory from parent process */
    shmctl(shmid, IPC_RMID, 0); /* delete memory block               */
    
    printf("parent: exiting\n");
  }


  
  return 0;
}

Related Solutions

Write a C program that creates a toy scheduler for the child processes in part A....
Write a C program that creates a toy scheduler for the child processes in part A. This program takes as input the number of processes, and all of the PIDs that are being echoed. HINT: Look up redirecting echo output. The program will schedule the ”processes” (note that these are not true processes, this is a toy system. You are effectively only scheduling echo statements). The result of the scheduler will be to echo the PID and current system time...
C programming language The program first creates a child process CP. So, there are two processes:...
C programming language The program first creates a child process CP. So, there are two processes: The parent process does the following: a. compute the summary of all the even number from 1, 2, .. 1000, and output this summary; b. wait for the termination of the child process CP, then terminate; The child process does the following: a. compute the summary of all the odd number from 1, 2, .. 1000, and output this summary; b. terminates;
In C language, Write a program called minishell that creates two child processes: one to execute...
In C language, Write a program called minishell that creates two child processes: one to execute 'ls -al' and the other to execute ‘grep minishell.c’. After the forks, the original parent process waits for both child processes to finish before it terminates. The parent should print out the pid of each child after it finishes. The standard output of 'ls -al' process should be piped to the input to the 'grep minishell.c' process. Make sure you close the unnecessary open...
Write a C program that creates 5 threads sends the thread index as an argument to...
Write a C program that creates 5 threads sends the thread index as an argument to the thread execution procedure/function. Also, the main process/thread joins the newly created threads sequentially one after the other. From the thread procedure print “I am a thread and my index is “ [print the correct index number]. From the main thread after the join print “I am the main thread and just completed joining thread index “ [print the correct index].
You will write a C program to fork a child process. The child process will print...
You will write a C program to fork a child process. The child process will print the current time/date. The parent will wait for the child to complete. Here is how you can programmatically call the Linux date command from a C program. execlp("/bin/date","date",NULL); Your C file should be named myfork.c The binary will be named myfork.
Write a C# program that prompts the user to enter in three values. The first indicates...
Write a C# program that prompts the user to enter in three values. The first indicates a starting (whole number positive) value, the second an ending value and the third an increase value. output a table of squares and square roots, every increase value from the start to the end value. For instance if the values were: start 3, end 20, and increase 4. the table would consist of 3,7,11,15,19 and their squares and square roots. Please include all code...
For your first project, write a C program (not a C++ program!)that will read in a...
For your first project, write a C program (not a C++ program!)that will read in a given list of non-negative integers and a target integer and checks if there exist two integers in the list that sum up to the target integer. Example:List: 31, 5, 8, 28, 15, 21, 11, 2 Target: 26 Yes!, 44 No! your C program will contain the following: •Write a function that will make a copy of the values from one array to another array....
Write a program in C++ that generates and displays the first N three digit odd numbers....
Write a program in C++ that generates and displays the first N three digit odd numbers. Whereas the number N is provided by the user.
write this program in C++ Write a program that prompts a user for three characters. The...
write this program in C++ Write a program that prompts a user for three characters. The program must make sure that the input is a number 10 - 100 inclusive. The program must re prompt the user until a correct input is entered. Finally output the largest and the lowest value. Example 1: Input : 10 Input : 20 Input : 30 The largest is 30. The lowest is 10. Example 2: Input : 100 Input : 50 Input :...
Write a C program to synchronize multiple producer processes and multiple consumer processes using monitor
Write a C program to synchronize multiple producer processes and multiple consumer processes using monitor
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT