#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
void ChildProcess();
void ParentProcess();
int main()
{
pid_t pid;
pid = fork();
if (pid == 0)
{
ChildProcess();
}
else
{
ParentProcess();
}
}
void ChildProcess()
{
printf("Running Child Process\n");
}
void ParentProcess()
{
printf("Running Parent Process\n");
}
The aim of this activity is to build a Arithmetic logic Unit in which each process will work over different arithmetic operations. Consider a process name creator that spawns multiple child processes. Creator will span four processes one for addition one for subtraction one multiplication and one for Division. After all the child processes are created their creator shall terminate, and each of the children should be in a wait/pause state. Implement the process as a C Program.
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
int main(){
pid_t child_a, child_b,child_c,child_d;
child_a = fork();
if (child_a == 0) {
printf("Created Child PID = %d for Multiplication \n",getpid());
pause();
}
else {
child_b = fork();
if (child_b == 0) {
printf("Created Child PID = %d for Division \n",getpid());
pause();
}
else {
child_c = fork();
if (child_c == 0) {
printf("Created Child PID = %d for Addition \n",getpid());
pause();
}
else {
child_d = fork();
if (child_d == 0) {
printf("Created Child PID = %d for Substraction \n",getpid());
pause();
}
else {
exit(0);
}
}
}
}
}
Implement the C program in which main program accepts the integers to be sorted. Main program uses the fork system call to create a new process called a child process. Parent process sorts the integers using bubble sort and waits for child process using wait system call to sort the integers using merge sort.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
// Function to perform Bubble Sort
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap elements
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
// Function to merge two subarrays
void merge(int arr[], int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;
int leftArr[n1], rightArr[n2];
for (int i = 0; i < n1; i++)
leftArr[i] = arr[left + i];
for (int j = 0; j < n2; j++)
rightArr[j] = arr[mid + 1 + j];
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (leftArr[i] <= rightArr[j]) {
arr[k] = leftArr[i];
i++;
} else {
arr[k] = rightArr[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = leftArr[i];
i++;
k++;
}
while (j < n2) {
arr[k] = rightArr[j];
j++;
k++;
}
}
// Function to perform Merge Sort
void mergeSort(int arr[], int left, int right) {
if (left < right) {
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n], arr_copy[n];
printf("Enter the elements: ");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
arr_copy[i] = arr[i]; // Copying array for Merge Sort
}
pid_t pid = fork();
if (pid < 0) {
printf("Fork failed!\n");
return 1;
}
if (pid == 0) {
// Child Process: Perform Merge Sort
printf("Child process (PID: %d) performing Merge Sort...\n", getpid());
mergeSort(arr_copy, 0, n - 1);
printf("Sorted array (Merge Sort): ");
for (int i = 0; i < n; i++)
printf("%d ", arr_copy[i]);
printf("\n");
} else {
// Parent Process: Perform Bubble Sort
bubbleSort(arr, n);
printf("Parent process (PID: %d) performing Bubble Sort...\n", getpid());
printf("Sorted array (Bubble Sort): ");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
wait(NULL); // Parent waits for child to complete
printf("Child process finished execution. Parent terminating.\n");
}
return 0;
}
clean
#include <rtx51tny.h>
#include <reg51.h>
#define MAX_PARTICLES 10000
sbit LED_PIN = P1^0;
sbit DOOR_PIN = P1^1;
volatile unsigned int particle_count = 0;
void task0 (void) _task_ 0 {
os_create_task(0);
os_create_task(1); // Task for continuous signal
os_create_task(2); // Task for blinking LED
os_create_task(3); // Task for serial communication
os_create_task(4); // Task for particle monitoring
os_delete_task(0);
}
void task1 (void) _task_ 1 {
while (1)
P0 = 0xFF; // Continuous high signal on PORT0
}
void task2 (void) _task_ 2 {
while(1){
P1 ^= LED_PIN; // Toggle LED os_wait (K_TMO, 10, 0);
}
}
void init_serial(){
TMOD = 0x20;
TH1 = 0xFD;
SCON = 0x50;
TR1 = 1;
}
char read_serial() {
while (RI == 0);
RI = 0;
return SBUF;
}
void task3 (void) _task_ 3 {
init_serial();
while (1) {
char input = read_serial();
if (input == 'b') {
P1 |= DOOR_PIN; // Open door
particle_count ++;
os_wait (K_TMO, 50, 0);
P1 &= ~DOOR_PIN; // Close door
}
}
}
void task4 (void) _task_ 4 {
while (1) {
// Simulated particle sensor input
if (particle_count > MAX_PARTICLES) {
P1 &= ~DOOR_PIN; // Ensure door is closed
}
os_wait (K_TMO, 10, 0);
}
}
dish
#include <rtx51tny.h>
#include <reg51.h>
// For Direction 1
sbit r1 = P1^0; /* I/O Pin: red output */
sbit y1 = P1^1; /* I/O Pin: yellow output */
sbit g1 = P1^2; /* I/O Pin: green output */
sbit stop1 = P1^3; /* I/O Pin: stop output */
sbit walk1 = P1^4; /* I/O Pin: walk output */
sbit Sensor1 = P1^5; /* I/O Pin: sensor input */
// For Direction 2
sbit r2 = P2^0; /* I/O Pin: red output */
sbit y2 = P2^1; /* I/O Pin: yellow output */
sbit g2 = P2^2; /* I/O Pin: green output */
sbit stop2 = P2^3; /* I/O Pin: stop output */
sbit walk2 = P2^4; /* I/O Pin: walk output */
sbit Sensor2 = P2^5; /* I/O Pin: sensor input */
#define t_interval 1000000 /* predefined time duration */
/********************************************************************/
/* Input Controls */
/* */
/* 'X' - Button1 (Pedestrain walk request) - Serial Port */
/* 'Y' - Button2 (Pedestrain walk request) - Serial Port */
/* P1.5 - Sensor1 (Car incoming) - IO Port */
/* P2.5 - Sensor2 (Car incoming) - IO Port */
/********************************************************************/
unsigned char ch;
long int i;
bit flag1, flag2;
unsigned char msg1[] = "Sensor1 triggered\n";
unsigned char msg2[] = "Sensor2 triggered\n";
unsigned char msg3[] = "Button1 triggered\n";
unsigned char msg4[] = "Button2 triggered\n";
void send(unsigned char c) { // scom subroutine
SBUF = c;
while(TI == 0);
TI = 0;
}
void job0 (void) _task_ 0 {
SCON = 0x50; // scom config
TMOD = 0x20;
TH1 = 0xFA;
TR1 = 1;
r1=1; y1=0; g1=0; // initialization (idle state)
stop1=1; walk1=0; Sensor1=0;
r2=1; y2=0; g2=0;
stop2=1; walk2=0; Sensor2=0;
os_create_task(1); // create task 1
os_create_task(2); // create task 2
os_create_task(3); // create task 3
os_send_signal(0);
while(1){
os_wait(K_SIG, 0, 0);
os_send_signal(1); // activate task 1
}
}
void job1 (void) _task_ 1 { // for direction 1
while(1) {
os_wait(K_SIG, 0, 0);
if(!Sensor1) // detecting rising edge (@sensor1)
flag1=1;
if(Sensor1 && flag1 && r2==1) {
for(i=0; i18; i++) // sending ack message
send(msg1[i]);
flag1=0; // handling signals
r1=0; y1=1;
for(i=0; i