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...
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 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 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 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
C++ DO not use arrays to write this program. Write a program that repeatedly generates three...
C++ DO not use arrays to write this program. Write a program that repeatedly generates three random integers in the range [1, 100] and continues as follows: If the right-most digit of all the three integers is equal, the program displays them in ascending order on the screen and continues. If the generated integers have different right-most digits, they are not displayed and the program continues. The program terminates once the right-most digits of all the three random numbers are...
Write a program (in C, or Java, or C++, or C#) that creates three new threads...
Write a program (in C, or Java, or C++, or C#) that creates three new threads (besides the already existing main thread) and synchronizes them in such a way that each thread displays it's thread id in turn for 5 iterations. The output of the program should look like this: Thread 1 - iteration no. 1 Thread 2 - iteration no. 1 Thread 3 - iteration no. 1 Thread 1 - iteration no. 2 Thread 2 - iteration no. 2...
Develop and write a program in c/c++ that will find the first 100 primes (starting at...
Develop and write a program in c/c++ that will find the first 100 primes (starting at 1) and write them to a file named "primes.dat".
Write in C++ Write a program that accepts the names of three political parties and the...
Write in C++ Write a program that accepts the names of three political parties and the number of votes each received in the last mayoral election. Display the percentage of the vote each party received.   Be sure to provide labels (party name) and number-align your output values.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT