In: Computer Science
Use fork()
Create a program that reads characters from an input file. Use fork to create parent and child processes.
The parent process reads the input file. The input file only has letters, numbers. The parent process calculates and prints the frequency of the symbols in the message, creates the child processes, then prints the information once the child processes complete their execution.
The child processes receives the information from the parent, generates the code of the assigned symbol by the parent, and saves the generated code into a file.
Example:
Input.txt: aaaannnaaakkllaaaaap
Output.txt:
a frequency = 12
n frequency = 3
k frequency = 2
l frequency = 2
p frequency = 1
Original Message: aaaannnaaakkllaaaaap
Remember: fork() is not available on windows machine, you can use cygwin if want to execute on windows.
Here it the code:
main.c (CODE)
code text
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include<sys/wait.h>
//For mapping character and its count.
struct icmap{
char c;
int count;
};
int main(void) {
//File handler for output and input.
FILE *input, *output;
//For reading file character by character.
char ch;
//Storing file text while reading char by char.
char originalMessage[100];
//Creating a map of size 1.
//When reading file, dynamically increase the size of map.
struct icmap* map = malloc(sizeof(struct icmap));
//Overall map array length.
int length = 1;
//For loop count
int i,j;
//Flag for check read letter is already in map or not.
int addNewLetter = 0;
//Child process id, to know which process is of child.
pid_t childProcessId;
//Openning input file for reading char by char.
input = fopen("input.txt", "r");
//Read first char and store it in map.
ch = fgetc(input);
map[0].c = ch;
map[0].count = 0;
j = 0;
//Loop through file text until end of file.
while(ch != EOF){
//Store the file text.
originalMessage[j++] = ch;
addNewLetter = 1;
//Check for char already exist or not.
//If already exist increase its count by 1.
//And mark the the flag, not to add this letter.
for(i=0;i<length;i++){
if(map[i].c == ch){
map[i].count = map[i].count + 1;
addNewLetter = 0;
break;
}
}
//If letter is new then increase the size of map, and add it.
if(addNewLetter == 1){
length++;
map = realloc(map, sizeof(struct icmap) * length);
map[length-1].count = 1;
map[length-1].c = ch;
}
ch = fgetc(input);
}
//Close the input file.
fclose(input);
printf("Frequency calculation completed. Now creating child process.\n");
//Create child process.
childProcessId = fork();
if(childProcessId == 0){
//Processing from child process.
//Open the file and save all information.
output = fopen("output.txt", "w");
for(int i=0;i<length;i++){
fprintf(output, "%c frequency = %d\n",map[i].c, map[i].count);
originalMessage[i] = map[i].c;
}
//save the original message.
fprintf(output, "Original Message: %s",originalMessage);
//Close the file.
fclose(output);
printf("Child process complete.\n");
//Exit the child process, so main process continue its execution.
exit(0);
}else{
//Parent process. wait for child process to complete.
wait(NULL);
}
printf("Output file saved successfully.");
return 0;
}
Output
Output File
Code without using any struct
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include<sys/wait.h>
const int MAX_CHAR = 100;
int main(void) {
//File handler for output and input.
FILE *input, *output;
//For reading file character by character.
char ch;
//Storing file text while reading char by char.
char originalMessage[MAX_CHAR];
//storing freq with letter respectively
char chars[MAX_CHAR];
int freqs[MAX_CHAR];
//Overall array length.
int length = 0;
//For loop count
int i,j;
//Flag for check read letter is already in map or not.
int addNewLetter = 0;
//Child process id, to know which process is of child.
pid_t childProcessId;
//Openning input file for reading char by char.
input = fopen("input.txt", "r");
//Read first char and store it in map.
ch = fgetc(input);
j = 0;
//Loop through file text until end of file.
while(ch != EOF){
//Store the file text.
originalMessage[j++] = ch;
addNewLetter = 1;
//Check for char already exist or not.
//If already exist increase corresponding frequency count by 1.
//And mark the the flag, not to add this letter.
for(i=0;i<length;i++){
if(chars[i] == ch){
freqs[i] = freqs[i] + 1;
addNewLetter = 0;
break;
}
}
//If letter is new then add letter to array and increase the frequency and length value..
if(addNewLetter == 1){
length++;
freqs[length-1] = 1;
chars[length-1] = ch;
}
ch = fgetc(input);
}
//Close the input file.
fclose(input);
printf("Frequency calculation completed. Now creating child process.\n");
//Create child process.
childProcessId = fork();
if(childProcessId == 0){
//Processing from child process.
//Open the file and save all information.
output = fopen("output.txt", "w");
for(int i=0;i<length;i++){
fprintf(output, "%c frequency = %d\n",chars[i], freqs[i]);
}
//save the original message.
fprintf(output, "Original Message: %s",originalMessage);
//Close the file.
fclose(output);
printf("Child process complete.\n");
//Exit the child process, so main process continue its execution.
exit(0);
}else{
//Parent process. wait for child process to complete.
wait(NULL);
}
printf("Output file saved successfully.");
return 0;
}