Copy Below C Language Code & Enjoy Using Free KST Learning Code
Code Examples
C
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <string.h>
#include <ctype.h>
#define TOTAL_ADDRESSES 3
// Structure to send data from Parent to Child
struct ProcessData {
int baseRegister;
int limitRegister;
int addresses[TOTAL_ADDRESSES];
};
// Structure to send result from Child to Parent
struct ResultData {
int status[TOTAL_ADDRESSES]; // 1 for valid, 0 for invalid
int totalValid;
};
// Function to extract digits from Student ID
void extractDigits(char studentID[], char digits[]) {
int i, j = 0;
for (i = 0; studentID[i] != '\0'; i++) {
if (isdigit(studentID[i])) {
digits[j] = studentID[i];
j++;
}
}
digits[j] = '\0';
}
int main() {
char studentID[50];
char studentName[100];
char subjectCode[20];
char digits[50];
int firstTwoDigits;
int lastFourDigits;
int baseRegister;
int limitRegister;
int endAddress;
int pipeParentToChild[2];
int pipeChildToParent[2];
pid_t pid;
struct ProcessData processData;
struct ResultData resultData;
// Creating first pipe for Parent to Child communication
if (pipe(pipeParentToChild) == -1) {
printf("Pipe creation failed.\n");
return 1;
}
// Creating second pipe for Child to Parent communication
if (pipe(pipeChildToParent) == -1) {
printf("Pipe creation failed.\n");
return 1;
}
// Taking student information input from user
printf("Enter Student ID: ");
scanf("%s", studentID);
getchar(); // To clear newline character from input buffer
printf("Enter Student Name: ");
fgets(studentName, sizeof(studentName), stdin);
studentName[strcspn(studentName, "\n")] = '\0';
printf("Enter Subject Code: ");
scanf("%s", subjectCode);
// Extract digits from Student ID
extractDigits(studentID, digits);
// Check if extracted digits are enough
if (strlen(digits) < 4) {
printf("Invalid Student ID. Student ID must contain at least 4 digits.\n");
return 1;
}
// First two digits for Base Register
firstTwoDigits = (digits[0] - '0') * 10 + (digits[1] - '0');
// Last four digits for Limit Register
lastFourDigits = atoi(&digits[strlen(digits) - 4]);
// Calculate Base Register and Limit Register
baseRegister = firstTwoDigits * 1000;
limitRegister = lastFourDigits;
// Calculate valid address range
endAddress = baseRegister + limitRegister - 1;
// Taking three memory addresses as input
printf("\nEnter memory address 1: ");
scanf("%d", &processData.addresses[0]);
printf("Enter memory address 2: ");
scanf("%d", &processData.addresses[1]);
printf("Enter memory address 3: ");
scanf("%d", &processData.addresses[2]);
// Store base and limit registers in structure
processData.baseRegister = baseRegister;
processData.limitRegister = limitRegister;
// Create child process
pid = fork();
if (pid < 0) {
printf("Fork failed.\n");
return 1;
}
// Child Process
if (pid == 0) {
int i;
int startRange, endRange;
// Close unused write end of Parent to Child pipe
close(pipeParentToChild[1]);
// Close unused read end of Child to Parent pipe
close(pipeChildToParent[0]);
// Read data sent by Parent Process
read(pipeParentToChild[0], &processData, sizeof(processData));
startRange = processData.baseRegister;
endRange = processData.baseRegister + processData.limitRegister - 1;
resultData.totalValid = 0;
// Check each memory address
for (i = 0; i < TOTAL_ADDRESSES; i++) {
if (processData.addresses[i] >= startRange &&
processData.addresses[i] <= endRange) {
resultData.status[i] = 1;
resultData.totalValid++;
} else {
resultData.status[i] = 0;
}
}
// Send result back to Parent Process
write(pipeChildToParent[1], &resultData, sizeof(resultData));
// Close used pipe ends
close(pipeParentToChild[0]);
close(pipeChildToParent[1]);
exit(0);
}
// Parent Process
else {
int i;
// Close unused read end of Parent to Child pipe
close(pipeParentToChild[0]);
// Close unused write end of Child to Parent pipe
close(pipeChildToParent[1]);
// Send Base Register, Limit Register, and addresses to Child Process
write(pipeParentToChild[1], &processData, sizeof(processData));
// Wait for child process to finish
wait(NULL);
// Read result from Child Process
read(pipeChildToParent[0], &resultData, sizeof(resultData));
// Display final result
printf("\n--- Result ---\n");
printf("Student ID: %s\n", studentID);
printf("Student Name: %s\n", studentName);
printf("Subject Code: %s\n", subjectCode);
printf("Digits Extracted: %s\n", digits);
printf("Base Register: %d\n", baseRegister);
printf("Limit Register: %d\n", limitRegister);
printf("Valid Process Address Range: %d to %d\n\n", baseRegister, endAddress);
for (i = 0; i < TOTAL_ADDRESSES; i++) {
if (resultData.status[i] == 1) {
printf("Address %d: Valid process address\n", processData.addresses[i]);
} else {
printf("Address %d: Invalid address. Trap generated\n", processData.addresses[i]);
}
}
printf("\nTotal Valid Addresses checked by Child Process: %d\n", resultData.totalValid);
// Close used pipe ends
close(pipeParentToChild[1]);
close(pipeChildToParent[0]);
}
return 0;
}