Page 1 of 1

Using Unix system calls, fork(), wait(), read() and write(), write a C program for integerbasic arithmetics. Your progra

Posted: Tue Jul 12, 2022 8:20 am
by answerhappygod
Using Unix system calls, fork(), wait(), read() and write(),write a C program for integerbasic arithmetics. Your program shouldfollow the sequential steps, given below.
• Prompts the message "This program performs basicarithmetics",
• Gets in an infinite loop, then
1. Writes the message "Enter an arithmetic statement, e.g., 34 +132 > ",
2. Reads the input line (you can use read(0, line, 255) whereline is a 255-array),
3. Forks, then – parent writes the message "Created a child tomake your operation, waiting" then calls wait() to wait for itschild. – child process calls the function childFunction(char *) andnever returns.
4. The child, through childFunction(char *line),
– writes the message "I am a child working for my parent"
– usessscanf() to convert the input line into an integer, acharacter and an integer, respectively.
– in case of wrong statement, the child calls exit(50)
– in case of division by zero, the child calls exit(100)
– in case of a wrong op the child calls exit(200)
– otherwise, it performs the appropriate arithmeticoperation,
– uses sprint() to create an output buffer consisting of n1 opn2 = result. Output example: 13 - 4 = 9
– writes the output buffer to the screen
– calls exit(0)
5. Once the child terminates, the parent checks the returnedstatus value and if it is 50, 100 or 200, writes "Wrong statement","Division by zero" or "Wrong operator", respectively.
6. The parent goes back to 1.
Important:
• All reads/writes must be done using read()/write()
• You can use the returned value of sscanf() for detecting a"Wrong statement"