Question

In: Computer Science

Write a C or C++ program using the fork() system call function. You will need to...

Write a C or C++ program using the fork() system call function. You will need to create 3 processes – each process will perform a simple task.

Firstly, create an integer "counter" initialized to a random value between 1 and 100. Print this number to the console. This can be done by:

  • Including the stdio.h and stdlib.h libraries

  • Using the rand() function to generate your randomly generated number

The main thread consists of the parent process. Your job is to create 3 child processes, as follow:

  • Process 1 prints out its current process ID and its parent process ID

    • The parent process will wait until this child is completed using the wait() function

  • Process 2 will create a for loop that increments the "counter" variable by 1, ten times. Print out the counter for each iteration.

    • The parent process will not wait for this child process to end, but instead it will print out the counter value to show that the child process does not share memory with the parent process.

    • Note: There’s a chance the code will execute out of order because the parentprocess does not wait for the child process.

  • Process 3 will create a for loop that increments the "counter" variable by 1, ten times. Print out the counter for each iteration.

    • The parent process will not wait for this child process and instead, the parent process will kill the child with the kill command.

    • Note: This is to show that this will either not run at all or only partially run due to being killed manually.

To run this CPP program on Unix or Linux, type: g++ prog1.cpp

Solutions

Expert Solution

#include<stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include<stdlib.h>
#include<sys/wait.h>
int main()
{
//counter init
int counter =rand()%100+1;
printf("Counter:%d\n",counter);
//creates process 1
int pid1=fork();
if(pid1==0)//child
{
printf("Process ID:%d\nParnetID:%d\n",getpid(),getppid());
  
}
else{
wait(NULL);
//create proccess 2
int pid2=fork();
if(pid2==0)
{
for(int i=0;i<10;++i)
{
printf("Process 2: Counter:%d\n",counter);
counter++;
}
}
else{
//print counter
printf("Parent Process: Counter:%d\n",counter);
//create process 3
int pid3=fork();
if(pid3==0)
{
for(int i=0;i<10;++i)
{
printf("Process 3: Counter:%d\n",counter);
counter++;
}
}
else{
kill(pid3,SIGKILL);}
}
}
  
}
  

  


COMMENT DOWN FOR ANY QUERIES AND ,

LEAVE A THUMBS UP IF THIS ANSWER HELPS YOU.


Related Solutions

11. A fork() function call is a system call. true or false 12. A fork() function...
11. A fork() function call is a system call. true or false 12. A fork() function call in a user program is executed in user mode, not in kernel mode. true or false 13. Alice writes a high-level program using some programming language on her home desktop computer and using compilation it transforms into an executable file. Bob's home desktop computer will not execute Alice's executable file. What language was Alice's program written with? 14. Alice rewrites her high-level program...
Can you solve this C program by using Function? Q1. Write a C program to ring...
Can you solve this C program by using Function? Q1. Write a C program to ring the computer bell at any number of times you specify. Use the system clock as a delay, you need to include the time header file.
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 Write a function to swap two elements of an integer array. Call...
Write a c program Write a function to swap two elements of an integer array. Call the function to swap the first element, i[0] with last element i[n], second element i[1] with the last but one element i[n-1] and so on. Should handle arrays with even and odd number of elements. Call the swap function with the following arrays and print results in each case before and after swapping. i. int arr1[] = {0, 1, 2, 3, 30, 20, 10,...
USING PYTHON Write a program to create a number list. It will call a function to...
USING PYTHON Write a program to create a number list. It will call a function to calculate the average values in the list. Define main ():                        Declare variables and initialize them                        Create a list containing numbers (int/float)                        Call get_avg function that will return the calculated average values in the list.                                       Use a for loop to loop through the values in the list and calculate avg                        End main()
Write a C program using system call I/O to a) open an existing text file passed...
Write a C program using system call I/O to a) open an existing text file passed to your program as a command line argument, then b) display the content of the file, c) ask the user what information he/she wants to append d) receive the info from the user via keyboard e) append the info received in d) to the end of the file f) display the updated content of the file
For this assignment, you need to write a parallel program in C++ using OpenMP for vector...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector addition. Assume A, B, C are three vectors of equal length. The program will add the corresponding elements of vectors A and B and will store the sum in the corresponding elements in vector C (in other words C[i] = A[i] + B[i]). Every thread should execute an approximately equal number of loop iterations. The only OpenMP directive you are allowed to use is:...
Write a program for hotel booking system using C++ Program Requirement 1. You can write any...
Write a program for hotel booking system using C++ Program Requirement 1. You can write any program based on the title assigned. 2. The program must fulfill ALL the requirements below. The requirements listed below are the MINIMUM requirement. Your program may extend beyond the requirements if needed. a) Create at least one (1) base class. b) Create at least two (2) derived classes that inherit from the base class created in 2(a). c) Create at least one (1) object...
write C++ program using functions (separate function for each bottom) Write a program to find if...
write C++ program using functions (separate function for each bottom) Write a program to find if a number is large word for two given bottom base - bottom1 and bottom2. You can predict that a number, when converted to any given base shall not exceed 10 digits. . the program should ask from user to enter a number that it should ask to enter the base ranging from 2 to 16 after that it should check if the number is...
Using c++, write a program that will display your name as a void function then will...
Using c++, write a program that will display your name as a void function then will perform the following by user-defined functions: a. to compute for the sum of two numbers (n1, n2) using function.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT