Using system calls you have learned so far in the course and paying special atte
ID: 3740325 • Letter: U
Question
Using system calls you have learned so far in the course and paying special attention to system calls in chapter 10, build a C/C++ program to implement the following requirements. 1. The program requests any kind of a file (i.e. .txt (ASCII), bin (Binary),exe (Binary), .docx (Binary) and/or any other extension) of any size 2. Program will check for file's existence. If not exists, print an error like the one shown below: usage: sighandler filename.extension Example 1 sighandler myfile.txt Example 2: sighandler myfile.docx Example 3: sighandler myfile.bin 3. If the file exists: a. Read the file, character by character (NOT A WHOLE BUFFER) b. If the character is an ascii, fire a SIGUSR1 interrupt (Signal), increment a ascii character counter by 1 c. If the character is a non ascii, fire again SIGUSR1 interrupt (Signal), increment a non ascii character counter by 1 *items a, b and c are continued in a loop until the EOF is reached. d. After the EOF file is reached, fire SIGUSR2 interrupt (Signal) to print the counters as shown below S /assignment05.exe story.txt Reading File Name: story.txt Total ASCII characters: 6911 Total Non ASCII characters: 4 e. For both SIGUSR1 and SIGUSR2 interrupts, you must write ONLY ONE signal handler (Multiple signal handlers are not acceptable). f. Run your application and get screen dumps for at least three (3) types of input files (ex. .txt, .bin, .docx) Here are screen dumps of actual runs.... sassignment0s.exe oo Less Par ameters s./assignment05. exe story.txt usage: sighander filename. extension Reading File Name: story. txt Example 1: sighandler myfile.txt Exanple 2: sighandler myfile.docx Example 3: sighandler nyfile.bin Total ASCII characters: 6911 Total Non ASCII characters: 4 $./assignnent05.exe story. docx Reading File Name: story. docx Total ASCII characters: 7279 Total Non ASCII characters 4369 s/assignment05.exe assignment05. exe Reading File Name: assignment05. exe otal ASCII characters:152428 otal Non ASCII characters: 7349Explanation / Answer
Hi there,
Here I am writting this code for linux systems in C.
As question have three parts:
1) to expect the file name from console.
2) validate the existance of file name.
3) if file exists, maintain two counters for ascii and non ascii characters.
#include <stdio.h>
#include <stdbool.h>
#include <sys/stat.h>
int file_pres(const char* filename){
struct stat buffer;
int exist = stat(filename,&buffer); /* CHeck the existance of file*/
if(exist == 0)
{
printf("file name: %s ", filename);
return 1;
}
else
{
return 0;
}
}
/* HANDLER for signals */
void handler()
{
printf(" SIGNAL HANDLER. ")
}
int main(int argc, char **argv)
{
FILE *fp = NULL;
char c;
unsigned int ascii = 0;
unsigned int non_ascii = 0;
printf("argc = %u %s %s ", argc, argv[0], argv[1]);
/* Let write signal handler for signale SIGUSR1 and SIGUSR2 */
signal(SIGUSR1, handler);
signal(SIGUSR2, handler);
if ((argc < 2) || (file_pres(argv[1]) != 1))
{
printf("file not exist!”);
return 0;
}
fp = fopen(argv[1], "r");
while((c = fgetc(fp)) != EOF)
{
printf("%c", c);
if((c >= 0) && (c <= 127))
{
printf("fire SIGUSR1 ");
kill (getpid(), 10); /*signal no for SIGUSR1*/
ascii++;
}
else
{
printf("fire SIGUSR1 ");
kill (getpid(), 10); /*signal no for SIGUSR1*/
non_ascii++;
}
}
printf("fire SIGUSR2 ");
kill (getpid(), 12); /*signal no for SIGUSR2 is 12*/
printf("Total ASCII Characters: %u ", ascii);
printf("Total non ASCII Characters: %u ", non_ascii);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.